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
>
36
#include <
pineforge/ta_compare_band.hpp
>
37
38
namespace
pineforge
{
39
namespace
source
{
40
41
class
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
49
public
:
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
{
60
::pineforge::observe_owned_members
(out, cci_, dev_);
61
}
62
};
63
64
}
// namespace source
65
}
// namespace pineforge
66
67
// Feature macro: the class above exists.
68
#define PINEFORGE_HAS_PINE_CCI 1
pineforge::OwnedStorage
Definition
owned_storage.hpp:66
pineforge::source::PineCci::observe_owned_storage
void observe_owned_storage(OwnedStorage &out) const noexcept
Definition
pine_ta_cci.hpp:59
pineforge::source::PineCci::compute
double compute(double src)
Definition
pine_ta_cci.hpp:51
pineforge::source::PineCci::PineCci
PineCci(int length)
Definition
pine_ta_cci.hpp:50
pineforge::source::PineCci::recompute
double recompute(double src)
Definition
pine_ta_cci.hpp:55
pineforge::ta::CCI
Definition
ta.hpp:914
pineforge::ta::Dev
Definition
ta.hpp:1713
pineforge::source
Definition
exit_payload_readback.hpp:5
pineforge
Definition
array.hpp:42
pineforge::float_band_eq
bool float_band_eq(double a, double b)
Definition
ta_compare_band.hpp:38
pineforge::observe_owned_members
void observe_owned_members(OwnedStorage &out, const T &... values) noexcept
Definition
owned_storage.hpp:263
ta.hpp
ta_compare_band.hpp
include
pineforge
source
pine_ta_cci.hpp
PineForge unreleased — Apache-2.0 —
github.com/pineforge-4pass/pineforge-engine
—
pineforge.dev