pythoninteractive-brokers

Interactive Brokers IBKR Gateway API - How to fix "No security definition has been found for the request"?


Requesting contract details for NQ futures... Error 200, reqId 9: No security definition has been found for the request, contract: Future(symbol='NQ', exchange='GLOBEX', currency='USD') No contract details found for NQ. Please verify your parameters in TWS/IB Gateway.

#!/usr/bin/env python3
from ib_insync import IB, Future
from datetime import datetime
import sys

def parse_expiry(expiry):
    """
    Parse the expiry string from the contract details.
    IB typically provides expiry as YYYYMM or YYYYMMDD.
    For comparison, we convert to a datetime object.
    """
    if len(expiry) == 6:
        # Assume format is YYYYMM; we'll use the 1st of the month for comparison.
        try:
            return datetime.strptime(expiry, "%Y%m")
        except Exception as e:
            print(f"Error parsing expiry {expiry}: {e}")
            return None
    elif len(expiry) == 8:
        try:
            return datetime.strptime(expiry, "%Y%m%d")
        except Exception as e:
            print(f"Error parsing expiry {expiry}: {e}")
            return None
    else:
        return None

def main():
    # Connect to IBKR Gateway on localhost using port 4002.
    ib = IB()
    print("Connecting to IBKR Gateway...")
    ib.connect('-----------', 4002, clientId=1)
    
    # Create a generic NQ futures contract.
    contract = Future()
    contract.symbol = "NQ"
    contract.secType = "FUT"
    contract.exchange = "GLOBEX"
    contract.currency = "USD"
    # Do not specify lastTradeDateOrContractMonth so that IB returns all available contracts.

    print("Requesting contract details for NQ futures...")
    details = ib.reqContractDetails(contract)
    
    if not details:
        print("No contract details found for NQ. Please verify your parameters in TWS/IB Gateway.")
        ib.disconnect()
        sys.exit(1)
  
  
    


It would be great if you could help me with this.
tried different exchanges, tried loading different contract symbols, built a test connection.py ,connects to IBKR Gateway 

Solution

  • GLOBEX is an old exchange code that is no longer valid. It should be CME.

    You can look up exchange codes on IBKR's website: https://www.interactivebrokers.com/en/trading/products-exchanges.php

    Or you can look it up on QuantRocket's website with fewer clicks: https://www.quantrocket.com/data/?modal=ibkrexchangesmodal