Python IBAPI reqContractDetails 在 运行 第二次时不会 return 结果
Python IBAPI reqContractDetails won't return result when run second time
我想请求合同详细信息,我第一次 运行 代码时它起作用了,但是当我再次按 运行 时,它不会 return 任何东西。如果我退出 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()
没有错误消息,只是没有结果:"Process finished with exit code 0"。
app.run()
启动一个线程从套接字读取。您的程序永远不会停止 运行,因此始终连接到交易平台。尝试点击 TWS 中的数据菜单按钮。它显示了您的所有连接。您将看到客户端 0 保持连接状态。显然关闭 pyCharm 会杀死程序。
您还要求在程序准备好之前提供合同详细信息。您应该等待 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()
我想请求合同详细信息,我第一次 运行 代码时它起作用了,但是当我再次按 运行 时,它不会 return 任何东西。如果我退出 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()
没有错误消息,只是没有结果:"Process finished with exit code 0"。
app.run()
启动一个线程从套接字读取。您的程序永远不会停止 运行,因此始终连接到交易平台。尝试点击 TWS 中的数据菜单按钮。它显示了您的所有连接。您将看到客户端 0 保持连接状态。显然关闭 pyCharm 会杀死程序。
您还要求在程序准备好之前提供合同详细信息。您应该等待 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()