R Generating a 1 min spaced time sequence

不羁岁月 提交于 2019-12-04 13:56:01

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!