Skip to main content

wickra_core/indicators/
rectangle_range.rs

1//! Rectangle / Range chart pattern.
2
3use crate::indicators::pattern_swing::{
4    approx_equal, recent_legs, SwingTracker, LEVEL_TOLERANCE, SWING_THRESHOLD,
5};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9/// Rectangle / Range — price oscillating between a roughly horizontal support
10/// and resistance, a mean-reversion (range-trading) structure.
11///
12/// Built on confirmed swing pivots ([`SWING_THRESHOLD`] = 5%); recognised when the
13/// last two highs and the last two lows are each flat within [`LEVEL_TOLERANCE`]
14/// (3%):
15///
16/// ```text
17/// flat highs (resistance) AND flat lows (support):
18///   last pivot a low  → +1  (a bounce off support — buy the range)
19///   last pivot a high → -1  (a rejection at resistance — sell the range)
20/// ```
21///
22/// Unlike the breakout patterns the rectangle is range-bound, so the sign
23/// encodes the actionable mean-reversion direction of the just-confirmed touch.
24/// Output is `+1.0` / `-1.0` / `0.0`; never `None`.
25#[derive(Debug, Clone)]
26pub struct RectangleRange {
27    swing: SwingTracker,
28    has_emitted: bool,
29}
30
31impl RectangleRange {
32    /// Construct a new Rectangle / Range detector.
33    pub const fn new() -> Self {
34        Self {
35            swing: SwingTracker::new(SWING_THRESHOLD, 4),
36            has_emitted: false,
37        }
38    }
39}
40
41impl Default for RectangleRange {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl Indicator for RectangleRange {
48    type Input = Candle;
49    type Output = f64;
50
51    fn update(&mut self, candle: Candle) -> Option<f64> {
52        self.has_emitted = true;
53        if !self.swing.update(candle) {
54            return Some(0.0);
55        }
56        let pivots = self.swing.pivots();
57        if pivots.len() < 4 {
58            return Some(0.0);
59        }
60        let (high_old, high_new, low_old, low_new) = recent_legs(pivots);
61        let flat_highs = approx_equal(high_old, high_new, LEVEL_TOLERANCE);
62        let flat_lows = approx_equal(low_old, low_new, LEVEL_TOLERANCE);
63        if flat_highs && flat_lows {
64            let last_is_high = pivots[pivots.len() - 1].direction > 0.0;
65            return Some(if last_is_high { -1.0 } else { 1.0 });
66        }
67        Some(0.0)
68    }
69
70    fn reset(&mut self) {
71        self.swing.reset();
72        self.has_emitted = false;
73    }
74
75    fn warmup_period(&self) -> usize {
76        // Four confirmed pivots; the earliest confirmation of the fourth is bar 5.
77        5
78    }
79
80    fn is_ready(&self) -> bool {
81        self.has_emitted
82    }
83
84    fn name(&self) -> &'static str {
85        "RectangleRange"
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92    use crate::indicators::pattern_swing::candles_for_pivots;
93    use crate::traits::BatchExt;
94
95    fn run(pivots: &[f64]) -> Vec<f64> {
96        let mut indicator = RectangleRange::new();
97        candles_for_pivots(pivots)
98            .into_iter()
99            .map(|c| indicator.update(c).unwrap())
100            .collect()
101    }
102
103    #[test]
104    fn accessors_and_metadata() {
105        let indicator = RectangleRange::new();
106        assert_eq!(indicator.name(), "RectangleRange");
107        assert_eq!(indicator.warmup_period(), 5);
108        assert!(!indicator.is_ready());
109        assert!(!RectangleRange::default().is_ready());
110    }
111
112    #[test]
113    fn range_bounce_off_support_is_plus_one() {
114        // Flat highs (120, 121), flat lows (100, 99); last pivot a low → +1.
115        let out = run(&[120.0, 100.0, 121.0, 99.0]);
116        assert_eq!(*out.last().unwrap(), 1.0);
117    }
118
119    #[test]
120    fn range_rejection_at_resistance_is_minus_one() {
121        // Same range but ending on a high pivot → -1.
122        let out = run(&[130.0, 100.0, 120.0, 99.0, 121.0]);
123        assert_eq!(*out.last().unwrap(), -1.0);
124    }
125
126    #[test]
127    fn trending_highs_are_not_a_rectangle() {
128        // Rising highs break the flat-resistance requirement → no rectangle.
129        let out = run(&[120.0, 100.0, 140.0, 99.0]);
130        assert_eq!(*out.last().unwrap(), 0.0);
131    }
132
133    #[test]
134    fn reset_clears_state() {
135        let mut indicator = RectangleRange::new();
136        for c in candles_for_pivots(&[120.0, 100.0, 121.0]) {
137            let _ = indicator.update(c);
138        }
139        indicator.reset();
140        assert!(!indicator.is_ready());
141        let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
142        assert_eq!(indicator.update(c), Some(0.0));
143    }
144
145    #[test]
146    fn batch_equals_streaming() {
147        let candles = candles_for_pivots(&[120.0, 100.0, 121.0, 99.0]);
148        let mut a = RectangleRange::new();
149        let mut b = RectangleRange::new();
150        assert_eq!(
151            a.batch(&candles),
152            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
153        );
154    }
155}