rtradingibrokers

R IBrokers API fails to reqHistoricalData for expired months


In an effort to download data from IB into R I have followed the steps here: IBrokers request Historical Futures Contract Data?. Which are about the same as here: https://cran.r-project.org/web/packages/IBrokers/vignettes/IBrokers.pdf.

It all works. With one exception: reqHistoricalData does not work with expired months. Running the following code gives error message: "Warning message: In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, : No security definition has been found for the request"

#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")

The IB FAQ says the following on that message: "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." (found at https://www.interactivebrokers.com/en/software/api/apiguide/tables/frequentlyaskedquestions.htm)

Would greatly appreciate any insight into this. Thank you.


Solution

  • You need to set include_expired to true. I'm guessing the code would be:

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

    The complete list of args from the docs is:

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

    And to quote the help page:

    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.

    So you could also try using

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