下订单后如何在盈透证券(IBPY)中获得交易价格和佣金? [英] How to get the trading price and commission in Interactive Brokers (IBPY) after placing an order?

查看:234
本文介绍了下订单后如何在盈透证券(IBPY)中获得交易价格和佣金?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://interactivebrokers.github.io/tws-api/也许有用关联. 这张图片来自Interacitve Brokers的Java API指南,我想要的数字是交易日志中的价格和佣金.

推荐答案

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

您应该等待connect()完成,我通常只使用nextOrderId回调在准备就绪时通知我,但是在python中您可以sleep(2)或在这种情况下我正在使用笔记本电脑,因此我只运行下一个细胞.

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

此打印

ID 0001f4e8.57427bd9.01.01 PRICE 1.31565
ID 0001f4e8.57427bd9.01.01 COM 2.6313`

在某些时候不要忘记tws.disconnect()

这篇关于下订单后如何在盈透证券(IBPY)中获得交易价格和佣金?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆