How to get the trading price and commission in Interactive Brokers (IBPY) after placing an order?

南笙酒味 提交于 2019-12-19 11:18:03

问题


http://interactivebrokers.github.io/tws-api/ maybe a useful link. This picture is from java API guide of Interacitve Brokers and the numbers I want are price and commission in trade log.


回答1:


from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.ext.CommissionReport import CommissionReport
from ib.ext.TickType import TickType as tt

Make functions to handle each type of callback you're interested in.

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()

You should wait for connect() to finish, I usually just use the nextOrderId callback to notify me when ready but in python you could sleep(2) or in this case I'm using the notebook so I just run the next cells later.

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

This prints

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

Don't forget tws.disconnect() at some point



来源:https://stackoverflow.com/questions/37382451/how-to-get-the-trading-price-and-commission-in-interactive-brokers-ibpy-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!