IB API - get all expirys and strikes of a stock symbol

Discussion in 'Automated Trading' started by dangqiwu, Nov 12, 2013.

  1. dangqiwu

    dangqiwu

    Given a stock symbol, how to get all expirys and strikes of its option chain on IB API? I can't find such an API function yet.
     
  2. I don't think ib has that kind of apis.
     
  3. dangqiwu

    dangqiwu

    I don't either. How did IB TWS do that? Is there a good algorithm to find all expiries and strikes without too much server communication?
     
  4. Butterfly

    Butterfly

    the function is available in TWS, but for the IB API, I suspect you need to build your own functions, shouldn't be that difficult to build

    you could create the different instrument in a loop and test if a "close" attribute is present.
     
  5. If you call reqContractDetailsEx and don't specify the strike price or expiry date you get back the whole chain.
     
  6. Butterfly

    Butterfly

    This is great, better than I expected from IB for this
     
  7. dangqiwu

    dangqiwu

    thanks Steve, good to know this.
     
  8. Butterfly

    Butterfly

    I just tested this in Python, and the IB server did return a long string of replies with different contract objects.
     
  9. dangqiwu

    dangqiwu

  10. Butterfly

    Butterfly

    This is what I did in Python to receive all the contract details. Don't forget the currency attributes, it's always something we tend to forget.

    Code:
    # INSERT code for IB Connect etc...
    
    order_ticker = Contract()
    order_ticker.m_symbol = 'MSFT'
    order_ticker.m_secType = 'OPT'
    order_ticker.m_currency = 'USD'
    
    # You need to enter a dedicated RequestID, can be arbitrary or automatically incremented. 
    # Here we use ReqID = 1
    
    ibgw_conTradeChannel.reqContractDetails(1,order_ticker)
    
    This will give the following raw output, a number of contract details objects for you to catch and assign properly. A lot of work would need to be done to filter out what you need and don't need. I haven't experienced this yet as I just found out about it like you did.

     
    #10     Nov 13, 2013