python - 盈透证券(IB)Python API : Unable to run the IB tutorial sample on VS Code

标签 python api interactive-brokers

我是Python新手,我使用的是Windows 7,并且已经下载并安装了TWS API (9.76.01)(我的TWS运行的是972.1),并按照指示成功安装了ibapi python:

1) python setup.py sdist
2) python setup.py bdist_wheel
3) python -m pip install --user --upgrade dist/ibapi-9.76.01-py3-none-any.whl

我加载了 VS Code 并从 IB 复制了教程示例代码:https://cdcdyn.interactivebrokers.com/webinars/TA-2018-TWS-Python-Receiving-Market-Data-Study-Notes.pdf (Youtube视频:https://www.youtube.com/watch?v=GmTPDzcko6k)

我使用的示例代码来自文档“为 AAPL 请求流媒体市场数据的示例”的第 2 页

运行代码后,终端上没有任何输出。但是,VS Code 上的“main()”末尾有一条消息,说明以下屏幕截图:

Unable to import 'ibapi.client'pylint(import-error)
Unable to import 'ibapi.wrapper'pylint(import-error)
Unable to import 'ibapi.contract'pylint(import-error)
Unable to import 'ibapi.ticktype'pylint(import-error)
Undefined variable 'TestApp'pylint(undefined-variable)

我是否遗漏了某些内容或步骤?谢谢!

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum


class TestApp(EWrapper, EClient):
 def __init__(self):
 EClient.__init__(self, self)
 def error(self, reqId, errorCode, errorString):
 print("Error: ", reqId, " ", errorCode, " ", errorString)
 def tickPrice(self, reqId, tickType, price, attrib):
 print("Tick Price. Ticker Id:", reqId, "tickType:",
       TickTypeEnum.to_str(tickType), "Price:", price, end=' ')

 def tickSize(self, reqId, tickType, size):
 print("Tick Size. Ticker Id:", reqId, "tickType:",
       TickTypeEnum.to_str(tickType), "Size:", size)


def main():
 app = TestApp()
 app.connect("127.0.0.1", 7497, 0)
 contract = Contract()
 contract.symbol = "AAPL"
 contract.secType = "STK"
 contract.exchange = "SMART"
 contract.currency = "USD"
 contract.primaryExchange = "NASDAQ"
 # switch to delayed-frozen data if live is not available
 app.reqMarketDataType(4)
 app.reqMktData(1, contract, "", False, False, [])
 app.run()


if __name__ == "__main__":
 main()

enter image description here

最佳答案

如果您查看 TWS API 安装,您会发现一个名为 ibapi 的文件夹。其中包含定义您缺少的 TWS 类的 Python 模块。您需要设置 PYTHONPATH 环境变量以包含此目录。

关于python - 盈透证券(IB)Python API : Unable to run the IB tutorial sample on VS Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137625/

相关文章:

php - tmhOAuth 不起作用 - Twitter API 1.1

api - 反向维基百科地理标记查找

java - IB 网关作为服务运行

interactive-brokers - 在盈透证券 API 中获取上市期权和 future 的参数

python - 对于所有列,给定列中的所有行都必须匹配

python - 在 Python UML 类图中指定 self 参数

amazon-web-services - Aws API 端点类型 - Http 与 http 代理之间有什么区别?

python - "TclError: no display name and no $DISPLAY environment variable"nltk错误

python - 使用 xlwt 将 Python 列表写入 Excel 行

链接到 Ox 的 C 库