fix(orders): full-close exit re-expands to grown position under pyramiding#46
Merged
Merged
Conversation
…iding A qty_percent=100 strategy.exit re-issued every bar while the position grows via pyramiding had its reserved qty frozen at the size from first issue: clear_existing_exit_order captured the prior order's absolute qty as preserved_reserved_qty, and compute_exit_reserved_qty then honored it unconditionally, overriding the 100% intent. At the TP touch only the first FIFO lot closed at the true limit; the residual lots exited one bar late at a re-priced limit. Gate the preserved-qty carry to genuine partial (qty_percent < 100) re-issues, so a re-issued full-close exit recomputes against the full current position and closes the whole stack at the single TP touch. ctest: 75/75 passed (incl. test_multi_tier_exit_precedence, test_fills_edge, test_accounting_reconciliation); adds test_full_close_while_pyramiding. Corpus: verify_corpus --all = 251 excellent / 1 documented anomaly, zero tier regressions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
A
qty_percent=100strategy.exitre-issued every bar while the position grows via pyramiding/DCA had its reserved quantity frozen at the size from when it was first placed.clear_existing_exit_ordercaptured the prior order's absolute qty intopreserved_reserved_qty, andcompute_exit_reserved_qtyhonored it unconditionally — overriding the 100% intent. At the TP touch only the first FIFO lot closed at the true limit; the residual lots exited one bar late at a re-priced limit, fragmenting one logical exit across two bars and producing wrong exit prices for all but the first lot.Fix (1 logical line)
Gate the preserved-qty carry to genuine partial (
qty_percent < 100) re-issues:A re-issued full-close exit now falls through to recompute against the full current position (the prior order is already cleared from
pending_orders_), so the whole stack closes at the single TP touch. Partial re-issues keep the carry (no double-reserve); the explicit-qty /from_entrybracket path never calls this function and is unaffected.Gates
test_multi_tier_exit_precedence,test_fills_edge,test_accounting_reconciliation; addstest_full_close_while_pyramiding(reverting the gate flips it to 5 failures — the test has teeth).verify_corpus --all= 251 excellent / 1 documented anomaly — exactly baseline, zero tier regressions.🤖 Generated with Claude Code