from ib.opt import Connection, messagefrom ib.ext.Contract import Contractfrom ib.ext.Order import Orderfrom ib.ext.CommissionReport import CommissionReportfrom ib.ext.TickType import TickType as tt
使函数可以处理您感兴趣的每种回调类型。
def error_handler(msg): print (msg)def execDetails(msg): print('ID',msg.execution.m_execId,'PRICE',msg.execution.m_price)def commReport(msg): print('ID',msg.commissionReport.m_execId,'COM',msg.commissionReport.m_commission)tws = Connection.create(port = 4001, clientId=123)tws.register(execDetails, message.execDetails)tws.register(commReport, message.commissionReport)tws.register(error_handler, 'Error')tws.connect()
您应该等待
connect()完成,我通常只在准备就绪时使用nextOrderId回调通知我,但是在python中您可以sleep(2)或在这种情况下,我使用的是笔记本,因此我稍后才运行下一个单元格。
fx = Contract()fx.m_secType = "CASH" fx.m_symbol = "USD"fx.m_currency = "CAD"fx.m_exchange = "IDEALPRO"#tws.reqMktData(1,fx,"",False)ord = Order()ord.m_orderType = 'MKT'ord.m_totalQuantity = 100000ord.m_action = 'SELL'tws.placeOrder(123,fx,ord) #increment this every order
此打印
ID 0001f4e8.57427bd9.01.01 PRICE 1.31565ID 0001f4e8.57427bd9.01.01 COM 2.6313`
别忘
tws.disconnect()了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)