Repository navigation
Commit 7e20b07
fix: arm trails at the bar open; sub-tick trail_offset is a zero offset
Two trailing-exit rules diagnosed from TradingView tapes (TV is ground
truth). Family: winthetrade-ema-9-vwap-strategy-with-atr-trailing-stop
(strategy.exit("x", trail_points = atr*2, trail_offset = atr*2) -- a
price-unit ATR passed as TICKS) on 7 lanes: matchPct 100, entry/exit
timestamps exact, exit PRICE wrong on the same bar, so PnL / equity /
qty diverge from there.
(B) The bar's open is the first price a resting trail observes.
resolve_exit_path_fill only folded prices into the trail state AFTER
each O->L / O->H segment (update_exit_trail_state). With a POOC close
fill the carried best is the entry price itself (apply_market_order_fill
same_bar_close_fill guard), so a trail whose activation level the next
bar's open already sits past stayed dormant through an adverse-first
leg, armed only at the favourable extreme and filled at extreme -/+
offset. TV's emulator arms at the open with best = open and fills the
retrace at open -/+ offset.
OANDA:XAUUSD 15m long 2025-04-04 10:45Z @3110.31; 11:00Z bar
O 3110.40 H 3136.775 L 3109.24 C 3134.46, atr*2 ~ 15 ticks
(activation 3110.325, offset 0.015): TV "Long Exit" 3110.385 =
open - 0.015 (PnL 24.47); engine 3136.76 = high - 0.015 (PnL 8631).
194 of the 195 differing XAUUSD exits fit TV = open -/+ k ticks vs
engine = extreme -/+ the same k, always adverse-leg-first
(|O-L| < |H-O| for longs) with the open beyond the activation.
NASDAQ:AAPL 15m short 2025-04-02 18:45Z @222.93; 04-03 13:30Z bar
O 205.54 L 202.52: TV 205.55 (open + 1 tick), engine 202.53.
NYSE:F 1D long 2025-04-21 @9.47; 04-22 O 9.55 H 9.72: TV 9.54,
engine 9.71.
Fix: after try_exit_open_gap_fill declines (same liveness condition --
path cursor at the open, non-entry bar unless magnifier / cascade
waypoint), observe_exit_trail_open folds bar.open into the running best
and arms the trail iff the OPEN itself is past the activation. It never
arms from the carried best, so the #148 one-shot rule (an explicit zero
offset never retro-arms) is untouched; for exit-at-activation shapes an
open past the level was already gap-filled, so it is a pure best-price
observation there.
(A) A trail_offset in [0, 1) ticks is TV's explicit-zero one-shot trail.
compute_exit_trail_state gated the zero rule on trail_offset != 0.0 and
only then floored, so an offset in (0, 1) became a FINITE zero distance:
exits_at_activation = false and the stop rode exactly on the running
extreme, filling at the bar's best price. TV floors first and applies
the zero-offset rule: fill at the activation crossing, or at the open
when the bar opens past it, and no carried arming.
Pinned with `lab tv` on OANDA:EURUSD 15m (2025-04-01 -> 05-01): the
tapes for strategy.exit("x", "L", trail_points=3, trail_offset=0),
trail_offset=0.5 and trail_offset=0.9 are byte-identical
(190 rows, sha256 36aa80ac...).
EURUSD 15m short 2025-03-31 03:45Z @1.08330, atr*2 ~ 0.0006 "ticks"
(activation ceil -> 1 tick = 1.08329, floor(offset) = 0); exit bar
O 1.08330 L 1.08314: TV 1.08329 (the activation), engine 1.08314.
Fix: floor(trail_offset) first; a zero-tick result takes the existing
explicit-zero path (trail_offset_price NaN, one_shot_zero_offset_trail).
Whole-tick and larger fractional offsets keep the nils123456-orb /
legalrice floor rule unchanged; negative offsets are untouched. The
trail-peak fold in engine_fills.cpp already floors, so it agrees.
Tests: tests/test_trail_open_arm_subtick_offset.cpp (105 checks) pins
the XAUUSD / AAPL / F shapes at the resolver and, for XAUUSD and AAPL,
end to end through a POOC close-fill entry; 0 == 0.5 == 0.9 == 0.6 at
the activation, at a gapped open, and for the #148 no-retro-arm hold
(resolver and engine level, with the omitted-offset durable-arming
control); whole-tick 1.0 / 1.4 keep the floored trailing distance.
Against the pre-fix source 29 of those checks fail, all of them the
tape cells.
One existing pin contradicted the TV rule and is re-pinned, not
dropped: test_path_resolve_extra.cpp fl_sub (0f1f06e, a factorial
round, 2026-08-25) asserted that a 0.6-tick offset "rides the extreme
itself once armed (distinct from an explicit 0)" -- fill at the peak
102.00 -- as an extrapolation of the floor rule with no tape behind
the sub-tick cell. It now expects the activation 101.00 and equality
with the explicit-0 fill, with the `lab tv` evidence in the comment.
Every other trail pin (test_path_resolve_extra, the #148 retro-arm
cells, test_margin_call_trail_exit_chronology D, cascade G4) passes
unchanged. ctest 153/153 (was 152/152 before the new binary), scripts
unittest 102/102.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_014vizwdJMsDkHsjmS4StvZY1 parent 11e61d4 commit 7e20b07
4 files changed
Lines changed: 601 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
563 | 563 | | |
564 | 564 | | |
565 | 565 | | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
584 | 598 | | |
585 | 599 | | |
586 | | - | |
| 600 | + | |
| 601 | + | |
587 | 602 | | |
588 | 603 | | |
589 | 604 | | |
| |||
612 | 627 | | |
613 | 628 | | |
614 | 629 | | |
615 | | - | |
616 | | - | |
| 630 | + | |
617 | 631 | | |
618 | 632 | | |
619 | 633 | | |
| |||
712 | 726 | | |
713 | 727 | | |
714 | 728 | | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
715 | 777 | | |
716 | 778 | | |
717 | 779 | | |
| |||
884 | 946 | | |
885 | 947 | | |
886 | 948 | | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
887 | 957 | | |
888 | 958 | | |
889 | 959 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
518 | | - | |
519 | | - | |
520 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
521 | 528 | | |
522 | 529 | | |
523 | 530 | | |
524 | 531 | | |
525 | 532 | | |
526 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
527 | 539 | | |
528 | 540 | | |
529 | 541 | | |
| |||
0 commit comments