我的问题是计算包含刻度数据的不规则时间序列的频率。 问题始于约书亚的优秀提示在此结束:http://quantivity.wordpress.com/2009/12/27/quote-arrival-frequency-distribution-for-tick-data/#comment-175
# 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"))
现在,当我们在endPoints中有这个tick数据的频率时,如何将其添加回原来的EURUSD xts? 问题是endPoints不包含任何时间戳或类似信息,以便能够将其添加回EURUSD对象中的列。 此外,我尝试在EURUSD上使用to.minutes的尝试也没有奏效,因为它似乎并不总是以相同的方式进行索引。
一如既往地非常感谢任何提示!
答案 0 :(得分: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)
答案 1 :(得分:-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)