Skip to main content

wickra_core/indicators/
cypher.rs

1//! Cypher harmonic pattern.
2
3use crate::indicators::pattern_swing::{ratios_in, xabcd, SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Cypher — a 5-point (X-A-B-C-D) harmonic pattern whose C leg is measured
8/// against XA (not AB) and whose D retraces the XC leg by `0.786`:
9///
10/// ```text
11/// AB / XA ∈ [0.382, 0.618]
12/// BC / XA ∈ [1.13, 1.414]  (C extends beyond A, measured on XA)
13/// CD / XC ∈ [0.74, 0.83]   (≈ 0.786 retracement of XC — the D completion)
14/// ```
15///
16/// Output is `+1.0` (bullish, D a swing low), `-1.0` (bearish, D a swing high),
17/// or `0.0`; never `None`. See `crates/wickra-core/src/indicators/cypher.rs`.
18#[derive(Debug, Clone)]
19pub struct Cypher {
20    swing: SwingTracker,
21    has_emitted: bool,
22}
23
24impl Cypher {
25    /// Construct a new Cypher detector.
26    pub const fn new() -> Self {
27        Self {
28            swing: SwingTracker::new(SWING_THRESHOLD, 5),
29            has_emitted: false,
30        }
31    }
32}
33
34impl Default for Cypher {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl Indicator for Cypher {
41    type Input = Candle;
42    type Output = f64;
43
44    fn update(&mut self, candle: Candle) -> Option<f64> {
45        self.has_emitted = true;
46        if !self.swing.update(candle) {
47            return Some(0.0);
48        }
49        let pivots = self.swing.pivots();
50        if pivots.len() < 5 {
51            return Some(0.0);
52        }
53        let p = xabcd(pivots);
54        let xa = (p.a - p.x).abs();
55        let ab = (p.b - p.a).abs();
56        let bc = (p.c - p.b).abs();
57        let xc = (p.c - p.x).abs();
58        let cd = (p.d - p.c).abs();
59        let matched = ratios_in(&[
60            (ab / xa, 0.382, 0.618),
61            (bc / xa, 1.13, 1.414),
62            (cd / xc, 0.74, 0.83),
63        ]);
64        if matched {
65            return Some(if p.bullish { 1.0 } else { -1.0 });
66        }
67        Some(0.0)
68    }
69
70    fn reset(&mut self) {
71        self.swing.reset();
72        self.has_emitted = false;
73    }
74
75    fn warmup_period(&self) -> usize {
76        6
77    }
78
79    fn is_ready(&self) -> bool {
80        self.has_emitted
81    }
82
83    fn name(&self) -> &'static str {
84        "Cypher"
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91    use crate::indicators::pattern_swing::candles_for_pivots;
92    use crate::traits::BatchExt;
93
94    fn run(pivots: &[f64]) -> Vec<f64> {
95        let mut indicator = Cypher::new();
96        candles_for_pivots(pivots)
97            .into_iter()
98            .map(|c| indicator.update(c).unwrap())
99            .collect()
100    }
101
102    #[test]
103    fn accessors_and_metadata() {
104        let indicator = Cypher::new();
105        assert_eq!(indicator.name(), "Cypher");
106        assert_eq!(indicator.warmup_period(), 6);
107        assert!(!indicator.is_ready());
108        assert!(!Cypher::default().is_ready());
109    }
110
111    #[test]
112    fn bullish_cypher_is_plus_one() {
113        let out = run(&[150.0, 100.0, 140.0, 120.0, 168.0, 114.55]);
114        assert_eq!(*out.last().unwrap(), 1.0);
115        assert!(out[..out.len() - 1].iter().all(|&x| x == 0.0));
116    }
117
118    #[test]
119    fn bearish_cypher_is_minus_one() {
120        let out = run(&[150.0, 110.0, 130.0, 82.0, 135.45]);
121        assert_eq!(*out.last().unwrap(), -1.0);
122    }
123
124    #[test]
125    fn out_of_ratio_does_not_trigger() {
126        let out = run(&[150.0, 100.0, 140.0, 110.0, 135.0, 105.0]);
127        assert_eq!(*out.last().unwrap(), 0.0);
128    }
129
130    #[test]
131    fn reset_clears_state() {
132        let mut indicator = Cypher::new();
133        for c in candles_for_pivots(&[150.0, 100.0, 140.0]) {
134            let _ = indicator.update(c);
135        }
136        indicator.reset();
137        assert!(!indicator.is_ready());
138        let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
139        assert_eq!(indicator.update(c), Some(0.0));
140    }
141
142    #[test]
143    fn batch_equals_streaming() {
144        let candles = candles_for_pivots(&[150.0, 100.0, 140.0, 120.0, 168.0, 114.55]);
145        let mut a = Cypher::new();
146        let mut b = Cypher::new();
147        assert_eq!(
148            a.batch(&candles),
149            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
150        );
151    }
152}