Skip to main content

wickra_core/indicators/
two_crows.rs

1//! Two Crows candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Two Crows — a 3-bar bearish reversal pattern that appears after an advance.
7///
8/// ```text
9/// bar1 green (long white)
10/// bar2 red   & its body gaps up above bar1's body  (bar2.close > bar1.close)
11/// bar3 red   & opens inside bar2's body            (bar2.close < bar3.open < bar2.open)
12///            & closes inside bar1's body            (bar1.open  < bar3.close < bar1.close)
13/// ```
14///
15/// Output is `−1.0` when the pattern completes and `0.0` otherwise. Two Crows is
16/// a single-direction (bearish-only) pattern, so it never emits `+1.0`. The
17/// first two bars always return `0.0` because the three-bar window is not yet
18/// filled. Pattern-shape check only — no trend filter is applied; combine with a
19/// trend indicator for actionable signals.
20///
21/// # Signed ±1 encoding
22///
23/// This detector emits the uniform candlestick sign convention shared across the
24/// pattern family — `−1.0` bearish, `0.0` no pattern — so it drops straight into
25/// a machine-learning feature matrix as a single dimension.
26///
27/// # Example
28///
29/// ```
30/// use wickra_core::{Candle, Indicator, TwoCrows};
31///
32/// let mut indicator = TwoCrows::new();
33/// indicator.update(Candle::new(10.0, 12.2, 9.9, 12.0, 1.0, 0).unwrap());
34/// indicator.update(Candle::new(14.0, 14.2, 12.9, 13.0, 1.0, 1).unwrap());
35/// let out = indicator
36///     .update(Candle::new(13.5, 13.6, 10.9, 11.0, 1.0, 2).unwrap());
37/// assert_eq!(out, Some(-1.0));
38/// ```
39#[derive(Debug, Clone, Default)]
40pub struct TwoCrows {
41    prev: Option<Candle>,
42    prev_prev: Option<Candle>,
43    has_emitted: bool,
44}
45
46impl TwoCrows {
47    /// Construct a new Two Crows detector.
48    pub const fn new() -> Self {
49        Self {
50            prev: None,
51            prev_prev: None,
52            has_emitted: false,
53        }
54    }
55}
56
57impl Indicator for TwoCrows {
58    type Input = Candle;
59    type Output = f64;
60
61    fn update(&mut self, candle: Candle) -> Option<f64> {
62        self.has_emitted = true;
63        let pp = self.prev_prev;
64        let p = self.prev;
65        self.prev_prev = self.prev;
66        self.prev = Some(candle);
67        let (Some(bar1), Some(bar2)) = (pp, p) else {
68            return Some(0.0);
69        };
70        if bar1.close > bar1.open
71            && bar2.close < bar2.open
72            && bar2.close > bar1.close
73            && candle.close < candle.open
74            && candle.open < bar2.open
75            && candle.open > bar2.close
76            && candle.close > bar1.open
77            && candle.close < bar1.close
78        {
79            return Some(-1.0);
80        }
81        Some(0.0)
82    }
83
84    fn reset(&mut self) {
85        self.prev = None;
86        self.prev_prev = None;
87        self.has_emitted = false;
88    }
89
90    fn warmup_period(&self) -> usize {
91        3
92    }
93
94    fn is_ready(&self) -> bool {
95        self.has_emitted
96    }
97
98    fn name(&self) -> &'static str {
99        "TwoCrows"
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106    use crate::traits::BatchExt;
107
108    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
109        Candle::new(open, high, low, close, 1.0, ts).unwrap()
110    }
111
112    #[test]
113    fn accessors_and_metadata() {
114        let t = TwoCrows::new();
115        assert_eq!(t.name(), "TwoCrows");
116        assert_eq!(t.warmup_period(), 3);
117        assert!(!t.is_ready());
118    }
119
120    #[test]
121    fn two_crows_is_minus_one() {
122        let mut t = TwoCrows::new();
123        // bar1 green 10->12; bar2 red 14->13 (body above bar1); bar3 red
124        // opens 13.5 (inside [13,14]) and closes 11 (inside [10,12]).
125        assert_eq!(t.update(c(10.0, 12.2, 9.9, 12.0, 0)), Some(0.0));
126        assert_eq!(t.update(c(14.0, 14.2, 12.9, 13.0, 1)), Some(0.0));
127        assert_eq!(t.update(c(13.5, 13.6, 10.9, 11.0, 2)), Some(-1.0));
128    }
129
130    #[test]
131    fn no_gap_up_yields_zero() {
132        let mut t = TwoCrows::new();
133        // bar2 red but its body does not gap above bar1's body.
134        t.update(c(10.0, 12.2, 9.9, 12.0, 0));
135        t.update(c(11.5, 12.0, 10.4, 11.0, 1));
136        assert_eq!(t.update(c(11.0, 11.2, 9.9, 10.5, 2)), Some(0.0));
137    }
138
139    #[test]
140    fn third_close_below_first_body_yields_zero() {
141        let mut t = TwoCrows::new();
142        t.update(c(10.0, 12.2, 9.9, 12.0, 0));
143        t.update(c(14.0, 14.2, 12.9, 13.0, 1));
144        // bar3 closes 9.5, below bar1's body low (10) -> not Two Crows.
145        assert_eq!(t.update(c(13.5, 13.6, 9.4, 9.5, 2)), Some(0.0));
146    }
147
148    #[test]
149    fn first_two_bars_return_zero() {
150        let mut t = TwoCrows::new();
151        assert_eq!(t.update(c(10.0, 12.2, 9.9, 12.0, 0)), Some(0.0));
152        assert_eq!(t.update(c(14.0, 14.2, 12.9, 13.0, 1)), Some(0.0));
153    }
154
155    #[test]
156    fn batch_equals_streaming() {
157        let candles: Vec<Candle> = (0..40)
158            .map(|i| {
159                let base = 100.0 + i as f64;
160                if i % 3 == 0 {
161                    c(base, base + 0.5, base - 1.0, base + 0.4, i)
162                } else {
163                    c(base + 1.5, base + 1.7, base - 0.2, base + 0.6, i)
164                }
165            })
166            .collect();
167        let mut a = TwoCrows::new();
168        let mut b = TwoCrows::new();
169        assert_eq!(
170            a.batch(&candles),
171            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
172        );
173    }
174
175    #[test]
176    fn reset_clears_state() {
177        let mut t = TwoCrows::new();
178        t.update(c(10.0, 12.2, 9.9, 12.0, 0));
179        t.update(c(14.0, 14.2, 12.9, 13.0, 1));
180        t.update(c(13.5, 13.6, 10.9, 11.0, 2));
181        assert!(t.is_ready());
182        t.reset();
183        assert!(!t.is_ready());
184        assert_eq!(t.update(c(10.0, 12.2, 9.9, 12.0, 0)), Some(0.0));
185    }
186}