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

Daniel Cegiełka d@n|e|@ceg|e|k@ @end|ng |rom gm@||@com
Fri Sep 6 16:10:27 CEST 2019



> Wiadomość napisana przez Vladimir Morozov <vmorozov2006 using gmail.com> w dniu 06.09.2019, o godz. 04:51:
> 
> Dear xts experts
> 
> I use R and XTS to store dynamically growing price time-series for currency
> pair rates (e.g. time series of EUR/USD, EUR/JPY, etc growing with each new
> incoming market price - say, one per second).
> 
> 

(…)

> it all happens quite slowly in R. How do I accelerate it?


1) forgotten xts acceleration:

https://github.com/joshuaulrich/xts/blob/master/src/rbind.c#L540 <https://github.com/joshuaulrich/xts/blob/master/src/rbind.c#L540>

to activate it add the -DRBIND_APPEND flag to Makevars (linux/unix/macos) or Makevars.win (Windows), and recompile xts sources.



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

Now you need xts index, eg:

i <- Sys.time() + 1:5
> i
[1] "2019-09-06 15:47:48 CEST" "2019-09-06 15:47:49 CEST" "2019-09-06 15:47:50 CEST" "2019-09-06 15:47:51 CEST"
[5] "2019-09-06 15:47:52 CEST”


Our xts object:

> x <- .xts(x, index = i)
                    [,1] [,2] [,3] [,4]
2019-09-06 15:47:48   NA   NA   NA   NA
2019-09-06 15:47:49   NA   NA   NA   NA
2019-09-06 15:47:50   NA   NA   NA   NA
2019-09-06 15:47:51   NA   NA   NA   NA
2019-09-06 15:47:52   NA   NA   NA   NA


And now you can use this xts object without memory copying:


> x[1,] <- c(1, 2, 3, 4)
> x
                    [,1] [,2] [,3] [,4]
2019-09-06 15:47:48    1    2    3    4
2019-09-06 15:47:49   NA   NA   NA   NA
2019-09-06 15:47:50   NA   NA   NA   NA
2019-09-06 15:47:51   NA   NA   NA   NA
2019-09-06 15:47:52   NA   NA   NA   NA

> x[2,] <- c(11, 22, 33, 44)
> x
                    [,1] [,2] [,3] [,4]
2019-09-06 15:47:48    1    2    3    4
2019-09-06 15:47:49   11   22   33   44
2019-09-06 15:47:50   NA   NA   NA   NA
2019-09-06 15:47:51   NA   NA   NA   NA
2019-09-06 15:47:52   NA   NA   NA   NA


etc.

That's what you meant.

Best,
Daniel


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20190906/115ffec8/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20190906/115ffec8/attachment.sig>


More information about the R-SIG-Finance mailing list