ib_insync "contract can't be hashed"

189 views Asked by At

A process I have that's been running for a couple of years suddenly stopped working. I avoided updating much in the way of python and packages to avoid that..

I've now updated ib_insync to the latest version, and no improvement. debugging a little gives me this:

the code import ib_insync as ibis ib = ibis.IB() contract = ibis.Contract() contract.secType = 'STK' contract.currency = 'USD' contract.exchange = 'SMART' contract.localSymbol = 'AAPL' ib.qualifyContracts(contract)

Result: File "/Users/macuser/anaconda3/lib/python3.6/site-packages/ib_insync/client.py", line 244, in send if field in empty: File "/Users/macuser/anaconda3/lib/python3.6/site-packages/ib_insync/contract.py", line 153, in hash raise ValueError(f'Contract {self} can't be hashed') ValueError: Contract Contract(secType='STK', exchange='SMART', currency='USD', localSymbol='AAPL') can't be hashed Exception ignored in: <bound method IB.del of <IB connected to 127.0.0.1:7497 clientId=6541>> Traceback (most recent call last): File "/Users/macuser/anaconda3/lib/python3.6/site-packages/ib_insync/ib.py", line 233, in del File "/Users/macuser/anaconda3/lib/python3.6/site-packages/ib_insync/ib.py", line 281, in disconnect File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1306, in info File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1442, in _log File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1452, in handle File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1514, in callHandlers File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 863, in handle File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1069, in emit File "/Users/macuser/anaconda3/lib/python3.6/logging/init.py", line 1059, in _open NameError: name 'open' is not defined



| => python --version
Python 3.6.4 :: Anaconda, Inc.
2

There are 2 answers

0
William Henderson On

I am not OP, but have had a similar problem. I am attempting to send an order to IB using ib_insync.

contract = Stock('DKS','SMART','USD')
order = LimitOrder('SELL', 1, 1)

try:
    ib.qualifyContracts(contract)
    trade = ib.placeOrder(contract, order)
except Exception as e:
    print(e)

Here is the exception that is returned:

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

I understand that lists and other mutable can't be hashed, but I don't understand why this wouldn't work. It pretty clearly follows the examples in the ib_insync docs.

0
William Henderson On

FYI - I was able to resolve this issue by updating to the latest ib_insync version. Perhaps this will help you as well.