How to get the MIC codes for exchanges on IB?

Discussion in 'Interactive Brokers' started by jrd, Feb 23, 2025.

  1. jrd

    jrd

    I'm using the TWS API, and to have consistency in the naming of exchanges between all brokers, including IB, I use MIC codes in my system instead of the IB names. MIC stands for Market Identifier Code and is an effort by ISO to create a unified naming system for financial markets. For example, the MIC code for NYSE is XNYS.

    Now, the task is to convert the names of all 162 IB exchanges to MIC codes. The listings here https://www.iso20022.org/market-identifier-codes do not contain the acronyms that IB uses, and as far as I'm aware, IB does not display MIC codes for their exchanges, so automatic cross referencing is impossible. So far, the only option is to manually check and enter the MIC code for every exchange. Is there a better solution? Is there a way to use IB API to get more information about the exchanges? For example, the LEI code?
     
  2. In python:

    # IB Primary Exchange to MIC mapping
    ib_to_mic = {
    "NYSE": "XNYS",
    "NASDAQ": "XNAS",
    "AMEX": "XASE",
    "ARCA": "ARCX",
    "BATS": "BATS",
    "IEX": "IEXG",
    "CBOE": "XCBO",
    "CME": "XCME",
    "ICE": "XICE",
    "LSE": "XLON",
    "Euronext": "XPAR",
    "TSX": "XTSE",
    "HKEX": "XHKG",
    "ASX": "XASX",
    }

    # Example: Get MIC for a given IB primary exchange
    primary_exchange = "NASDAQ" # Example retrieved from IB contract details
    mic_code = ib_to_mic.get(primary_exchange, "Unknown MIC")

    print(f"MIC Code for {primary_exchange}: {mic_code}")


    You can use this with the ib_insync library:

    from ib_insync import *

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

    # Define stock contract
    contract = Stock('AAPL', 'SMART', 'USD')

    # Request contract details
    contract_details = ib.reqContractDetails(contract)

    # Check if contract details exist
    if contract_details:
    primary_exchange = contract_details[0].contract.primaryExchange # Get primary exchange
    mic_code = ib_to_mic.get(primary_exchange, "Unknown MIC")
    print(f"Primary Exchange: {primary_exchange}, MIC Code: {mic_code}")
    else:
    print("No contract details found.")

    # Disconnect
    ib.disconnect()
     
  3. jrd

    jrd

    Thank you so much for the detailed answer which I'm sure will be useful, but the question was not about programming, but on how to pair each of the 162 IB exchanges with their respective MIC code. In other words, you've made a list of 14 pairs. How to generate this list? What sources did you use? How to ensure it's accurate?
     
  4. Basically you have to brute force it. Grab all of the MIC codes, feed them into something like ChatGPT and request the IB primary exchange. Build your list. No guarantees on accuracy but the more you use it the more accurate it will get.
     
  5. jrd

    jrd

    Greatly appreciated, thank you.
     
  6. spy

    spy

    [​IMG]
     
  7. hilmar22

    hilmar22

    The best thing about standards is there are so many to choose from.
     
    spy likes this.