5

How do I get historical data of an INDEX into R from Interactive Brokers? If it were futures, I would use this command (as suggested here IBrokers request Historical Futures Contract Data?):

library(twsInstrument)
a <- reqHistoricalData(tws, getContract("ESJUN2013"))

But the corresponding commanding with the connid of the S&P Index gives an error:

> a <- reqHistoricalData(tws, getContract("11004968"))
Connected with clientId 110.
Contract details request complete. Disconnected.
waiting for TWS reply on ES ....failed.
Warning message:
In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106,  :
  Error validating request:-'uc' : cause - HMDS Expired Contract Violation:contract can not expire.

P.S. Someone with enough points should create a tag for IBrokers

Community
  • 1
  • 1
mchangun
  • 9,104
  • 18
  • 68
  • 95
  • 1
    Where did you get that `conId` ("11004968")? If you want the S&P index, you need to get the [SPX contract](http://www1.interactivebrokers.ch/contract_info/v3.8/index.php?action=Details&site=GEN&conid=416904). You can do this with `getContract("SPX")`, `getContract(synthetic("SPX", "USD"))`, `getContract("416904")` etc. if you want to use [twsInstrument](https://r-forge.r-project.org/R/?group_id=1113), or use `twsIndex` from [IBrokers](https://code.google.com/p/ibrokers/) as geektrader demonstrated. – GSee Mar 21 '13 at 12:05

2 Answers2

5

I don't have market data access to index data, but I think following should work.

reqHistoricalData(tws, twsIndex(symbol = "SPX", exch = "CBOE"))
## waiting for TWS reply on SPX ....failed.
## NULL

## Warning message:
## In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106,  :
##  Historical Market Data Service error message:No market data permissions for CBOE IND

Following is result of reqContractDetails using similar approach as above which proves that the contract object is created properly by twsIndex

reqContractDetails(tws, twsIndex(symbol = "SPX", exch = "CBOE"))
## [[1]]
## List of 18
##  $ version       : chr "8"
##  $ contract      :List of 16
##   ..$ conId          : chr "416904"
##   ..$ symbol         : chr "SPX"
##   ..$ sectype        : chr "IND"
##   ..$ exch           : chr "CBOE"
##   ..$ primary        : chr ""
##   ..$ expiry         : chr ""
##   ..$ strike         : chr "0"
##   ..$ currency       : chr "USD"
##   ..$ right          : chr ""
##   ..$ local          : chr "SPX"
##   ..$ multiplier     : chr ""
##   ..$ combo_legs_desc: chr ""
##   ..$ comboleg       : chr ""
##   ..$ include_expired: chr ""
##   ..$ secIdType      : chr ""
##   ..$ secId          : chr ""
##   ..- attr(*, "class")= chr "twsContract"
##  $ marketName    : chr "SPX"
##  $ tradingClass  : chr "SPX"
##  $ conId         : chr "416904"
##  $ minTick       : chr "0.01"
##  $ orderTypes    : chr [1:22] "ACTIVETIM" "ADJUST" "ALERT" "ALLOC" ...
##  $ validExchanges: chr "CBOE"
##  $ priceMagnifier: chr "1"
##  $ underConId    : chr "0"
##  $ longName      : chr "S&P 500 Stock Index"
##  $ contractMonth : chr ""
##  $ industry      : chr "Indices"
##  $ category      : chr "Broad Range Equity Index"
##  $ subcategory   : chr "*"
##  $ timeZoneId    : chr "CST"
##  $ tradingHours  : chr "20130321:0830-1500;20130322:0830-1500"
##  $ liquidHours   : chr "20130321:0830-1500;20130322:0830-1500"
## 
CHP
  • 16,581
  • 4
  • 35
  • 56
0

How does one request contract info for non-US stocks, I have tried the two calls below and i get an error in contract details but these are the symbols and exchange for the two securities, I can extract information for them in TWS.

reqContractDetails(tws, twsEquity(symbol = "BMW", exch = "IBIS"))

reqContractDetails(tws, twsEquity(symbol = "BP", exch = "EBS"))

I even tried with exch='SMART' but it fails for BMW and fetches the NYSE line for BP. Is this call only for US stocks?

Arun
  • 265
  • 1
  • 3
  • 12
  • 2
    I figured it out, you have to specify currency for non-US stocks reqContractDetails(tws, twsEquity(symbol = "BMW", exch = "IBIS",currency='EUR')) – Arun Jun 08 '15 at 17:53