Skip to content

Commit f631685

Browse files
luisleo526claude
andcommitted
ta: bar-addressed extremum ring for conditional highest/lowest callers
TradingView keeps the source history of a window call site (ta.highest, ta.lowest, ta.highestbars, ta.lowestbars) in a ring of K = length + 1 slots addressed by bar_index % K: a call on bar b writes slot[b % K] = src and returns the extremum over the WRITTEN slots (b - k) % K, k in [0, length). A call that executes every bar sees the plain positional window (unchanged). A call that executes conditionally — inside an `if` block that does not run every bar — keeps stale values in the slots it did not rewrite, skips never-written slots, and is na iff bar_index < length - 1, NOT until `length` executions have accumulated. The engine's per-call deque was na until `length` pushes and never dropped a value by bar distance: wrong from the second execution on. Pins (2026-09-03, `lab tv`, NYSE:F 1D, 2025-04-01..2026-05-01): if bar_index % 7 == 3: v = ta.highest(high, 5); size = round(v*100) bars 3,10,17,24,31,38,45,52,59,66 -> na, 9.73, 10.10, 10.62, 10.78, 10.78, 10.78, 10.78, 10.78, 11.85 — 39/39 entry sizes exact; cadence 9 (bars 3,12,21,...): na, 9.73, 10.32, 10.70, 10.70, 10.65, 10.87, 11.85 — 30/30 exact (call 6 = 10.65: a per-call buffer would still answer 10.70). Production: colasbreugnon-sma F@1D 31/31 only with K = 6; jayentriken BBWP ETH 358/364 stop exits. Plumbing (no codegen change): ta::bar_context() is a thread-local {installed, bar_index, origin}; ta::BarContextScope installs it RAII- style. The engine installs the chart's Pine bar_index (origin = bar_index_offset_, so a range-truncated feed still warms up over its own first `length` bars) around on_bar, and the requested context's evaluated-bar index (complete: eval_complete_count - 1, partial/ lookahead: eval_complete_count, calling-boundary replay: the index it replays) around every evaluate_security dispatch — HTF aggregation, lookahead partials, historical projections, lower-tf emulation/chunking and the auxiliary feed all route through the new dispatch_security_eval. With no context installed (unit tests, standalone use) every compute() is its own bar: byte-identical to the deque. Highest/Lowest/HighestBars/LowestBars share one ExtremeRing. Every-bar callers are proven byte-identical to the previous deque (reproduced verbatim in the test) over random series with and without na gaps, with recompute() ticks, in no-context, chart and offset-origin modes. Ties still resolve to the oldest member. The *Bars variants now record an na input as a positional gap like Highest/Lowest (the deque skipped it without advancing, so its offset pointed at the wrong bar); na in still answers na out. ta.sma inside `if` (per-execution buffer) is untouched. Tests: tests/test_ta_extremes_ring.cpp (cadence-7 and cadence-9 tapes replayed bar by bar, na-until-bar-(length-1), lowest mirror, *Bars offsets, every-bar identity, HTF-context sanity). Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dwbd239WT9zfcTLYM5UpUU Claude-Session: https://claude.ai/code/session_01Jv3d6jrrCwdfxaZKV8MNTy
1 parent 11e61d4 commit f631685

9 files changed

Lines changed: 779 additions & 164 deletions

File tree

‎docs/pine_v6_coverage_detail.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,14 +790,14 @@ 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 | |
794-
| `ta.highestbars()` | fn | ✅ Runtime | `ta::HighestBars` 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 |
794+
| `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 | |
800-
| `ta.lowestbars()` | fn | ✅ Runtime | `ta::LowestBars` class | |
799+
| `ta.lowest()` | fn | ✅ Runtime | `ta::Lowest` class | same bar-addressed ring as `ta.highest` (mirror) |
800+
| `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 |
803803
| `ta.median()` | fn | ✅ Runtime | `ta::Median` class | |

‎include/pineforge/engine.hpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,11 @@ class BacktestEngine {
22202220
int64_t feed_count = 0;
22212221
int64_t eval_complete_count = 0;
22222222
int64_t eval_partial_count = 0;
2223+
// Requested-context bar index of the latest dispatch_security_eval()
2224+
// (the ring address its TA members saw, see ta::bar_context()); -1
2225+
// before the first dispatch. The calling-boundary replay re-dispatches
2226+
// the same bar under the same index.
2227+
int64_t ta_bar_index = -1;
22232228
// ``request.security_lower_tf`` returns one element per
22242229
// synthesised sub-bar of the current chart bar, so the codegen
22252230
// needs to know which sub-bar inside the current chart bar is
@@ -2380,6 +2385,17 @@ class BacktestEngine {
23802385
int security_lower_tf_sub_bar_index(int sec_id) const;
23812386
void validate_security_timeframes(const std::string& input_tf);
23822387
bool security_series_slot_is_new(int sec_id) const;
2388+
// The one path to evaluate_security(): installs the requested context's
2389+
// bar index for the evaluator's TA members (ta::bar_context()) for the
2390+
// duration of the dispatch. `bar_index` is the 0-based index of the
2391+
// requested-context bar being evaluated — the just-completed bucket for a
2392+
// complete evaluation (eval_complete_count - 1), the in-progress bucket
2393+
// for a partial/lookahead one (eval_complete_count) — so every
2394+
// compute()/recompute() dispatch of one requested bar rewrites the same
2395+
// ring slot, and a conditional window call inside the security expression
2396+
// is addressed exactly like TradingView addresses it.
2397+
void dispatch_security_eval(SecurityEvalState& state, const Bar& bar,
2398+
bool publish, int64_t bar_index);
23832399
// KI-55 range-start gate for one evaluator: true when the input bar at
23842400
// `input_ts` belongs to an HTF bucket that opened before
23852401
// security_range_start_ms_ (always false while the flag is off). The

‎include/pineforge/ta.hpp‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,76 @@ namespace ta {
2929
// byte-identical to the prior src-seed behavior.
3030
bool& ema_na_warmup_flag();
3131

32+
// Per-context bar index for bar-addressed window state (thread-local).
33+
//
34+
// TradingView keeps the source history of a window call site (``ta.highest``,
35+
// ``ta.lowest``, ``ta.highestbars``, ``ta.lowestbars``) in a ring of
36+
// ``K = length + 1`` slots addressed by ``bar_index % K``. A call on bar ``b``
37+
// writes ``slot[b % K] = src`` and returns the extremum over the WRITTEN slots
38+
// ``(b - k) % K``, ``k in [0, length)``. For a call that executes every bar
39+
// that is the plain positional window. For a call that executes
40+
// CONDITIONALLY (inside an ``if`` block that does not run every bar) the slots
41+
// not rewritten keep their stale values from earlier executions, never-written
42+
// slots are skipped, and the result is na iff ``bar_index < length - 1`` — NOT
43+
// until ``length`` executions have accumulated. Pinned 2026-09-03 with
44+
// ``lab tv`` on NYSE:F 1D (``if bar_index % 7 == 3: v = ta.highest(high, 5)``
45+
// 39/39 entry sizes exact, cadence 9 30/30; production: colasbreugnon-sma
46+
// F@1D 31/31 only with K = 6). ``ta.sma`` inside ``if`` is NOT ring-addressed
47+
// (per-execution buffer, pinned separately) and is untouched.
48+
//
49+
// The engine installs the executing context's bar index around every dispatch
50+
// of generated code: the chart's Pine ``bar_index`` around ``on_bar`` and the
51+
// requested context's evaluated-bar index around each ``evaluate_security``
52+
// (HTF / lower-tf / auxiliary feeds). ``origin`` is the first bar index the
53+
// context has data for — the warmup reference — so a feed that starts after
54+
// TradingView's hidden first chart bar (``bar_index_offset``) still warms up
55+
// over its own first ``length`` bars, exactly as before. With no context
56+
// installed (the default — unit tests, standalone use) every ``compute()`` is
57+
// its own bar and the objects are byte-identical to the previous positional
58+
// deque.
59+
struct BarContext {
60+
bool installed = false;
61+
long long bar_index = 0; // ring address: the context's current bar index
62+
long long origin = 0; // warmup reference: the context's first bar index
63+
};
64+
BarContext& bar_context();
65+
66+
// RAII installer: sets the thread-local BarContext for the enclosed dispatch
67+
// and restores the previous one on exit (also during stack unwinding), so
68+
// chart and request.security evaluation never see each other's index.
69+
class BarContextScope {
70+
BarContext previous_;
71+
72+
public:
73+
BarContextScope(long long bar_index, long long origin);
74+
~BarContextScope();
75+
BarContextScope(const BarContextScope&) = delete;
76+
BarContextScope& operator=(const BarContextScope&) = delete;
77+
};
78+
79+
// Bar-addressed extremum ring shared by Highest / Lowest / HighestBars /
80+
// LowestBars — the TradingView rule documented at bar_context() above.
81+
class ExtremeRing {
82+
public:
83+
struct Result {
84+
double value; // na: window not formed yet, or no written non-na member
85+
int bars_back; // k of the extremum slot (0 = the current bar)
86+
};
87+
explicit ExtremeRing(int length);
88+
// advance = true -> compute(): records src for the context's current bar
89+
// advance = false -> recompute(): rewrites the current bar's slot
90+
// An na src is recorded as a gap (skipped by the extremum, never a member).
91+
// Ties resolve to the OLDEST member (strict comparison, oldest slot first).
92+
Result update(double src, bool advance, bool want_max);
93+
94+
private:
95+
int length_;
96+
std::vector<double> values_;
97+
std::vector<unsigned char> written_;
98+
long long own_bar_ = -1; // cadence when no context is installed
99+
bool has_written_ = false;
100+
};
101+
32102
class RMA {
33103
double output_val;
34104
double sum;
@@ -158,8 +228,7 @@ class MACD {
158228
// --- Highest ---
159229

160230
class Highest {
161-
std::deque<double> buffer;
162-
int length;
231+
ExtremeRing ring_;
163232

164233
public:
165234
explicit Highest(int length);
@@ -170,8 +239,7 @@ class Highest {
170239
// --- Lowest ---
171240

172241
class Lowest {
173-
std::deque<double> buffer;
174-
int length;
242+
ExtremeRing ring_;
175243

176244
public:
177245
explicit Lowest(int length);
@@ -597,8 +665,7 @@ class Median {
597665
// --- HighestBars ---
598666

599667
class HighestBars {
600-
int length_;
601-
std::deque<double> buffer_;
668+
ExtremeRing ring_;
602669

603670
public:
604671
explicit HighestBars(int length);
@@ -609,8 +676,7 @@ class HighestBars {
609676
// --- LowestBars ---
610677

611678
class LowestBars {
612-
int length_;
613-
std::deque<double> buffer_;
679+
ExtremeRing ring_;
614680

615681
public:
616682
explicit LowestBars(int length);

‎src/engine_aux_security.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ void BacktestEngine::feed_aux_security_for_chart_bar(int chart_index) {
279279
state.current_bar = bar;
280280
state.current_sub_bar_count = 1;
281281
state.eval_complete_count++;
282-
evaluate_security(state.sec_id, bar, true);
282+
dispatch_security_eval(state, bar, true,
283+
state.eval_complete_count - 1);
283284
state.lower_tf_sub_bar_index++;
284285
}
285286
state.lower_tf_input_buffer.clear();

‎src/engine_run.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ void BacktestEngine::invoke_chart_on_bar(const Bar& bar) {
8787
}
8888
} scope(chart_ema_na_warmup_);
8989

90+
// Bar-addressed window state (ta::bar_context()): the chart context's TA
91+
// members address their rings by the Pine bar_index the script sees, and
92+
// warm up from the feed's first bar (pine index bar_index_offset_).
93+
// Every tick of one script bar (compute() then recompute() under the bar
94+
// magnifier) shares the index, so they rewrite the same slot.
95+
ta::BarContextScope bar_scope(pine_bar_index(), bar_index_offset_);
96+
9097
named_entry_cancelled_incarnation_in_current_eval_.clear();
9198
on_bar(bar);
9299
}
@@ -764,6 +771,7 @@ void BacktestEngine::run(const Bar* bars, int n) {
764771
state.eval_partial_count = 0;
765772
state.current_bar = Bar{};
766773
state.current_sub_bar_count = 0;
774+
state.ta_bar_index = -1;
767775
}
768776

769777
for (int i = 0; i < n; i++) {

‎src/engine_security.cpp‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ void BacktestEngine::validate_security_timeframes(const std::string& input_tf) {
266266
}
267267

268268

269+
void BacktestEngine::dispatch_security_eval(SecurityEvalState& state,
270+
const Bar& bar, bool publish,
271+
int64_t bar_index) {
272+
state.ta_bar_index = bar_index;
273+
// The requested context starts at its own bar 0 (origin 0): its TA members
274+
// warm up over the first `length` evaluated bars, exactly as before.
275+
ta::BarContextScope bar_scope(static_cast<long long>(bar_index), 0);
276+
evaluate_security(state.sec_id, bar, publish);
277+
}
278+
279+
269280
bool BacktestEngine::security_series_slot_is_new(int sec_id) const {
270281
if (security_history_publication_replay_) {
271282
return false;
@@ -298,8 +309,11 @@ void BacktestEngine::publish_security_eval_state_at_calling_boundary(
298309
// the final requested value that was already evaluated for the completed
299310
// caller, before the chart body runs and before that retained input is fed.
300311
// security_series_slot_is_new() returns false in this scope, so generated
301-
// TA sites recompute the current slot instead of advancing their cadence.
302-
evaluate_security(state.sec_id, state.current_bar, true);
312+
// TA sites recompute the current slot instead of advancing their cadence —
313+
// under the same requested-context bar index as the evaluation replayed.
314+
dispatch_security_eval(state, state.current_bar, true,
315+
state.ta_bar_index >= 0 ? state.ta_bar_index
316+
: state.eval_complete_count);
303317
}
304318

305319

@@ -457,7 +471,8 @@ void BacktestEngine::feed_security_eval_state(
457471
state.current_bar = b;
458472
state.current_sub_bar_count = 1;
459473
state.eval_complete_count++;
460-
evaluate_security(state.sec_id, b, true);
474+
dispatch_security_eval(state, b, true,
475+
state.eval_complete_count - 1);
461476
state.lower_tf_sub_bar_index++;
462477
}
463478
state.lower_tf_input_buffer.clear();
@@ -500,7 +515,8 @@ void BacktestEngine::feed_security_eval_state(
500515
state.current_bar = synthetic_bar;
501516
state.current_sub_bar_count = 1;
502517
state.eval_complete_count++;
503-
evaluate_security(state.sec_id, synthetic_bar, true);
518+
dispatch_security_eval(state, synthetic_bar, true,
519+
state.eval_complete_count - 1);
504520
state.lower_tf_sub_bar_index++;
505521
}
506522
return;
@@ -541,8 +557,10 @@ void BacktestEngine::feed_security_eval_state(
541557
} else {
542558
state.eval_partial_count++;
543559
}
544-
evaluate_security(state.sec_id, projected_bar,
545-
projection.is_complete);
560+
dispatch_security_eval(state, projected_bar, projection.is_complete,
561+
projection.is_complete
562+
? state.eval_complete_count - 1
563+
: state.eval_complete_count);
546564
return;
547565
}
548566

@@ -577,7 +595,8 @@ void BacktestEngine::feed_security_eval_state(
577595
// replaced.
578596
publish = calling_bar_complete;
579597
}
580-
evaluate_security(state.sec_id, ab.bar, publish);
598+
dispatch_security_eval(state, ab.bar, publish,
599+
state.eval_complete_count - 1);
581600
} else if (state.lookahead_on) {
582601
if (state.heikinashi) apply_ha(ab.bar, /*commit=*/false);
583602
state.current_bar = ab.bar;
@@ -587,7 +606,9 @@ void BacktestEngine::feed_security_eval_state(
587606
// merged history without adding a second evaluator/TA dispatch.
588607
const bool publish = state.publish_gate_tf_seconds > 0
589608
&& calling_bar_complete;
590-
evaluate_security(state.sec_id, ab.bar, publish);
609+
// Partial (in-progress) bucket: the index the completion will carry.
610+
dispatch_security_eval(state, ab.bar, publish,
611+
state.eval_complete_count);
591612
} else {
592613
state.current_bar = ab.bar;
593614
if (state.gaps_on) {

0 commit comments

Comments
 (0)