Search code examples
pythoninteractive-brokerstwsib-insync

IBKR: No security definition has been found for the request, contract:


I am new in both IBKR and its API, the code given below is giving error on qualifyContracts method:

Error 200, reqId 308: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=808.33, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 309: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=816.67, right='C', exchange='CBOE', currency='USD')
Error 200, reqId 310: No security definition has been found for the request, contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=825.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=1.67, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=3.33, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=5.0, right='C', exchange='CBOE', currency='USD')
Unknown contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230303', strike=6.67, right='C', exchange='CBOE', currency='USD')

The code given below:

from ib_insync import *
from random import getrandbits

if __name__ == '__main__':
    ib = IB()
    client_id = getrandbits(5)
    print('Connecting with the CLIENT ID = ', client_id)
    PORT = 4002
    ib.connect('127.0.0.1', PORT, clientId=client_id)
    # contract = Contract(conId=76792991, exchange='SMART',currency='USD')
    contract = Stock('TSLA', 'SMART', 'USD')
    print('Setting Market Data Type')

    ib.reqMarketDataType(3)
    print('1')
    ib.qualifyContracts(contract)
    print('CON ID = ', contract.conId)
    chains = ib.reqSecDefOptParams(contract.symbol, '', contract.secType, contract.conId)
    cboe_chains = [c for c in chains if c.exchange == 'CBOE']
    # # print(cboe_chains)
    # contracts = [Option(cboe_chains[0].underlyingConId, c.symbol, c.secType, c.exchange, c.currency, c.strike, c.right,
    #                     c.lastTradeDateOrContractMonth) for c in cboe_chains]

    opts = []
    for chain in cboe_chains:
        print(chain)
        for strike in chain.strikes:
            print(strike)
            option = Option(symbol=chain.tradingClass, lastTradeDateOrContractMonth=chain.expirations[0], strike=strike,
                            exchange='CBOE', currency='USD', right='C')
            opts.append(option)
    #
    #         qualified_contract = ib.qualifyContracts(option)
    #         print(qualified_contract)
    #     print('-----------------------------------')
    print(opts)
    qualified_contract = ib.qualifyContracts(*opts) # Error comes here

Solution

  • I always define the contract with a default primary exchange, SMART does not work as expected all the time. Also, I am not sure if 'CBOE' is a valid exchange name for IBKR. Check this:

    def contract(
        symbol, sec_type="STK", currency="USD", exchange="SMART", primaryExchange="ISLAND"
    ):
        contract = Contract()
        contract.symbol = symbol
        contract.secType = sec_type
        contract.currency = currency
        contract.exchange = exchange
        # Specify the Primary Exchange attribute to avoid contract ambiguity
        contract.primaryExchange = primaryExchange
        return contract
    
    
    contract = Stock('TSLA', 'SMART', 'USD', 'NASDAQ')