Skip to content

Commit c8993aa

Browse files
luisleo526claude
authored andcommitted
engine: TradingView's sliding-window sum re-sums when the compensation's magnitude, added to the source, is under-applied (round 10, family W stage 2: onuriano/jayentriken stochRSI residue plateaus)
The re-sum trigger of KahanWindowSum (ta.sma, math.sum) reads |fl(x + |c|) - x| < |c| instead of round 9's |fl(x - c) - x| < |c|: the quantity TradingView tests is the compensation's MAGNITUDE added to the incoming source. On ordinary sources (|c| << |x|) both forms round alike; on residue-only plateaus (|x| ~ 1e-15 ~ |c|), on sources within |c| of a power of two, and on negative sources they differ and TradingView follows x + |c| (its decision flips under negation of the series). Eviction, add and the post-re-sum state are unchanged. Pinned by pineforge-workflow r9-famW-5 on 21,509 labelled decisions: the onuriano and jayentriken NYSE:F@15 oracles (math.sum(stoch, 3) 7,018/7,018, math.sum(k, 3) 7,016/7,016 each, bit-exact; round 9: 6,920 and 6,991), the stdev sums 7,029/7,029 x2, and 1,003 lab tv literal replays (dense +-40 ulp neighbourhoods, sign mirrors, binade straddles, window lengths 3 and 5). test_ta_window_round10 carries 15 oracle segments and 57 literal replays (24 decide differently under the round-9 form); test_ta_window_underapplied is unchanged and still bit-exact. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ade627e commit c8993aa

6 files changed

Lines changed: 489 additions & 22 deletions

File tree

‎include/pineforge/window_sum.hpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ namespace pineforge {
1212
// * the ring stores the COMPENSATED addend y = x - c each source entered
1313
// with, and subtracts that y (through the same Kahan step) when the
1414
// source leaves the window `length` bars later -- sub before add;
15-
// * on the bar whose incoming source UNDER-applies the carried compensation
16-
// (c != 0 and, with y0 = fl(x - c), |y0 - x| < |c|: fully swallowed or
17-
// rounded toward x -- round-9 family-W pin generalising the round-7
18-
// swallow case), the sum is re-summed newest-first over the window's
19-
// sources, c is reset and the bar's ring addend is its raw source.
15+
// * on the bar whose incoming source UNDER-applies the MAGNITUDE of the
16+
// carried compensation when it is ADDED (c != 0 and, with z = fl(x + |c|),
17+
// |z - x| < |c|: fully swallowed or rounded toward x -- the round-10
18+
// family-W pin; round 9's x - c form and round 7's swallow case are the
19+
// cases where x - c and x + |c| round alike), the sum is re-summed
20+
// newest-first over the window's sources, c is reset and the bar's ring
21+
// addend is its raw source.
2022
//
2123
// Both users divide S by `length` themselves (ta.sma) or emit S (math.sum).
2224
class KahanWindowSum {

‎src/ta_moving_averages.cpp‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ namespace pineforge {
5252
// Bit-exact 7,018/7,018 on math.sum(stoch, 3) (round-7 trigger 6,849) and
5353
// every k vs d sign / ta.crossover event on the lane (round 7: 24 signs,
5454
// 12 events wrong). The re-summed bar stores its raw source as its ring
55-
// addend (c is 0 after the re-sum). Known open pin: on residue-only
56-
// inputs (|x| ~ 1e-15 comparable to |c|) TradingView's decision is a
57-
// SIGNED comparison (it flips under negation of the series) that this
58-
// magnitude form reproduces on 6,991/7,016 math.sum(k, 3) bars; no k vs d
59-
// decision on the lane depends on those bits.
55+
// addend (c is 0 after the re-sum);
56+
// * round 10 (family W, stage 2: onuriano macd-stoch-rsi on NYSE:F@15, whose
57+
// d = sma(k, 3) sits on residue-only plateaus |k| ~ 1e-15 ~ |c|) closed
58+
// that pin: the quantity TradingView tests is src + |c|, the compensation's
59+
// MAGNITUDE added to the source -- fire iff |fl(x + |c|) - x| < |c|. On
60+
// ordinary sources (|c| << |x|) x - c and x + |c| round alike, so the
61+
// round-9 form was exact there; on residue-only sources, on sources within
62+
// |c| of a power of two, and on negative sources the two forms differ and
63+
// TradingView follows x + |c| (its decision flips under negation of the
64+
// series because x + |c| is not odd in x). Pinned on 21,509 labelled
65+
// decisions: the two NYSE:F@15 stochRSI oracles (jayentriken ta.stoch and
66+
// onuriano literal stoch: math.sum(stoch, 3) 7,018/7,018 and math.sum(k, 3)
67+
// 7,016/7,016 each, bit-exact), the stdev sums 7,029/7,029 x2, and 1,003
68+
// literal-replay decisions (pineforge-workflow r9-famW-5, lab tv w5-scan*:
69+
// dense +-40 ulp neighbourhoods, sign mirrors, binade straddles, window
70+
// lengths 3 and 5).
6071
//
6172
// The stochRSI consequence (round-7 family D, jayentriken): k = sma(stoch, 3)
6273
// and d = sma(k, 3) sit on ALGEBRAIC ties whenever stoch plateaus; TradingView
@@ -69,7 +80,7 @@ namespace pineforge {
6980
// c_pre = c
7081
// if t >= n: y = -y[t-n] - c; T = S + y; c = (T - S) - y; S = T
7182
// y = x - c; T = S + y; c = (T - S) - y; S = T; store y
72-
// if c_pre != 0 and |fl(x - c_pre) - x| < |c_pre|:
83+
// if c_pre != 0 and |fl(x + |c_pre|) - x| < |c_pre|:
7384
// S = (x + x[1]) + ... + x[n-1]; c = 0; store x instead of y
7485
// sma = S / n
7586

@@ -99,14 +110,19 @@ void KahanWindowSum::enter(double src, double comp_before) {
99110
addends_.push_front(y);
100111
values_.push_front(src);
101112
if (comp_before != 0.0) {
102-
// Round-9 pin (family W): the re-sum fires whenever the carried
103-
// compensation is UNDER-applied to the incoming source -- the
104-
// compensated addend fl(src - c) landed closer to src than the exact
105-
// src - c would have (fully swallowed, the round-7 case, or rounded
106-
// toward src: c = 1.25 ulp(src) applied as 1 ulp). An exact
107-
// subtraction, or one that rounds away from src, continues.
108-
const double y0 = src - comp_before;
109-
if (std::fabs(y0 - src) < std::fabs(comp_before)) {
113+
// Round-10 pin (family W, stage 2): the re-sum fires whenever the
114+
// MAGNITUDE of the carried compensation, ADDED to the incoming source,
115+
// is under-applied -- fl(src + |c|) landed closer to src than the exact
116+
// src + |c| would have (fully swallowed, the round-7 case; rounded
117+
// toward src, the round-9 case; or, on residue-only sources, the
118+
// rounding of src + |c| that the src - c form does not see). An exact
119+
// add, or one that rounds away from src, continues. Note the sign:
120+
// TradingView tests |c| added to src, whatever the sign of c, while the
121+
// arithmetic itself subtracts c -- the two agree except when src - c
122+
// and src + |c| round differently (residue-only sources, sources within
123+
// |c| of a power of two, negative sources).
124+
const double z = src + std::fabs(comp_before);
125+
if (std::fabs(z - src) < std::fabs(comp_before)) {
110126
double total = 0.0;
111127
for (double v : values_) { // newest first
112128
total += v;

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(TEST_SOURCES
1717
test_ta_sma_window_residual
1818
test_ta_sma_stochrsi_ties
1919
test_ta_window_underapplied
20+
test_ta_window_round10
2021
test_ta_na_source_rules
2122
test_matrix
2223
test_matrix_identity

‎tests/test_ta_window_round10.cpp‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// test_ta_window_round10 -- round 10 (family W, stage 2: onuriano
2+
// macd-stoch-rsi on NYSE:F@15, whose d = ta.sma(k, 3) sits on residue-only
3+
// plateaus): TradingView's sliding-window sum re-sums the window whenever the
4+
// MAGNITUDE of the carried Kahan compensation, ADDED to the incoming source,
5+
// is under-applied -- fire iff c != 0 and |fl(x + |c|) - x| < |c|
6+
// (window_sum.hpp). Round 9's x - c form is the special case where x - c and
7+
// x + |c| round alike; it decides wrongly on residue-only sources, on sources
8+
// within |c| of a power of two and on negative sources.
9+
//
10+
// Pinned 2026-09-06 by pineforge-workflow r9-famW-5 on 21,509 labelled
11+
// decisions: the onuriano and jayentriken NYSE:F@15 oracles (math.sum(stoch,
12+
// 3) 7,018/7,018 and math.sum(k, 3) 7,016/7,016 each, bit-exact), the stdev
13+
// sums (7,029/7,029 x2) and 1,003 lab tv literal replays (dense +-40 ulp
14+
// neighbourhoods of x = 8/3 u, sign mirrors, binade straddles, window lengths
15+
// 3 and 5). Every segment below is TradingView's exact double series: the
16+
// oracle segments start cold at a bar from which this arithmetic reproduces
17+
// TradingView through the decisive bar (where the round-9 form decides the
18+
// other way) and six bars beyond; the literal segments carry TradingView's
19+
// echoed inputs (its decimal-literal parser is not correctly rounded) and its
20+
// printed sums.
21+
22+
#include <pineforge/ta.hpp>
23+
#include <pineforge/math.hpp>
24+
#include <pineforge/na.hpp>
25+
26+
#include <cmath>
27+
#include <cstdio>
28+
29+
#include "test_ta_window_round10_data.hpp"
30+
31+
using namespace pineforge;
32+
33+
static int tests_passed = 0;
34+
static int tests_failed = 0;
35+
36+
#define CHECK_TRUE(cond, tag) do { \
37+
if (cond) { tests_passed++; } \
38+
else { tests_failed++; std::printf("FAIL %s\n", (tag)); } \
39+
} while (0)
40+
41+
static bool same_bits(double a, double b) {
42+
if (std::isnan(a) && std::isnan(b)) return true;
43+
return a == b && std::signbit(a) == std::signbit(b);
44+
}
45+
46+
int main() {
47+
int decisive = 0;
48+
for (const Round10Seg& seg : kRound10Segs) {
49+
math::Sum sum(seg.length);
50+
ta::SMA sma(seg.length);
51+
int bad = 0;
52+
for (int i = 0; i < seg.len; ++i) {
53+
const double s = sum.compute(seg.x[i]);
54+
const double m = sma.compute(seg.x[i]);
55+
if (i < seg.length - 1 || std::isnan(seg.tv[i])) continue;
56+
if (!same_bits(s, seg.tv[i])) {
57+
if (bad++ < 3) std::printf(" %s: bar %d math.sum %.17g != TradingView %.17g\n", seg.name, i, s, seg.tv[i]);
58+
}
59+
if (!same_bits(m, seg.tv[i] / seg.length)) {
60+
if (bad++ < 3) std::printf(" %s: bar %d ta.sma %.17g != TradingView %.17g\n", seg.name, i, m, seg.tv[i] / seg.length);
61+
}
62+
}
63+
if (!seg.round9_ok) decisive++;
64+
char tag[200];
65+
std::snprintf(tag, sizeof tag, "%s: math.sum / ta.sma bit-exact (%d bad)%s", seg.name, bad,
66+
seg.round9_ok ? "" : " [round-9 form decides differently here]");
67+
CHECK_TRUE(bad == 0, tag);
68+
}
69+
// The fixture set must keep exercising the bars that separate the two forms.
70+
CHECK_TRUE(decisive >= 20, "at least 20 segments where the round-9 x - c form and the round-10 x + |c| form disagree");
71+
72+
std::printf("test_ta_window_round10: %d passed, %d failed\n", tests_passed, tests_failed);
73+
return tests_failed == 0 ? 0 : 1;
74+
}

0 commit comments

Comments
 (0)