Search code examples
pythoninteractive-brokersforex

Interactive Brokers: Unable to fetch Forex Historical data


I am trying IB very the first time. I am trying to fetch historical data of $EUR but I am getting an error:

Error 162, reqId 3: Historical Market Data Service error message:No historical market data for EUR/CASH@FXSUBPIP Last 1800, contract: Contract(secType='CASH', symbol='EUR', exchange='IDEALPRO', currency='USD')

Below is my code:

import datetime

from ib_insync import *

if __name__ == '__main__':
    ib = IB()
    r = ib.connect('127.0.0.1', port=7497, clientId=1)
    contract = Contract()
    contract.symbol = "EUR"
    contract.secType = "CASH"
    contract.currency = "USD"
    contract.exchange = "IDEALPRO"

    data = ib.reqHistoricalData(
        contract=contract,
        endDateTime='',
        durationStr='100 D',
        barSizeSetting='30 mins',
        useRTH=True,
        whatToShow='ADJUSTED_LAST'
    )

Solution

  • The issue here is not on the definition of the FOREX contract itself, but the bar settings requested.

    durationStr='100 D',
    barSizeSetting='30 mins',

    The barSizeSetting is not available for the durationStr selected. If you check the TWS GUI and pull the historical data chart for the FX contract you'll see the lowest bar size available for 100 Day duration is 2 hours.

    You should always check if the functionality you are demanding from the API is available through the TWS GUI. IBKR's API talks to either TWS or IBGateway which in turn send the request to IBKR. If the functionality is not available through the GUI it most likely is not available through the API.