【发布时间】:2014-10-01 23:11:40
【问题描述】:
我正在尝试从 IB 获取以下数据(纳斯达克 100 电子迷你期货期权数据)。我正在使用 snapShot 回调(包括在下面)。有人能告诉我我的代码有什么问题吗?
require(IBrokers)
tws <- twsConnect()
test3<- twsFOP("NQ","GLOBEX",expiry="20141121",strike="4000",right="C")
test4 <- reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot)
非常感谢。除了指向 twsFuture 的 CRAN 文档外,我在网上搜索了很多关于 twsFOP 的文档。快照调用如下:
snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...)
{
if (missing(eWrapper))
eWrapper <- eWrapper()
names(eWrapper$.Data$data) <- eWrapper$.Data$symbols
con <- twsCon[[1]]
if (inherits(twsCon, "twsPlayback")) {
sys.time <- NULL
while (TRUE) {
if (!is.null(timestamp)) {
last.time <- sys.time
sys.time <- as.POSIXct(strptime(paste(readBin(con,
character(), 2), collapse = " "), timestamp))
if (!is.null(last.time)) {
Sys.sleep((sys.time - last.time) * playback)
}
curMsg <- .Internal(readBin(con, "character",
1L, NA_integer_, TRUE, FALSE))
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, format(sys.time,
timestamp), file, ...)
}
else {
curMsg <- readBin(con, character(), 1)
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
Sys.sleep(5 * playback)
}
}
}
else {
while (TRUE) {
socketSelect(list(con), FALSE, NULL)
curMsg <- .Internal(readBin(con, "character", 1L,
NA_integer_, TRUE, FALSE))
if (!is.null(timestamp)) {
processMsg(curMsg, con, eWrapper, format(Sys.time(),
timestamp), file, ...)
}
else {
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
}
if (!any(sapply(eWrapper$.Data$data, is.na)))
return(do.call(rbind, lapply(eWrapper$.Data$data,
as.data.frame)))
}
}
}
【问题讨论】:
-
当你尝试运行时会发生什么?
-
对我来说很合适。我implemented this exact same function 我知道它适用于期货、期权、股票和外汇(我没有尝试过 FOP)。当我运行您的代码时,我能够连接到市场数据场。在市场价格发生变化之前,您不会获得任何数据。