Skip to main content

wickra_core/indicators/
breakaway.rs

1//! Breakaway candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Breakaway — a 5-bar reversal that fades an exhausted run. A trend gaps away on
7/// the second bar, drifts two more bars in the same direction, then the fifth bar
8/// snaps the other way and closes back inside the body gap left between the first
9/// and second bars, signalling the move has broken away from the crowd and is
10/// turning.
11///
12/// ```text
13/// bullish (+1.0)  — appears in a decline:
14///   bar1 black (close < open)
15///   bar2 black & its body gaps DOWN below bar1's body  (bar2.open < bar1.close)
16///   bar3 extends lower            (high & low below bar2)
17///   bar4 black & extends lower    (high & low below bar3)
18///   bar5 green & closes inside the bar1/bar2 body gap   (bar2.open < close < bar1.close)
19///
20/// bearish (−1.0) — the mirror in an advance:
21///   bar1 white (close > open)
22///   bar2 white & its body gaps UP above bar1's body    (bar2.open > bar1.close)
23///   bar3 extends higher           (high & low above bar2)
24///   bar4 white & extends higher   (high & low above bar3)
25///   bar5 red & closes inside the bar1/bar2 body gap     (bar1.close < close < bar2.open)
26/// ```
27///
28/// The middle bar (`bar3`) may be either colour — only its high/low must extend
29/// the run. Output is `+1.0` bullish, `−1.0` bearish, `0.0` otherwise. The first
30/// four bars always return `0.0` because the five-bar window is not yet filled.
31/// Pattern-shape check only — no trend filter is applied; combine with a trend
32/// indicator for actionable signals. Recognition uses TA-Lib's
33/// `CDLBREAKAWAY` body-gap and high/low ordering rules directly; it does not add
34/// TA-Lib's rolling body-length average, matching the geometric house style of
35/// the other multi-bar patterns in this family.
36///
37/// # Signed ±1 encoding
38///
39/// This detector emits the uniform candlestick sign convention shared across the
40/// pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no pattern — so it
41/// drops straight into a machine-learning feature matrix where the bullish and
42/// bearish variants occupy a single dimension.
43///
44/// # Example
45///
46/// ```
47/// use wickra_core::{Breakaway, Candle, Indicator};
48///
49/// let mut indicator = Breakaway::new();
50/// indicator.update(Candle::new(20.0, 20.2, 14.8, 15.0, 1.0, 0).unwrap());
51/// indicator.update(Candle::new(14.0, 14.1, 11.9, 12.0, 1.0, 1).unwrap());
52/// indicator.update(Candle::new(12.5, 13.0, 10.5, 11.0, 1.0, 2).unwrap());
53/// indicator.update(Candle::new(11.0, 11.5, 9.0, 9.5, 1.0, 3).unwrap());
54/// let out = indicator
55///     .update(Candle::new(9.5, 14.7, 9.4, 14.5, 1.0, 4).unwrap());
56/// assert_eq!(out, Some(1.0));
57/// ```
58#[derive(Debug, Clone, Default)]
59pub struct Breakaway {
60    c1: Option<Candle>,
61    c2: Option<Candle>,
62    c3: Option<Candle>,
63    c4: Option<Candle>,
64    has_emitted: bool,
65}
66
67impl Breakaway {
68    /// Construct a new Breakaway detector.
69    pub const fn new() -> Self {
70        Self {
71            c1: None,
72            c2: None,
73            c3: None,
74            c4: None,
75            has_emitted: false,
76        }
77    }
78}
79
80impl Indicator for Breakaway {
81    type Input = Candle;
82    type Output = f64;
83
84    fn update(&mut self, candle: Candle) -> Option<f64> {
85        self.has_emitted = true;
86        let bar1 = self.c1;
87        let bar2 = self.c2;
88        let bar3 = self.c3;
89        let bar4 = self.c4;
90        self.c1 = self.c2;
91        self.c2 = self.c3;
92        self.c3 = self.c4;
93        self.c4 = Some(candle);
94        let (Some(bar1), Some(bar2), Some(bar3), Some(bar4)) = (bar1, bar2, bar3, bar4) else {
95            return Some(0.0);
96        };
97        // Bullish: a decline gaps lower, runs two more bars down, then a green
98        // bar5 closes back inside the bar1/bar2 body gap.
99        if bar1.close < bar1.open
100            && bar2.close < bar2.open
101            && bar2.open < bar1.close
102            && bar3.high < bar2.high
103            && bar3.low < bar2.low
104            && bar4.close < bar4.open
105            && bar4.high < bar3.high
106            && bar4.low < bar3.low
107            && candle.close > candle.open
108            && candle.close > bar2.open
109            && candle.close < bar1.close
110        {
111            return Some(1.0);
112        }
113        // Bearish: the mirror — an advance gaps higher, runs two more bars up,
114        // then a red bar5 closes back inside the bar1/bar2 body gap.
115        if bar1.close > bar1.open
116            && bar2.close > bar2.open
117            && bar2.open > bar1.close
118            && bar3.high > bar2.high
119            && bar3.low > bar2.low
120            && bar4.close > bar4.open
121            && bar4.high > bar3.high
122            && bar4.low > bar3.low
123            && candle.close < candle.open
124            && candle.close < bar2.open
125            && candle.close > bar1.close
126        {
127            return Some(-1.0);
128        }
129        Some(0.0)
130    }
131
132    fn reset(&mut self) {
133        self.c1 = None;
134        self.c2 = None;
135        self.c3 = None;
136        self.c4 = None;
137        self.has_emitted = false;
138    }
139
140    fn warmup_period(&self) -> usize {
141        5
142    }
143
144    fn is_ready(&self) -> bool {
145        self.has_emitted
146    }
147
148    fn name(&self) -> &'static str {
149        "Breakaway"
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156    use crate::traits::BatchExt;
157
158    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
159        Candle::new(open, high, low, close, 1.0, ts).unwrap()
160    }
161
162    #[test]
163    fn accessors_and_metadata() {
164        let t = Breakaway::new();
165        assert_eq!(t.name(), "Breakaway");
166        assert_eq!(t.warmup_period(), 5);
167        assert!(!t.is_ready());
168    }
169
170    #[test]
171    fn bullish_breakaway_is_plus_one() {
172        let mut t = Breakaway::new();
173        assert_eq!(t.update(c(20.0, 20.2, 14.8, 15.0, 0)), Some(0.0));
174        assert_eq!(t.update(c(14.0, 14.1, 11.9, 12.0, 1)), Some(0.0));
175        assert_eq!(t.update(c(12.5, 13.0, 10.5, 11.0, 2)), Some(0.0));
176        assert_eq!(t.update(c(11.0, 11.5, 9.0, 9.5, 3)), Some(0.0));
177        assert_eq!(t.update(c(9.5, 14.7, 9.4, 14.5, 4)), Some(1.0));
178    }
179
180    #[test]
181    fn bearish_breakaway_is_minus_one() {
182        let mut t = Breakaway::new();
183        assert_eq!(t.update(c(15.0, 20.2, 14.8, 20.0, 0)), Some(0.0));
184        assert_eq!(t.update(c(21.0, 23.1, 20.9, 23.0, 1)), Some(0.0));
185        assert_eq!(t.update(c(22.5, 24.5, 21.5, 24.0, 2)), Some(0.0));
186        assert_eq!(t.update(c(24.0, 26.5, 23.0, 26.0, 3)), Some(0.0));
187        assert_eq!(t.update(c(27.0, 27.2, 20.4, 20.5, 4)), Some(-1.0));
188    }
189
190    #[test]
191    fn no_body_gap_yields_zero() {
192        let mut t = Breakaway::new();
193        // bar2 does not gap below bar1's body (bar2.open >= bar1.close).
194        t.update(c(20.0, 20.2, 14.8, 15.0, 0));
195        t.update(c(16.0, 16.1, 13.9, 14.0, 1));
196        t.update(c(13.5, 14.0, 11.5, 12.0, 2));
197        t.update(c(12.0, 12.5, 10.0, 10.5, 3));
198        assert_eq!(t.update(c(10.5, 15.7, 10.4, 15.5, 4)), Some(0.0));
199    }
200
201    #[test]
202    fn bullish_close_outside_gap_yields_zero() {
203        let mut t = Breakaway::new();
204        t.update(c(20.0, 20.2, 14.8, 15.0, 0));
205        t.update(c(14.0, 14.1, 11.9, 12.0, 1));
206        t.update(c(12.5, 13.0, 10.5, 11.0, 2));
207        t.update(c(11.0, 11.5, 9.0, 9.5, 3));
208        // bar5 closes at 13.0 — below bar2.open (14), so outside the body gap.
209        assert_eq!(t.update(c(9.5, 13.2, 9.4, 13.0, 4)), Some(0.0));
210    }
211
212    #[test]
213    fn first_four_bars_return_zero() {
214        let mut t = Breakaway::new();
215        assert_eq!(t.update(c(20.0, 20.2, 14.8, 15.0, 0)), Some(0.0));
216        assert_eq!(t.update(c(14.0, 14.1, 11.9, 12.0, 1)), Some(0.0));
217        assert_eq!(t.update(c(12.5, 13.0, 10.5, 11.0, 2)), Some(0.0));
218        assert_eq!(t.update(c(11.0, 11.5, 9.0, 9.5, 3)), Some(0.0));
219    }
220
221    #[test]
222    fn batch_equals_streaming() {
223        let candles: Vec<Candle> = (0..40)
224            .map(|i| {
225                let base = 100.0 + i as f64;
226                c(base, base + 2.0, base - 0.5, base + 1.5, i)
227            })
228            .collect();
229        let mut a = Breakaway::new();
230        let mut b = Breakaway::new();
231        assert_eq!(
232            a.batch(&candles),
233            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
234        );
235    }
236
237    #[test]
238    fn reset_clears_state() {
239        let mut t = Breakaway::new();
240        t.update(c(20.0, 20.2, 14.8, 15.0, 0));
241        t.update(c(14.0, 14.1, 11.9, 12.0, 1));
242        t.update(c(12.5, 13.0, 10.5, 11.0, 2));
243        t.update(c(11.0, 11.5, 9.0, 9.5, 3));
244        t.update(c(9.5, 14.7, 9.4, 14.5, 4));
245        assert!(t.is_ready());
246        t.reset();
247        assert!(!t.is_ready());
248        assert_eq!(t.update(c(20.0, 20.2, 14.8, 15.0, 0)), Some(0.0));
249    }
250}