Skip to content

Commit 5f87ccb

Browse files
committed
tests: replay the 35 family-AC zero-offset trail tapes on registry bars; re-pin the one-shot fixtures
test_zero_offset_trail_rides: 32 `lab tv` tapes (NYSE:F 15m feed 80f404ae85ef, NASDAQ:AAPL 15m feed ae2b03d3736f) replayed end to end through BacktestEngine, each under the sensor's own issue mode (re-issued per bar, once, alongside the entry, or the probe's close*1.5%/mintick re-issue), asserting TV's exit price and bar. Re-pinned to the tape-confirmed rule: test_trail_fill_snap (the explicit-0 AAPL 196.135 open is a path level fill at position 0, still 196.13 end to end), test_path_resolve_extra and test_trail_open_arm_subtick_offset (the #148 fixtures now carry the best family Z actually carries — the issuing close — and HOLD; a carried best past the activation arms every zero-tick offset, the contract the command layer's restart relies on; a gapped open arms the trail and it rides), test_margin_call_trail_exit_ chronology fixture D (the explicit zero arms from the placement close like the omitted offset). ctest 190/190.
1 parent bde0911 commit 5f87ccb

7 files changed

Lines changed: 574 additions & 88 deletions

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ set(TEST_SOURCES
174174
test_close_id_retires_ledger
175175
test_trail_open_arm_subtick_offset
176176
test_trail_fill_snap
177+
test_zero_offset_trail_rides
177178
test_trail_ref_entry_bar_extreme
178179
test_trail_activation_tick_bar
179180
test_session_predicates_daily_chart

‎tests/test_margin_call_trail_exit_chronology.cpp‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
* BEFORE the extreme on the path -> no slice (fail-closed chronology
3030
* is preserved, the hook is not simply switched on for trails).
3131
* C. Emulator off -> nothing fires.
32-
* D. #148 regression pin: an EXPLICIT trail_offset=0 must not retro-arm
33-
* from the carried post-entry extreme, while an OMITTED trail_offset
34-
* still does.
32+
* D. Arming pin: an EXPLICIT trail_offset=0 arms from the placement close
33+
* exactly like an OMITTED trail_offset (round 10 family AC); the #148
34+
* retro-arm is closed by the family-Z restart at the command layer.
3535
*/
3636

3737
#include <cassert>
@@ -212,29 +212,38 @@ static void test_trail_chronology_disabled_emulator_stays_quiet() {
212212
CHECK(near(eng.exit_price(0), 3821.06));
213213
}
214214

215-
// ---- D: #148 arming semantics stay put ------------------------------------
216-
217-
// An EXPLICIT trail_offset=0 is TV's one-shot exit-at-activation trail and
218-
// must NOT retro-arm from the carried post-entry extreme (#148, a6e46ca);
219-
// an OMITTED trail_offset still does. Both cells share this fixture: the
220-
// trail is armed on bar 1 (carried best = bar 1 close 3810, already past the
221-
// 3821.06 activation for a short), and bar 2 opens ABOVE the activation and
222-
// never trades down to it. Only a carried armed state can fill there.
223-
static void test_zero_offset_arming_semantics_unchanged() {
224-
std::printf("test_zero_offset_arming_semantics_unchanged\n");
215+
// ---- D: the explicit zero arms from the placement close like the omitted --
216+
217+
// Both cells share this fixture: the exit is issued on bar 1 at its close
218+
// 3810 (the running extreme restarts there — round 9 family Z), already past
219+
// the 3821.06 activation for a short, and bar 2 opens ABOVE that level and
220+
// never trades down to it. Only a carried armed state can fill there — and
221+
// since round 10 family AC (test_zero_offset_trail_rides: TV fills the
222+
// placement-armed zero-offset trail at the open that gaps through its
223+
// level, f-gapdown-0404-1600-tp4 / 0423-1345-tp7b) the explicit zero carries
224+
// it exactly as the omitted offset does. The #148 retro-arm (a6e46ca) is
225+
// closed by the family-Z restart at the command layer, not by refusing the
226+
// carried best here.
227+
static void test_zero_offset_arms_from_the_placement_close_like_omitted() {
228+
std::printf("test_zero_offset_arms_from_the_placement_close_like_omitted\n");
225229
std::vector<Bar> bars = {
226230
mk_bar(1000, 3879.36, 3879.36, 3879.36, 3879.36, 1.0),
227231
mk_bar(2000, 3879.36, 3879.36, 3800.00, 3810.00, 1.0),
228232
mk_bar(3000, 3830.00, 3835.00, 3825.00, 3832.00, 1.0),
229233
mk_bar(4000, 3832.00, 3836.00, 3826.00, 3833.00, 1.0),
230234
};
231235

232-
// Explicit zero: no retro-arm -> the position is still open at the end.
236+
// Explicit zero: armed at the placement close 3810, the open 3830 gaps
237+
// through the level -> the open print, bar 2.
233238
TrailChronologyShortProbe explicit_zero(/*disable_mc=*/false,
234239
/*trail_offset=*/0.0);
235240
explicit_zero.run(bars.data(), (int)bars.size());
236-
CHECK(explicit_zero.trade_count() == 0);
237-
CHECK(near(explicit_zero.position_size(), -2.119, 1e-9));
241+
CHECK(explicit_zero.trade_count() == 1);
242+
if (explicit_zero.trade_count() == 1) {
243+
CHECK(near(explicit_zero.exit_price(0), 3830.0));
244+
CHECK(explicit_zero.exit_bar(0) == 2);
245+
}
246+
CHECK(near(explicit_zero.position_size(), 0.0));
238247

239248
// Omitted offset: the carried extreme keeps the activation armed, so
240249
// the exit fills at bar 2's open.
@@ -257,7 +266,7 @@ int main() {
257266
test_trail_exit_slices_at_adverse_extreme_first();
258267
test_trail_exit_before_extreme_stays_quiet();
259268
test_trail_chronology_disabled_emulator_stays_quiet();
260-
test_zero_offset_arming_semantics_unchanged();
269+
test_zero_offset_arms_from_the_placement_close_like_omitted();
261270

262271
std::printf("\n%d passed, %d failed\n", tests_passed, tests_failed);
263272
return (tests_failed > 0) ? 1 : 0;

‎tests/test_path_resolve_extra.cpp‎

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,44 +361,62 @@ static void test_resolve_exit_trail_fills() {
361361
CHECK(near(fl_tp.fill_price, 101.0));
362362
}
363363

364-
// ── explicit-zero-offset trails never retro-arm from the carried best ──
364+
// ── explicit-zero-offset trails arm from the carried best the command layer
365+
// hands them (the #148 retro-arm is closed at the source, not here) ──
365366
//
366-
// An EXPLICIT trail_offset=0 trail FILLS at its activation crossing, so in
367-
// TV it can never be carried into a later bar already armed. When the exit is
368-
// re-issued each bar with trail_points derived from close (level_t =
369-
// entry ± prevBarClose*perc), a refreshed activation can drop UNDER a peak
370-
// that was set beneath an older, higher level; deriving "armed" from that
371-
// carried peak retro-armed a phantom stop that gap-filled at the next bar's
372-
// open. An OMITTED trail_offset keeps the carried arming (see the omitted
373-
// control below — TV fills those at the open off the carried peak).
367+
// #148 (a6e46ca): when the exit is re-issued each bar with trail_points
368+
// derived from close (level_t = entry ± prevBarClose*perc), a refreshed
369+
// activation can drop UNDER a peak that was set beneath an older, higher
370+
// level; deriving "armed" from that carried peak retro-armed a phantom stop
371+
// that gap-filled at the next bar's open. The resolver used to refuse the
372+
// carried best for an explicit 0 altogether. Since round 9 family Z the
373+
// COMMAND layer restarts the running extreme at the issuing bar's close on
374+
// every trail_points change (engine_strategy_commands.cpp), so the peak is
375+
// never carried; and round 10 family AC pinned (test_zero_offset_trail_rides)
376+
// that TV DOES arm the explicit-zero trail at the placement close and then
377+
// rides the raw running best — so the resolver now trusts the best it is
378+
// handed for every offset shape.
374379
// Real discriminating bars from the boztilkiserhan-serhan1 WMA+RSI
375380
// scalp tape (long entered 2025-04-09 00:15 @1478.84, trailPerc 1.5%,
376381
// trail_offset passed as literal 0):
377382
//
378383
// 14:00 bar peaks at 1501.03 under that bar's level 1501.15
379384
// (trail_points = 1486.70*1.5/0.01 = 2230.05 -> ceil 2231t) — no cross.
380385
// 14:15 order refreshes from close 1475.99 -> 2213.985 -> ceil 2214t
381-
// -> activation 1500.98 < carried peak 1501.03.
386+
// -> activation 1500.98; the extreme restarts at that close 1475.99.
382387
// TV HOLDS through 16:30 (level from close 1489.89 -> 2235t -> 1501.19,
383-
// crossed by high 1509.00, TV row px 1501.19). The engine used to
384-
// retro-arm at the peak and exit at the 14:15 open 1475.99.
385-
static void test_zero_offset_trail_never_retro_arms() {
386-
std::printf("test_zero_offset_trail_never_retro_arms\n");
388+
// crossed by high 1509.00, TV row px 1501.19).
389+
static void test_zero_offset_trail_arms_from_the_carried_best() {
390+
std::printf("test_zero_offset_trail_arms_from_the_carried_best\n");
387391

388-
// 14:15 bar: nothing crosses 1500.98 intrabar and the open is BELOW the
389-
// activation — the position must HOLD (previously: gap-fill at open).
392+
// 14:15 bar with the best family Z actually carries (the issuing close
393+
// 1475.99 < activation 1500.98): dormant, the open is BELOW the
394+
// activation and nothing crosses it intrabar — HOLD, as TV does.
390395
Bar serhan_hold = mk(1475.99, 1491.82, 1475.89, 1486.23);
396+
ExitPathFill hold = resolve_exit_path_fill(
397+
serhan_hold, PositionSide::LONG, kNaN, kNaN,
398+
/*trail_points=*/2213.985, /*trail_price=*/kNaN,
399+
/*trail_offset=*/0.0, /*entry=*/1478.84,
400+
/*best_start=*/1475.99, false, false, kMintick);
401+
CHECK(hold.should_fill == false);
402+
403+
// The contract: a carried best past the activation ARMS the explicit-zero
404+
// trail (level = that best) and the open through it fills as a print —
405+
// exactly what an omitted offset does. Only the command layer's restart
406+
// keeps the #148 peak out of here.
391407
ExitPathFill retro = resolve_exit_path_fill(
392408
serhan_hold, PositionSide::LONG, kNaN, kNaN,
393409
/*trail_points=*/2213.985, /*trail_price=*/kNaN,
394410
/*trail_offset=*/0.0, /*entry=*/1478.84,
395411
/*best_start=*/1501.03, false, false, kMintick);
396-
CHECK(retro.should_fill == false);
397-
398-
// OMITTED offset control: the same bars with trail_offset = na keep the
399-
// carried arming — the pre-armed level gap-fills at the open. This is
400-
// the TV-pinned omitted-offset behavior (see the probe pin below); only
401-
// the explicit literal 0 is the one-shot activation exit.
412+
CHECK(retro.should_fill == true);
413+
CHECK(near(retro.fill_price, 1475.99));
414+
CHECK(retro.at_bar_open == true);
415+
CHECK(retro.open_is_trail_level == false);
416+
417+
// OMITTED offset: the same bars with trail_offset = na keep the carried
418+
// arming — the pre-armed level gap-fills at the open. This is the
419+
// TV-pinned omitted-offset behavior (see the probe pin below).
402420
ExitPathFill retro_nan_off = resolve_exit_path_fill(
403421
serhan_hold, PositionSide::LONG, kNaN, kNaN,
404422
/*trail_points=*/2213.985, /*trail_price=*/kNaN,
@@ -433,19 +451,32 @@ static void test_zero_offset_trail_never_retro_arms() {
433451
CHECK(tvx.should_fill == true);
434452
CHECK(near(tvx.fill_price, 1501.19));
435453

436-
// SHORT dual: refreshed activation 99.02 rises ABOVE the carried trough
437-
// 99.00; the bar opens above it and never falls to it -> HOLD
438-
// (previously: retro-armed and gap-filled at the open 100.50).
454+
// SHORT dual: activation 99.02; the best family Z carries is the issuing
455+
// close 99.50 (above the activation: dormant), the bar opens above it and
456+
// never falls to it -> HOLD.
439457
Bar short_hold = mk(100.50, 100.80, 99.60, 100.10);
458+
ExitPathFill s_hold = resolve_exit_path_fill(
459+
short_hold, PositionSide::SHORT, kNaN, kNaN,
460+
/*trail_points=*/98, /*trail_price=*/kNaN,
461+
/*trail_offset=*/0.0, /*entry=*/100,
462+
/*best_start=*/99.50, false, false, kMintick);
463+
CHECK(s_hold.should_fill == false);
464+
// ... and a carried trough 99.00 past the activation arms it: the open
465+
// 100.50 gaps through the level 99.00 -> the open print (round 10 family
466+
// AC, f-gapdown-0404-1600-tp4 / 0423-1345-tp7b mirrored).
440467
ExitPathFill s_retro = resolve_exit_path_fill(
441468
short_hold, PositionSide::SHORT, kNaN, kNaN,
442469
/*trail_points=*/98, /*trail_price=*/kNaN,
443470
/*trail_offset=*/0.0, /*entry=*/100,
444471
/*best_start=*/99.00, false, false, kMintick);
445-
CHECK(s_retro.should_fill == false);
446-
447-
// Control: the "open gapped past the activation" TV rule is untouched —
448-
// no pre-arming is needed for it. activation 101, open 102 -> open fill.
472+
CHECK(s_retro.should_fill == true);
473+
CHECK(near(s_retro.fill_price, 100.50));
474+
CHECK(s_retro.at_bar_open == true);
475+
476+
// Control: activation 101, carried best 101.03 (armed, level 101.03),
477+
// open 102 above it raises the best to 102; the low-first path's first
478+
// leg crosses that level at once -> a level fill at 102 (round 10 family
479+
// AC; the consumer floors it, 102 is on-tick).
449480
Bar gap_past = mk(102, 103, 101.2, 102);
450481
ExitPathFill gap = resolve_exit_path_fill(
451482
gap_past, PositionSide::LONG, kNaN, kNaN,
@@ -605,7 +636,7 @@ int main() {
605636
test_exit_order_touch_gap_arms();
606637
test_path_cross_kind_priority_order();
607638
test_resolve_exit_trail_fills();
608-
test_zero_offset_trail_never_retro_arms();
639+
test_zero_offset_trail_arms_from_the_carried_best();
609640
test_fractional_trail_offset_truncates();
610641
test_entry_bar_blocks_no_trail_exit();
611642
std::printf("\n%d passed, %d failed\n", tests_passed, tests_failed);

‎tests/test_trail_fill_snap.cpp‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,12 @@ void test_trail_level_tick_grid_snap() {
240240

241241
void test_resolver_aapl_long_arms_at_subtick_open() {
242242
std::printf("test_resolver_aapl_long_arms_at_subtick_open\n");
243-
// activation 191.91 + 384t = 195.75 < open 196.135: fires at the open as
244-
// the trail's LEVEL (open_is_trail_level) — omitted offset and explicit 0
245-
// alike. The raw open is reported; the consumer snaps it directionally.
243+
// activation 191.91 + 384t = 195.75 < open 196.135. OMITTED offset: fires
244+
// at the open as the trail's LEVEL (open_is_trail_level). The raw open is
245+
// reported; the consumer snaps it directionally.
246246
const double tp = scalper_trail_points(191.92, 0.01);
247-
const double offsets[] = {kNaN, 0.0};
248-
for (double off : offsets) {
249-
ExitPathFill f = trail_fill(kAapl0422_1330, PositionSide::LONG, tp, off,
247+
{
248+
ExitPathFill f = trail_fill(kAapl0422_1330, PositionSide::LONG, tp, kNaN,
250249
/*entry=*/191.91, /*best_start=*/193.43,
251250
/*mintick=*/0.01);
252251
CHECK(f.should_fill == true);
@@ -256,6 +255,23 @@ void test_resolver_aapl_long_arms_at_subtick_open() {
256255
CHECK(f.is_limit == false);
257256
CHECK(near(f.path_position, 0.0));
258257
}
258+
// EXPLICIT 0 (round 10 family AC, test_zero_offset_trail_rides): the open
259+
// ARMS the trail with best = open and it rides; the adverse-first leg
260+
// crosses the level 196.135 at once — a path LEVEL fill at position 0,
261+
// not an open print (the consumer floors it to the same TV 196.13; the
262+
// print rounding would give 196.14, which the aapl-pre-tp100 tape refutes).
263+
{
264+
ExitPathFill f = trail_fill(kAapl0422_1330, PositionSide::LONG, tp, 0.0,
265+
/*entry=*/191.91, /*best_start=*/193.43,
266+
/*mintick=*/0.01);
267+
CHECK(f.should_fill == true);
268+
CHECK(f.fill_price == 196.135);
269+
CHECK(f.is_trail == true);
270+
CHECK(f.at_bar_open == false);
271+
CHECK(f.open_is_trail_level == false);
272+
CHECK(f.is_limit == false);
273+
CHECK(near(f.path_position, 0.0));
274+
}
259275
// trail_offset=1: arms at the open with best = open, the adverse-first
260276
// leg (|O-L| = 0.175 < |H-O| = 1.365) crosses 196.135 - 1t = 196.125 —
261277
// a level fill (TV prints the floored 196.12).

0 commit comments

Comments
 (0)