2013-04-21 33 views
2

我正在研究R期貨合約。期貨市場在美東時間下午6點開市,第二天美國東部時間下午5點結束。我正在處理小時級別的數據。當我使用quantmod時,它假設開放時間是上午12:00,關閉時間是晚上11:59。有沒有辦法改變開放和關閉時間,還是有更好的方法來解決問題?使用quantmod設置開盤時間和結束時間

+1

你能告訴我們你到底在做什麼嗎?你是從更高頻率計算每日OHLC信息,即小時數據嗎? – 2013-04-21 06:42:02

+0

我正在計算sd,mean,trends,並繪製了一些內置的功能,如MACD和BBands。通常每小時的比例尺太細,所以我需要移動到每日或每月的比例(to.daily,to.monthly)。這是開放和關閉時間不同步的時候。 – user1223862 2013-04-22 14:01:48

回答

1

這裏常用的技巧是設置時區,以便午夜匹配日期結束。 EST的問題是夏令時/冬令開關,但由於你的市場是23小時,而不是24小時,你不需要處理它(而不是外匯市場)。

如果您試圖一次處理一天的數據,在R中,我使用下面的示例代碼。這是對rollapply.right的修改。我的腳本和data位於UTC時區。 (data可能是標記數據,或小時數據,或其間的任何內容)。

其基本思想是獲取數據的副本,將該副本移動到不同的時區,在其上運行endpoints,然後在原始數據上使用endpoints的結果。 '7 * 3600'的調整會在晚上5點前進到午夜。

rollapply_chunks.FX.xts=function(data,width,FUN,...,on="days",k=1){ 
data <- try.xts(data) 

x2 <- data 
index(x2) <- index(x2)+(7*3600) 
indexTZ(x2) <- 'America/New_York' 

ep <- endpoints(x2,on=on,k=k) #The end point of each calendar day (when on="days"). 
    #Each entry points to the final bar of the day. ep[1]==0. 

if(length(ep)<2){ 
    stop("Cannot divide data up") 
}else if(length(ep)==2){ #Can only fit one chunk in. 
    sp <- 1;ep <- ep[-1] 
}else{ 
    sp <- ep[1:(length(ep)-width)]+1 
    ep <- ep[(width+1):length(ep)] 
} 

xx <- lapply(1:length(ep), function(ix) FUN(.subset_xts(data,sp[ix]:ep[ix]),...)) 
xx <- do.call(rbind,xx) #Join them up as one big matrix/data.frame. 

tt <- index(data)[ep] #Implicit align="right". Use sp for align="left" 
res <- xts(xx, tt) 
return (res) 
} 
相關問題