1

我的问题与计算包含刻度数据的不规则时间序列的频率有关。问题从 Joshua 的优秀技巧在这里结束的地方开始: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 中有这个刻度数据的频率时,如何将它添加回原始的 EURUSD xts?问题是 endPoints 不包含任何时间戳或类似信息,无法将其添加回 EURUSD 对象中的列。此外,我尝试在 EURUSD 上使用 to.minutes 的尝试也没有奏效,因为它似乎并不总是以相同的方式索引。

一如既往地非常感谢任何提示!

4

2 回答 2

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)
于 2013-05-06T13:57:18.477 回答
-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)
于 2013-05-06T06:44:13.613 回答