Skip to main content

wickra_core/indicators/
advance_block.rs

1//! Advance Block candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Advance Block — a 3-bar bearish warning: three green candles still pushing to
7/// higher closes, but visibly running out of steam — each real body shrinks while
8/// the upper shadows lengthen, hinting the advance is about to stall.
9///
10/// ```text
11/// all three green & higher closes
12/// each opens inside the prior body
13/// shrinking bodies   (body3 < body2 < body1)
14/// upper shadow of bar3 >= upper shadow of bar2 and bar3 has an upper shadow
15/// ```
16///
17/// Output is `−1.0` when the pattern completes and `0.0` otherwise. Advance Block
18/// is a single-direction (bearish-only) warning, so it never emits `+1.0`. The
19/// first two bars always return `0.0` because the three-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` bearish, `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::{AdvanceBlock, Candle, Indicator};
33///
34/// let mut indicator = AdvanceBlock::new();
35/// indicator.update(Candle::new(10.0, 13.1, 9.9, 13.0, 1.0, 0).unwrap());
36/// indicator.update(Candle::new(12.0, 14.3, 11.9, 14.0, 1.0, 1).unwrap());
37/// let out = indicator
38///     .update(Candle::new(13.5, 15.0, 13.4, 14.5, 1.0, 2).unwrap());
39/// assert_eq!(out, Some(-1.0));
40/// ```
41#[derive(Debug, Clone, Default)]
42pub struct AdvanceBlock {
43    prev: Option<Candle>,
44    prev_prev: Option<Candle>,
45    has_emitted: bool,
46}
47
48impl AdvanceBlock {
49    /// Construct a new Advance Block detector.
50    pub const fn new() -> Self {
51        Self {
52            prev: None,
53            prev_prev: None,
54            has_emitted: false,
55        }
56    }
57}
58
59impl Indicator for AdvanceBlock {
60    type Input = Candle;
61    type Output = f64;
62
63    fn update(&mut self, candle: Candle) -> Option<f64> {
64        self.has_emitted = true;
65        let pp = self.prev_prev;
66        let p = self.prev;
67        self.prev_prev = self.prev;
68        self.prev = Some(candle);
69        let (Some(bar1), Some(bar2)) = (pp, p) else {
70            return Some(0.0);
71        };
72        let body1 = bar1.close - bar1.open;
73        let body2 = bar2.close - bar2.open;
74        let body3 = candle.close - candle.open;
75        let upper2 = bar2.high - bar2.close;
76        let upper3 = candle.high - candle.close;
77        if bar1.close > bar1.open
78            && bar2.close > bar2.open
79            && candle.close > candle.open
80            && bar2.close > bar1.close
81            && candle.close > bar2.close
82            && bar2.open >= bar1.open
83            && bar2.open <= bar1.close
84            && candle.open >= bar2.open
85            && candle.open <= bar2.close
86            && body2 < body1
87            && body3 < body2
88            && upper3 >= upper2
89            && upper3 > 0.0
90        {
91            return Some(-1.0);
92        }
93        Some(0.0)
94    }
95
96    fn reset(&mut self) {
97        self.prev = None;
98        self.prev_prev = None;
99        self.has_emitted = false;
100    }
101
102    fn warmup_period(&self) -> usize {
103        3
104    }
105
106    fn is_ready(&self) -> bool {
107        self.has_emitted
108    }
109
110    fn name(&self) -> &'static str {
111        "AdvanceBlock"
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    use crate::traits::BatchExt;
119
120    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
121        Candle::new(open, high, low, close, 1.0, ts).unwrap()
122    }
123
124    #[test]
125    fn accessors_and_metadata() {
126        let t = AdvanceBlock::new();
127        assert_eq!(t.name(), "AdvanceBlock");
128        assert_eq!(t.warmup_period(), 3);
129        assert!(!t.is_ready());
130    }
131
132    #[test]
133    fn advance_block_is_minus_one() {
134        let mut t = AdvanceBlock::new();
135        assert_eq!(t.update(c(10.0, 13.1, 9.9, 13.0, 0)), Some(0.0));
136        assert_eq!(t.update(c(12.0, 14.3, 11.9, 14.0, 1)), Some(0.0));
137        assert_eq!(t.update(c(13.5, 15.0, 13.4, 14.5, 2)), Some(-1.0));
138    }
139
140    #[test]
141    fn strong_advance_yields_zero() {
142        let mut t = AdvanceBlock::new();
143        // Bodies grow instead of shrinking -> a strong advance, not blocked.
144        assert_eq!(t.update(c(10.0, 11.1, 9.9, 11.0, 0)), Some(0.0));
145        assert_eq!(t.update(c(10.5, 12.6, 10.4, 12.5, 1)), Some(0.0));
146        assert_eq!(t.update(c(11.5, 14.1, 11.4, 14.0, 2)), Some(0.0));
147    }
148
149    #[test]
150    fn no_upper_shadow_growth_yields_zero() {
151        let mut t = AdvanceBlock::new();
152        t.update(c(10.0, 13.1, 9.9, 13.0, 0));
153        t.update(c(12.0, 14.3, 11.9, 14.0, 1));
154        // bar3 shrinking body but no upper shadow -> not blocked.
155        assert_eq!(t.update(c(13.5, 14.5, 13.4, 14.5, 2)), Some(0.0));
156    }
157
158    #[test]
159    fn first_two_bars_return_zero() {
160        let mut t = AdvanceBlock::new();
161        assert_eq!(t.update(c(10.0, 13.1, 9.9, 13.0, 0)), Some(0.0));
162        assert_eq!(t.update(c(12.0, 14.3, 11.9, 14.0, 1)), Some(0.0));
163    }
164
165    #[test]
166    fn batch_equals_streaming() {
167        let candles: Vec<Candle> = (0..40)
168            .map(|i| {
169                let base = 100.0 + i as f64;
170                c(base, base + 2.0, base - 0.2, base + 1.5, i)
171            })
172            .collect();
173        let mut a = AdvanceBlock::new();
174        let mut b = AdvanceBlock::new();
175        assert_eq!(
176            a.batch(&candles),
177            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
178        );
179    }
180
181    #[test]
182    fn reset_clears_state() {
183        let mut t = AdvanceBlock::new();
184        t.update(c(10.0, 13.1, 9.9, 13.0, 0));
185        t.update(c(12.0, 14.3, 11.9, 14.0, 1));
186        t.update(c(13.5, 15.0, 13.4, 14.5, 2));
187        assert!(t.is_ready());
188        t.reset();
189        assert!(!t.is_ready());
190        assert_eq!(t.update(c(10.0, 13.1, 9.9, 13.0, 0)), Some(0.0));
191    }
192}