简体   繁体   English

如何通过Interactive Brokers TWS Java API获得P&L的交易

[英]How to get P&L on a trade through Interactive Brokers TWS Java API

有没有办法通过其Java API获得IB TWS特定交易的盈亏(每日和总到日期)?

You can, but not in the way you seem to be asking. 你可以,但不是你想要的方式。 All profit and loss in the API is calculated by you until the trade is closed and then you can use the commissionReport method of the wrapper. API中的所有损益都由您计算,直到交易结束,然后您可以使用包装器的commissionReport方法。 A commissionReport is sent after every execDetails. 在每个execDetails之后发送一个commissionReport。 API doc API文档

You can always check your statements for previous profits and losses. 您可以随时查看您的报表以了解以前的损益。

The flow is like this. 流程是这样的。

  • place trade and get fill price from execDetails 从execDetails进行交易并获得填充价格
  • get opening commission from commissionReport 从CommissionReport获得开放佣金
  • on every tick calculate open position profit, use bid/ask for realism, but it's all forex has anyway 在每个滴答计算开放头寸利润,使用买/卖现实主义,但无论如何它都是外汇
  • close trade and get price from execDetails 关闭交易并从execDetails获得价格
  • get commission from commissionReport again 从CommissionReport再次获得佣金
  • calculate closed trade profit/loss 计算已结算的贸易盈亏
  • also note that commissionReport has a field m_realizedPNL you can use, but I've never tried it. 另请注意,commissionReport有一个你可以使用的字段m_realizedPNL,但我从未尝试过。

In the TWS v9.72+ API there is a reqPnl method on the EClient which can be used to subscribe to real-time PnL (unrealized and realized) updates for a full portfolio via the associated method on the EWrapper 在TWS v9.72 + API中, reqPnl上有一个reqPnl方法,可用于通过EWrapper上的相关方法订阅完整产品组合的实时PnL(未实现和已实现)更新。

https://interactivebrokers.github.io/tws-api/classIBApi_1_1EClient.html#a0351f22a77b5ba0c0243122baf72fa45 https://interactivebrokers.github.io/tws-api/classIBApi_1_1EClient.html#a0351f22a77b5ba0c0243122baf72fa45

Additionally, for a single contractID, you can use: reqPnLSingle on the Client. 此外,对于单个contractID,您可以在客户端上使用: reqPnLSingle

https://interactivebrokers.github.io/tws-api/interfaceIBApi_1_1EWrapper.html#aebeb008f2b763d7bed2969b66bbd1b33 https://interactivebrokers.github.io/tws-api/interfaceIBApi_1_1EWrapper.html#aebeb008f2b763d7bed2969b66bbd1b33

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM