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

前端 未结 1 1509
独厮守ぢ
独厮守ぢ 2021-01-16 17:28

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

相关标签:
1条回答
  • 2021-01-16 18:18
    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

    0 讨论(0)
提交回复
热议问题