how to handle async error in ib_insync with python3.7?
Asked Answered
S

2

6

Sorry I may ask a stupid question but I am just a beginner in python and algotrading. I am now using Python 3.7 and ibapi with ib_insync to try connect TWS. However, as Python 3.7 use async as a keyword so when I try to code with ib_insync:

from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 7496, clientId=1)

contract = Forex('EURUSD')
bars = ib.reqHistoricalData(contract, endDateTime='', durationStr='30 D', barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True)

df = util.df(bars)
print(df['date', 'open', 'high', 'low', 'close'])

It end up like this:

File "C:/Users/leung/Documents/MONEY/Algotrade/venv/test1.py", line 1, in <module>
    from ib_insync import *
  File "C:\Users\leung\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ib_insync\__init__.py", line 24, in <module>
    from .ib import *
  File "C:\Users\leung\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ib_insync\ib.py", line 11, in <module>
    from ib_insync.client import Client
  File "C:\Users\leung\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ib_insync\client.py", line 10, in <module>
    from ibapi.client import EClient
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 906, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1254, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1235, in _legacy_get_spec
  File "<frozen importlib._bootstrap>", line 441, in spec_from_loader
  File "<frozen importlib._bootstrap_external>", line 594, in spec_from_file_location
  File "C:\Users\leung\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ibapi-9.73.7-py3.7.egg\ibapi\client.py", line 60
    self.async = False
             ^
SyntaxError: invalid syntax

Process finished with exit code 1

I know I need to change the name async to other name. I tried to modify the file client.py in ibapi but it seems not work at all. Which part of code I should change in order to get it work?

Squall answered 14/8, 2018 at 11:51 Comment(0)
I
2

Install newest version (API_Version=9.75.01) of IB API SW - this issue is fixed there. It is Python 3.7 dependent - same code worked well running Python 3.6 for me.

Insurance answered 12/4, 2019 at 13:36 Comment(0)
R
1

I stumbled upon same error in API 973.07. In my case I use MacOS, so file path looks different from your Windows environment, but idea is the same. To fix the issue I just commented out line 60 in file IBJts/source/pythonclient/build/lib/ibapi/client.py to look like:

#        self.async = False

Not sure if it will affect anything else, but at least I can compile and properly install library via:

python3 setup.py install

PS in line 123 os the same file there is a comment

#TODO: support async mode

Hopefully my change doesn't break anything

Reina answered 8/9, 2018 at 2:34 Comment(1)
I tried but it doesn't work, at the end I fixed the problem by use python 3.6Squall

© 2022 - 2024 — McMap. All rights reserved.