Repository navigation
Paper engine: book realized P&L and gross loss per exact execution (stacked on #350; realized-loss delivery-order major) - #353
Closed
seathatflowsinourveins wants to merge 1 commit into
Conversation
… accounting replay The accounting replay summed a status delta's executions into one notional before computing P&L, so a sell filled at a gain and a loss inside one delta netted the loss away, and the result depended on whether the stream or the REST snapshot arrived first. Replay now books each execution that tiles the delta separately (QuantConnect/Lean 985ef30: SecurityPortfolioModel.cs per-fill closing P&L; TradeStatistics.cs accumulates losses separately), falling back to the delta notional only while executions are incomplete. Two regressions: mixed-profit gross loss across stream-first, REST-first, duplicate and restart deliveries, and basis preservation at each booking. With the fix reverted, 4 cases fail (rest_first, duplicates, restart, basis). Paper suites (safety, runner, native, transport): 407 tests OK. Implemented by GPT-6 (gpt-6-astra, effort max) test-first; verified by the coordinator. Pins: source-hashes.json safety.py and manifests/evidence.json. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
seathatflowsinourveins
deleted the branch
claude/paper-engine-crossfamily-fixes-20260926
September 26, 2026 16:57
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This is the stacked follow-up to #350, and it closes the pre-existing realized-loss delivery-order major from the retroactive cross-family review. The accounting replay (
Ledger._reconcile_execution_accounting,blueprints/us-equities/adaptive-paper/safety.py) summed a status delta's executions into one notional before computing P&L. Take a two-share sell with one fill at a gain and one at a loss inside the same delta: the replay netted the loss away. The resulting gross loss then depended on whether the stream or the REST snapshot arrived first. With two shares bought at $100 and sold at $110 and $90, REST-first delivery booked a gross loss of $0 where stream-first booked $10, so the $5 loss cap did not halt.Replay now books each exact execution that tiles the delta separately. It falls back to the delta's notional only while the executions are still incomplete, and a later replay over the complete executions corrects it.
_execution_fillsreturns exact (qty, notional) pairs;_executions_notionalkeeps its old contract on top of it.Base:
claude/paper-engine-crossfamily-fixes-20260926(#350). Merge #350 first; GitHub then retargets this PR tomain.Evidence
tests/test_adaptive_paper_safety.py):test_mixed_profit_gross_loss_is_exact_across_delivery_sequencescovers stream-first, REST-first, duplicate and restart deliveries. It checks cash, realized P&L, cumulative loss, gross loss, the halt, redelivery after reopening,read_ledgerand the blocked intents.test_mixed_profit_replay_preserves_basis_at_each_bookingcovers a basis change between executions of one sell.safety.pyreverted, 4 cases fail:rest_first,duplicates,restartand the basis test.stream_firstpasses on both versions, as expected.adaptive-paper-20260921, private TMPDIR):tests.test_adaptive_paper_safety,_runner,_nativeand_transportran 407 tests, all OK.tests.test_trading_gates: 26 OK.scripts/validate.pypassed.safety.pyentry inadaptive-paper/source-hashes.jsonmoves72abaaca…→e0ba9233…, andmanifests/evidence.jsonre-registers the 3 files. Historical receipts keep the hashes of the code they ran.gpt-6-astra, effort max, workspace-write sandbox) implemented it test-first. The coordinator (Claude) reviewed the diff and re-ran every check above. An independent read-only Claude review follows in a comment.Limitations
SOTA sources
985ef30,Common/Securities/SecurityPortfolioModel.cs: closing P&L per fill. https://github.com/QuantConnect/Lean/blob/985ef30/Common/Securities/SecurityPortfolioModel.cs985ef30,Common/Statistics/TradeStatistics.cs: losing P&L accumulated separately from net. https://github.com/QuantConnect/Lean/blob/985ef30/Common/Statistics/TradeStatistics.cstrade_updatesfill and partial_fill carry per-execution qty and price): https://docs.alpaca.markets/docs/websocket-streaming🤖 Generated with Claude Code