下订单后如何在盈透证券(IBPY)中获得交易价格和佣金?

发布于 2021-01-29 16:44:20
关注者
0
被浏览
44
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。
    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
    

    使函数可以处理您感兴趣的每种回调类型。

    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 = 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`
    

    别忘tws.disconnect()



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看