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    #[inline]
72    fn update(&mut self, candle: Candle) -> Option<CamarillaPivotsOutput> {
73        let (h, l, c) = (candle.high, candle.low, candle.close);
74        let range = h - l;
75        let pp = (h + l + c) / 3.0;
76        let w1 = range * CAM / 12.0;
77        let w2 = range * CAM / 6.0;
78        let w3 = range * CAM / 4.0;
79        let w4 = range * CAM / 2.0;
80        let out = CamarillaPivotsOutput {
81            pp,
82            r1: c + w1,
83            r2: c + w2,
84            r3: c + w3,
85            r4: c + w4,
86            s1: c - w1,
87            s2: c - w2,
88            s3: c - w3,
89            s4: c - w4,
90        };
91        self.ready = true;
92        Some(out)
93    }
94
95    fn reset(&mut self) {
96        self.ready = false;
97    }
98
99    #[inline]
100    fn warmup_period(&self) -> usize {
101        1
102    }
103
104    #[inline]
105    fn is_ready(&self) -> bool {
106        self.ready
107    }
108
109    #[inline]
110    fn name(&self) -> &'static str {
111        "Camarilla"
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    use crate::traits::BatchExt;
119
120    fn c(h: f64, l: f64, close: f64, ts: i64) -> Candle {
121        Candle::new(close, h, l, close, 1.0, ts).unwrap()
122    }
123
124    #[test]
125    fn formula_reference_values() {
126        // H=110, L=90, C=105, range=20.
127        let levels = Camarilla::new().update(c(110.0, 90.0, 105.0, 0)).unwrap();
128        let range = 20.0;
129        assert!((levels.r1 - (105.0 + range * 1.1 / 12.0)).abs() < 1e-12);
130        assert!((levels.r2 - (105.0 + range * 1.1 / 6.0)).abs() < 1e-12);
131        assert!((levels.r3 - (105.0 + range * 1.1 / 4.0)).abs() < 1e-12);
132        assert!((levels.r4 - (105.0 + range * 1.1 / 2.0)).abs() < 1e-12);
133        assert!((levels.s1 - (105.0 - range * 1.1 / 12.0)).abs() < 1e-12);
134        assert!((levels.s4 - (105.0 - range * 1.1 / 2.0)).abs() < 1e-12);
135    }
136
137    #[test]
138    fn resistance_strictly_widens_with_index() {
139        let levels = Camarilla::new().update(c(120.0, 80.0, 110.0, 0)).unwrap();
140        assert!(levels.r4 > levels.r3);
141        assert!(levels.r3 > levels.r2);
142        assert!(levels.r2 > levels.r1);
143        assert!(levels.r1 > 110.0);
144        assert!(levels.s1 < 110.0);
145        assert!(levels.s2 < levels.s1);
146        assert!(levels.s3 < levels.s2);
147        assert!(levels.s4 < levels.s3);
148    }
149
150    #[test]
151    fn constant_series_collapses_levels() {
152        let levels = Camarilla::new().update(c(50.0, 50.0, 50.0, 0)).unwrap();
153        assert_eq!(levels.r4, 50.0);
154        assert_eq!(levels.s4, 50.0);
155        assert_eq!(levels.pp, 50.0);
156    }
157
158    #[test]
159    fn warmup_and_ready() {
160        let mut p = Camarilla::new();
161        assert!(!p.is_ready());
162        assert_eq!(p.warmup_period(), 1);
163        p.update(c(11.0, 9.0, 10.0, 0));
164        assert!(p.is_ready());
165    }
166
167    #[test]
168    fn reset_clears_state() {
169        let mut p = Camarilla::new();
170        p.update(c(11.0, 9.0, 10.0, 0));
171        p.reset();
172        assert!(!p.is_ready());
173    }
174
175    #[test]
176    fn batch_equals_streaming() {
177        let candles: Vec<Candle> = (0_i32..40)
178            .map(|i| {
179                c(
180                    f64::from(i) + 2.0,
181                    f64::from(i),
182                    f64::from(i) + 1.0,
183                    i.into(),
184                )
185            })
186            .collect();
187        let mut a = Camarilla::new();
188        let mut b = Camarilla::new();
189        assert_eq!(
190            a.batch(&candles),
191            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
192        );
193    }
194
195    #[test]
196    fn accessors_and_metadata() {
197        let p = Camarilla::new();
198        assert_eq!(p.warmup_period(), 1);
199        assert_eq!(p.name(), "Camarilla");
200    }
201}