Trouble Fetching Forex Data Using ib_insync Library

217 Views Asked by At

Hello fellow developers,

I'm encountering an issue while trying to fetch forex data using the ib_insync library for the Interactive Brokers TWS API. I've been trying to create a Python app that queries the API for forex pair prices, but I'm not receiving any data updates. I'd like to seek your expertise in understanding and resolving this issue.

Context:

I've successfully connected to TWS using the IB class and established a connection without any problems. I'm using the Forex class to specify the currency pair (e.g., CADJPY or JPYCAD). I've used the reqMktData method to request streaming data for the chosen pair. I've set up the pendingTickersEvent to listen for data updates and have assigned a function to process incoming data. Issue: Despite setting up the code as mentioned below, I'm not receiving any data updates. The script runs without errors, but no data is being printed to the console.

I tried both CADJPY and JPYCAD as the currency pair, but neither worked. I've checked my TWS API settings and ensured that I have the necessary permissions for market data access. I've experimented with different event loop handling methods, including util.run() and custom asyncio loops, but the issue persists. I've verified that my connection to TWS is active and stable.

I'm using Python 3.9 and I'm on a windows 10 laptop with horrible specs, but I shudder to think that a simple market API would require vast amounts of processing power, and I've got 28 GB of ram. I've also tried this on a desktop with 32 gb of ram and an old Q6600 processor running windows 10 as well.

from ib_insync import IB, Forex, util

# Define a function to process incoming data
def onForexUpdate(tickers):
    for ticker in tickers:
        if ticker.contract == cadjpy:
            print(f'CADJPY Bid: {ticker.bid}, Ask: {ticker.ask}')

# Connect to TWS
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

# Qualify and request streaming data for CADJPY
cadjpy = Forex('CADJPY')
ib.qualifyContracts(cadjpy)
ib.reqMktData(cadjpy, snapshot=False)

# Assign the function to the pendingTickers event
ib.pendingTickersEvent += onForexUpdate

# Run the event loop using ib_insync's asyncio event loop
util.run()

0

There are 0 best solutions below