[R-SIG-Finance] how to grow XTS series in R dynamically ? And Quickly!

Vladimir Morozov vmorozov2006 @end|ng |rom gm@||@com
Fri Sep 6 20:04:51 CEST 2019


Hi Daniel
Thanks a lot.
Those are very helpful ideas.

rbind_append --> it still has to allocate memory for the resulting
series... so if memory allocation was the main reason for slow performance,
maybe rbind_append doesn't change much? what do you think?

preallocating regular-interval time-series is a good idea.
however financial data are irregularly spaced (sometimes there may not be
any price updates for a few secs, even more).
even if we postulate that prices are allowed to change no more than once
per second, there's a lot of uses for the frequency of price updates, not
only the values of the prices (the simplest assumption is the poisson
arrival process for the updates, but there are many fancier, more powerful
models...)
so, pre-allocating a regularly spaced 1-sec interval xts series dumbs down
many things!

i wish i could pre-allocate the vector for the values and maybe indices,
but then do the assignment of the sort:
(say, in C++ i would have a method)
    price.set_next_point(time, value);

thanks!

On Sat, Sep 7, 2019 at 12:08 AM Daniel Cegiełka <daniel.cegielka using gmail.com>
wrote:

>
>
> > Wiadomość napisana przez Daniel Cegiełka <daniel.cegielka using gmail.com> w
> dniu 06.09.2019, o godz. 16:10:
> >
>
> >
> > 2) preallocation
> >
> > preallocate_matrix <- function(n)
> > {
> >     x <- matrix()
> >     length(x) <- 4 * n      # bid, ask, bid_size, ask_size
> >     dim(x) <- c(n, 4)       # see: ?dim
> >     return(x)
> > }
> >
> > > x <- preallocate_matrix(5)
> > > x
> >      [,1] [,2] [,3] [,4]
> > [1,]   NA   NA   NA   NA
> > [2,]   NA   NA   NA   NA
> > [3,]   NA   NA   NA   NA
> > [4,]   NA   NA   NA   NA
> > [5,]   NA   NA   NA   NA
>
> ?matrix
>
> Usage
> matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
>        dimnames = NULL)
>
> so we don't even need preallocate_matrix() function
>
> > x <- .xts(matrix(nrow = 5, ncol = 4), index = Sys.time() + 1:5)
> > x
>                     [,1] [,2] [,3] [,4]
> 2019-09-06 17:07:27   NA   NA   NA   NA
> 2019-09-06 17:07:28   NA   NA   NA   NA
> 2019-09-06 17:07:29   NA   NA   NA   NA
> 2019-09-06 17:07:30   NA   NA   NA   NA
> 2019-09-06 17:07:31   NA   NA   NA   NA
>
>
>
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list