Skip to main content

wickra_core/indicators/
harami.rs

1//! Bullish / Bearish Harami candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Harami — a 2-bar reversal pattern. The current candle's body sits entirely
7/// inside the previous candle's body and points in the opposite direction.
8///
9/// ```text
10/// prev_body  = |prev.close − prev.open|
11/// curr_body  = |curr.close − curr.open|
12/// bullish    = prev red & curr green
13///             & curr.open >= prev.close & curr.close <= prev.open
14///             & curr_body < prev_body
15/// bearish    = prev green & curr red
16///             & curr.open <= prev.close & curr.close >= prev.open
17///             & curr_body < prev_body
18/// ```
19///
20/// Output is `+1.0` for a bullish harami (small green inside a prior red),
21/// `−1.0` for a bearish harami (small red inside a prior green), `0.0`
22/// otherwise. The first bar always returns `0.0`. Pattern-shape check only —
23/// no trend filter is applied; combine with a trend indicator for actionable
24/// signals.
25///
26/// # Example
27///
28/// ```
29/// use wickra_core::{Candle, Harami, Indicator};
30///
31/// let mut indicator = Harami::new();
32/// indicator.update(Candle::new(12.0, 12.5, 9.5, 10.0, 1.0, 0).unwrap());
33/// let out = indicator
34///     .update(Candle::new(10.5, 11.5, 10.4, 11.0, 1.0, 1).unwrap());
35/// assert_eq!(out, Some(1.0));
36/// ```
37#[derive(Debug, Clone, Default)]
38pub struct Harami {
39    prev: Option<Candle>,
40    has_emitted: bool,
41}
42
43impl Harami {
44    /// Construct a new Harami detector.
45    pub const fn new() -> Self {
46        Self {
47            prev: None,
48            has_emitted: false,
49        }
50    }
51}
52
53impl Indicator for Harami {
54    type Input = Candle;
55    type Output = f64;
56
57    fn update(&mut self, candle: Candle) -> Option<f64> {
58        self.has_emitted = true;
59        let prev = self.prev;
60        self.prev = Some(candle);
61        let Some(p) = prev else {
62            return Some(0.0);
63        };
64        let prev_body = (p.close - p.open).abs();
65        let curr_body = (candle.close - candle.open).abs();
66        if prev_body <= 0.0 || curr_body <= 0.0 || curr_body >= prev_body {
67            return Some(0.0);
68        }
69        let prev_red = p.close < p.open;
70        let prev_green = p.close > p.open;
71        let curr_green = candle.close > candle.open;
72        let curr_red = candle.close < candle.open;
73        // Bullish: small green strictly inside prior red body (open >= prev.close, close <= prev.open).
74        if prev_red && curr_green && candle.open >= p.close && candle.close <= p.open {
75            Some(1.0)
76        } else if prev_green && curr_red && candle.open <= p.close && candle.close >= p.open {
77            Some(-1.0)
78        } else {
79            Some(0.0)
80        }
81    }
82
83    fn reset(&mut self) {
84        self.prev = None;
85        self.has_emitted = false;
86    }
87
88    fn warmup_period(&self) -> usize {
89        2
90    }
91
92    fn is_ready(&self) -> bool {
93        self.has_emitted
94    }
95
96    fn name(&self) -> &'static str {
97        "Harami"
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use crate::traits::BatchExt;
105
106    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
107        Candle::new(open, high, low, close, 1.0, ts).unwrap()
108    }
109
110    #[test]
111    fn accessors_and_metadata() {
112        let h = Harami::new();
113        assert_eq!(h.name(), "Harami");
114        assert_eq!(h.warmup_period(), 2);
115        assert!(!h.is_ready());
116    }
117
118    #[test]
119    fn bullish_harami_is_plus_one() {
120        let mut h = Harami::new();
121        // Prior red 12 -> 10 (body 2). Current green 10.5 -> 11 inside.
122        assert_eq!(h.update(c(12.0, 12.5, 9.5, 10.0, 0)), Some(0.0));
123        assert_eq!(h.update(c(10.5, 11.5, 10.4, 11.0, 1)), Some(1.0));
124    }
125
126    #[test]
127    fn bearish_harami_is_minus_one() {
128        let mut h = Harami::new();
129        // Prior green 10 -> 12 (body 2). Current red 11.5 -> 11 inside.
130        assert_eq!(h.update(c(10.0, 12.5, 9.5, 12.0, 0)), Some(0.0));
131        assert_eq!(h.update(c(11.5, 11.6, 10.9, 11.0, 1)), Some(-1.0));
132    }
133
134    #[test]
135    fn larger_body_is_not_harami() {
136        let mut h = Harami::new();
137        h.update(c(11.0, 11.2, 9.8, 10.0, 0));
138        // Current body bigger than prior.
139        assert_eq!(h.update(c(9.5, 12.0, 9.5, 11.5, 1)), Some(0.0));
140    }
141
142    #[test]
143    fn same_direction_is_not_harami() {
144        let mut h = Harami::new();
145        h.update(c(10.0, 12.5, 9.5, 12.0, 0));
146        // Smaller candle but also green -> 0.
147        assert_eq!(h.update(c(11.0, 11.6, 10.9, 11.5, 1)), Some(0.0));
148    }
149
150    #[test]
151    fn first_bar_returns_zero() {
152        let mut h = Harami::new();
153        assert_eq!(h.update(c(10.0, 11.0, 9.0, 11.0, 0)), Some(0.0));
154    }
155
156    #[test]
157    fn batch_equals_streaming() {
158        let candles: Vec<Candle> = (0..40)
159            .map(|i| {
160                let base = 100.0 + i as f64;
161                if i % 2 == 0 {
162                    c(base + 2.0, base + 2.5, base - 0.5, base, i)
163                } else {
164                    c(base + 1.0, base + 1.5, base + 0.7, base + 1.3, i)
165                }
166            })
167            .collect();
168        let mut a = Harami::new();
169        let mut b = Harami::new();
170        assert_eq!(
171            a.batch(&candles),
172            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
173        );
174    }
175
176    #[test]
177    fn reset_clears_state() {
178        let mut h = Harami::new();
179        h.update(c(12.0, 12.5, 9.5, 10.0, 0));
180        h.update(c(10.5, 11.5, 10.4, 11.0, 1));
181        assert!(h.is_ready());
182        h.reset();
183        assert!(!h.is_ready());
184        // After reset the next bar again has no prev.
185        assert_eq!(h.update(c(12.0, 12.5, 9.5, 10.0, 0)), Some(0.0));
186    }
187}