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

时间:2016-05-23 03:57:29

标签: python interactive-brokers ibpy

1 个答案:

答案 0 :(得分: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

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

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`

不要忘记tws.disconnect()

相关问题