2012-07-25 89 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

相關問題