我試圖從IB(納斯達克100電子迷你期貨期權數據)中獲取一些數據。我正在使用snapShot回調(下面包含)。有人能告訴我我的代碼有什麼問題嗎?IBrokers twsF R呼叫
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)
非常感謝。我在網上搜索了很高的和低的,並且在twsFOP上發現了很少的文檔,除了指向twsFuture的CRAN文檔。快照電話如下:
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)))
}
}
}
當您嘗試運行時會發生什麼? – DMT 2014-10-01 23:46:40
看起來很對我。我[實現了完全相同的功能](https://r-forge.r-project.org/scm/viewvc.php/pkg/twsInstrument/R/get_quote.R?view=markup&root=twsinstrument),我知道它的作品期貨,期權,股票和外匯(我還沒有試過FOP)。當我運行你的代碼時,我可以連接到市場數據場。在市場出現價格變化之前,您不會獲得任何數據。 – GSee 2014-10-02 00:09:40