Skip to main content

wickra_core/indicators/
ladder_bottom.rs

1//! Ladder Bottom candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Ladder Bottom — a 5-bar bullish reversal. Three long black candles step the
7/// market down like rungs of a ladder, a fourth black candle finally shows an
8/// upper shadow (the first sign of buying), and a white candle then gaps up into
9/// its body to confirm the turn.
10///
11/// ```text
12/// bar1, bar2, bar3 black, with consecutively lower opens AND closes
13/// bar4 black with an upper shadow         (high4 > open4)
14/// bar5 white, opens above bar4's body      (open5 > open4)  and closes up
15/// ```
16///
17/// Output is `+1.0` when the pattern completes and `0.0` otherwise. Ladder Bottom
18/// is a single-direction (bullish-only) reversal, so it never emits `−1.0`. The
19/// first four bars always return `0.0` because the five-bar window is not yet
20/// filled. Pattern-shape check only — no trend filter is applied; combine with a
21/// trend indicator for actionable signals.
22///
23/// # Signed ±1 encoding
24///
25/// This detector emits the uniform candlestick sign convention shared across the
26/// pattern family — `+1.0` bullish, `0.0` no pattern — so it drops straight into
27/// a machine-learning feature matrix as a single dimension.
28///
29/// # Example
30///
31/// ```
32/// use wickra_core::{Candle, Indicator, LadderBottom};
33///
34/// let mut indicator = LadderBottom::new();
35/// indicator.update(Candle::new(20.0, 20.1, 17.9, 18.0, 1.0, 0).unwrap());
36/// indicator.update(Candle::new(18.0, 18.1, 15.9, 16.0, 1.0, 1).unwrap());
37/// indicator.update(Candle::new(16.0, 16.1, 13.9, 14.0, 1.0, 2).unwrap());
38/// indicator.update(Candle::new(14.0, 15.0, 12.4, 12.5, 1.0, 3).unwrap());
39/// let out = indicator
40///     .update(Candle::new(15.0, 17.1, 14.9, 17.0, 1.0, 4).unwrap());
41/// assert_eq!(out, Some(1.0));
42/// ```
43#[derive(Debug, Clone, Default)]
44pub struct LadderBottom {
45    c1: Option<Candle>,
46    c2: Option<Candle>,
47    c3: Option<Candle>,
48    c4: Option<Candle>,
49    has_emitted: bool,
50}
51
52impl LadderBottom {
53    /// Construct a new Ladder Bottom detector.
54    pub const fn new() -> Self {
55        Self {
56            c1: None,
57            c2: None,
58            c3: None,
59            c4: None,
60            has_emitted: false,
61        }
62    }
63}
64
65impl Indicator for LadderBottom {
66    type Input = Candle;
67    type Output = f64;
68
69    #[inline]
70    fn update(&mut self, candle: Candle) -> Option<f64> {
71        let bar1 = self.c1;
72        let bar2 = self.c2;
73        let bar3 = self.c3;
74        let bar4 = self.c4;
75        self.c1 = self.c2;
76        self.c2 = self.c3;
77        self.c3 = self.c4;
78        self.c4 = Some(candle);
79        let (Some(bar1), Some(bar2), Some(bar3), Some(bar4)) = (bar1, bar2, bar3, bar4) else {
80            return None;
81        };
82        self.has_emitted = true;
83        if bar1.close < bar1.open
84            && bar2.close < bar2.open
85            && bar3.close < bar3.open
86            && bar2.open < bar1.open
87            && bar2.close < bar1.close
88            && bar3.open < bar2.open
89            && bar3.close < bar2.close
90            && bar4.close < bar4.open
91            && bar4.high > bar4.open
92            && candle.close > candle.open
93            && candle.open > bar4.open
94        {
95            return Some(1.0);
96        }
97        Some(0.0)
98    }
99
100    fn reset(&mut self) {
101        self.c1 = None;
102        self.c2 = None;
103        self.c3 = None;
104        self.c4 = None;
105        self.has_emitted = false;
106    }
107
108    #[inline]
109    fn warmup_period(&self) -> usize {
110        5
111    }
112
113    #[inline]
114    fn is_ready(&self) -> bool {
115        self.has_emitted
116    }
117
118    #[inline]
119    fn name(&self) -> &'static str {
120        "LadderBottom"
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use crate::traits::BatchExt;
128
129    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
130        Candle::new(open, high, low, close, 1.0, ts).unwrap()
131    }
132
133    #[test]
134    fn accessors_and_metadata() {
135        let t = LadderBottom::new();
136        assert_eq!(t.name(), "LadderBottom");
137        assert_eq!(t.warmup_period(), 5);
138        assert!(!t.is_ready());
139    }
140
141    #[test]
142    fn ladder_bottom_is_plus_one() {
143        let mut t = LadderBottom::new();
144        assert_eq!(t.update(c(20.0, 20.1, 17.9, 18.0, 0)), None);
145        assert_eq!(t.update(c(18.0, 18.1, 15.9, 16.0, 1)), None);
146        assert_eq!(t.update(c(16.0, 16.1, 13.9, 14.0, 2)), None);
147        assert_eq!(t.update(c(14.0, 15.0, 12.4, 12.5, 3)), None);
148        assert_eq!(t.update(c(15.0, 17.1, 14.9, 17.0, 4)), Some(1.0));
149    }
150
151    #[test]
152    fn fourth_bar_without_upper_shadow_yields_zero() {
153        let mut t = LadderBottom::new();
154        t.update(c(20.0, 20.1, 17.9, 18.0, 0));
155        t.update(c(18.0, 18.1, 15.9, 16.0, 1));
156        t.update(c(16.0, 16.1, 13.9, 14.0, 2));
157        // bar4 opens at its high -> no upper shadow.
158        t.update(c(14.0, 14.0, 12.4, 12.5, 3));
159        assert_eq!(t.update(c(15.0, 17.1, 14.9, 17.0, 4)), Some(0.0));
160    }
161
162    #[test]
163    fn not_three_descending_blacks_yields_zero() {
164        let mut t = LadderBottom::new();
165        // bar2 is not lower than bar1.
166        t.update(c(20.0, 20.1, 17.9, 18.0, 0));
167        t.update(c(21.0, 21.1, 18.9, 19.0, 1));
168        t.update(c(16.0, 16.1, 13.9, 14.0, 2));
169        t.update(c(14.0, 15.0, 12.4, 12.5, 3));
170        assert_eq!(t.update(c(15.0, 17.1, 14.9, 17.0, 4)), Some(0.0));
171    }
172
173    #[test]
174    fn first_four_bars_return_zero() {
175        let mut t = LadderBottom::new();
176        assert_eq!(t.update(c(20.0, 20.1, 17.9, 18.0, 0)), None);
177        assert_eq!(t.update(c(18.0, 18.1, 15.9, 16.0, 1)), None);
178        assert_eq!(t.update(c(16.0, 16.1, 13.9, 14.0, 2)), None);
179        assert_eq!(t.update(c(14.0, 15.0, 12.4, 12.5, 3)), None);
180    }
181
182    #[test]
183    fn batch_equals_streaming() {
184        let candles: Vec<Candle> = (0..40)
185            .map(|i| {
186                let base = 200.0 - i as f64;
187                c(base, base + 0.1, base - 2.1, base - 2.0, i)
188            })
189            .collect();
190        let mut a = LadderBottom::new();
191        let mut b = LadderBottom::new();
192        assert_eq!(
193            a.batch(&candles),
194            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
195        );
196    }
197
198    #[test]
199    fn reset_clears_state() {
200        let mut t = LadderBottom::new();
201        t.update(c(20.0, 20.1, 17.9, 18.0, 0));
202        t.update(c(18.0, 18.1, 15.9, 16.0, 1));
203        t.update(c(16.0, 16.1, 13.9, 14.0, 2));
204        t.update(c(14.0, 15.0, 12.4, 12.5, 3));
205        t.update(c(15.0, 17.1, 14.9, 17.0, 4));
206        assert!(t.is_ready());
207        t.reset();
208        assert!(!t.is_ready());
209        assert_eq!(t.update(c(20.0, 20.1, 17.9, 18.0, 0)), None);
210    }
211}