Skip to content
All library documents

Configuring Interactive Brokers Contracts for Forex Historical Data

Article Quant Q&A · Author: Escachator

Summary

The document explains why a historical-data request for the EUR/USD currency pair may return no observations even when the connection to Trader Workstation succeeds. The example specifies the instrument as a stock and uses stock-market settings, which do not match the intended forex contract.

The accepted answer recommends configuring the contract as cash, routing it through IDEALPRO, and selecting an appropriate quote field such as midpoint, bid, or ask. This illustrates that a successful API connection does not guarantee a valid data request: instrument type, venue, and requested data basis must also match. The exchange is not a broader guide to IbPy, request timing, or error handling, and it supplies no independent test results. Its advice is specific to the forex contract described; other instruments and data needs may require different contract and historical-data settings.

Key ideas

  • A successful connection to Trader Workstation does not ensure that a historical-data request is valid.
  • Forex pairs require contract settings that identify them as cash instruments.
  • The suggested routing venue for this request is IDEALPRO.
  • Historical forex data can be requested using midpoint, bid, or ask values.

Tags

Full text
# Downloading Data from Interactive Brokers using IbPy


# Downloading Data from Interactive Brokers using IbPy












I am trying to download data from Interactive Broker using the code below and I am able to create the connection with the Trader Work Station (I get a "True" after `con.connect()`) but there is no output, i.e. the `dataDownload` variable becomes empty.

Anyone know why this can be?

```
# Adapted from: http://godelsmarket.blogspot.co.uk/2012/07/non-gui-ib-historical-data-downloader.html

from time import sleep, strftime, localtime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message

new_symbolinput = ['EUR.USD']
newDataList = []
dataDownload = []

def historical_data_handler(msg):
  global newDataList
  #print msg.reqId, msg.date, msg.open, msg.high, msg.low, msg.close, msg.volume
  if ('finished' in str(msg.date)) == False:
    new_symbol = new_symbolinput[msg.reqId]
    dataStr = '%s, %s, %s, %s, %s, %s, %s' % (new_symbol, strftime("%Y-%m-%d %H:%M:%S", localtime(int(msg.date))), msg.open, msg.high, msg.low, msg.close, msg.volume)
    newDataList = newDataList + [dataStr]
  else:
    new_symbol = new_symbolinput[msg.reqId]
    filename = 'minutetrades' + new_symbol + '.csv'
    csvfile = open('csv_day_test/' + filename,'wb')
    for item in newDataList:
      csvfile.write('%s \n' % item)
    csvfile.close()
    newDataList = []
    global dataDownload
    dataDownload.append(new_symbol)

con = ibConnection()
con.register(historical_data_handler, message.historicalData)
con.connect()

symbol_id = 0
for i in new_symbolinput:
  print i
  qqq = Contract()
  qqq.m_symbol = i
  qqq.m_secType = 'STK'
  qqq.m_exchange = 'SMART'
  qqq.m_currency = 'USD'
  con.reqHistoricalData(symbol_id, qqq, '', '1 D', '1 min', 'TRADES', 1, 2)
  symbol_id = symbol_id + 1
  sleep(0.5)

print dataDownload
```

## Answer by Sagaponack FX (score 3, accepted)

https://quant.stackexchange.com/a/17091

You'll want to adjust the contract inputs you're sending to IB

secType should be CASH, exchange should be IDEALPRO, and add in whatToShow = "MIDPOINT" or "BID" or "ASK"

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.