Skip to main content

wickra_core/indicators/
gartley.rs

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