erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.83k stars 765 forks source link

'list' object has no attribute 'includeExpired' #564

Closed kadnan closed 1 year ago

kadnan commented 1 year ago

I am calling qualifyContracts on the list of Options object and it is giving an error:

AttributeError: 'list' object has no attribute 'includeExpired'

Below is my code:

from ib_insync import *
from random import getrandbits
from time import sleep
import logging, sys

if __name__ == '__main__':
    # logging.basicConfig(
    #     level=logging.INFO,
    #     format="%(asctime)s [%(levelname)s] %(message)s",
    #     handlers=[
    #         # logging.FileHandler("debug.log"),
    #         logging.StreamHandler(sys.stdout)
    #     ]
    # )
    ib = IB()

    client_id = getrandbits(5)
    print('Connecting with the CLIENT ID = ', client_id)
    PORT = 4002
    ticker = 'TSLA'
    ib.connect('127.0.0.1', PORT, clientId=client_id)

    # Define a contract for the underlying stock
    contract = Stock('TSLA', 'SMART', 'USD')
    ib.reqMarketDataType(3)
    ib.reqContractDetails(contract)
    sleep(2)
    ib.qualifyContracts(contract)
    print('CON ID = ', contract.conId)
    chains = ib.reqSecDefOptParams(contract.symbol, '', contract.secType, contract.conId)
    smart_chains = [c for c in chains if c.exchange == 'SMART']
    valid_option_contract_details = []

    for chain in smart_chains:
        print(chain)
        print('*************************')
        strikes = chain.strikes
        expirations = chain.expirations
        print(len(chain.expirations), len(chain.strikes))
        for expiration in expirations:
            if len(valid_option_contract_details) > 0:
                break
            for strike in strikes:
                try:
                    if len(valid_option_contract_details) > 0:
                        break
                    print('Option Contract for Expiry :{} and Strike: {}'.format(expiration, strike))
                    print('==============================================================')
                    option_contract = Option('TSLA',lastTradeDateOrContractMonth=expiration,strike=strike,right='PUT')
                    details = ib.reqContractDetails(option_contract)
                    ib.sleep(2)
                    if len(details) > 0:
                        # print('DETAIL FOUND')
                        print(details)
                        valid_option_contract_details.append(details)
                    # print(len(valid_option_contract_details))
                except Exception as ex:
                    print('Exception in req Con')
                    print(str(ex))

        if len(valid_option_contract_details) > 0:
            qualified_contracts = ib.qualifyContracts(*valid_option_contract_details)
            print('Displaying Qualified Contracts')
            print(qualified_contracts)

    print('DONE')
    # ticker = ib.reqMktData(contract, genericTickList='', snapshot=False, regulatorySnapshot=False)
    # ib.sleep(2)
    # ib.disconnect()

Ib sync Version: 0.9.81

kadnan commented 1 year ago

I found the reason: valid_option_contract_details.append(details) should be: valid_option_contract_details.append(option_contract)

But then it gave another error which I better ask here:

Ambiguous contract: Option(symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='PUT'), possibles are [Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='SMART', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='AMEX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='CBOE', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='PHLX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='PSE', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='ISE', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='BOX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='BATS', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='NASDAQOM', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='CBOE2', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='NASDAQBX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='MIAX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='GEMINI', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='EDGX', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='MERCURY', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='PEARL', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='EMERALD', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA'), Contract(secType='OPT', conId=611049133, symbol='TSLA', lastTradeDateOrContractMonth='20230310', strike=15.0, right='P', multiplier='100', exchange='IBUSOPT', currency='USD', localSymbol='TSLA  230310P00015000', tradingClass='TSLA')]
805karansaini commented 1 year ago

Hi @kadnan the method 'ib.qualifyContracts' take the contracts as input. and This will fill in the missing fields in the contract, especially the conId. But sometimes the contacts can be Ambiguous. So while creating the option_contract = Option('TSLA',lastTradeDateOrContractMonth=expiration,strike=strike,right='PUT') I would suggest you also specify more details about the contract. (I think giving exchange = 'SMART' will do the trick in this case.)