gpt4 book ai didi

python - 下单后如何获取盈透证券(IBPY)的交易价格和佣金?

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:53 27 4
gpt4 key购买 nike

最佳答案

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

关于python - 下单后如何获取盈透证券(IBPY)的交易价格和佣金?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com