Keyboard Shortcuts
Likes
Search
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:
? |
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]>:
--
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
After dividing by multiplier I can get average open price?
I know average cost in dollars that IB returns.?
In other words, there are always 2 calls.?
|
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. |