Python 下订单后,如何在交互式经纪人(IBPY)中获得交易价格和佣金?

Python 下订单后,如何在交互式经纪人(IBPY)中获得交易价格和佣金?,python,interactive-brokers,ibpy,Python,Interactive Brokers,Ibpy,也许是一个有用的链接。 创建函数来处理您感兴趣的每种回调类型 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.commissio

也许是一个有用的链接。

创建函数来处理您感兴趣的每种回调类型

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中,您可以睡眠(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 = 100000
ord.m_action = 'SELL'
tws.placeOrder(123,fx,ord) #increment this every order
这张照片

ID 0001f4e8.57427bd9.01.01 PRICE 1.31565
ID 0001f4e8.57427bd9.01.01 COM 2.6313`

别忘了在某个时候断开连接()

非常感谢!你的代码完美地解决了我的问题。
ID 0001f4e8.57427bd9.01.01 PRICE 1.31565
ID 0001f4e8.57427bd9.01.01 COM 2.6313`