Skip to content

Commit 7e20b07

Browse files
luisleo526claude
andcommitted
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_014vizwdJMsDkHsjmS4StvZY
1 parent 11e61d4 commit 7e20b07

4 files changed

Lines changed: 601 additions & 25 deletions

File tree

‎src/engine_path_resolve.cpp‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -563,27 +563,42 @@ ExitTrailState compute_exit_trail_state(bool is_long, double trail_points,
563563
// Absolute activation price level (no entry-relative tick rounding).
564564
s.activation_level = trail_price;
565565
}
566-
// TV treats an explicit zero offset like an omitted offset: the exit fires
567-
// at the activation crossing itself. Keep zero represented as NaN here so
568-
// it follows the existing activation-only path; positive offsets retain
569-
// the ordinary best-price-minus/plus-offset trailing behaviour.
570-
if (!std::isnan(trail_offset) && trail_offset != 0.0) {
571-
// A fractional trail_offset (ticks) is TRUNCATED to whole ticks by
572-
// TradingView, not rounded up: the trailing level sits
573-
// floor(offset) ticks behind the running extreme. Measured on every
574-
// non-gap trailing exit whose level could be recovered from the TV
575-
// tape (level = TV fill + slippage ticks): nils123456-orb-strat on
576-
// BINANCE:ETHUSDT.P (trail_offset = price / mintick, slippage 0)
577-
// 11/11 and legalrice2697-nse-elite-strategy-v6-full-system on
578-
// OANDA:EURUSD (atr * 4 / mintick, slippage 2) 58/62 sat exactly
579-
// ONE tick nearer the extreme than the ceil() level, for offsets
580-
// with a fractional part both below and above .5 (never zero for
581-
// the .5+ half, which rules out round-to-nearest). Whole-tick
582-
// offsets are unchanged.
583-
s.trail_offset_price = std::floor(trail_offset) * syminfo_mintick;
566+
// A fractional trail_offset (ticks) is TRUNCATED to whole ticks by
567+
// TradingView, not rounded up: the trailing level sits floor(offset)
568+
// ticks behind the running extreme. Measured on every non-gap trailing
569+
// exit whose level could be recovered from the TV tape (level = TV fill
570+
// + slippage ticks): nils123456-orb-strat on BINANCE:ETHUSDT.P
571+
// (trail_offset = price / mintick, slippage 0) 11/11 and
572+
// legalrice2697-nse-elite-strategy-v6-full-system on OANDA:EURUSD
573+
// (atr * 4 / mintick, slippage 2) 58/62 sat exactly ONE tick nearer the
574+
// extreme than the ceil() level, for offsets with a fractional part both
575+
// below and above .5 (never zero for the .5+ half, which rules out
576+
// round-to-nearest). Whole-tick offsets are unchanged.
577+
//
578+
// The truncation comes FIRST, and an offset that truncates to ZERO ticks
579+
// -- an explicit 0 or any sub-tick value in (0, 1) -- is TV's
580+
// exit-at-activation trail (the one-shot rule below), NOT a zero-distance
581+
// trail riding on the running extreme. Keep it represented as NaN so it
582+
// follows the activation-only path; positive whole-tick offsets retain
583+
// the ordinary best-price-minus/plus-offset trailing behaviour. Pinned
584+
// with `lab tv` on OANDA:EURUSD 15m (2025-04-01 -> 05-01): the tapes for
585+
// strategy.exit("x", "L", trail_points=3, trail_offset=0),
586+
// trail_offset=0.5 and trail_offset=0.9 are byte-identical (190 rows,
587+
// sha256 36aa80ac...). The engine used to keep floor(0.6) = 0 as a
588+
// FINITE zero distance, so the stop sat on the running extreme and
589+
// filled at the bar's best price: winthetrade ema-9-vwap ATR trail
590+
// (trail_offset = atr*2 passed as ticks) on OANDA:EURUSD 15m, short
591+
// 2025-03-31 03:45Z @1.08330, atr*2 ~ 0.0006 "ticks" -> activation
592+
// ceil -> 1 tick = 1.08329, offset floor -> 0; exit bar O 1.08330
593+
// L 1.08314: TV 1.08329 (the activation), engine 1.08314 (the low).
594+
const double trail_offset_ticks = std::floor(trail_offset); // NaN stays NaN
595+
const bool zero_tick_offset = (trail_offset_ticks == 0.0); // explicit [0, 1)
596+
if (!std::isnan(trail_offset_ticks) && !zero_tick_offset) {
597+
s.trail_offset_price = trail_offset_ticks * syminfo_mintick;
584598
}
585599
s.exits_at_activation = std::isnan(s.trail_offset_price);
586-
// An EXPLICIT trail_offset=0 is TV's one-shot exit-at-activation trail:
600+
// An EXPLICIT trail_offset=0 (or a sub-tick offset that truncates to 0
601+
// ticks, see above) is TV's one-shot exit-at-activation trail:
587602
// it FILLS at the activation crossing itself, so it can never survive
588603
// into a later bar in the armed state — an armed zero-offset trail is an
589604
// executed one. Deriving "armed" for it from the running post-entry
@@ -612,8 +627,7 @@ ExitTrailState compute_exit_trail_state(bool is_long, double trail_points,
612627
// only a carried armed state can produce. Offset>0 trails likewise keep
613628
// the reconstruction: activation is durable for every trail that keeps
614629
// running after it activates.
615-
const bool one_shot_zero_offset_trail =
616-
!std::isnan(trail_offset) && trail_offset == 0.0;
630+
const bool one_shot_zero_offset_trail = zero_tick_offset;
617631
if (!one_shot_zero_offset_trail && !std::isnan(s.best_price)) {
618632
s.trail_active = is_long ? (s.best_price >= s.activation_level)
619633
: (s.best_price <= s.activation_level);
@@ -712,6 +726,54 @@ void update_exit_trail_state(bool is_long, bool rising, bool falling,
712726
}
713727
}
714728

729+
// Fold the bar's OPEN into the trail state before the intrabar path walk.
730+
// TradingView's broker emulator sees the open as the FIRST traded price of
731+
// the bar: a trail whose activation level the open already sits past arms AT
732+
// the open with best = open, so the trailing level on the first leg is
733+
// open -/+ offset. The engine used to observe prices only at the END of each
734+
// path segment (update_exit_trail_state), so on an adverse-leg-first bar
735+
// (|O-L| < |H-O| for a long) the trail stayed dormant through the retrace,
736+
// armed only at the favourable extreme and filled at extreme -/+ offset --
737+
// the worst-case price TV never prints.
738+
//
739+
// Evidence (winthetrade-ema-9-vwap-strategy-with-atr-trailing-stop family,
740+
// trail_points = trail_offset = atr*2 passed as ticks; the entries are POOC
741+
// close fills, so the carried best is the entry price itself):
742+
// OANDA:XAUUSD 15m long 2025-04-04 10:45Z @3110.31; 11:00Z bar O 3110.40
743+
// H 3136.775 L 3109.24 C 3134.46, 15 ticks / 15 ticks (mintick 0.001):
744+
// TV "Long Exit" 3110.385 = open - 0.015 (PnL 24.47); engine 3136.76 =
745+
// high - 0.015 (PnL 8631). 194 of the 195 differing XAUUSD exits fit
746+
// TV = open -/+ k ticks vs engine = extreme -/+ the same k, always on an
747+
// adverse-leg-first path with the open past the activation level.
748+
// NASDAQ:AAPL 15m short 2025-04-02 18:45Z @222.93; 04-03 13:30Z bar
749+
// O 205.54 L 202.52: TV 205.55 (open + 1 tick), engine 202.53 (low + 1).
750+
// NYSE:F 1D long 2025-04-21 @9.47; 04-22 bar O 9.55 H 9.72: TV 9.54,
751+
// engine 9.71.
752+
//
753+
// Only the OPEN itself arms here -- never the carried best: a one-shot
754+
// zero-offset trail must not retro-arm from a carried extreme (#148, see
755+
// compute_exit_trail_state), and an open already past its activation has
756+
// been filled by try_exit_open_gap_fill before this runs, so for the
757+
// exit-at-activation shapes this is a pure best-price observation.
758+
void observe_exit_trail_open(bool is_long, double open, ExitTrailState* trail) {
759+
if (!trail->has_trail) return;
760+
if (is_long) {
761+
if (std::isnan(trail->best_price) || open > trail->best_price) {
762+
trail->best_price = open;
763+
}
764+
if (!trail->trail_active && open >= trail->activation_level) {
765+
trail->trail_active = true;
766+
}
767+
} else {
768+
if (std::isnan(trail->best_price) || open < trail->best_price) {
769+
trail->best_price = open;
770+
}
771+
if (!trail->trail_active && open <= trail->activation_level) {
772+
trail->trail_active = true;
773+
}
774+
}
775+
}
776+
715777
// On the entry bar, an EXIT order whose stop/limit lies on the wrong side of
716778
// entry would have triggered before the position opened — block it.
717779
bool entry_bar_blocks_no_trail_exit(bool is_long,
@@ -884,6 +946,14 @@ ExitPathFill resolve_exit_path_fill(const Bar& bar,
884946
fill.path_position = 0.0;
885947
return fill;
886948
}
949+
// No gap fill: the open is still the first price this bar the
950+
// resting order saw. Fold it into the trail's running best (and arm
951+
// the trail if the open is already past its activation) before the
952+
// segment walk, exactly as TV's emulator does -- see
953+
// observe_exit_trail_open for the tapes. Same liveness condition as
954+
// the gap shortcut: a mid-path cursor or a non-magnifier entry bar
955+
// means the order did not exist at the open.
956+
observe_exit_trail_open(is_long, bar.open, &trail);
887957
}
888958

889959
double path[4];

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ set(TEST_SOURCES
141141
test_exit_bracket_position_cycle_lifetime
142142
test_range_end_close
143143
test_close_id_retires_ledger
144+
test_trail_open_arm_subtick_offset
144145
)
145146

146147
find_package(Threads REQUIRED)

‎tests/test_path_resolve_extra.cpp‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,15 +515,27 @@ static void test_fractional_trail_offset_truncates() {
515515
/*trail_points=*/100, /*trail_price=*/kNaN, /*trail_offset=*/50.0, /*entry=*/100,
516516
/*best_start=*/kNaN, false, false, kMintick);
517517
CHECK(near(fl_int.fill_price, 101.50));
518-
// Sub-tick offsets (0 < offset < 1) truncate to zero ticks: the level
519-
// rides the extreme itself once armed (distinct from an explicit 0,
520-
// which is the exit-at-activation shape). peak 102 -> fill @ 102.00.
518+
// Sub-tick offsets (0 < offset < 1) truncate to zero ticks AND then
519+
// follow the explicit-zero exit-at-activation rule: fill AT the
520+
// activation crossing (101 on the L->H leg), NOT at the peak 102.
521+
// This cell used to pin the peak (a finite zero-distance trail riding
522+
// the extreme, "distinct from an explicit 0") as an extrapolation of
523+
// the floor rule; it was never tape-backed and TradingView refutes it:
524+
// `lab tv` on OANDA:EURUSD 15m 2025-04-01 -> 05-01 gives byte-identical
525+
// tapes for trail_offset = 0, 0.5 and 0.9 (190 rows, sha256
526+
// 36aa80ac...). Full pins, including the gapped open and the #148
527+
// no-retro-arm hold, live in test_trail_open_arm_subtick_offset.cpp.
521528
ExitPathFill fl_sub = resolve_exit_path_fill(
522529
trail_long, PositionSide::LONG, kNaN, kNaN,
523530
/*trail_points=*/100, /*trail_price=*/kNaN, /*trail_offset=*/0.6, /*entry=*/100,
524531
/*best_start=*/kNaN, false, false, kMintick);
525532
CHECK(fl_sub.should_fill == true);
526-
CHECK(near(fl_sub.fill_price, 102.00));
533+
CHECK(near(fl_sub.fill_price, 101.00));
534+
ExitPathFill fl_zero = resolve_exit_path_fill(
535+
trail_long, PositionSide::LONG, kNaN, kNaN,
536+
/*trail_points=*/100, /*trail_price=*/kNaN, /*trail_offset=*/0.0, /*entry=*/100,
537+
/*best_start=*/kNaN, false, false, kMintick);
538+
CHECK(near(fl_sub.fill_price, fl_zero.fill_price));
527539
}
528540

529541
static void test_entry_bar_blocks_no_trail_exit() {

0 commit comments

Comments
 (0)