Re: [R-sig-finance] Aggregating tick by tick timeSeries [C1]

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [R-sig-finance] Aggregating tick by tick timeSeries [C1]

anass.mouhsine


Hi Jeff,

I used the xts package as you advised.
I have a timeSeries object called test

>test[1000:1005,]
                                     V10  V7
2005-04-08 10:55:16 0.3202287 448
2005-04-08 10:55:16 0.3945675 552
2005-04-08 10:55:16 0.0000000   0
2005-04-08 10:55:18 0.0000000   0
2005-04-08 10:55:30 0.0000000   0
2005-04-08 10:55:35 0.0000000   0

the first column stand for the spread*volume and the second one for the
traded volume.
I want to calculate the Volume Weighted Spread hourly.

>test.xts=as.xts(test)

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

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

>VWAS
[1] 0.0006517149 0.0011587963 0.0008660431 0.0007342648 0.0010022736
[6] 0.0009949343 0.0009114276 0.0009170628 0.0008334971

Here I have what I want but need to improve the result:
1) I do not know how to get back to a timeSeries object using reclass() and
have a timeDate object as an index.
2) Is it possible to do the same calculation from a timeSeries object of
two columns (spread,volume)  whithout calculating a column spread* volume
and doing two univariate sums.

Could you give some hints on the subject?

regards,
Anass


|------------------------------                                            
|            jeff.a.ryan@gmail                                            
|            .com                                                          
|                                                                          
|            03/05/2008 16:21                                              
|                                                                          
                                                                           
                                                                           
                                                                        To
                                               Anass                      
                                               MOUHSINE/fr/socgen@socgen  
                                                                        cc
                                               [hidden email]
                                               z.ch                        
                                                                   Subject
                                               Re: [R-SIG-Finance]        
                                               [R-sig-finance] Aggregating
                                               tick by tick timeSeries    
                                               [C1]                        
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Anass,

If you want the mean/sd/etc for each hour ?aggregate from zoo will do
the job very well. To get there from timeSeries you can use the new
'xts' package:

myTS [a timeSeries object]

as.xts(myTS) [ now an 'xts' object - which inherits from zoo]

--or--

as.zoo(myTS)

you can even put it back to a timeSeries with little information loss
if you use xts via 'reclass' (changing the series outside of xts
functions currently isn't perfect - but it is good)

If you want to just aggregate the data into OHLC per time period -
to.period in xts is fast and flexible:

to.period(myTS,'minutes',15)
to.period(myTS,'hours')
to.monthly(myTS)

All these work by converting and reconverting to an 'xts' class
internally - so most any (including timeSeries) class will just work
with the function.  In addition it is all compiled code - so it works
well.

One caveat - you'll need to get the most recent xts from
http://r-forge.r-project.org/projects/xts - as there was a minor bug
in the transition of 'to.period' code from quantmod to xts.

Jeff

On Wed, Mar 5, 2008 at 8:21 AM,  <[hidden email]> wrote:

>
>  Hi guys,
>
>
>  I have a timeSeries object like this one
>                         V10
>  2005-04-08 17:31:41 0.01
>  2005-04-08 17:31:57 0.02
>  2005-04-08 17:32:00 0.02
>  2005-04-08 17:32:57 0.02
>  2005-04-08 17:38:34 0.02
>  2005-04-08 17:38:49 0.01
>
>  and I would like to aggregate the timeSeries in hours or minutes in
order
>  to apply whatever function on the aggregated data (e.g mean, standard
dev,

>  etc...)
>  I have seen some aggreagation functions like aggregate in the fSeries
>  package but it aggregates only monthly or quaterly.
>  I am sure some of you guys were faced to this kind of issue.
>
>  Could anyone give me some hints on how to solve my problem?
>
>  thanks in advance
>
>  Anass
>
*************************************************************************
>  This message and any attachments (the "message") are
con...{{dropped:10}}
>
>  _______________________________________________
>  [hidden email] mailing list
>  https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>  -- Subscriber-posting only.
>  -- If you want to post, subscribe first.
>



--
There's a way to do it better - find it.
Thomas A. Edison



*************************************************************************
This message and any attachments (the "message") are con...{{dropped:10}}

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: [R-sig-finance] Aggregating tick by tick timeSeries [C1]

Jeffrey Ryan
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)

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.
Reply | Threaded
Open this post in threaded view
|

Re: [R-sig-finance] Aggregating tick by tick timeSeries [NC]

anass.mouhsine

thanks Jeff for your detailed answer.
It worked just fine.

regards,
Anass





|------------------------------                                            
|            jeff.a.ryan@gmail                                            
|            .com                                                          
|                                                                          
|            03/21/2008 16:53                                              
|                                                                          
                                                                           
                                                                           
                                                                        To
                                               Anass                      
                                               MOUHSINE/fr/socgen@socgen  
                                                                        cc
                                               [hidden email]
                                               z.ch                        
                                                                   Subject
                                               Re: [R-SIG-Finance]        
                                               [R-sig-finance] Aggregating
                                               tick by tick timeSeries    
                                               [C1]                        
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




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)

*************************************************************************
This message and any attachments (the "message") are con...{{dropped:10}}

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.