PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
pine_ta_cci.hpp
Go to the documentation of this file.
1#pragma once
2// TradingView's ta.cci on a window whose mean deviation is zero up to
3// rounding.
4//
5// ta::CCI (<pineforge/ta.hpp>) divides (src - mean) by 0.015 times the mean
6// deviation unless that deviation is exactly 0. Identical values whose
7// decimal spelling is not a binary fraction do not reach an exact 0: three
8// values of 0.1 sum to 0.30000000000000004, the mean is one step above 0.1,
9// every absolute deviation is that one step (2^-56, about 1.4e-17), and the
10// quotient is -1 / 0.015, about -66.67.
11//
12// TradingView's ta.cci answers 0 there. On a synthetic script whose source
13// is 0.1 on every bar its call site sees, TradingView's own ta.dev(x, 3) and
14// ta.sma(x, 3) - x are both about 1.4e-17, not zero, and ta.cci(x, 3) is 0;
15// ta.cci(1.0, 3) is 0 as well. PineCci therefore treats a mean deviation
16// inside the comparison band of <pineforge/ta_compare_band.hpp> (at most
17// 1e-10 from zero, the band TradingView's relational operators follow) as
18// zero and answers 0. Outside the band every value is ta::CCI's own, bit for
19// bit, and na stays na.
20//
21// The deviation is read from a ta::Dev that receives the same calls: ta::Dev
22// and ta::CCI keep the same window (the last `length` values that are not na)
23// and use the same arithmetic in the same order, so ta::Dev's value is the
24// deviation ta::CCI divides by.
25//
26// Not reproduced: TradingView's ta.cci carries state from earlier bars. After
27// a history of much larger values the same three values of 0.1 read -50
28// there. That is the value an ordinary window gives when one of its three
29// members is higher than the other two, and no mean shared by three equal
30// members can give it. PineCci, like ta::CCI, computes each window from its
31// members alone and answers 0 in that case too.
32//
33// The members forwarded are those of ta::CCI: the length constructor,
34// compute, recompute and observe_owned_storage.
35#include <pineforge/ta.hpp>
37
38namespace pineforge {
39namespace source {
40
41class PineCci {
42 ta::CCI cci_;
43 ta::Dev dev_;
44
45 static double flat_as_zero(double value, double deviation) {
46 return float_band_eq(deviation, 0.0) ? 0.0 : value;
47 }
48
49public:
50 explicit PineCci(int length) : cci_(length), dev_(length) {}
51 double compute(double src) {
52 const double deviation = dev_.compute(src);
53 return flat_as_zero(cci_.compute(src), deviation);
54 }
55 double recompute(double src) {
56 const double deviation = dev_.recompute(src);
57 return flat_as_zero(cci_.recompute(src), deviation);
58 }
59 void observe_owned_storage(OwnedStorage& out) const noexcept {
61 }
62};
63
64} // namespace source
65} // namespace pineforge
66
67// Feature macro: the class above exists.
68#define PINEFORGE_HAS_PINE_CCI 1
void observe_owned_storage(OwnedStorage &out) const noexcept
double compute(double src)
double recompute(double src)
bool float_band_eq(double a, double b)
void observe_owned_members(OwnedStorage &out, const T &... values) noexcept