【问题标题】:IBrokers twsFOP call in RIBrokers twsFOP 在 R 中调用
【发布时间】: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)。当我运行您的代码时,我能够连接到市场数据场。在市场价格发生变化之前,您不会获得任何数据。

标签: r ibrokers


【解决方案1】:

在价格更新之前,该函数不会返回。

如果我将仪器更改为未来,它就可以正常工作。

test3<- twsFUT("NQ","GLOBEX",expiry="20141219")
test4 <- reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot)
test4
#     BidSize BidPrice AskPrice AskSize    Last LastSize Volume
#NQZ4      14     3984  3984.25       1 3984.25       11   1702

您的 FOP 工具似乎有效,因为您可以致电 reqContractDetails(tws, test3) 并取回所有合同详细信息。

最后,使用 FOP 合约的市场数据调用看起来也是正确的。我可以使用您的代码连接到市场数据场...

test3<- twsFOP("NQ","GLOBEX",expiry="20141121",strike="4000",right="C")
reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot)
#2 -1 2104 Market data farm connection is OK:usfuture 
#2 -1 2106 HMDS data farm connection is OK:ushmds.us 
#2 -1 2107 HMDS data farm connection is inactive but should be available upon demand.cashhmds 
#2 -1 2106 HMDS data farm connection is OK:ushmds 

现在我们只需要等到价格更新。


如果您想在不等待更新的情况下获得最新价格,可以使用reqHistoricalData 拉取它,当前时间为endDateTime。如果您想要买价、卖价和交易的数据,那么您必须提出 3 个单独的请求。以下是如何从历史数据服务中获取最后一笔交易

dat <- reqHistoricalData(tws, test3, 
                  endDateTime=paste(format(Sys.time(), "%Y%m%d %H:%M:%S")), 
                  barSize="1 min", 
                  duration="5 D", useRTH=0, whatToShow="TRADES")
#waiting for TWS reply on NQ .... done.
last(dat)
#                    NQX4 C4000.Open NQX4 C4000.High NQX4 C4000.Low NQX4 C4000.Close NQX4 C4000.Volume NQX4 C4000.WAP
2014-10-01 16:14:00          101.75          101.75         101.75           101.75                 0         101.75
                    NQX4 C4000.hasGaps NQX4 C4000.Count
2014-10-01 16:14:00                  0                0

您需要使用whatToShow="BID"whatToShow="ASK" 来获取买卖数据。

【讨论】:

  • 感谢大家的帮助。是否有可能在不等待新价格更新的情况下获得最新价格或更好的当前买入价或卖出价?
  • @jd8585 不是真的。你可以reqHistoricalData。我会更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多