如何使用python3.7处理ib_insync中的异步错误?

时间:2018-08-14 11:51:18

标签: python python-3.x algorithmic-trading interactive-brokers ib-api

对不起,我可能会问一个愚蠢的问题,但我只是python和algotrading的初学者。我现在使用带有ib_insync的Python 3.7和ibapi尝试连接交易平台。但是,由于Python 3.7使用async作为关键字,所以当我尝试使用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'])

它最终像这样:

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

我知道我需要将异步名称更改为其他名称。我试图在ibapi中修改文件client.py,但似乎根本不起作用。我应该更改代码的哪一部分才能使其正常工作?

2 个答案:

答案 0 :(得分:1)

我在API 973.07中偶然发现了相同的错误。在我的情况下,我使用MacOS,因此文件路径看起来与Windows环境不同,但是想法是相同的。为了解决这个问题,我只是在文件IBJts / source / pythonclient / build / lib / ibapi / client.py中注释了第60行,如下所示:

#        self.async = False

不确定是否会影响其他任何内容,但至少我可以通过以下方式编译并正确安装库:

python3 setup.py install

第123行的PS在同一文件中有注释

#TODO: support async mode

希望我的更改没有任何改变

答案 1 :(得分:1)

安装IB API SW的最新版本(API_Version = 9.75.01)-修复了该问题。它取决于Python 3.7-对我来说,相同的代码在运行Python 3.6时效果很好。