Self-test of flag_lib.js against flag_spec.md, run 2026-11-14T08:12:45.923Z 1. ATR(14), Wilder, by hand [ok] seed = the mean of the first 14 true ranges - 3.357142857142857 [ok] Wilder smoothing to the last bar of the first session - 3.443784 vs 3.443784 [ok] a session's first bar: true range = its own high - low (the 100-point overnight gap left out) - TR 4 [ok] the ATR runs on across the session break 2. Hand-built sessions: the pattern [ok] canonical bull flag: one signal, on the 10:10 bar (the earliest possible) - 1 signals [ok] pole start, pole top and a 5-bar flag - S+0 T+1 n 5 [ok] a pole of exactly 2 ATR (8 points, ATR 4 before the pole) is taken [ok] channel: slope -0.5, U(5) = 1003, D(5) = 1001.75 - U 1003 D 1001.75 [ok] a pole 1 tick short of 2 ATR (7.75 points): no signal [ok] a 10-bar pole is taken - 1 signals [ok] an 11-bar pole (its low 10 bars before the top) is not: measured from the window, the pole is under 2 ATR [ok] a 4-bar flag gives no signal [ok] a 20-bar flag with slope 0 is taken - 1 signals [ok] a 21-bar flag is not [ok] a pullback of exactly 50% of the pole is taken [ok] a pullback 1 tick deeper is not [ok] a rising bull flag (slope > 0) gives no signal [ok] (the same bars without the slope rule would signal: the check bites) [ok] a close exactly on U(n) is no signal [ok] a close 1 tick above U(n) is a signal [ok] a flag bar equal to the pole top: T stays at the earlier bar (ties to the earliest) - U 1003.5 [ok] two equal lows before the top: the pole starts at the later one (ties to the latest) - S+1 [ok] a gap at the open as the pole (its start would be in the previous session): no signal [ok] a flag bar printing a new high becomes the pole top (no signal on the 10:10 bar) [ok] a close below the lower line ends the pattern: the next bar above the line gives no signal [ok] (the same next bar after a close inside the channel is a signal) - 615 [ok] one signal per pole top: a second close above the refitted channel is ignored - 1 signals [ok] a breakout on the 15:30 bar is a signal [ok] on the 15:35 bar it is not (counted outside the window) [ok] the 13:00 market holiday (Thanksgiving 2024) is not a session and not on the chart [ok] on the 13:15 day a breakout on the 12:45 bar is a signal (its ATR not inflated by the 40-point holiday bars) [ok] on the 13:15 day the 12:50 bar is outside the window [ok] the trade closes at 13:15 on a short session - 2024-11-29 13:15 [ok] the mirror image is a bear flag signal (D = 997) - dir -1 D 997 3. Hand-built trades [ok] entry at the open of 10:11, stop 1 tick under the flag low (1002), R 1.25 - 2024-03-05 10:11 1003.25 1002 [ok] no level touched: out at the 16:00 close, the cost paid - close 2024-03-05 16:00 -15.76 [ok] the ATR at the signal bar (for the trailing distance) - 3.058677 [ok] 1R target - 1004.5 2024-03-05 10:12 [ok] 2R target at 1005.75 not reached: out at the close [ok] stop and target in the same minute: the stop - stop 1002 [ok] a minute opening beyond the stop fills at its open - 1001 [ok] an entry beyond the stop: untradable, no trade [ok] the stop counts the breakout bar's low (1002 -> 1001.75) [ok] trailing: the stop moves only at the 5-minute close (10:15) to 1005.75, then is hit at 10:16 - trail 1005.75 2024-03-05 10:16 [ok] (updated inside the bar instead, it would have stopped out at 10:14) - 2024-03-05 10:14 [ok] measured move: target entry + pole (1011.25), a minute opening at 1012 fills at 1012 - 1012 [ok] the trailing stop never moves back when price falls and the highest high stays - close stop 1005.75 [ok] a signal while the position is open is skipped [ok] (after the first trade has exited, the next signal is taken) 4-7. The real data engine: 2373779 minutes, 1689 sessions, 131301 bars, 455 signals (bull 277, bear 178); both sides on one bar 0; 2.9 s reference (reported before the comparison): 2373779 minutes, 1689 sessions, 131301 bars, 455 signals; trades FLAG-1R 449, FLAG-2R 443, FLAG-TR 441, FLAG-MM 440 4. No look-ahead: 50 random cuts of the data [ok] every signal before each cut is identical to the full run - 9952 signal comparisons, 0 cuts differ 5. Engine against reference, trade by trade [ok] FLAG-1R: 449 trades, identical [ok] FLAG-2R: 443 trades, identical [ok] FLAG-TR: 441 trades, identical [ok] FLAG-MM: 440 trades, identical [ok] the same sessions, bars and signals in both 6. The control at offset 0 [ok] FLAG-1R: the unshifted control reproduces the real trades - 449 trades, avg -23.4215 vs -23.4215 [ok] FLAG-2R: the unshifted control reproduces the real trades - 443 trades, avg -22.7577 vs -22.7577 [ok] FLAG-TR: the unshifted control reproduces the real trades - 441 trades, avg -54.1954 vs -54.1954 [ok] FLAG-MM: the unshifted control reproduces the real trades - 440 trades, avg -52.1464 vs -52.1464 7. Mutations (the engine changed, the reference not): each must be caught [ok] caught: ATR at the pole top instead of before the pole - 451 trades vs 449 [ok] caught: a 60% pullback allowed - 802 trades vs 449 [ok] caught: the slope rule removed - 603 trades vs 449 [ok] caught: the stop without its tick - 449 trades vs 449 [ok] caught: 4-bar flags allowed - 690 trades vs 449 [ok] caught: the trailing stop updated inside the bar - 441 trades vs 441 65 checks, 0 failed PASSED