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.
Hope this helps,
闯ü谤驳别苍