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

Vladimir Morozov vmorozov2006 @end|ng |rom gm@||@com
Sat Sep 7 08:15:05 CEST 2019


Thanks, Ilya

On Sat, Sep 7, 2019, 3:47 AM Ilya Kipnis <ilya.kipnis using gmail.com> wrote:

> Vladimir,
>
> Question--do you have to do operations on the newly updated xts each time
> you append it? For instance, when I backtest strategies and I don't know
> how long the output would be, I start off by allocating an empty list,
> keep updating the list, and rbind it at the end.
>
> EG:
>
> results <- list()
> for(i in 1:n){
>   #output <- do something
>   results[[i]] <-output
> }
> results <- do.call(rbind, results)
>
> This might help, as this doesn't keep copying the larger list over and
> over again, which I remember doing before I learned about this method.
>
> Hope this helps.
>
> -Ilya
>
> On Fri, Sep 6, 2019 at 2:05 PM Vladimir Morozov <vmorozov2006 using gmail.com>
> wrote:
>
>> 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]]
>>
>> _______________________________________________
>> R-SIG-Finance using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions
>> should go.
>>
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list