Search code examples
pythontradingtwsib-api

Interactive Brokers TWS API: problems with getting futures prices via app.reqMktData


I've created a working piece of code which gets real time futures prices from IB TWS based on this topic https://quant.stackexchange.com/questions/55569/interactive-broker-api-error-321

It works for futures MNQU3 (this is Nasdaq-100 Index from the example above). However, when I try to put another futures it returns an error "200 No security definition has been found for the request". Below is the code which is created by analogy and which I'm trying to fix. The key lines are 31-37 and 41-47

Real time market data is bought for futures section and my TWS station says all market data farms are connected. May be there are some known rules for filling in information about futures that I break, or may be commodity and index futures have different tepmlates? Thanks

Screenshots of successfull and problematic runs

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

import threading
import time



class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 1:
            print('The current ask price is: ', price)

def run_loop():
    app.run()

app = IBapi()
app.connect('127.0.0.1', 7497, 123)

#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(1) #Sleep interval to allow time for connection to server

# working example

#fut_contract = Contract()
#fut_contract.symbol = 'MNQU3'
#fut_contract.secType = 'FUT'
#fut_contract.exchange = 'CME'
#fut_contract.tradingClass = 'MNQ'
#fut_contract.lastTradeDateOrContractMonth = '202309'
#fut_contract.currency = 'USD'

# does not work

fut_contract = Contract()
fut_contract.symbol = 'NGQ3'
fut_contract.secType = 'FUT'
fut_contract.exchange = 'CME'
fut_contract.tradingClass = 'NG'
fut_contract.lastTradeDateOrContractMonth = '202308'
fut_contract.currency = 'USD'

#Request Market Data
app.reqMktData(1, fut_contract, '', False, False, [])

time.sleep(10) #Sleep interval to allow time for incoming price data
app.disconnect()

Also tried some other futures codes, but non of them seemed to word, except MNQU3


Solution

  • Wrong exchange, NG is on NYMEX not CME. You can search contracts here https://pennies.interactivebrokers.com/cstools/contract_info/v3.10/index.php