Skip to content

Commit 1eef78e

Browse files
luisleo526claude
andcommitted
Kernel: ta::PivotPointLevels computes the pivot levels of an anchored period, held or developing (R5 lane TA1, item 4)
ta.pivot_point_levels(type, anchor, developing) with an anchor other than true on every bar, or developing = true, had no route (the codegen warns and computes the previous bar's pivots): ta::pivot_point_levels is a free function of one high, low and close. TradingView's reference: anchor "The condition that triggers the reset of the pivot point calculations. When true, calculations reset; when false, results calculated at the last reset persist."; developing "If false, the values are those calculated the last time the anchor condition was true. They remain constant until the anchor condition becomes true again. If true, the pivots are developing, i.e., they constantly recalculate on the data developing between the point of the last anchor (or bar zero if the anchor condition was never true) and the current bar."; Remarks "The developing parameter cannot be true when type is set to "Woodie", because the Woodie calculation for a period depends on that period's open ... If used together, the indicator will return a runtime error." The formulas are TradingView's Help Center "Pivot Points Standard" page, whose Woodie reads "P = (prevHigh + prevLow + 2 * currOpen) / 4" and whose DM compares prevClose with prevOpen. API (ta.hpp): enum class PivotLevelsType {Traditional, Fibonacci, Woodie, Classic, DM, Camarilla}; PivotLevelsType pivot_levels_type(const std::string&) (std::invalid_argument for any other name); class PivotPointLevels with compute / recompute (PivotLevelsType or const std::string& type, bool anchor, bool developing, double open, double high, double low, double close) -> std::vector<double> of 11 [P, R1, S1, R2, S2, R3, S3, R4, S4, R5, S5], na for the levels a type does not define; feature macro PF_PIVOT_LEVELS_HAS_ANCHOR. A period runs from an anchored bar (bar 0 before the first) to the bar before the next, aggregated as its first finite open, highest finite high, lowest finite low, last finite close. developing = false answers the levels computed on the last anchored bar from the period it closed (Woodie with that bar's open), held to the next anchor, na before the first; developing = true recomputes the period in progress, this bar included, on every bar. Woodie + developing throws std::runtime_error from compute() / recompute() -- what the generated pine_runtime_error() throws (log.hpp:11) -- which fails the run through the consumer's callback catch; spliced into the pivot probe's generated code the harness reports "pineforge engine rejected run: pivot point levels: the Woodie type has no developing levels (a Woodie period's levels need the open of the period after it)" from strategy_get_last_error. The free function is untouched. Anchored on every bar the form closes a one-bar period, the previous bar, so it computes what today's lowering computes wherever the free function spells the documented formula: on the corpus 15m feed (222,295 bars) every slot of Fibonacci, Classic and Camarilla and P, R1, S1, R2, S2, R3 of Traditional are bit-identical on every bar. They differ where the free function's formula is its own: Traditional S3 on 1,268 bars (low - 2 * (high - P) rounds differently from P * 2 - (2 * H - L)), Traditional R4..S5 on every bar (P + 3 * (H - L) is not P * 3 + (H - 3 * L)), Woodie P..S2 on the 118,503 bars whose open is not the previous close and R3..S4 on every bar, DM on 221,900 bars (close == high / close == low is not DM's open-close test). Routed through the form, the one corpus probe that calls the function (ta-pivot-point-levels-break-01, Traditional P / R1 / S1) books engine_trades.csv byte-identical to the free function's, 7663/7663 matched to its tape. Witness tests/test_ta_pivot_point_levels.cpp: the Help Center formulas transcribed, the period re-aggregated from its bars on every bar, bit for bit, six types x developing false / true (Woodie false) x anchors on bar 0 / never / every bar / random / periodic / late x na inputs, per-bar type and developing switches, a hand-pinned period, compute -> recompute -> recompute sequences with discarded anchors / types / flags, the Woodie + developing error on compute and recompute, the six names and four refused ones, and the per-slot equivalence above on 3,000 synthetic bars. Fail-before on d292f19: "error: no type named 'PivotPointLevels' in namespace 'pineforge::ta'". Pass-after: 217 passed, 0 failed; a compute() that forgets the held levels' saved copy fails 1 check, Woodie on the closed period's own open fails 17. docs/design/native-feature-parity.md: ta_misc.cpp:28 -> :29, re-anchored by check_doc_anchors.py --fix after the <stdexcept> include. ADR-0001 rule 2: generic. (a) Mechanism: pivot levels of a period delimited by an anchor series are textbook indicators any caller drives with its own anchor; the six types are the standard names, and nothing branches on a platform. (b) Knob: none -- a new indicator class, opt-in per call site; no spec field, hook, hash input, layout of an existing type or epoch, and no digest folds a ta:: instance. The error text names no Pine member. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent de61247 commit 1eef78e

6 files changed

Lines changed: 738 additions & 2 deletions

File tree

‎docs/coverage.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,32 @@ Woodie pivots use this runtime's close-based fallback because the free
448448
function does not receive the period open required by TradingView's full
449449
Woodie formula.
450450
451+
### `PivotPointLevels` — pivot levels of an anchored period
452+
453+
`PivotPointLevels::compute(type, anchor, developing, open, high, low, close)`
454+
(and `recompute`, with the same arguments) is the stateful form of Pine's
455+
`ta.pivot_point_levels(type, anchor, developing)`. `type` is a
456+
`PivotLevelsType` or one of the six names (`pivot_levels_type(name)`, which
457+
refuses any other name with `std::invalid_argument`). A period runs from an
458+
anchored bar (bar 0 before the first anchor) to the bar before the next
459+
anchored bar, aggregated as its first open, highest high, lowest low and last
460+
close (finite values only). With `developing = false` it answers the levels
461+
computed on the last anchored bar from the period that bar closed -- Woodie
462+
with that bar's own open, the open of the period the levels are for -- and
463+
holds them until the next anchored bar (`na` before the first). With
464+
`developing = true` it recomputes the levels of the period in progress on
465+
every bar; Woodie has no developing levels, and that pair throws
466+
`std::runtime_error`, which fails the run like any exception out of a script
467+
callback (`strategy_get_last_error` carries the text). The formulas are the
468+
standard definitions, operation by operation (Traditional `R3 = P * 2 + (H -
469+
2 * L)`, ..., `S5 = P * 4 - (4 * H - L)`; Woodie `P = (H + L + 2 * open) /
470+
4`, `R3 = H + 2 * (P - L)`, `R4 = R3 + (H - L)`; DM's `X` on the period's
471+
open against its close). Anchored on every bar with `developing = false` it
472+
equals the free function above for every level of Fibonacci, Classic and
473+
Camarilla and for P, R1, S1, R2 and S2 of Traditional; the free function's
474+
Traditional R3..S5, Woodie and DM formulas are its own
475+
(`PF_PIVOT_LEVELS_HAS_ANCHOR`, `tests/test_ta_pivot_point_levels.cpp`).
476+
451477
## Math (`<pineforge/math.hpp>`)
452478
453479
The runtime exposes only two pieces under math:

‎docs/pine_v6_coverage_detail.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ All **✅ Runtime** — backed by `PineMatrix` (`matrix.hpp` / `matrix.cpp`) for
808808
| `ta.percentile_linear_interpolation()` | fn | ✅ Runtime | `ta::PercentileLinearInterpolation` class | |
809809
| `ta.percentile_nearest_rank()` | fn | ✅ Runtime | `ta::PercentileNearestRank` class | |
810810
| `ta.percentrank()` | fn | ✅ Runtime | `ta::PercentRank` class | |
811-
| `ta.pivot_point_levels()` | fn | ✅ Runtime | Free function `ta::pivot_point_levels(method, high, low, close)` | Woodie uses close-based fallback |
811+
| `ta.pivot_point_levels()` | fn | ✅ Runtime | Free function `ta::pivot_point_levels(method, high, low, close)`; with `anchor` / `developing`: `ta::PivotPointLevels` class | Free function: Woodie uses close-based fallback. `PivotPointLevels::compute(type, anchor, developing, open, high, low, close)` accumulates the anchored period, held or developing; Woodie + developing throws (R5 lane TA1) |
812812
| `ta.pivothigh()` | fn | ✅ Runtime | `ta::PivotHigh` class | |
813813
| `ta.pivotlow()` | fn | ✅ Runtime | `ta::PivotLow` class | |
814814
| `ta.range()` | fn | ✅ Runtime | `ta::Range` class | |

‎include/pineforge/ta.hpp‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,86 @@ class Dev {
12871287
std::vector<double> pivot_point_levels(const std::string& method,
12881288
double high, double low, double close);
12891289

1290+
// --- Pivot point levels of an anchored period ---
1291+
1292+
// The six pivot types. DM is DeMark's.
1293+
enum class PivotLevelsType : unsigned char {
1294+
Traditional = 0,
1295+
Fibonacci = 1,
1296+
Woodie = 2,
1297+
Classic = 3,
1298+
DM = 4,
1299+
Camarilla = 5,
1300+
};
1301+
1302+
// The type spelled "Traditional", "Fibonacci", "Woodie", "Classic", "DM" or
1303+
// "Camarilla"; std::invalid_argument for any other name.
1304+
PivotLevelsType pivot_levels_type(const std::string& name);
1305+
1306+
// The 11 pivot levels [P, R1, S1, R2, S2, R3, S3, R4, S4, R5, S5] of a
1307+
// period of bars, na for the levels a type does not define. A period runs
1308+
// from an anchored bar (bar 0 before the first anchor) to the bar before the
1309+
// next anchored bar; a period is aggregated as its first finite open, highest
1310+
// finite high, lowest finite low and last finite close.
1311+
//
1312+
// developing = false: the levels computed on the last anchored bar, from the
1313+
// period that bar closed (and, for Woodie, that bar's own open -- the open of
1314+
// the period the levels are for), held until the next anchored bar; all na
1315+
// before the first anchored bar that closes a non-empty period.
1316+
// developing = true: the levels of the period in progress, this bar included,
1317+
// recomputed on every bar (DM reads the period's own open). Woodie has no
1318+
// developing levels -- its levels need the open of the period after the one
1319+
// measured -- so compute() and recompute() throw std::runtime_error for that
1320+
// pair, which fails a run as any exception out of a script callback does.
1321+
//
1322+
// The formulas are the standard pivot definitions, Traditional's R3..S5 and
1323+
// Woodie's among them, evaluated operation by operation as written: P =
1324+
// (H + L + C) / 3, R1 = P * 2 - L, ..., R3 = P * 2 + (H - 2 * L), ..., R5 =
1325+
// P * 4 + (H - 4 * L); Woodie P = (H + L + 2 * O_next) / 4, R3 = H + 2 * (P -
1326+
// L), R4 = R3 + (H - L); DM X = O == C ? H + L + 2 * C : C > O ? 2 * H + L +
1327+
// C : 2 * L + H + C, P = X / 4, R1 = X / 2 - L, S1 = X / 2 - H; Camarilla R1 =
1328+
// C + 1.1 * (H - L) / 12, ..., R5 = (H / L) * C (na on a zero low), S5 = C -
1329+
// (R5 - C). Anchored on every bar with developing = false, this is the free
1330+
// function above wherever that function spells the same formula (every level
1331+
// of Fibonacci, Classic and Camarilla; P, R1, S1, R2 and S2 of Traditional);
1332+
// the free function's Traditional R3..S5, Woodie and DM formulas differ.
1333+
// recompute() re-runs the current bar from the state its first compute()
1334+
// found.
1335+
class PivotPointLevels {
1336+
public:
1337+
PivotPointLevels() = default;
1338+
std::vector<double> compute(PivotLevelsType type, bool anchor, bool developing,
1339+
double open, double high, double low, double close);
1340+
std::vector<double> recompute(PivotLevelsType type, bool anchor, bool developing,
1341+
double open, double high, double low, double close);
1342+
// The type by name (pivot_levels_type()).
1343+
std::vector<double> compute(const std::string& type, bool anchor, bool developing,
1344+
double open, double high, double low, double close);
1345+
std::vector<double> recompute(const std::string& type, bool anchor, bool developing,
1346+
double open, double high, double low, double close);
1347+
1348+
private:
1349+
struct Period {
1350+
double open = na<double>();
1351+
double high = na<double>();
1352+
double low = na<double>();
1353+
double close = na<double>();
1354+
void enter(double o, double h, double l, double c);
1355+
};
1356+
struct Levels {
1357+
double v[11];
1358+
};
1359+
static Levels no_levels();
1360+
1361+
Period period_; // the period in progress
1362+
Levels held_ = no_levels(); // the levels of the last closed period
1363+
Period saved_period_;
1364+
Levels saved_held_ = no_levels();
1365+
};
1366+
1367+
// Feature macro: PivotPointLevels / PivotLevelsType / pivot_levels_type exist.
1368+
#define PF_PIVOT_LEVELS_HAS_ANCHOR 1
1369+
12901370
} // namespace ta
12911371

12921372
} // namespace pineforge

‎src/ta_misc.cpp‎

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ta_misc.cpp — stats + helpers + free functions: Linreg, PercentRank, PercentileNearestRank, PercentileLinearInterpolation, Correlation, BarsSince, ValueWhen, pivot_point_levels
2+
* ta_misc.cpp — stats + helpers + free functions: Linreg, PercentRank, PercentileNearestRank, PercentileLinearInterpolation, Correlation, BarsSince, ValueWhen, pivot_point_levels, PivotPointLevels
33
*
44
* Carved out of ta.cpp during the v0.1 file-split (phase 6) so the
55
* 66-class TA library becomes navigable. Every class declared in
@@ -14,6 +14,7 @@
1414
#include <algorithm>
1515
#include <cmath>
1616
#include <numeric>
17+
#include <stdexcept>
1718
#include <string>
1819
#include <vector>
1920

@@ -438,5 +439,155 @@ std::vector<double> pivot_point_levels(const std::string& method,
438439
return out;
439440
}
440441

442+
// ============================================================================
443+
// Pivot point levels of an anchored period
444+
// ============================================================================
445+
446+
namespace {
447+
448+
// The levels of the period (o, h, l, c) -- next_open is the open of the
449+
// period the levels are for, which only Woodie reads -- into out[11], each
450+
// formula evaluated operation by operation as the standard definition spells
451+
// it. A formula missing one of its inputs leaves all eleven na, as the free
452+
// function does for a missing high, low or close.
453+
void pivot_levels_of(PivotLevelsType type, double o, double h, double l, double c,
454+
double next_open, double* out) {
455+
for (int i = 0; i < 11; ++i) out[i] = na<double>();
456+
switch (type) {
457+
case PivotLevelsType::Woodie: {
458+
if (is_na(h) || is_na(l) || is_na(next_open)) return;
459+
const double P = (h + l + 2.0 * next_open) / 4.0;
460+
const double R3 = h + 2.0 * (P - l);
461+
const double S3 = l - 2.0 * (h - P);
462+
out[0] = P;
463+
out[1] = 2.0 * P - l; out[2] = 2.0 * P - h;
464+
out[3] = P + (h - l); out[4] = P - (h - l);
465+
out[5] = R3; out[6] = S3;
466+
out[7] = R3 + (h - l); out[8] = S3 - (h - l);
467+
return;
468+
}
469+
case PivotLevelsType::DM: {
470+
if (is_na(o) || is_na(h) || is_na(l) || is_na(c)) return;
471+
double x;
472+
if (o == c) x = h + l + 2.0 * c;
473+
else if (c > o) x = 2.0 * h + l + c;
474+
else x = 2.0 * l + h + c;
475+
out[0] = x / 4.0;
476+
out[1] = x / 2.0 - l; out[2] = x / 2.0 - h;
477+
return;
478+
}
479+
case PivotLevelsType::Traditional:
480+
case PivotLevelsType::Fibonacci:
481+
case PivotLevelsType::Classic:
482+
case PivotLevelsType::Camarilla:
483+
break;
484+
}
485+
if (is_na(h) || is_na(l) || is_na(c)) return;
486+
const double P = (h + l + c) / 3.0;
487+
out[0] = P;
488+
switch (type) {
489+
case PivotLevelsType::Traditional:
490+
out[1] = P * 2.0 - l; out[2] = P * 2.0 - h;
491+
out[3] = P + (h - l); out[4] = P - (h - l);
492+
out[5] = P * 2.0 + (h - 2.0 * l); out[6] = P * 2.0 - (2.0 * h - l);
493+
out[7] = P * 3.0 + (h - 3.0 * l); out[8] = P * 3.0 - (3.0 * h - l);
494+
out[9] = P * 4.0 + (h - 4.0 * l); out[10] = P * 4.0 - (4.0 * h - l);
495+
return;
496+
case PivotLevelsType::Fibonacci:
497+
out[1] = P + 0.382 * (h - l); out[2] = P - 0.382 * (h - l);
498+
out[3] = P + 0.618 * (h - l); out[4] = P - 0.618 * (h - l);
499+
out[5] = P + (h - l); out[6] = P - (h - l);
500+
return;
501+
case PivotLevelsType::Classic:
502+
out[1] = 2.0 * P - l; out[2] = 2.0 * P - h;
503+
out[3] = P + (h - l); out[4] = P - (h - l);
504+
out[5] = P + 2.0 * (h - l); out[6] = P - 2.0 * (h - l);
505+
out[7] = P + 3.0 * (h - l); out[8] = P - 3.0 * (h - l);
506+
return;
507+
case PivotLevelsType::Camarilla: {
508+
out[1] = c + 1.1 * (h - l) / 12.0; out[2] = c - 1.1 * (h - l) / 12.0;
509+
out[3] = c + 1.1 * (h - l) / 6.0; out[4] = c - 1.1 * (h - l) / 6.0;
510+
out[5] = c + 1.1 * (h - l) / 4.0; out[6] = c - 1.1 * (h - l) / 4.0;
511+
out[7] = c + 1.1 * (h - l) / 2.0; out[8] = c - 1.1 * (h - l) / 2.0;
512+
const double R5 = l != 0.0 ? (h / l) * c : na<double>();
513+
out[9] = R5;
514+
out[10] = is_na(R5) ? na<double>() : c - (R5 - c);
515+
return;
516+
}
517+
case PivotLevelsType::Woodie:
518+
case PivotLevelsType::DM:
519+
return;
520+
}
521+
}
522+
523+
} // namespace
524+
525+
PivotLevelsType pivot_levels_type(const std::string& name) {
526+
if (name == "Traditional") return PivotLevelsType::Traditional;
527+
if (name == "Fibonacci") return PivotLevelsType::Fibonacci;
528+
if (name == "Woodie") return PivotLevelsType::Woodie;
529+
if (name == "Classic") return PivotLevelsType::Classic;
530+
if (name == "DM") return PivotLevelsType::DM;
531+
if (name == "Camarilla") return PivotLevelsType::Camarilla;
532+
throw std::invalid_argument("pivot point levels: unknown type '" + name + "'");
533+
}
534+
535+
void PivotPointLevels::Period::enter(double o, double h, double l, double c) {
536+
if (is_na(open) && !is_na(o)) open = o;
537+
if (!is_na(h) && (is_na(high) || h > high)) high = h;
538+
if (!is_na(l) && (is_na(low) || l < low)) low = l;
539+
if (!is_na(c)) close = c;
540+
}
541+
542+
PivotPointLevels::Levels PivotPointLevels::no_levels() {
543+
Levels levels;
544+
for (double& v : levels.v) v = na<double>();
545+
return levels;
546+
}
547+
548+
std::vector<double> PivotPointLevels::compute(PivotLevelsType type, bool anchor, bool developing,
549+
double open, double high, double low, double close) {
550+
if (developing && type == PivotLevelsType::Woodie) {
551+
throw std::runtime_error(
552+
"pivot point levels: the Woodie type has no developing levels (a Woodie "
553+
"period's levels need the open of the period after it)");
554+
}
555+
saved_period_ = period_;
556+
saved_held_ = held_;
557+
if (anchor) {
558+
// This bar closes the period in progress and opens the next one: the
559+
// held levels are the closed period's, with this bar's open as the
560+
// open of the period they are for.
561+
pivot_levels_of(type, period_.open, period_.high, period_.low, period_.close, open,
562+
held_.v);
563+
period_ = Period{};
564+
}
565+
period_.enter(open, high, low, close);
566+
if (!developing) return std::vector<double>(held_.v, held_.v + 11);
567+
std::vector<double> out(11);
568+
pivot_levels_of(type, period_.open, period_.high, period_.low, period_.close, na<double>(),
569+
out.data());
570+
return out;
571+
}
572+
573+
std::vector<double> PivotPointLevels::recompute(PivotLevelsType type, bool anchor, bool developing,
574+
double open, double high, double low,
575+
double close) {
576+
period_ = saved_period_;
577+
held_ = saved_held_;
578+
return compute(type, anchor, developing, open, high, low, close);
579+
}
580+
581+
std::vector<double> PivotPointLevels::compute(const std::string& type, bool anchor, bool developing,
582+
double open, double high, double low, double close) {
583+
return compute(pivot_levels_type(type), anchor, developing, open, high, low, close);
584+
}
585+
586+
std::vector<double> PivotPointLevels::recompute(const std::string& type, bool anchor,
587+
bool developing, double open, double high,
588+
double low, double close) {
589+
return recompute(pivot_levels_type(type), anchor, developing, open, high, low, close);
590+
}
591+
441592
} // namespace ta
442593
} // namespace pineforge

‎tests/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,10 @@ list(APPEND TEST_SOURCES test_ta_kc_basis)
10181018
# R5 lane TA1: ta::KC / ta::KCW's useTrueRange against f_kc / f_kcw.
10191019
# Source-free, so it also runs in the kernel-only profile.
10201020
list(APPEND TEST_SOURCES test_ta_kc_range)
1021+
# R5 lane TA1: ta::PivotPointLevels, held and developing, against the pivot
1022+
# formulas re-aggregated naively, and against ta::pivot_point_levels.
1023+
# Source-free, so it also runs in the kernel-only profile.
1024+
list(APPEND TEST_SOURCES test_ta_pivot_point_levels)
10211025

10221026
# R4-D L4b-fix restores every removed ShortSeed, affordability, sizing and
10231027
# deferred-close oracle as an individually compiled native-route twin. Keep

0 commit comments

Comments
 (0)