erdewit / ib_insync

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

Contract can't be hashed #676

Closed jjharr closed 5 months ago

jjharr commented 6 months ago

I'm using ib_insync-0.9.86, which is the current version as of today.

Error is:

ValueError: Contract Stock(symbol='TZA', exchange='SMART', currency='USD') can't be hashed

To reproduce:

    hist = ib.fetch_history(Stock('AMD', 'SMART', 'USD'),
                    datetime.datetime(2023, 12, 21, 0, 0, 0, tzinfo=zoneinfo.ZoneInfo('America/New_York')),
                    duration_str = '1 day',
                    bar_size = '1 secs',
                    )
mattsta commented 6 months ago

Where is fetch_history from?

You're probably trying to use Stock() as a dict key, but the Stock classes aren't frozen/immutable, so they can't be used as dict keys.

Simplest solution is to index by symbol only, or you can combine (symbol, exchange, currency) into a tuple for indexing your results too.

erdewit commented 5 months ago

The conId is used as the hash for a contract, so the conId field must be filled for the contract to be hashable. This can be done with ib.qualifyContracts(contract), for example.