Skip to main content

wickra_core/indicators/
engulfing.rs

1//! Bullish / Bearish Engulfing candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Engulfing — a 2-bar reversal pattern. The current candle's body fully
7/// engulfs the prior 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 engulfing, `−1.0` for a bearish one, and
21/// `0.0` otherwise. The first bar always returns `0.0` because no previous
22/// body exists to engulf. Pattern-shape check only — no trend filter is
23/// applied; combine with a trend indicator for actionable signals.
24///
25/// # Signed ±1 encoding
26///
27/// This detector already emits the uniform candlestick sign convention shared
28/// across the pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no
29/// pattern — so it drops straight into a machine-learning feature matrix where
30/// the bullish and bearish variants of the pattern occupy a single dimension.
31///
32/// # Example
33///
34/// ```
35/// use wickra_core::{Candle, Engulfing, Indicator};
36///
37/// let mut indicator = Engulfing::new();
38/// // Prior red candle followed by a larger green engulfing candle.
39/// indicator.update(Candle::new(11.0, 11.2, 9.8, 10.0, 1.0, 0).unwrap());
40/// let out = indicator
41///     .update(Candle::new(9.5, 12.0, 9.5, 11.5, 1.0, 1).unwrap());
42/// assert_eq!(out, Some(1.0));
43/// ```
44#[derive(Debug, Clone, Default)]
45pub struct Engulfing {
46    prev: Option<Candle>,
47    has_emitted: bool,
48}
49
50impl Engulfing {
51    /// Construct a new Engulfing detector.
52    pub const fn new() -> Self {
53        Self {
54            prev: None,
55            has_emitted: false,
56        }
57    }
58}
59
60impl Indicator for Engulfing {
61    type Input = Candle;
62    type Output = f64;
63
64    fn update(&mut self, candle: Candle) -> Option<f64> {
65        self.has_emitted = true;
66        let prev = self.prev;
67        self.prev = Some(candle);
68        let Some(p) = prev else {
69            return Some(0.0);
70        };
71        let prev_body = (p.close - p.open).abs();
72        let curr_body = (candle.close - candle.open).abs();
73        if prev_body <= 0.0 || curr_body <= prev_body {
74            return Some(0.0);
75        }
76        let prev_red = p.close < p.open;
77        let prev_green = p.close > p.open;
78        let curr_green = candle.close > candle.open;
79        let curr_red = candle.close < candle.open;
80        if prev_red && curr_green && candle.open <= p.close && candle.close >= p.open {
81            Some(1.0)
82        } else if prev_green && curr_red && candle.open >= p.close && candle.close <= p.open {
83            Some(-1.0)
84        } else {
85            Some(0.0)
86        }
87    }
88
89    fn reset(&mut self) {
90        self.prev = None;
91        self.has_emitted = false;
92    }
93
94    fn warmup_period(&self) -> usize {
95        2
96    }
97
98    fn is_ready(&self) -> bool {
99        self.has_emitted
100    }
101
102    fn name(&self) -> &'static str {
103        "Engulfing"
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use crate::traits::BatchExt;
111
112    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
113        Candle::new(open, high, low, close, 1.0, ts).unwrap()
114    }
115
116    #[test]
117    fn accessors_and_metadata() {
118        let e = Engulfing::new();
119        assert_eq!(e.name(), "Engulfing");
120        assert_eq!(e.warmup_period(), 2);
121        assert!(!e.is_ready());
122    }
123
124    #[test]
125    fn bullish_engulfing_is_plus_one() {
126        let mut e = Engulfing::new();
127        // Prior red 11 -> 10, current green 9.5 -> 11.5 (body 2 > 1).
128        assert_eq!(e.update(c(11.0, 11.2, 9.8, 10.0, 0)), Some(0.0));
129        assert_eq!(e.update(c(9.5, 12.0, 9.5, 11.5, 1)), Some(1.0));
130    }
131
132    #[test]
133    fn bearish_engulfing_is_minus_one() {
134        let mut e = Engulfing::new();
135        // Prior green 10 -> 11, current red 12 -> 9.
136        assert_eq!(e.update(c(10.0, 11.2, 9.8, 11.0, 0)), Some(0.0));
137        assert_eq!(e.update(c(12.0, 12.0, 9.0, 9.0, 1)), Some(-1.0));
138    }
139
140    #[test]
141    fn same_direction_is_not_engulfing() {
142        let mut e = Engulfing::new();
143        e.update(c(10.0, 11.0, 9.8, 11.0, 0));
144        // Another green candle that engulfs but matches direction -> 0.
145        assert_eq!(e.update(c(9.5, 12.0, 9.5, 11.5, 1)), Some(0.0));
146    }
147
148    #[test]
149    fn smaller_body_is_not_engulfing() {
150        let mut e = Engulfing::new();
151        e.update(c(11.0, 11.2, 8.0, 8.5, 0));
152        // Body 0.5 < 2.5 -> not engulfing.
153        assert_eq!(e.update(c(8.6, 9.0, 8.4, 8.7, 1)), Some(0.0));
154    }
155
156    #[test]
157    fn first_bar_returns_zero() {
158        let mut e = Engulfing::new();
159        assert_eq!(e.update(c(10.0, 11.0, 9.0, 11.0, 0)), Some(0.0));
160    }
161
162    #[test]
163    fn batch_equals_streaming() {
164        let candles: Vec<Candle> = (0..40)
165            .map(|i| {
166                let base = 100.0 + i as f64;
167                if i % 3 == 0 {
168                    c(base + 1.0, base + 1.5, base - 0.5, base, i)
169                } else {
170                    c(base - 1.0, base + 2.0, base - 1.5, base + 2.0, i)
171                }
172            })
173            .collect();
174        let mut a = Engulfing::new();
175        let mut b = Engulfing::new();
176        assert_eq!(
177            a.batch(&candles),
178            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
179        );
180    }
181
182    #[test]
183    fn reset_clears_state() {
184        let mut e = Engulfing::new();
185        e.update(c(10.0, 11.0, 9.0, 11.0, 0));
186        e.update(c(11.0, 12.0, 10.0, 12.0, 1));
187        assert!(e.is_ready());
188        e.reset();
189        assert!(!e.is_ready());
190        // After reset the next bar again has no prev.
191        assert_eq!(e.update(c(11.0, 11.2, 9.8, 10.0, 0)), Some(0.0));
192    }
193}