|
| 1 | +// test_ta_window_underapplied -- round 9 (family W, jayentriken stochRSI on |
| 2 | +// six lanes): TradingView's sliding-window sum re-sums the window whenever the |
| 3 | +// carried Kahan compensation is UNDER-applied to the incoming source |
| 4 | +// (|fl(x - c) - x| < |c|: fully swallowed, the round-7 case, or rounded toward |
| 5 | +// x), and ta.stdev is sqrt(Sxx / n - m * m) over the same sliding sums |
| 6 | +// (window_sum.hpp, ta.hpp StdDev). Pinned 2026-09-05 on full-precision |
| 7 | +// NYSE:F@15 oracles exported by pineforge-workflow (famw-f15-kdhi-*, |
| 8 | +// famw-f15-sd-*: every value is TradingView's exact double, tiny residues |
| 9 | +// scaled by 2^60 on export). |
| 10 | +// |
| 11 | +// Every segment below starts cold at a bar from which the fitted arithmetic |
| 12 | +// reproduces TradingView bit-for-bit through the pinned re-sum bar (the |
| 13 | +// round-7 trigger misses each one: bars 747, 1156, 2324, 2659, 3362, 4351, |
| 14 | +// 4879, 5623 of math.sum(stoch, 3) and 262, 305 of math.sum(k, 3)); the |
| 15 | +// stdev segment is the lane's own range start (bar 0 = 2025-04-01 13:30Z). |
| 16 | + |
| 17 | +#include <pineforge/ta.hpp> |
| 18 | +#include <pineforge/math.hpp> |
| 19 | +#include <pineforge/na.hpp> |
| 20 | + |
| 21 | +#include <cmath> |
| 22 | +#include <cstdio> |
| 23 | + |
| 24 | +#include "test_ta_window_underapplied_data.hpp" |
| 25 | + |
| 26 | +using namespace pineforge; |
| 27 | + |
| 28 | +static int tests_passed = 0; |
| 29 | +static int tests_failed = 0; |
| 30 | + |
| 31 | +#define CHECK_TRUE(cond, tag) do { \ |
| 32 | + if (cond) { tests_passed++; } \ |
| 33 | + else { tests_failed++; std::printf("FAIL %s\n", (tag)); } \ |
| 34 | +} while (0) |
| 35 | + |
| 36 | +static bool same_bits(double a, double b) { |
| 37 | + if (std::isnan(a) && std::isnan(b)) return true; |
| 38 | + return a == b && std::signbit(a) == std::signbit(b); |
| 39 | +} |
| 40 | + |
| 41 | +int main() { |
| 42 | + // 1. math.sum(x, n) / ta.sma(x, n): the re-sum trigger. |
| 43 | + for (const WindowSumSeg& seg : kWindowSumSegs) { |
| 44 | + math::Sum sum(seg.length); |
| 45 | + ta::SMA sma(seg.length); |
| 46 | + int bad = 0; |
| 47 | + for (int i = 0; i < seg.len; ++i) { |
| 48 | + const double s = sum.compute(seg.x[i]); |
| 49 | + const double m = sma.compute(seg.x[i]); |
| 50 | + if (i < seg.length - 1) continue; |
| 51 | + if (!same_bits(s, seg.tv[i])) { |
| 52 | + if (bad++ < 3) std::printf(" %s: bar %d math.sum %.17g != TradingView %.17g\n", seg.name, i, s, seg.tv[i]); |
| 53 | + } |
| 54 | + if (!same_bits(m, seg.tv[i] / seg.length)) { |
| 55 | + if (bad++ < 3) std::printf(" %s: bar %d ta.sma %.17g != TradingView %.17g\n", seg.name, i, m, seg.tv[i] / seg.length); |
| 56 | + } |
| 57 | + } |
| 58 | + char tag[200]; |
| 59 | + std::snprintf(tag, sizeof tag, "%s: math.sum / ta.sma bit-exact (%d bad)", seg.name, bad); |
| 60 | + CHECK_TRUE(bad == 0, tag); |
| 61 | + } |
| 62 | + |
| 63 | + // 2. ta.stdev(close, 7) = sqrt(Sxx/7 - m*m) over the sliding sums; ta.sma basis = Sx/7. |
| 64 | + for (const StdevSeg& seg : kStdevSegs) { |
| 65 | + math::Sum sx(7), sxx(7); |
| 66 | + ta::SMA basis(7); |
| 67 | + ta::StdDev sd(7); |
| 68 | + int bad = 0; |
| 69 | + for (int i = 0; i < seg.len; ++i) { |
| 70 | + const double c = seg.close[i]; |
| 71 | + const double vsx = sx.compute(c); |
| 72 | + const double vsxx = sxx.compute(c * c); |
| 73 | + const double vb = basis.compute(c); |
| 74 | + const double vsd = sd.compute(c); |
| 75 | + if (i < 6) continue; |
| 76 | + if (!same_bits(vsx, seg.tv_sx[i]) || !same_bits(vsxx, seg.tv_sxx[i]) || |
| 77 | + !same_bits(vb, seg.tv_basis[i]) || !same_bits(vsd, seg.tv_stdev[i])) { |
| 78 | + if (bad++ < 3) std::printf(" %s: bar %d sx %.17g/%.17g sxx %.17g/%.17g basis %.17g/%.17g stdev %.17g/%.17g\n", |
| 79 | + seg.name, i, vsx, seg.tv_sx[i], vsxx, seg.tv_sxx[i], vb, seg.tv_basis[i], vsd, seg.tv_stdev[i]); |
| 80 | + } |
| 81 | + } |
| 82 | + char tag[200]; |
| 83 | + std::snprintf(tag, sizeof tag, "%s: math.sum(close), math.sum(close*close), ta.sma, ta.stdev bit-exact (%d bad)", seg.name, bad); |
| 84 | + CHECK_TRUE(bad == 0, tag); |
| 85 | + } |
| 86 | + |
| 87 | + // 3. recompute() replays a bar bit-for-bit (intrabar path): feed each |
| 88 | + // stdev bar twice (a wrong first tick, then the real source). |
| 89 | + { |
| 90 | + const StdevSeg& seg = kStdevSegs[0]; |
| 91 | + ta::StdDev sd(7); |
| 92 | + int bad = 0; |
| 93 | + for (int i = 0; i < seg.len; ++i) { |
| 94 | + sd.compute(seg.close[i] + 0.5); |
| 95 | + const double v = sd.recompute(seg.close[i]); |
| 96 | + if (i >= 6 && !same_bits(v, seg.tv_stdev[i])) bad++; |
| 97 | + } |
| 98 | + CHECK_TRUE(bad == 0, "ta.stdev recompute() replays the bar bit-for-bit"); |
| 99 | + } |
| 100 | + |
| 101 | + std::printf("test_ta_window_underapplied: passed=%d failed=%d\n", tests_passed, tests_failed); |
| 102 | + return tests_failed == 0 ? 0 : 1; |
| 103 | +} |
0 commit comments