Search code examples
forextwsinteractive-brokers

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


Is there any way to get Profit and Loss(daily & total till date) on a particular trade made on IB TWS through its Java API?


Solution

  • 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. A commissionReport is sent after every execDetails. API doc

    You can always check your statements for previous profits and losses.

    The flow is like this.

    • place trade and get fill price from execDetails
    • get opening commission from 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
    • get commission from commissionReport again
    • calculate closed trade profit/loss
    • also note that commissionReport has a field m_realizedPNL you can use, but I've never tried it.