Getting parameters of listed options & futures in Interactive Brokers API

后端 未结 3 1674
清酒与你
清酒与你 2020-12-28 10:17

There are a lot of examples showing how to get particular asset\'s price from Interactive Brokers. However, when I want to get the whole chain of options for one asset, I do

3条回答
  •  悲&欢浪女
    2020-12-28 11:03

    Figured this out myself.

    There is a function which is able to request the details of listed securities, reqContractDetails. Some sample code requesting E-mini SPX futures (symbol ES) is shown below.

    from ib.ext.Contract import Contract
    from ib.ext.ContractDetails import ContractDetails
    from ib.opt import ibConnection, message
    import time
    
    def watcher(msg):
        print msg
    
    contracts = [] # to store all the contracts
    def contractDetailsHandler(msg):
        contracts.append(msg.contractDetails.m_summary)
    
    con = ibConnection()
    con.registerAll(watcher)
    con.register(contractDetailsHandler, 'ContractDetails')
    con.connect()
    
    contract = Contract()
    contract.m_symbol = "ES"
    contract.m_exchange = "GLOBEX"
    contract.m_currency = "USD"
    contract.m_secType = "FUT"
    
    con.reqContractDetails(1, contract)
    
    time.sleep(2)
    
    con.disconnect()
    

    Now the contracts are saved in the contracts list, we can get all available expirations by:

    for c in contracts:
        print c.m_expiry
    

    Output:

    20140919
    20141219
    20150320
    20150619
    20150918
    

    In an obvious way, this can be extended to options as well.

提交回复
热议问题