Skip to main content

wickra_core/indicators/
abandoned_baby.rs

1//! Abandoned Baby candlestick pattern.
2
3use crate::error::{Error, Result};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Abandoned Baby — a strong 3-bar reversal where a doji is "abandoned" by price
8/// gaps on both sides, isolating it from the candles before and after.
9///
10/// ```text
11/// tol           = tolerance * max(|bar2.open|, |bar2.close|)
12/// bar2 doji                                   (|bar2.close − bar2.open| <= tol)
13///
14/// bullish  (+1.0): bar1 red, bar2 gaps fully below bar1 (bar2.high < bar1.low),
15///                  bar3 green and gaps fully above bar2 (bar3.low > bar2.high)
16/// bearish  (−1.0): bar1 green, bar2 gaps fully above bar1 (bar2.low > bar1.high),
17///                  bar3 red and gaps fully below bar2 (bar3.high < bar2.low)
18/// ```
19///
20/// Output is `0.0` otherwise. The first two bars always return `0.0` because the
21/// three-bar window is not yet filled. `tolerance` defaults to `0.001` (10 bps
22/// relative) and bounds how flat the middle candle must be to count as a doji; it
23/// must lie in `[0, 1)`. Pattern-shape check only — no trend filter is applied;
24/// combine with a trend indicator for actionable signals.
25///
26/// # Signed ±1 encoding
27///
28/// This detector emits the uniform candlestick sign convention shared across the
29/// pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no pattern — so it
30/// drops straight into a machine-learning feature matrix where the bullish and
31/// bearish variants occupy a single dimension.
32///
33/// # Example
34///
35/// ```
36/// use wickra_core::{AbandonedBaby, Candle, Indicator};
37///
38/// let mut indicator = AbandonedBaby::new();
39/// indicator.update(Candle::new(20.0, 20.1, 14.9, 15.0, 1.0, 0).unwrap());
40/// indicator.update(Candle::new(13.0, 13.1, 12.9, 13.0, 1.0, 1).unwrap());
41/// let out = indicator
42///     .update(Candle::new(16.0, 18.1, 15.9, 18.0, 1.0, 2).unwrap());
43/// assert_eq!(out, Some(1.0));
44/// ```
45#[derive(Debug, Clone)]
46pub struct AbandonedBaby {
47    tolerance: f64,
48    prev: Option<Candle>,
49    prev_prev: Option<Candle>,
50    has_emitted: bool,
51}
52
53impl Default for AbandonedBaby {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl AbandonedBaby {
60    /// Construct a detector with the default relative doji tolerance (1e-3).
61    pub const fn new() -> Self {
62        Self {
63            tolerance: 0.001,
64            prev: None,
65            prev_prev: None,
66            has_emitted: false,
67        }
68    }
69
70    /// Construct a detector with a custom relative doji tolerance.
71    ///
72    /// `tolerance` must lie in `[0, 1)`.
73    pub fn with_tolerance(tolerance: f64) -> Result<Self> {
74        if !(0.0..1.0).contains(&tolerance) {
75            return Err(Error::InvalidPeriod {
76                message: "abandoned baby tolerance must lie in [0, 1)",
77            });
78        }
79        Ok(Self {
80            tolerance,
81            prev: None,
82            prev_prev: None,
83            has_emitted: false,
84        })
85    }
86
87    /// Configured relative doji tolerance.
88    pub fn tolerance(&self) -> f64 {
89        self.tolerance
90    }
91}
92
93impl Indicator for AbandonedBaby {
94    type Input = Candle;
95    type Output = f64;
96
97    fn update(&mut self, candle: Candle) -> Option<f64> {
98        self.has_emitted = true;
99        let pp = self.prev_prev;
100        let p = self.prev;
101        self.prev_prev = self.prev;
102        self.prev = Some(candle);
103        let (Some(bar1), Some(bar2)) = (pp, p) else {
104            return Some(0.0);
105        };
106        let tol = self.tolerance * bar2.open.abs().max(bar2.close.abs());
107        let bar2_is_doji = (bar2.close - bar2.open).abs() <= tol;
108        if !bar2_is_doji {
109            return Some(0.0);
110        }
111        // Bullish: red bar1, doji gaps below, green bar3 gaps above.
112        if bar1.close < bar1.open
113            && bar2.high < bar1.low
114            && candle.close > candle.open
115            && candle.low > bar2.high
116        {
117            return Some(1.0);
118        }
119        // Bearish: green bar1, doji gaps above, red bar3 gaps below.
120        if bar1.close > bar1.open
121            && bar2.low > bar1.high
122            && candle.close < candle.open
123            && candle.high < bar2.low
124        {
125            return Some(-1.0);
126        }
127        Some(0.0)
128    }
129
130    fn reset(&mut self) {
131        self.prev = None;
132        self.prev_prev = None;
133        self.has_emitted = false;
134    }
135
136    fn warmup_period(&self) -> usize {
137        3
138    }
139
140    fn is_ready(&self) -> bool {
141        self.has_emitted
142    }
143
144    fn name(&self) -> &'static str {
145        "AbandonedBaby"
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152    use crate::traits::BatchExt;
153
154    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
155        Candle::new(open, high, low, close, 1.0, ts).unwrap()
156    }
157
158    #[test]
159    fn rejects_invalid_tolerance() {
160        assert!(AbandonedBaby::with_tolerance(-0.01).is_err());
161        assert!(AbandonedBaby::with_tolerance(1.0).is_err());
162    }
163
164    #[test]
165    fn accepts_valid_tolerance() {
166        let t = AbandonedBaby::with_tolerance(0.0).unwrap();
167        assert!((t.tolerance() - 0.0).abs() < 1e-12);
168    }
169
170    #[test]
171    fn accessors_and_metadata() {
172        let t = AbandonedBaby::default();
173        assert_eq!(t.name(), "AbandonedBaby");
174        assert_eq!(t.warmup_period(), 3);
175        assert!(!t.is_ready());
176        assert!((t.tolerance() - 0.001).abs() < 1e-12);
177    }
178
179    #[test]
180    fn bullish_abandoned_baby_is_plus_one() {
181        let mut t = AbandonedBaby::new();
182        assert_eq!(t.update(c(20.0, 20.1, 14.9, 15.0, 0)), Some(0.0));
183        assert_eq!(t.update(c(13.0, 13.1, 12.9, 13.0, 1)), Some(0.0));
184        assert_eq!(t.update(c(16.0, 18.1, 15.9, 18.0, 2)), Some(1.0));
185    }
186
187    #[test]
188    fn bearish_abandoned_baby_is_minus_one() {
189        let mut t = AbandonedBaby::new();
190        assert_eq!(t.update(c(15.0, 20.1, 14.9, 20.0, 0)), Some(0.0));
191        assert_eq!(t.update(c(22.0, 22.1, 21.9, 22.0, 1)), Some(0.0));
192        assert_eq!(t.update(c(19.0, 19.1, 16.9, 17.0, 2)), Some(-1.0));
193    }
194
195    #[test]
196    fn middle_not_doji_yields_zero() {
197        let mut t = AbandonedBaby::new();
198        t.update(c(20.0, 20.1, 14.9, 15.0, 0));
199        // Middle bar has a wide body -> not a doji.
200        assert_eq!(t.update(c(13.0, 14.0, 11.0, 11.5, 1)), Some(0.0));
201        assert_eq!(t.update(c(16.0, 18.1, 15.9, 18.0, 2)), Some(0.0));
202    }
203
204    #[test]
205    fn no_gap_yields_zero() {
206        let mut t = AbandonedBaby::new();
207        t.update(c(20.0, 20.1, 14.9, 15.0, 0));
208        // Doji overlaps bar1's range -> no gap.
209        assert_eq!(t.update(c(15.0, 15.1, 14.9, 15.0, 1)), Some(0.0));
210        assert_eq!(t.update(c(16.0, 18.1, 15.9, 18.0, 2)), Some(0.0));
211    }
212
213    #[test]
214    fn first_two_bars_return_zero() {
215        let mut t = AbandonedBaby::new();
216        assert_eq!(t.update(c(20.0, 20.1, 14.9, 15.0, 0)), Some(0.0));
217        assert_eq!(t.update(c(13.0, 13.1, 12.9, 13.0, 1)), Some(0.0));
218    }
219
220    #[test]
221    fn batch_equals_streaming() {
222        let candles: Vec<Candle> = (0..40)
223            .map(|i| {
224                let base = 100.0 + (i as f64 * 0.3).sin() * 5.0;
225                c(base, base + 1.0, base - 1.0, base + 0.5, i)
226            })
227            .collect();
228        let mut a = AbandonedBaby::new();
229        let mut b = AbandonedBaby::new();
230        assert_eq!(
231            a.batch(&candles),
232            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
233        );
234    }
235
236    #[test]
237    fn reset_clears_state() {
238        let mut t = AbandonedBaby::new();
239        t.update(c(20.0, 20.1, 14.9, 15.0, 0));
240        t.update(c(13.0, 13.1, 12.9, 13.0, 1));
241        t.update(c(16.0, 18.1, 15.9, 18.0, 2));
242        assert!(t.is_ready());
243        t.reset();
244        assert!(!t.is_ready());
245        assert_eq!(t.update(c(20.0, 20.1, 14.9, 15.0, 0)), Some(0.0));
246    }
247}