Cover same-bar market pyramid adds per TV's open-tick fill priority#97
Merged
Conversation
When a pre-queued same-ID market add and its from_entry bracket exit both
fill on the same bar, TradingView covers the add (a dur-0 scratch) whenever
the add fills at-or-before the exit in the fill sequence; an exit that fills
first is sized to the pre-add position and the add survives to carry.
Probe-pinned rule (pf-probe-ki62-keepscratch-{gapped,intrabar}, 9,504/9,504
events; masayanfx reconstruction 4,190/4,190 including both exemplars):
- Intrabar exit fills (open inside the bracket) always cover the open-filled
add, scratching it at the bracket price.
- At the open tick, fill priority is buy market-like (market + triggered
stops), then sell market-like, then gapped-through limit orders. The only
keep cell is a short position whose open gaps at-or-through the buy stop.
Implementation:
- sort_orders_by_fill_phase: the blanket exit-before-same-dir-add tie-break
becomes the three-tier open-tick priority, scoped to a market add paired
with its own gapped from_entry bracket; all other pairs keep the old order.
- apply_exit_order_fill: after the frozen reserve drains pre-add slices,
any same-bar market-add slice of the same from_entry still open is
scratched dur-0 at the exit's fill price (cover_samebar_market_adds_on_exit;
PyramidEntry.market_pyramid_add flags the two market-add sites and is
propagated through fifo_drain). Prior-bar slices are never touched.
Gates: new suite 30/30 (21 RED on base); ctest 95/95; corpus 240/11/1;
masayanfx 10,105/10,105 (100% px, canonical P90s 0); keep/scratch probes
9,504/9,504 both; lukeborgerding, 3commas-bch-heikin, waranyutrkm pair,
ahtisham, ki65/coof probes and coof cohort byte-identical; full 412-slug
sweep 1 up / 0 down.
Known residuals (recorded, low/theoretical): comparator transitivity when a
third non-matching phase-0 exit shares the bar; ANY-mode percent-partial +
same-bar add coverage unverified vs TV; gapped-scratch pricing under nonzero
slippage untested (all pinned data ran slippage=0).
Co-Authored-By: Claude Fable 5 <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
Implements the probe-pinned KI-62 keep/scratch rule: when a pre-queued same-ID market add and its
from_entrybracket exit both fill on the same bar, the exit covers the add (dur-0 scratch) iff the add fills at-or-before the exit in TV's fill sequence. Intrabar exit fills always cover; at the open tick the priority is buy market-like → sell market-like → gapped-through limits, so the only keep cell is a short position gapped at-or-through its buy stop.Rule pinned at 100% event-level on two dedicated TV probes (9,504/9,504 collision events) and reproduces the masayanfx exemplar's full collision census (4,190/4,190, incl. both hand-verified exemplars).
Changes
sort_orders_by_fill_phase(src/engine_fills.cpp): three-tier open-tick priority for a market add paired with its own gapped from_entry bracket; every other pair keeps the existing order.cover_samebar_market_adds_on_exit(src/engine_orders.cpp, called fromapply_exit_order_fill): scratches surviving same-bar market-add slices of the exiting ID at the exit's fill price after the frozen pre-add reserve drains; prior-bar slices untouched. NewPyramidEntry.market_pyramid_addflag, propagated throughfifo_drain.test_same_bar_add_exit_coverage(9 cells / 30 checks; 21 RED on base).Verification
Known low/theoretical residuals (recorded, non-blocking per review)
close_entries_rule="ANY"percent-partial + same-bar add coverage unverified vs TV.🤖 Generated with Claude Code