R IBrokers API 无法为过期月份请求历史数据

R IBrokers API fails to reqHistoricalData for expired months

为了将数据从 IB 下载到 R,我按照以下步骤操作:IBrokers request Historical Futures Contract Data?. Which are about the same as here: https://cran.r-project.org/web/packages/IBrokers/vignettes/IBrokers.pdf

一切正常。除了一个例外:reqHistoricalData 不适用于过期月份。 运行 以下代码给出错误消息:“警告消息:在 errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, : No security definition has been found for请求"

#DOES NOT WORK (using expired month)
tws <- twsConnect()
mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201603"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')

#YET THE FOLLOWING DO WORK (using unexpired months)
mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201606"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')
mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201609"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')
getContract("ES_M6")

IB 常​​见问题解答对该消息的说明如下: "Why do I receive an error 200 - No security definition has been found for the request when I call reqContractDetails, reqMktData, or addOrder() for a stock contract? When using these methods for a stock contract, leave Global Symbol and Trading Class blank."(在 https://www.interactivebrokers.com/en/software/api/apiguide/tables/frequentlyaskedquestions.htm 找到)

非常感谢对此有任何见解。谢谢。

您需要将 include_expired 设置为 true。我猜代码是:

twsFuture("ES","GLOBEX","201603",include_expired='1')

文档中的完整参数列表是:

twsEquity(symbol,
          exch="SMART",
          primary,
          strike='0.0',
          currency='USD',
          right='',
          local='',
          multiplier='',
          include_expired='0',
          conId=0)

并引用帮助页面:

The endDateTime argument must be of the form 'CCYYMMDD HH:MM:SS TZ'. If not specified the current time as returned from the TWS server will be used. This is the preferred method for backfilling data. The ‘TZ’ portion of the string is optional.

所以你也可以尝试使用

reqHistoricalData(..., endDateTime='20160315 16:00:00')