ibpy 如何使用 interactive broker 的 API 获得佣金?
ibpy how to get commission using interactive broker's API?
我有以下使用 IB 的 python API 的代码,它应该同时显示价格和佣金:
from ib.ext.ExecutionFilter import ExecutionFilter
from ib.ext.CommissionReport import CommissionReport
from ib.opt import ibConnection, message
from time import sleep
#-- message handlers -------------------------------------------------
# print all messages from TWS
def watcher(msg):
pass
def ExecutionDetailsHandler(msg):
global execDetails
execDetails = msg.execution
#print execDetails.m_price
#print execDetails.m_side
def CommissionDetailsHandler(msg):
global commission
commission = msg.commissionReport
# global variable that stores the last Execution seen by
ExecutionDetailsHandler
CommissionDetailsHandler
execDetails = None
commission = None
#-- factories
#-----------------------------------------------------------
def makeExecFilter():
filter=ExecutionFilter()
return filter
#-- utilities --------------------------------------------------------
def getExecutionPrice():
filter=makeExecFilter()
con.reqExecutions(744,filter)
# wait for TWS message to come back to message handler
while execDetails is None:
print 'waiting'
sleep(1)
return execDetails.m_price
def getCommission():
filter=CommissionReport()
con.commissionReport(filter)
# wait for TWS message to come back to message handler
while commission is None:
print 'waiting'
sleep(1)
return commission.m_commission
con = ibConnection()
con.registerAll(watcher)
con.register(ExecutionDetailsHandler, 'ExecDetails')
con.register(CommissionDetailsHandler, 'commissionDetails')
con.connect()
price=getExecutionPrice()
c = getCommission()
con.disconnect()
print 'The price of one execution is:', price
print 'The commission fee is:', c
然而这只适用于执行价格,因为它在打印出来后显示价格信息。但是它没有显示佣金信息(在我的终端里它一直在等待),我的代码有什么问题吗?
感谢 brian 的回答,这很有效:
commission = None
def commReport(msg):
global commission
#print('ID',msg.commissionReport.m_execId,'COM',msg.commissionReport.m_commission)
commission = msg.commissionReport.m_commission
conn = Connection.create(port=7496, clientId=222)
conn.register(commReport, message.commissionReport)
conn.connect()
现在我很乐意在任何我想使用的地方使用佣金
我有以下使用 IB 的 python API 的代码,它应该同时显示价格和佣金:
from ib.ext.ExecutionFilter import ExecutionFilter
from ib.ext.CommissionReport import CommissionReport
from ib.opt import ibConnection, message
from time import sleep
#-- message handlers -------------------------------------------------
# print all messages from TWS
def watcher(msg):
pass
def ExecutionDetailsHandler(msg):
global execDetails
execDetails = msg.execution
#print execDetails.m_price
#print execDetails.m_side
def CommissionDetailsHandler(msg):
global commission
commission = msg.commissionReport
# global variable that stores the last Execution seen by
ExecutionDetailsHandler
CommissionDetailsHandler
execDetails = None
commission = None
#-- factories
#-----------------------------------------------------------
def makeExecFilter():
filter=ExecutionFilter()
return filter
#-- utilities --------------------------------------------------------
def getExecutionPrice():
filter=makeExecFilter()
con.reqExecutions(744,filter)
# wait for TWS message to come back to message handler
while execDetails is None:
print 'waiting'
sleep(1)
return execDetails.m_price
def getCommission():
filter=CommissionReport()
con.commissionReport(filter)
# wait for TWS message to come back to message handler
while commission is None:
print 'waiting'
sleep(1)
return commission.m_commission
con = ibConnection()
con.registerAll(watcher)
con.register(ExecutionDetailsHandler, 'ExecDetails')
con.register(CommissionDetailsHandler, 'commissionDetails')
con.connect()
price=getExecutionPrice()
c = getCommission()
con.disconnect()
print 'The price of one execution is:', price
print 'The commission fee is:', c
然而这只适用于执行价格,因为它在打印出来后显示价格信息。但是它没有显示佣金信息(在我的终端里它一直在等待),我的代码有什么问题吗?
感谢 brian 的回答,这很有效:
commission = None
def commReport(msg):
global commission
#print('ID',msg.commissionReport.m_execId,'COM',msg.commissionReport.m_commission)
commission = msg.commissionReport.m_commission
conn = Connection.create(port=7496, clientId=222)
conn.register(commReport, message.commissionReport)
conn.connect()
现在我很乐意在任何我想使用的地方使用佣金