¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 ¿ªÔÆÌåÓý

How to get position info in "points"


 

What is the most efficient way to get information about position including the fields below.
?
- open time?
- open price
- unrealized PnL?
?
Request "reqPositions" and "reqPositionsMulti" return these?
?
- reqId
- account
- contract
- avgCost
?
1. Instead of average cost, how do I get price of the stock / option / future when position was opened??
2. How do I get time when it was open??
3. There is a separate request for PnL but even if I get PnL, there are some missing pieces, e.g. I can't calculate the range in points between open and current prices of the asset.??


 

It sounds like you are looking in the wrong corner.

At any point in time, a "Position" represents the cumulative effect of one or more Trades (Executions) that were caused by one or more Orders for the instrument in the "Position". The attributes you call "open time" and "open price" are actually attributes of the "Execution" not the "Position".

The "Position" only know about sums and averages.

Your client receives real-time order and execution callbacks as the position changes. You could collect and persist at least the executions if you need to know later, how the position changed over time.

´³¨¹°ù²µ±ð²Ô

?

?

On Thu, Nov 28, 2024 at 01:27 PM, Andy Sanders wrote:

What is the most efficient way to get information about position including the fields below.
?
- open time?
- open price
- unrealized PnL?
?
Request "reqPositions" and "reqPositionsMulti" return these?
?
- reqId
- account
- contract
- avgCost
?
1. Instead of average cost, how do I get price of the stock / option / future when position was opened??
2. How do I get time when it was open??
3. There is a separate request for PnL but even if I get PnL, there are some missing pieces, e.g. I can't calculate the range in points between open and current prices of the asset.??

?


 

If you are not collecting order executions through the API as Juergen suggested, the other way to approach this is to collect and aggregate IBKR's trade reports or custom designed flex reports from email.


§é§ä, 28 §ß§à§ñ§Ò. 2024?§Ô., 18:04 ´³¨¹°ù²µ±ð²Ô Reinold via <TwsApiOnGroupsIo=[email protected]>:

It sounds like you are looking in the wrong corner.

At any point in time, a "Position" represents the cumulative effect of one or more Trades (Executions) that were caused by one or more Orders for the instrument in the "Position". The attributes you call "open time" and "open price" are actually attributes of the "Execution" not the "Position".

The "Position" only know about sums and averages.

Your client receives real-time order and execution callbacks as the position changes. You could collect and persist at least the executions if you need to know later, how the position changed over time.

´³¨¹°ù²µ±ð²Ô

?

?

On Thu, Nov 28, 2024 at 01:27 PM, Andy Sanders wrote:
What is the most efficient way to get information about position including the fields below.
?
- open time?
- open price
- unrealized PnL?
?
Request "reqPositions" and "reqPositionsMulti" return these?
?
- reqId
- account
- contract
- avgCost
?
1. Instead of average cost, how do I get price of the stock / option / future when position was opened??
2. How do I get time when it was open??
3. There is a separate request for PnL but even if I get PnL, there are some missing pieces, e.g. I can't calculate the range in points between open and current prices of the asset.??

?


--
Best,
DS


 

For the future readers.
Using average open price and the current price, you can get PnL in points.?
?
?

Average?

 double average_op = (op1 * lot1 + op2 * lot2 + opN * lotN) / (lot1 + lot2 + lotN);

Profit in points:

 double profit = Bid - average_op;
What I was expected to receive from IB API is an ability to see the PnL in points and dollars separately.?
Appeared that IB returns only average cost in dollars and only for one unit, so there is no need to divide it by number of contracts but it needs to be divided by multiplier.?
?
Examples?
?
What "reqOpenPositions" returns
  • 2 contracts of ESZ4 (ES futures) => 300540
  • 1 contract of E1AZ4 C6020 (ES call option) => 901.42
  • 1 contract of SPY (ETF stock) => 597.94
  • 1 contract of SPY 241203C00600000 (SPY call option) => 155
After dividing by multiplier I can get average open price?
  • ESZ4 => 300540 / 50 => 6010.80
  • E1AZ4 C6020 (ES call option) => 901.42 / 50 => 18.03
  • SPY (ETF stock) => 597.94 / 1 => 597.94
  • SPY 241203C00600000 (SPY call option) => 155 / 100 => 1.55
I know average cost in dollars that IB returns.?
  • to get PnL in points I can subtract average open price from the current price from "reqMktData"
  • to get PnL in dollars I can either make a call to "getPnl" or multiple PnL in points by tick value received from "reqMktData"
In other words, there are always 2 calls.?
  • reqPositions
  • reqMktData or reqPnl?


 

As ´³¨¹°ù²µ±ð²Ô mentioned, you need to keep track of PnL yourself based on the executions.
Reconciliating against reqPnl should be a governance / secondary check function and not be based on for trading.
?
As such it's not directly a TWS related question. For example, more advanced PnL engines will keep track of the following also:
- arrival time
- arrival price (what is current market price when you decide to go long)
- fill times
- fill prices
- average of fill prices
- slippage (fill price - arrival price)
- commisions
- market cost (fill price - mid price on each fill)
- price impact (mid price 3 minutes later vs fill price)
- MAE (in-trade)
- MFE (in-trade)
- draw-down across trades
- profit
- %win
- avg. win
etc...
?
You would need to pay alot of attention to the number handling (especially where you might have zeroes and a division - for example with 0 trades you would return something for %win even if the division by zero number of trades is undefined).
Depending on what you're doing you might want to optimize secondarily also for different parameters (like minimize price impact) or just use one of the existing TWS algos.