Skip to content

Commit 5215291

Browse files
committed
Merge branch 'round5/bar-indexed-extremes-ring' into round5/candidate
2 parents f8bde7e + 15cd7c5 commit 5215291

4 files changed

Lines changed: 164 additions & 19 deletions

File tree

‎docs/pine_v6_coverage_detail.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,13 @@ All **✅ Runtime** — backed by `PineMatrix` (`matrix.hpp` / `matrix.cpp`) for
790790
| `ta.dmi()` | fn | ✅ Runtime | `ta::DMI` class | |
791791
| `ta.ema()` | fn | ✅ Runtime | `ta::EMA` class | |
792792
| `ta.falling()` | fn | ✅ Runtime | `ta::Falling` class | |
793-
| `ta.highest()` | fn | ✅ Runtime | `ta::Highest` class | bar-addressed ring (K = length + 1 slots by `bar_index`, per context — chart and each `request.security`): a call inside an `if` that does not run every bar keeps stale slots, skips never-written ones, na iff `bar_index < length - 1` (NYSE:F 1D pins 39/39, 30/30); every-bar callers unchanged |
793+
| `ta.highest()` | fn | ✅ Runtime | `ta::Highest` class | bar-addressed ring (K = length + 1 slots by `bar_index`, per context — chart and each `request.security`): a call inside an `if` that does not run every bar keeps stale slots, skips never-written ones, na iff `bar_index < length - 1` (NYSE:F 1D pins 39/39, 30/30), read through a cached extremum with an implied bar — a call within `length` bars of the cached bar compares against the cache only and does not rescan the aliased slots (8 tapes, 696/696); every-bar callers unchanged |
794794
| `ta.highestbars()` | fn | ✅ Runtime | `ta::HighestBars` class | same ring as `ta.highest`; offset = slot distance of the extremum |
795795
| `ta.hma()` | fn | ✅ Runtime | `ta::HMA` class | |
796796
| `ta.kc()` | fn | ✅ Runtime | `ta::KC` class | |
797797
| `ta.kcw()` | fn | ✅ Runtime | `ta::KCW` class | |
798798
| `ta.linreg()` | fn | ✅ Runtime | `ta::Linreg` class | |
799-
| `ta.lowest()` | fn | ✅ Runtime | `ta::Lowest` class | same bar-addressed ring as `ta.highest` (mirror) |
799+
| `ta.lowest()` | fn | ✅ Runtime | `ta::Lowest` class | same bar-addressed ring and cached extremum as `ta.highest` (mirror; the 696/696 alias tapes are `ta.lowest(low, 10)`) |
800800
| `ta.lowestbars()` | fn | ✅ Runtime | `ta::LowestBars` class | same ring as `ta.lowest`; offset = slot distance of the extremum |
801801
| `ta.macd()` | fn | ✅ Runtime | `ta::MACD` class | |
802802
| `ta.max()` | fn | ✅ Runtime | `ta::AllTimeMax` class | Single-arg form only |

‎include/pineforge/ta.hpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ bool& ema_na_warmup_flag();
5656
// installed (the default — unit tests, standalone use) every ``compute()`` is
5757
// its own bar and the objects are byte-identical to the previous positional
5858
// deque.
59+
//
60+
// The ring is read through a cached extremum with an implied bar. Each call
61+
// site keeps ``{value, bar}``: the first call caches ``(src, b)``; a call on
62+
// bar ``b`` with ``b - bar < length`` only compares ``src`` against the cache
63+
// (strict — ties never displace the cached member); the new-extremum test
64+
// comes FIRST, so a bar whose ``src`` beats the cache takes it without
65+
// reading the slots even when the cache has aged out; otherwise, once the
66+
// cached extremum has aged out (``b - bar >= length`` — by bars, not calls)
67+
// the slots are rescanned, oldest first, and the cache becomes
68+
// ``(best, b - k_best)``; otherwise the cache is kept. The output is the
69+
// cached value (the *Bars offset is ``b - bar``). Pinned 2026-09-04 with 8
70+
// ``lab tv`` tapes on NYSE:F 1D (696/696 entries), the BINANCE:ETHUSDT.P
71+
// geometry pin (82/82) and the jayentriken BBWP ETH replay (593/593):
72+
// ``m = bar_index % 49; if m < 4 or 37 <= m < 47 or m == 48: v =
73+
// ta.lowest(low, 10)`` answers 9.88 (cached from bar 42) on bars 48..51
74+
// although bar 3's stale 9.20 sits one slot behind bar 48 — the plain ring
75+
// would say 9.20 — and rescans to 9.20 on bar 52 once the cache has aged;
76+
// on ETH the run-start bar 4990 (low 2648.62 < the expired cache 2650.47)
77+
// answers its own low and never reads the aliased 08:15 slot the plain ring
78+
// returned. For an every-bar caller the cache is exactly the positional
79+
// window, so nothing changes there.
5980
struct BarContext {
6081
bool installed = false;
6182
long long bar_index = 0; // ring address: the context's current bar index
@@ -97,6 +118,17 @@ class ExtremeRing {
97118
std::vector<unsigned char> written_;
98119
long long own_bar_ = -1; // cadence when no context is installed
99120
bool has_written_ = false;
121+
// TradingView's cached extremum with an implied bar (bar_context() docs):
122+
// the value the site last answered and the bar it is attributed to.
123+
bool cached_ = false;
124+
double cval_ = 0.0;
125+
long long cbar_ = 0;
126+
// The cache as it stood BEFORE the current bar's first tick, so a
127+
// recompute() (same bar, advance=false) restores it and re-applies.
128+
long long cache_seen_bar_ = -1;
129+
bool saved_cached_ = false;
130+
double saved_cval_ = 0.0;
131+
long long saved_cbar_ = 0;
100132
};
101133

102134
class RMA {

‎src/ta_extremes_volume.cpp‎

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ namespace ta {
3434
// does not run every bar) keep stale values in the slots they did not rewrite,
3535
// skip never-written slots, and are na iff bar_index < length - 1 — pinned
3636
// 2026-09-03 on NYSE:F 1D (cadence-7 39/39, cadence-9 30/30 entry sizes).
37+
// The slots are read through a cached extremum with an implied bar: a src
38+
// that strictly beats the cache takes it without reading the slots (aged or
39+
// not); otherwise an aged-out cache (by bars, not calls) rescans the slots
40+
// oldest first and re-caches (best, bar - k_best); otherwise the cache is
41+
// kept, leaving aliased stale slots unread — pinned 2026-09-04 on NYSE:F 1D
42+
// (8 tapes 696/696: t382 `m = bar_index % 49; m < 4 or 37 <= m < 47 or
43+
// m == 48`, ta.lowest(low, 10) -> bars 48..51 keep 9.88 over bar 3's
44+
// aliased 9.20, bar 52 rescans to 9.20), BINANCE:ETHUSDT.P 15 (82/82) and
45+
// the jayentriken BBWP ETH replay (593/593; the plain ring 587).
3746

3847
BarContext& bar_context() {
3948
// Thread-local so parallel in-process engines never cross-contaminate;
@@ -94,26 +103,64 @@ ExtremeRing::Result ExtremeRing::update(double src, bool advance, bool want_max)
94103
written_[cur] = 1;
95104
has_written_ = true;
96105

106+
// The cached extremum {cval_, cbar_} is maintained on every call, warmup
107+
// included (r5g8: bar 13 answers bar 4's 9.00 cached while still na).
108+
// A recompute() on the bar the cache was last touched restores the
109+
// pre-bar cache first, so every tick of one bar re-applies from the same
110+
// starting point (magnifier compute -> recompute).
111+
if (bar != cache_seen_bar_) {
112+
cache_seen_bar_ = bar;
113+
saved_cached_ = cached_;
114+
saved_cval_ = cval_;
115+
saved_cbar_ = cbar_;
116+
} else {
117+
cached_ = saved_cached_;
118+
cval_ = saved_cval_;
119+
cbar_ = saved_cbar_;
120+
}
121+
const auto rescan = [&]() {
122+
// Oldest slot first with a strict comparison: ties resolve to the
123+
// oldest member, exactly as the deque scan did (only the *Bars
124+
// offsets can tell). Never-written slots are skipped, not zero: the
125+
// campaign grades against UI-scraped tapes whose chart carried
126+
// pre-range writes (TradingView's deep-backtest export reads them
127+
// as 0 -- a documented, deliberate deviation).
128+
double best = na<double>();
129+
int best_k = 0;
130+
for (int k = length_ - 1; k >= 0; --k) {
131+
const std::size_t i = ring_slot(bar - k, K);
132+
if (!written_[i]) continue; // never written: skipped, not na
133+
const double v = values_[i];
134+
if (is_na(v)) continue; // written gap: skipped
135+
if (is_na(best) || (want_max ? v > best : v < best)) {
136+
best = v;
137+
best_k = k;
138+
}
139+
}
140+
cval_ = best;
141+
cbar_ = bar - best_k; // the implied bar of an aliased slot
142+
};
143+
if (!cached_) {
144+
cached_ = true;
145+
cval_ = src;
146+
cbar_ = bar;
147+
} else if (!is_na(src) && (is_na(cval_) || (want_max ? src > cval_ : src < cval_))) {
148+
// A strictly new extremum takes the cache without reading the slots,
149+
// aged-out cache or not (ETH bar 4990). Ties never displace it.
150+
cval_ = src;
151+
cbar_ = bar;
152+
} else if (bar - cbar_ >= static_cast<long long>(length_)) {
153+
// The cached extremum has aged out of the window (by bars, not
154+
// calls): rescan the slots, aliasing included.
155+
rescan();
156+
}
157+
// else: the cache is younger than `length` bars and src did not beat it.
158+
97159
// na until the context has seen `length` bars (TV: bar_index < length - 1;
98160
// `origin` keeps a range-truncated feed warming up over its own bars).
99161
if (bar - origin < static_cast<long long>(length_) - 1) return out;
100-
101-
double best = na<double>();
102-
int best_k = 0;
103-
// Oldest slot first with a strict comparison: ties resolve to the oldest
104-
// member, exactly as the deque scan did (only the *Bars offsets can tell).
105-
for (int k = length_ - 1; k >= 0; --k) {
106-
const std::size_t i = ring_slot(bar - k, K);
107-
if (!written_[i]) continue; // never written: skipped, not na
108-
const double v = values_[i];
109-
if (is_na(v)) continue; // written gap: skipped
110-
if (is_na(best) || (want_max ? v > best : v < best)) {
111-
best = v;
112-
best_k = k;
113-
}
114-
}
115-
out.value = best;
116-
out.bars_back = best_k;
162+
out.value = cval_;
163+
out.bars_back = static_cast<int>(bar - cbar_);
117164
return out;
118165
}
119166

‎tests/test_ta_extremes_ring.cpp‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,70 @@ static void test_htf_context_sanity() {
506506
CHECK(!ta::bar_context().installed);
507507
}
508508

509+
510+
// --- Cached extremum with an implied bar (pinned 2026-09-04: 8 NYSE:F 1D
511+
// alias tapes 696/696, BINANCE:ETHUSDT.P 15 geometry pin 82/82, jayentriken
512+
// BBWP ETH replay 593/593) ------------------------------------------------
513+
514+
// t382 geometry (`m = bar_index % 49; exec = m < 4 or 37 <= m < 47 or m == 48`,
515+
// ta.lowest(low, 10)): bar 3's stale 9.20 aliases one slot behind bar 48, yet
516+
// TV answers the cached 9.88 (from bar 42) on bars 48..51 and only rescans to
517+
// 9.20 on bar 52, once the cache has aged `length` bars.
518+
static void test_cached_extremum_keeps_over_aliased_slot_until_expiry() {
519+
std::printf("test_cached_extremum_keeps_over_aliased_slot_until_expiry\n");
520+
ta::Lowest lo(10);
521+
auto low_at = [](int b) {
522+
if (b == 3) return 9.20;
523+
if (b < 4) return 9.50;
524+
if (b == 42) return 9.88;
525+
if (b >= 37 && b <= 46) return 10.00;
526+
return 10.50; // bars 48..52
527+
};
528+
auto executes = [](int b) {
529+
const int m = b % 49;
530+
return m < 4 || (m >= 37 && m < 47) || m == 48 || (b >= 49 && b <= 52);
531+
};
532+
for (int b = 0; b <= 52; ++b) {
533+
ta::BarContextScope scope(b, 0);
534+
if (!executes(b)) continue;
535+
const double v = lo.compute(low_at(b));
536+
if (b < 9) CHECK(is_na(v)); // bar_index < length - 1
537+
if (b == 46) CHECK(near(v, 9.88)); // fresh window minimum, bar 42
538+
if (b >= 48 && b <= 51) CHECK(near(v, 9.88)); // cache (9.88, 42) younger than 10 bars: slot 3 unread
539+
if (b == 52) CHECK(near(v, 9.20)); // aged out: rescan reads the aliased slot
540+
}
541+
}
542+
543+
// ETH #88 geometry (ta.lowest(low, 10) inside `if strategy.position_size > 0`):
544+
// a 17-bar run whose 7th bar leaves 2641.73 in the slot that aliases k=6
545+
// behind the next run's first bar, then a 50-bar gap. TV answers the run-start
546+
// bar's own low 2648.62 because it beats the expired cache (2650.47) — the
547+
// new-extremum test comes before the rescan. The control run-start (r17g50 @
548+
// bar 67: own low above the cache) rescans and reads the aliased slot.
549+
static void run_eth88_geometry(double run_start_low, double expected) {
550+
ta::Lowest lo(10);
551+
for (int b = 0; b <= 87; ++b) {
552+
ta::BarContextScope scope(b, 0);
553+
const bool in_run = b >= 20 && b <= 36;
554+
if (!in_run && b != 87) continue;
555+
double src;
556+
if (b == 26) src = 2641.73; // the 7th bar of the run: slot 26 % 11 = 4
557+
else if (in_run) src = 2650.47 + (b % 3) * 0.5; // minimum 2650.47 at bars 21, 24, ...
558+
else src = run_start_low; // bar 87: 87 - 6 = 81, 81 % 11 = 4 -> the aliased slot
559+
const double v = lo.compute(src);
560+
if (b == 35) CHECK(near(v, 2641.73)); // cache (2641.73, 26) still 9 bars young
561+
if (b == 36) CHECK(near(v, 2650.47)); // aged out at 10 bars: rescan of 27..36
562+
if (b == 87) CHECK(near(v, expected));
563+
}
564+
}
565+
566+
static void test_new_extremum_precedes_expiry_rescan() {
567+
std::printf("test_new_extremum_precedes_expiry_rescan\n");
568+
run_eth88_geometry(2648.62, 2648.62); // ETH #88: below the expired cache -> own low, no rescan
569+
run_eth88_geometry(2652.00, 2641.73); // r17g50 @67: above the cache -> rescan reads the aliased slot
570+
CHECK(!ta::bar_context().installed);
571+
}
572+
509573
int main() {
510574
test_cadence7_pin();
511575
test_cadence9_pin();
@@ -514,6 +578,8 @@ int main() {
514578
test_bars_variants_ring();
515579
test_every_bar_identity();
516580
test_htf_context_sanity();
581+
test_cached_extremum_keeps_over_aliased_slot_until_expiry();
582+
test_new_extremum_precedes_expiry_rescan();
517583
std::printf("\n%d passed, %d failed\n", g_pass, g_fail);
518584
return g_fail ? 1 : 0;
519585
}

0 commit comments

Comments
 (0)