[R-SIG-Finance] [R-sig-finance] Aggregating tick by tick timeSeries [C1]

Jeff Ryan jeff.a.ryan at gmail.com
Fri Mar 21 16:53:13 CET 2008


Hi Anass,

My apologies for the delayed response, have been extra short on time
the last few weeks.

You can't get back to a timeSeries object using reclass() because
you've created an entirely new object - which is no longer of class
`xts`.  reclass() provides
class-conversion of an `xts` object only.  This should be possible in
the future though, once the period.apply family of functions are fully
incorporated into xts -- but it isn't now.

You didn't supply much data, but I think these randomly generated
numbers should provide a sufficient example of how to create a *new*
timeSeries object.

> test.xts <- xts(matrix(c(runif(3400),floor(runif(3400,200,500))),nc=2),(as.POSIXct('2005-04-08 10:55:16') + 1:34000))

> ep <- endpoints(test.xts,'hours')

> VWAS <- period.apply(test.xts[,1],ep,sum)/period.apply(test.xts[,2],ep,sum)

> timeSeries(VWAS,index(test.xts)[ep[-1]])
                              TS.1
2005-04-08 10:59:59 0.001369137492
2005-04-08 11:59:59 0.001432987385
2005-04-08 12:59:59 0.001427308452
2005-04-08 13:59:59 0.001428589601
2005-04-08 14:59:59 0.001430663890
2005-04-08 15:59:59 0.001425880024
2005-04-08 16:59:59 0.001423586660
2005-04-08 17:59:59 0.001427435076
2005-04-08 18:59:59 0.001434531036
2005-04-08 19:59:59 0.001433269336
2005-04-08 20:21:56 0.001420547518
>

an alternate approach which maintains the underlying zoo structure
(this moved from quantmod, and hasn't been updated to make use of xts)
AND is 5x faster than the pure R period.apply...

> VWAS2 <- xts:::period.sum(test.xts[,1],ep)/xts:::period.sum(test.xts[,2],ep)
> as.timeSeries(VWAS2)
                                 x
2005-04-08 10:59:59 0.001369137492
2005-04-08 11:59:59 0.001432987385
2005-04-08 12:59:59 0.001427308452
2005-04-08 13:59:59 0.001428589601
2005-04-08 14:59:59 0.001430663890
2005-04-08 15:59:59 0.001425880024
2005-04-08 16:59:59 0.001423586660
2005-04-08 17:59:59 0.001427435076
2005-04-08 18:59:59 0.001434531036
2005-04-08 19:59:59 0.001433269336
2005-04-08 20:21:56 0.001420547518

timeSeries by its nature stores all 'index' values for the date/time
as a character string, so the timeDate properties will persist if you
need those - as that is a separate conversion. (thanks to Yohan from
Rmetrics for pointing that out to me.)

As far as a more direct way to do the calculation - I don't think
there is.  I am sure there are many other ways to handle this though.

Jeff (with input from Josh)



More information about the R-SIG-Finance mailing list