Self-test of keltner_lib.js against keltner_spec.md, run 2026-11-24T08:26:24.433Z 1. EMA(20) and ATR(10) by hand [ok] EMA seed = the mean of the first 20 closes (none before) - 999.85 [ok] EMA alpha 2/21 to the 30th bar - 999.775667639 vs 999.775667639 [ok] ATR seed = the mean of the first 10 true ranges (the first bar: high - low) - 3.35 [ok] ATR Wilder to the 30th bar - 3.819738811 vs 3.819738811 [ok] the last Friday bar ends at 17:00 - 2024-03-08 17:00 [ok] across the weekend the true range includes the gap from Friday's close - TR 54 [ok] U and D = EMA +/- 2 ATR at the same bar [ok] across the daily 17:00-18:00 break the true range includes the gap - TR 11 2. Hand-built signals [ok] the 09:35 bar: EMA 1001, ATR 4.75, U 1010.5 (its own close and range included) - EMA 1001 ATR 4.75 U 1010.5 [ok] a close exactly on U: no signal [ok] a close 1 tick above U: a long signal on the 09:35 bar (its previous bar is the overnight 09:30 bar) - 1 signals [ok] a second close above U right after: no new signal (not a fresh break) - 575 [ok] a close back inside, then out again: a new signal (09:45) - 575,585 [ok] the mirror image: a short signal (close 1 tick below D) [ok] a close exactly on D: no signal [ok] a break on the 15:30 bar is a signal [ok] on the 15:35 bar it is not (counted outside the window) [ok] a break on an overnight bar (08:00) is not a signal - breaks 1,0 [ok] on a 13:15 day a break on the 12:45 bar is a signal [ok] the 12:50 bar is outside the window [ok] KC-CL closes at 13:15 on a short session - 2024-11-29 13:15 [ok] the 13:00 holiday (Thanksgiving 2024) is not a session [ok] its 09:35 break is no signal - breaks 1,0 [ok] its bars are on the chart: the next morning's first bar has the holiday's ATR - ATR 35.98 3. Hand-built trades [ok] entry at the open of 09:36, the first minute after the signal bar - 2024-03-05 09:36 1011 [ok] KC-CL: out at the 16:00 close, the cost paid - close 2024-03-05 16:00 -15.76 [ok] the data stops at 14:03 inside the signal bar: no next minute in the session, untradable - 845 {"signals":1,"ignored":0,"untradable":1} [ok] KC-CL: one trade a session, a second signal while the position is open is ignored - {"signals":2,"ignored":1,"untradable":0} [ok] a long, then a bar closing far below: it is also a fresh short break (09:40) - 575/1,580/-1 [ok] KC-MID-LS: the long exits at the 09:41 open; the short signal of its exit bar is ignored - 1 trades {"signals":2,"ignored":1,"untradable":0} [ok] KC-MID-L: the short signal is not even counted [ok] long-only versions ignore a short signal; KC-MID-LS takes it [ok] a signal bar that leaves the EMA at 1001; the next bar closes at 1001 = the middle line - EMA 1001 [ok] a close AT the middle line exits, at the open of the next minute (09:41) - mid 2024-03-05 09:41 1001.5 [ok] a close 1 tick above it does not; held to the session close - close 2024-03-05 16:00 [ok] the middle line crossed by the 16:00 bar: out at the session close - close 2024-03-05 16:00 990 [ok] the 15:59 and 16:00 minutes missing: the session close is the close of 15:58 - 2024-03-05 15:58 1013 4-7. The real data engine: 2373779 minutes, 1689 sessions, 475280 bars, 5543 signals (long 2847, short 2696); 3.4 s reference (reported before the comparison): 2373779 minutes, 1689 sessions, 475280 bars, 5543 signals; trades KC-MID-LS 2962, KC-MID-L 1460, KC-CL-LS 1599, KC-CL-L 1116 4. No look-ahead: 50 random cuts of the data [ok] every signal (with its lines) and every KC-MID-LS trade exited before each cut is identical to the full run - 126442 signal and 67935 trade comparisons, 0 cuts differ 5. Engine against reference, trade by trade [ok] KC-MID-LS: 2962 trades, identical [ok] KC-MID-L: 1460 trades, identical [ok] KC-CL-LS: 1599 trades, identical [ok] KC-CL-L: 1116 trades, identical [ok] the same sessions, bars and signals in both - 1689/1689 475280/475280 5543/5543 6. The control at offset 0 [ok] KC-MID-LS: the unshifted control reproduces the real trades - 2962 trades, the same average (values not printed before the run) [ok] KC-MID-L: the unshifted control reproduces the real trades - 1460 trades, the same average (values not printed before the run) [ok] KC-CL-LS: the unshifted control reproduces the real trades - 1599 trades, the same average (values not printed before the run) [ok] KC-CL-L: the unshifted control reproduces the real trades - 1116 trades, the same average (values not printed before the run) 7. Mutations (the engine changed, the reference not): each must be caught [ok] caught: an SMA(20) instead of the EMA - 3769 trades vs 2962 [ok] caught: a simple ATR instead of Wilder's - 3052 trades vs 2962 [ok] caught: a multiplier of 1.5 - 4500 trades vs 2962 [ok] caught: the fresh-break condition removed - 3009 trades vs 2962 [ok] caught: the entry at the signal bar's own close (look-ahead) - 2962 trades vs 2962 [ok] caught: indicators on regular-hours bars only - 2830 trades vs 2962 [ok] caught: the middle-line exit at the bar's close instead of the next minute's open - 2966 trades vs 2962 54 checks, 0 failed PASSED