gpt4 book ai didi

第二次运行时,Python IBAPI reqContractDetails 不会返回结果

转载 作者:行者123 更新时间:2023-12-05 03:59:44 24 4
gpt4 key购买 nike

我想请求契约(Contract)详细信息,并且它在我第一次运行代码时有效,但是当我再次按下运行时,它不会返回任何内容。如果我退出 TWS 和 pycharm 并重试,它将起作用我是 python 的新手,不了解它的整体工作原理,请帮助。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ContractSamples import ContractSamples


class TestApp(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, self)

def error(self, reqId:TickerId, errorCode:int, errorString:str):
print("Error: ", reqId, "", errorCode, "", errorString)

def contractDetails(self, reqId:int, contractDetails:ContractDetails):
print("contractDetail: ", reqId, " ", contractDetails)


def main():
app = TestApp()

app.connect("127.0.0.1", 7496, 0)

contract = Contract()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"

app.reqContractDetails(10, contract)

app.run()


if __name__ == "__main__":
main()

没有错误消息,只是没有结果:“进程已完成,退出代码为 0”。

最佳答案

app.run() 启动一个线程以从套接字读取数据。您的程序永远不会停止运行,因此始终连接到交易平台。尝试点击 TWS 中的数据菜单按钮。它显示了您的所有连接。您将看到客户端 0 保持连接状态。显然关闭 pyCharm 会终止程序。

您还需要在程序准备好之前提供契约(Contract)详细信息。您应该等待 nextValidId 然后发送请求。收到数据后,您可以停止程序,它将释放 clientId 以供将来连接使用。以下是我将如何更改您的程序。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ContractSamples import ContractSamples


class TestApp(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, self)

def nextValidId(self, orderId:int):
print("id", orderId)
contract = Contract()
contract.symbol = "IBKR"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"

self.reqContractDetails(10, contract)

def error(self, reqId:TickerId, errorCode:int, errorString:str):
print("Error: ", reqId, "", errorCode, "", errorString)

def contractDetails(self, reqId:int, contractDetails:ContractDetails):
print("contractDetail: ", reqId, " ", contractDetails)

def contractDetailsEnd(self, reqId:int):
print("end, disconnecting")
self.disconnect()

def main():
app = TestApp()

app.connect("127.0.0.1", 7496, 0)
app.run()

if __name__ == "__main__":
main()

关于第二次运行时,Python IBAPI reqContractDetails 不会返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56896031/

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