安装 IB API 时遇到错误 "IB API required"

Encounter error "IB API required" when IB API is installed

我正在试用这个新的 python 软件包 ib_insync。

https://github.com/erdewit/ib_insync

我运行下面的python脚本;

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)

# convert to pandas dataframe:
df = util.df(bars)
print(df[['date', 'open', 'high', 'low', 'close']])

我遇到错误 IB API from http://interactivebrokers.github.io is required

我已经在 C:\TW_API 文件夹下安装了 IB API ver 9.73.06。我正在使用 Windows 10 和 anaconda python v3.6 .

这是我的 TWS API 设置的快照;

我找到了我自己问题的答案。问题是 ib-api python 模块没有安装到我正在使用的 python 版本中

C:\TW_API\source\pythonclient 中找到 README.md 按照说明,我 运行 以下命令;

$ python setup.py bdist_wheel
$ python -m pip install --user --upgrade dist/ibapi-9.73.6-py3-none-any.whl

我的 python 脚本现在运行正常,没有错误。

安装 TWS API 后,找到文件夹 "TWS API"。在该文件夹中,找到名为 "source" 的文件夹,然后找到名为 "pythonclient" 的文件夹。

进入文件夹 "pythonclient" 后,复制其中的所有内容并将其粘贴到您的工作目录中。

您应该不会再遇到 IB API from http://interactivebrokers.github.io is required 错误。

这是当 Python 解释器找不到模块时发生的一般错误,因为它没有在 PYTHONPATH 环境变量中列出。一种选择是将 ibapi 模块安装为 Wheel。或者,如果您使用的是 IDE,例如 PyCharm,您只需右键单击 IDE 中项目目录中的 pythonclient 文件夹,然后选择 "Mark Directory as Source"。 recorded IBKR Python API webinar中提到了这一点。

由于PythonAPI程序依赖于API的特定版本,许多用户也喜欢使用虚拟环境(virtualenv)来组织特定于版本的依赖代码而不是全局安装所有东西。

对于 windows 10: 也许从 2022 年 2 月起,above-mentioned 选项的 none 有效。请尝试以下操作:

import sys
sys.path.append("C:\TWS_API\source\pythonclient")
import ibapi

TWS API 安装在这个文件夹中:c:\TWS_API