3

I would like to generate a 1 min spaced time sequence to paste then to a xts object. Basically, I've got a tick-by-tick dateTime object like that :

 [1] "2010-02-02 08:00:03 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET"
 [6] "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:05 CET" "2010-02-02 08:00:05 CET" "2010-02-02 08:00:05 CET"

I'm aggregating my xts series (by previous tick) to get a 1 min (equally)-spaced time series using an RTAQ package function :

price_1m<-aggregatets(price,FUN="previoustick",k=1,on="minutes")

The problem is that the time label is not aggregated that is the aggregated series is not labeled by a 1 min spaced time-object. This is due is part to the fact that there are seconds with no prices. To get a equally spaced time series, the functions fills the blanks with the previous tick price.

Thus, how can i create a 1 min spaced time sequence to get an artificial 1 min spaced time sequence?

Thanks.

marino89
  • 899
  • 1
  • 10
  • 16

1 Answers1

3

Out of interest does RTAQ offer anything not in another R package? Version 0.1 was released over two years ago, so it looks like a Dead Project. Anyway, you can still use XTS's to.minute() function, as it appears RTAQ use xts objects.

Here is some sample code I use to take ticks and convert into bars, as well as adding other columns such as mean/sd:

k=60
...
bars=to.period(x,k,period="secs")
colnames(bars)=c("Open","High","Low","Close")
ep=endpoints(x,"secs",k)
bars$Volume=period.apply(x,ep,length)
bars$mean=period.apply(x,ep,mean)
bars$sd=period.apply(x,ep, function(x){apply(x,2,sd)})
align.time(bars,k)  #Do this last

Instead of align.time I use align.time.down, so that ticks from 06:00:00 to 06:00:59.999 go into a bar labelled "06:00:00" not one labelled "06:01:00". This matches the historical data format I have. If you need it the definition is:

align.time.down=function(x,n){index(x)=index(x)-n;align.time(x,n)}

Finally, if you have whole minutes with no ticks, and still want a bar in your data for them, I use this (same k=60 as above):

full_index=do.call("c",mapply(
    seq.exclude_final_period.POSIXt,period_from,period_to,by=k,SIMPLIFY=F
    ))
bars=merge(bars,xts(,full_index),all=TRUE)

The seq.exclude_final_period.POSIXt function is defined as follows:

#' Helper for process_one_day_of_ticks(); intended as a
#' replacement for seq.POSIXt (which will not exclude the final period).
#' @internal Use from=from+by instead of to=to-by to exclude the
#      first period instead.
seq.exclude_final_period.POSIXt=function(from,to,by){
to=to-by    #Make the final entry exclusive
seq(from,to,by)
}

period_from and period_to are POSIXct objects, that describe the start and end of your trading session.

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • 1
    Version 0.1? See [Version 0.2](http://www.cran.r-project.org/web/packages/RTAQ/index.html) and [Version 0.3](https://r-forge.r-project.org/R/?group_id=316) – GSee Jul 25 '12 at 23:46
  • @Gsee Thanks! I'd only seen http://www.econ.kuleuven.be/public/n09022/RTAQ.htm which unfortunately looked official! – Darren Cook Jul 26 '12 at 00:01
  • Yes it's my case, I've got minutes without tick. I don't understand how to use your full.index, what should i put in arguments? – marino89 Jul 26 '12 at 08:30
  • Does anyone understand the full.index function? – marino89 Jul 26 '12 at 11:55
  • @user1474263 Sorry! I've just added the definition of `seq.exclude_final_period.POSIXt`. period_from and period_to are POSIX date objects that define the start and end of your trading period. (I exclude the final period for the same reason I use align.time.down()) (This code is quite old, back from when I was first learning R; looking at it now I suspect there is a more elegant way to achieve this.) – Darren Cook Jul 26 '12 at 23:08