2012-07-25 91 views
3

我想生成一个1分钟的间隔时间序列,然后粘贴到一个xts对象。 基本上,我有一个蜱由蜱DateTime对象那样:R生成1分钟的时间间隔序列

[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系列(以前打勾)使用得到1分钟(同样)-spaced时间序列一个RTAQ包功能:

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

的问题是,时间标签不聚集即聚集系列不是由1分钟间隔的时间标记的对象。这是因为有没有价格的秒钟的事实的一部分。为了获得等时间间隔的时间序列,函数用前一个嘀嗒标价填充空白。

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

谢谢。

+1

什么包定义了'aggregatets'函数? – 2012-07-25 13:42:13

+2

看看'?to.period'和朋友。 – 2012-07-25 13:56:18

+0

xts函数to.period的问题在于它将不均匀时间序列中的刻度线转换为另一个非均匀序列。 – marino89 2012-07-25 14:34:15

回答

2

出于兴趣,RTAQ是否提供任何不在其他R包中的东西? 0.1版在两年前发布,因此它看起来像一个死亡项目。无论如何,你仍然可以使用XTS的to.minute()函数,因为它显示RTAQ使用xts对象。

下面是我用带刻度并转换成棒,以及添加其他列,如平均值/ 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我用align.time.down,这样从06蜱:00:00至06:00:59.999进入标有“06:00:00”的酒吧,而不是标有“06:01:00”的酒吧。这与我拥有的历史数据格式相匹配。如果你需要它的定义是:

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

最后,如果你有整个分钟内无蜱,并仍然希望在您的数据为他们吧,我用这个(相同的K = 60以上):

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对象,描述的开始和交易时段结束。

+1

版本0.1?参见[版本0.2](http://www.cran.r-project.org/web/packages/RTAQ/index.html)和[版本0.3](https://r-forge.r-project.org/ R /?group_id = 316) – GSee 2012-07-25 23:46:37

+0

@Gsee谢谢!我只看到http://www.econ.kuleuven.be/public/n09022/RTAQ.htm,不幸看起来很正式! – 2012-07-26 00:01:24

+0

是的,这是我的情况,我有没有打勾的分钟。我不明白如何使用你的full.index,我应该在参数中输入什么内容? – marino89 2012-07-26 08:30:15

相关问题