← Changelog

Library-Wide PnL Math Audit and Side-Price Correction

Patch7 June 2026· library-wide

Audit / Issue Found

After the V2 fix that solved the catastrophic accounting bug in Glosten-Milgrom and Hawkes, every realized-profit-and-loss formula across the library was catalogued and verified. The audit grouped them into five patterns, of which two were already correct, two were already correctly fixed in earlier rounds, and one had a smaller systematic bias that needed cleanup. Five additional strategies got the fix.

Before the patch

The catalog turned up five families of realized profit-and-loss formulas:

Pattern A: (qty × bid - qty × break-even) - qty × bid × fee-percent is used by every market-sell exit across the trend market-makers. Bid is the correct close price for these because the exit goes through the market sell method, which fills at the live bid. The fee is correctly subtracted. This pattern is correct.

Pattern B: (mid - break-even) × sign × qty is used by the inventory-delta detectors in cartaJai, OFI flash, queue-reactive, microprice, the balance-drop detector in Roll model, and the balance-drop detector in Harris scalper. Mid is wrong as the close-price proxy. Post-only fills land at the strategy's own quoted ask (long sells) or quoted bid (short covers), which sit half a spread above or below mid. Market closes land at the live touch which sits half a spread on the other side. Using mid systematically biases realized profit-and-loss by roughly half a spread per fill in the wrong direction. The dominant direction is under-reporting profit on the routine post-only spread captures.

Pattern C: uses exit_price chosen based on whether the close was a market exit or a post-only exit. Used by cointegration pairs and mean-reversion oscillator. This pattern is correct (it was fixed in an earlier round).

Pattern D: uses match_price recovered from the pending-sells queue, which is the actual rung price the strategy posted at. Used by the Kalman grid. This pattern is correct.

Pattern E: uses cash-delta to recover the average sell price, then qty × (avg_sell_price - break-even). Used by Glosten-Milgrom and Hawkes after the V2 fix earlier in this session. This pattern is correct.

After the patch

The six strategies that used Pattern B now use side-appropriate close prices.

For cartaJai, OFI flash, queue-reactive, and microprice, the formula now picks the strategy's own quoted ask for long sells and quoted bid for short covers. A check against the carry-over cycle tag picks the live touch instead when a tagged market action fired in the same cycle, so cap-reduces and scratch closes book at the actual exchange touch rather than at the post-only quote. The tag-based switch is the correct distinction for these strategies.

For Roll model's balance-drop detector, the formula now uses the strategy's own exit-sell-price (the take-profit target set at break-even plus a small buffer) as the close-price proxy. The market scratch-sell path retains its direct Pattern A formula and is excluded from the detector via the attribution carry-over.

For Harris scalper's balance-drop detector, the formula now uses the strategy's own exit-price (the micro-profit target) as the close-price proxy. The market exit path retains its Pattern A formula and is similarly excluded.

Bottom line

Every realized-profit-and-loss formula in the library has been reviewed and is now mathematically correct against the actual fill prices the strategy receives, not against a mid-price approximation. The biggest gain is for the post-only-heavy strategies where the previous under-reporting could shave one to several basis points per fill from the apparent profit. Over hundreds of fills that adds up to real numbers on the dashboard that now match what the exchange settles.

Caveats: The fix uses the strategy's own quoted side rather than the per-fill actual price recovered from a cash delta, because the futures strategies do not expose a clean cash field. On a calm market the quoted side and the actual fill are within ticks of each other, so the bias is negligible. On a fast-moving market where the strategy refreshes its quotes between cycles, the per-fill actual price could differ from the quoted side; the bias in that case is bounded by the inter-cycle price drift and is much smaller than the chronic mid-as-proxy bias the fix replaces.