R产生间隔为1分钟的时间序列 [英] R Generating a 1 min spaced time sequence

查看:413
本文介绍了R产生间隔为1分钟的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个间隔为1分钟的时间序列,然后将其粘贴到xts对象. 基本上,我有一个像滴答滴答的dateTime对象:

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"

我正在汇总我的xts系列(按上一个刻度),以使用RTAQ封装函数获得1分钟(等间隔)的时间序列:

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")

问题是时间标签未聚合,这是聚合序列未由1分钟间隔的时间对象标记.这是由于存在几秒钟没有价格的事实.为了获得等距的时间序列,这些函数用先前的报价价格填充空白.

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.

因此,我如何创建一个1分钟间隔的时间序列以获得一个人为1分钟间隔的时间序列?

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

谢谢.

推荐答案

出于兴趣,RTAQ是否提供了另一个R包中未提供的内容?版本0.1是两年前发布的,因此看起来像是一个死项目.无论如何,您仍然可以使用XTS的to.minute()函数,因为RTAQ似乎使用xts对象.

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

我使用的是align.time.down,而不是align.time,以便从06:00:00到06:00:59.999的刻度线进入标记为"06:00:00"的栏中,而不是标记为"06:01:00"的栏中".这与我拥有的历史数据格式匹配.如果需要,定义为:

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)}

最后,如果您整分钟都没有滴答声,并且仍然想要在数据中加一个小节,我可以使用它(与上面的k = 60相同):

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)

seq.exclude_final_period.POSIXt函数的定义如下:

#' 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_fromperiod_toPOSIXct对象,它们描述交易时段的开始和结束.

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

这篇关于R产生间隔为1分钟的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆