Skip to main content

wickra_core/indicators/
flag_pennant.rs

1//! Flag / Pennant continuation chart pattern.
2
3use crate::indicators::pattern_swing::{SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Maximum size of the consolidation swing relative to the pole for a
8/// flag/pennant to qualify — the pullback must retrace less than half the pole.
9const MAX_RETRACE_FRACTION: f64 = 0.5;
10
11/// Flag / Pennant — a brief consolidation against a sharp prior move (the
12/// "pole"), resolving in the pole's direction.
13///
14/// Built on confirmed swing pivots ([`SWING_THRESHOLD`] = 5%); evaluated from the
15/// last three pivots `pole_start → pole_end → consolidation`:
16///
17/// ```text
18/// pole      = |pole_end − pole_start|     (the sharp impulse)
19/// pullback  = |consolidation − pole_end|  (the shallow counter-move)
20/// qualifies when pullback < 0.5 · pole
21/// bull flag : pole_end is a swing high → +1 (up-pole, continuation up)
22/// bear flag : pole_end is a swing low  → -1 (down-pole, continuation down)
23/// ```
24///
25/// The detector fires on the bar that confirms the consolidation pivot (the flag
26/// is complete; the breakout is expected to follow). Output is `+1.0` / `-1.0` /
27/// `0.0`; never `None`.
28#[derive(Debug, Clone)]
29pub struct FlagPennant {
30    swing: SwingTracker,
31    has_emitted: bool,
32}
33
34impl FlagPennant {
35    /// Construct a new Flag / Pennant detector.
36    pub const fn new() -> Self {
37        Self {
38            swing: SwingTracker::new(SWING_THRESHOLD, 3),
39            has_emitted: false,
40        }
41    }
42}
43
44impl Default for FlagPennant {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl Indicator for FlagPennant {
51    type Input = Candle;
52    type Output = f64;
53
54    fn update(&mut self, candle: Candle) -> Option<f64> {
55        self.has_emitted = true;
56        if !self.swing.update(candle) {
57            return Some(0.0);
58        }
59        let pivots = self.swing.pivots();
60        if pivots.len() < 3 {
61            return Some(0.0);
62        }
63        let n = pivots.len();
64        let pole_start = pivots[n - 3];
65        let pole_end = pivots[n - 2];
66        let consolidation = pivots[n - 1];
67        let pole = (pole_end.price - pole_start.price).abs();
68        let pullback = (consolidation.price - pole_end.price).abs();
69
70        if pole > 0.0 && pullback < MAX_RETRACE_FRACTION * pole {
71            // pole_end a high → up-pole → bull flag; a low → bear flag.
72            return Some(if pole_end.direction > 0.0 { 1.0 } else { -1.0 });
73        }
74        Some(0.0)
75    }
76
77    fn reset(&mut self) {
78        self.swing.reset();
79        self.has_emitted = false;
80    }
81
82    fn warmup_period(&self) -> usize {
83        // Three confirmed pivots; the earliest confirmation of the third is bar 4.
84        4
85    }
86
87    fn is_ready(&self) -> bool {
88        self.has_emitted
89    }
90
91    fn name(&self) -> &'static str {
92        "FlagPennant"
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use crate::indicators::pattern_swing::candles_for_pivots;
100    use crate::traits::BatchExt;
101
102    fn run(pivots: &[f64]) -> Vec<f64> {
103        let mut indicator = FlagPennant::new();
104        candles_for_pivots(pivots)
105            .into_iter()
106            .map(|c| indicator.update(c).unwrap())
107            .collect()
108    }
109
110    #[test]
111    fn accessors_and_metadata() {
112        let indicator = FlagPennant::new();
113        assert_eq!(indicator.name(), "FlagPennant");
114        assert_eq!(indicator.warmup_period(), 4);
115        assert!(!indicator.is_ready());
116        assert!(!FlagPennant::default().is_ready());
117    }
118
119    #[test]
120    fn bull_flag_is_plus_one() {
121        // Up-pole 100 → 140 (40), shallow pullback to 130 (10 < 20) → bull flag.
122        let out = run(&[150.0, 100.0, 140.0, 130.0]);
123        assert_eq!(*out.last().unwrap(), 1.0);
124    }
125
126    #[test]
127    fn bear_flag_is_minus_one() {
128        // Down-pole 140 → 100 (40), shallow pullback to 110 (10 < 20) → bear flag.
129        let out = run(&[140.0, 100.0, 110.0]);
130        assert_eq!(*out.last().unwrap(), -1.0);
131    }
132
133    #[test]
134    fn deep_pullback_is_not_a_flag() {
135        // Pole 100 → 140 (40) but pullback to 104 (36 > 20) → not a flag.
136        let out = run(&[150.0, 100.0, 140.0, 104.0]);
137        assert_eq!(*out.last().unwrap(), 0.0);
138    }
139
140    #[test]
141    fn reset_clears_state() {
142        let mut indicator = FlagPennant::new();
143        for c in candles_for_pivots(&[150.0, 100.0, 140.0]) {
144            let _ = indicator.update(c);
145        }
146        indicator.reset();
147        assert!(!indicator.is_ready());
148        let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
149        assert_eq!(indicator.update(c), Some(0.0));
150    }
151
152    #[test]
153    fn batch_equals_streaming() {
154        let candles = candles_for_pivots(&[150.0, 100.0, 140.0, 130.0]);
155        let mut a = FlagPennant::new();
156        let mut b = FlagPennant::new();
157        assert_eq!(
158            a.batch(&candles),
159            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
160        );
161    }
162}