我是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

我使用的示例代码来自文档“为APL请求流市场数据的示例”的第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()


python - 盈透证券(IB)Python API:无法在VS Code上运行IB教程示例-LMLPHP

最佳答案

如果浏览TWS API安装,将找到一个名为ibapi的文件夹。它包含定义您缺少的TWS类的Python模块。您需要设置PYTHONPATH环境变量以包含此目录。

Algorithmic Trading with Interactive Brokers

09-25 19:39