Skip to main content

wickra_core/indicators/
three_drives.rs

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