First let me apologize, somehow I must have missed your original response with questions, thanks again for taking the time to respond.? I am glad you were able to replicate the issue so that I am not alone.
I agree that this is not a high priority issue since it can be easily compensated for, and usually you only need to call reqPNL once.? My issue is that I am still building my platform and debugging.? My code keeps track of the PNL based on responses to trades, specifically I listen for the??call back and record entry and exit trade prices.? As a redundancy I subscribe to reqPNL to make sure the number match.
Since my code is ( or was ) prone to crashing the first thing my code does is reqPNL so it knows what the starting PNL is.? I mostly use IB gateway which does not have this issue.? But I noticed at random times my code froze, after digging I realized it froze waiting for the first PNL.
I will need to look into perhaps the update account value as an alternative, to waiting for the reqPNL
On Sun, Jul 17, 2022 at 11:22 PM 闯ü谤驳别苍 Reinold via <TwsApiOnGroupsIo=[email protected]> wrote:
I had a couple minutes and a paper account with no positions and was able to replicate the TWS behavior you report:
A (paper) account has no positions (other than cash)
The portfolio/account technically has unrealized and realized P&L of 0 but the (real-time data based) TWS portfolio window shows no Daily P&L values or positions at all.
In this case, subscriptions to trigger a single callback only for the first client that connects to TWS, and dailyPnL,, unrealizedPnL,, and realizedPnL? are reported as 0. That client does not receive any further callbacks (there are no positions) and all subsequent clients of that TWS do not event receive the initial callback.
That behavior is definitely inconsistent, but your client should not have to lock up. In fact, for my taste, I would not have expected a callback for the first client to take place:
Subscriptions to receive no callbacks unless the portfolio actually has a position entry for that instrument.
and are real-time market data based P&L calculations and really make only sense if the portfolio actually has positions
The TWS Portfolio window shows no entries or values in that case
You may want to take a look at your client architecture and consider a "PortfoilioModel" or "AccountModel" implementation similar to those used in Model-View-Controller architectures. Callbacks from TWS API subscriptions update values in the model when they happen and your client reads the most recent values from the model when it needs one. During startup, all model values are either initialized with 0 (or "undefined" if that is what you prefer) and your client operates properly with and without the callback.
The TWS API is asynchronous and event oriented in nature and your client must be resilient and always be prepared that an event just does happen (for many reasons).
A couple other quick observations:
TWS does maintain a proper subscription to reqPnL (and reqPnLSingle for non-existing positions) even if the initial zero value callback does not occur.
As soon as on order is placed and a position exists, real-time P&L callback arrive immediately
And finally, subscriptions to does trigger zero value updates for account values "RealizedPnL" and "UnrealizedPnL" during startup for all clients, even those that do not get the reqPnL callback.
You could file a trouble ticket with IBKR for this, but I think it would get a very low priority or may even be rejected by engineering right away.