2

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.

Community
  • 1
  • 1
Krug
  • 1,003
  • 13
  • 33

1 Answers1

2

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')
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
brian
  • 10,619
  • 4
  • 21
  • 79
  • Thanks very much. Still doesn't work. Different problem now. `mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201603",include_expired='1'), barSize='1 hour', duration='5 D', useRTH='0', whatToShow='TRADES')` returns "waiting for TWS reply on ES ....failed. Warning message: In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, : Historical Market Data Service error message:HMDS query returned no data: ESH6@GLOBEX Trades". It works just fine with 201606 (the current month). – Krug Apr 27 '16 at 21:59
  • 1
    If there's no data then you're just using the wrong date. Don't forget it has an expiry where it no longer trades. Use an earlier date before about the middle of the expiration month (for ES). So for H=Mar, use Mar 15/2016 as about the latest date. – brian Apr 27 '16 at 22:24