Skip to main content

wickra_core/indicators/
camarilla_pivots.rs

1//! Camarilla Pivot Points (Nick Stott).
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Camarilla Pivot Points output: four resistances, the pivot, four supports.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct CamarillaPivotsOutput {
9    /// Pivot Point: `(H + L + C) / 3` (informational, not in the Camarilla R/S formulas).
10    pub pp: f64,
11    /// Resistance 1: `C + (H − L)·1.1/12`.
12    pub r1: f64,
13    /// Resistance 2: `C + (H − L)·1.1/6`.
14    pub r2: f64,
15    /// Resistance 3: `C + (H − L)·1.1/4`.
16    pub r3: f64,
17    /// Resistance 4: `C + (H − L)·1.1/2`.
18    pub r4: f64,
19    /// Support 1: `C − (H − L)·1.1/12`.
20    pub s1: f64,
21    /// Support 2: `C − (H − L)·1.1/6`.
22    pub s2: f64,
23    /// Support 3: `C − (H − L)·1.1/4`.
24    pub s3: f64,
25    /// Support 4: `C − (H − L)·1.1/2`.
26    pub s4: f64,
27}
28
29/// Camarilla Pivot Points — Nick Stott's four-tier range-based level set.
30/// Anchored on the prior close rather than the typical price, with widths
31/// scaled by the constant `1.1` divided by `{12, 6, 4, 2}`.
32///
33/// ```text
34/// PP = (H + L + C) / 3
35/// R_n = C + (H − L) · 1.1 / d_n     S_n = C − (H − L) · 1.1 / d_n
36///   where d_1 = 12, d_2 = 6, d_3 = 4, d_4 = 2
37/// ```
38///
39/// R3/S3 are typically used as reversal levels; R4/S4 as breakout levels. As
40/// with the other pivot variants there are no parameters and no warmup — the
41/// first candle produces the first set of levels.
42///
43/// # Example
44///
45/// ```
46/// use wickra_core::{Camarilla, Candle, Indicator};
47///
48/// let prev = Candle::new(100.0, 110.0, 90.0, 105.0, 1.0, 0).unwrap();
49/// let levels = Camarilla::new().update(prev).unwrap();
50/// assert!(levels.r4 > levels.r3);
51/// assert!(levels.s4 < levels.s3);
52/// ```
53#[derive(Debug, Clone, Default)]
54pub struct Camarilla {
55    ready: bool,
56}
57
58impl Camarilla {
59    /// Construct a new Camarilla Pivot Points indicator.
60    pub const fn new() -> Self {
61        Self { ready: false }
62    }
63}
64
65const CAM: f64 = 1.1;
66
67impl Indicator for Camarilla {
68    type Input = Candle;
69    type Output = CamarillaPivotsOutput;
70
71    fn update(&mut self, candle: Candle) -> Option<CamarillaPivotsOutput> {
72        let (h, l, c) = (candle.high, candle.low, candle.close);
73        let range = h - l;
74        let pp = (h + l + c) / 3.0;
75        let w1 = range * CAM / 12.0;
76        let w2 = range * CAM / 6.0;
77        let w3 = range * CAM / 4.0;
78        let w4 = range * CAM / 2.0;
79        let out = CamarillaPivotsOutput {
80            pp,
81            r1: c + w1,
82            r2: c + w2,
83            r3: c + w3,
84            r4: c + w4,
85            s1: c - w1,
86            s2: c - w2,
87            s3: c - w3,
88            s4: c - w4,
89        };
90        self.ready = true;
91        Some(out)
92    }
93
94    fn reset(&mut self) {
95        self.ready = false;
96    }
97
98    fn warmup_period(&self) -> usize {
99        1
100    }
101
102    fn is_ready(&self) -> bool {
103        self.ready
104    }
105
106    fn name(&self) -> &'static str {
107        "Camarilla"
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use crate::traits::BatchExt;
115
116    fn c(h: f64, l: f64, close: f64, ts: i64) -> Candle {
117        Candle::new(close, h, l, close, 1.0, ts).unwrap()
118    }
119
120    #[test]
121    fn formula_reference_values() {
122        // H=110, L=90, C=105, range=20.
123        let levels = Camarilla::new().update(c(110.0, 90.0, 105.0, 0)).unwrap();
124        let range = 20.0;
125        assert!((levels.r1 - (105.0 + range * 1.1 / 12.0)).abs() < 1e-12);
126        assert!((levels.r2 - (105.0 + range * 1.1 / 6.0)).abs() < 1e-12);
127        assert!((levels.r3 - (105.0 + range * 1.1 / 4.0)).abs() < 1e-12);
128        assert!((levels.r4 - (105.0 + range * 1.1 / 2.0)).abs() < 1e-12);
129        assert!((levels.s1 - (105.0 - range * 1.1 / 12.0)).abs() < 1e-12);
130        assert!((levels.s4 - (105.0 - range * 1.1 / 2.0)).abs() < 1e-12);
131    }
132
133    #[test]
134    fn resistance_strictly_widens_with_index() {
135        let levels = Camarilla::new().update(c(120.0, 80.0, 110.0, 0)).unwrap();
136        assert!(levels.r4 > levels.r3);
137        assert!(levels.r3 > levels.r2);
138        assert!(levels.r2 > levels.r1);
139        assert!(levels.r1 > 110.0);
140        assert!(levels.s1 < 110.0);
141        assert!(levels.s2 < levels.s1);
142        assert!(levels.s3 < levels.s2);
143        assert!(levels.s4 < levels.s3);
144    }
145
146    #[test]
147    fn constant_series_collapses_levels() {
148        let levels = Camarilla::new().update(c(50.0, 50.0, 50.0, 0)).unwrap();
149        assert_eq!(levels.r4, 50.0);
150        assert_eq!(levels.s4, 50.0);
151        assert_eq!(levels.pp, 50.0);
152    }
153
154    #[test]
155    fn warmup_and_ready() {
156        let mut p = Camarilla::new();
157        assert!(!p.is_ready());
158        assert_eq!(p.warmup_period(), 1);
159        p.update(c(11.0, 9.0, 10.0, 0));
160        assert!(p.is_ready());
161    }
162
163    #[test]
164    fn reset_clears_state() {
165        let mut p = Camarilla::new();
166        p.update(c(11.0, 9.0, 10.0, 0));
167        p.reset();
168        assert!(!p.is_ready());
169    }
170
171    #[test]
172    fn batch_equals_streaming() {
173        let candles: Vec<Candle> = (0_i32..40)
174            .map(|i| {
175                c(
176                    f64::from(i) + 2.0,
177                    f64::from(i),
178                    f64::from(i) + 1.0,
179                    i.into(),
180                )
181            })
182            .collect();
183        let mut a = Camarilla::new();
184        let mut b = Camarilla::new();
185        assert_eq!(
186            a.batch(&candles),
187            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
188        );
189    }
190
191    #[test]
192    fn accessors_and_metadata() {
193        let p = Camarilla::new();
194        assert_eq!(p.warmup_period(), 1);
195        assert_eq!(p.name(), "Camarilla");
196    }
197}