2013-05-05 57 views
1

我的問題是計算含有滴答數據的不規則時間序列的頻率。 問題開始的地方約書亞的極好的提示在這裏結束:http://quantivity.wordpress.com/2009/12/27/quote-arrival-frequency-distribution-for-tick-data/#comment-175不確定滴答數據的滾動頻率xts時間序列

# create random bid/ask data 
require(xts) 
N <- 1e7 
data <- 1.2945+rnorm(N)/1000 
data <- cbind(data,data+runif(N)/1000) 
colnames(data) <- c("bid","ask") 
# create and order random times 
times <- Sys.time()-N:1+rnorm(N)*100 
times <- times[order(times)] 
# create xts object from data and times 
EURUSD <- xts(data, times) 
# create quote frequency chart 
plot(diff(endpoints(EURUSD,"minutes")),type='l') 

My problem continues from here: 
endPoints <- diff(endpoints(EURUSD,"minutes")) 

現在,當我們在端點蜱數據的這個頻率這可怎麼加回到原來的歐元兌美元XTS? 問題是,endPoints不包含任何時間戳或類似信息,無法將其添加回EURUSD對​​象中的列。 此外,我嘗試在歐元兌美元上使用to.minutes並未奏效,因爲它似乎並不總是以相同的方式進行索引。

一如既往將非常感謝任何提示!

回答

-1

看起來像我找到了一種方法來實現它。不是最美麗的,但它似乎工作:

data <- EURUSD 

#using the cut method to get the frequency 
freqs <- data.frame(table(cut(index(data), breaks="min"))) 

#getting it back into an xts and merging with the original 
freqs[,1] <- as.POSIXct(as.character(freqs[,1]), format = "%Y-%m-%d %H:%M:%s") 
freqxts <- xts(freqs[,-1], order.by=freqs[,1]) 
datawithtickspeed <- merge(data, freqxts) 
+0

你真的想爲每一分鐘添加一行嗎? – 2013-05-06 11:49:05

+0

不,不一定,但沒有找到更好的方法 – nikke 2013-05-06 13:45:12

1

可以使用的EURUSD指數在所需端點構建XTS對象。下面是我該怎麼做:

# calculate the desired endpoints 
ep <- endpoints(EURUSD,"minutes") 
# construct an xts object with a diff of the endpoints, 
# using the index values of EURUSD at the endpoints, and 
# merge it with the original data 
Data <- merge(EURUSD, freq=xts(diff(ep), index(EURUSD)[ep])) 
# back-fill NA, if desired 
Data$freq <- na.locf(Data$freq, fromLast=TRUE)