from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class MyWrapper(EWrapper):
def nextValidId(self, orderId:int):
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
# start program here or use threading
app.reqContractDetails(4444, contract)
def contractDetails(self, reqId, contractDetails):
print(reqId, contractDetails.contract)# my version doesnt use summary
def contractDetailsEnd(self, reqId):
print("ContractDetailsEnd. ", reqId)
# this is the logical end of your program
app.disconnect() # delete if threading and you want to stay connected
def error(self, reqId, errorCode, errorString):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))
from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"
app.run() # delete this line if threading
# def runMe():
# app.run()
# import threading
# thread = threading.Thread(target = runMe)
# thread.start()
# input('enter to disconnect')
# app.disconnect()
在启动消息阅读器之前,您需要数据。也许你能在数据开始之前得到它。
ib建议您在收到nextvalidid之后启动程序,这样您就知道所有的东西都运行正常了。由于python API在消息读取循环中阻塞,因此需要实现线程或构造程序以异步运行。
我已经演示了如何做到这一点,这样它就可以在没有用户输入的情况下运行,并且是事件驱动的,或者是异步的。这意味着程序会一直等到它应该做某件事,然后才做。
我已经包含了线程选项,只需更改注释。
ContractDetails.Summary已更改为Contract。我不确定它是否是python的摘要,不知道从哪里得到的。