gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:48:12 43 4
gpt4 key购买 nike

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

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com