Skip to main content

wickra_core/indicators/
belt_hold.rs

1//! Belt-hold candlestick pattern.
2
3use crate::error::{Error, Result};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Belt-hold — a single-bar reversal: a long candle that opens at one extreme of
8/// its range (an "opening marubozu") and runs the other way.
9///
10/// ```text
11/// range        = high − low
12/// bullish (+1.0): green, opens at the low  (open − low <= tol * range) & long body
13/// bearish (−1.0): red,   opens at the high (high − open <= tol * range) & long body
14/// long body    = |close − open| >= 0.5 * range
15/// ```
16///
17/// Output is `0.0` when the opening side carries a shadow, the body is short, or
18/// the range is degenerate. `shadow_tolerance` defaults to `0.05` (5 % of the bar
19/// range allowed on the opening side) and must lie in `[0, 1)`. Pattern-shape
20/// check only — no trend filter is applied; combine with a trend indicator for
21/// 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, `−1.0` bearish, `0.0` no pattern — so it
27/// drops straight into a machine-learning feature matrix where the bullish and
28/// bearish variants occupy a single dimension.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{BeltHold, Candle, Indicator};
34///
35/// let mut indicator = BeltHold::new();
36/// // Bullish belt-hold: opens at the low, closes near the high.
37/// let candle = Candle::new(10.0, 12.0, 10.0, 11.5, 1.0, 0).unwrap();
38/// assert_eq!(indicator.update(candle), Some(1.0));
39/// ```
40#[derive(Debug, Clone)]
41pub struct BeltHold {
42    shadow_tolerance: f64,
43    has_emitted: bool,
44}
45
46impl Default for BeltHold {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl BeltHold {
53    /// Construct a Belt-hold detector with the default 5 % opening-shadow tolerance.
54    pub const fn new() -> Self {
55        Self {
56            shadow_tolerance: 0.05,
57            has_emitted: false,
58        }
59    }
60
61    /// Construct a Belt-hold detector with a custom opening-shadow tolerance.
62    ///
63    /// `shadow_tolerance` must lie in `[0, 1)`.
64    pub fn with_tolerance(shadow_tolerance: f64) -> Result<Self> {
65        if !(0.0..1.0).contains(&shadow_tolerance) {
66            return Err(Error::InvalidPeriod {
67                message: "belt-hold shadow tolerance must lie in [0, 1)",
68            });
69        }
70        Ok(Self {
71            shadow_tolerance,
72            has_emitted: false,
73        })
74    }
75
76    /// Configured opening-shadow tolerance.
77    pub fn shadow_tolerance(&self) -> f64 {
78        self.shadow_tolerance
79    }
80}
81
82impl Indicator for BeltHold {
83    type Input = Candle;
84    type Output = f64;
85
86    fn update(&mut self, candle: Candle) -> Option<f64> {
87        self.has_emitted = true;
88        let range = candle.high - candle.low;
89        if range <= 0.0 {
90            return Some(0.0);
91        }
92        let body = candle.close - candle.open;
93        if body.abs() < 0.5 * range {
94            return Some(0.0);
95        }
96        let tol = self.shadow_tolerance * range;
97        // Bullish: opens at the low (no lower shadow), green body.
98        if body > 0.0 && candle.open - candle.low <= tol {
99            return Some(1.0);
100        }
101        // Bearish: opens at the high (no upper shadow), red body.
102        if body < 0.0 && candle.high - candle.open <= tol {
103            return Some(-1.0);
104        }
105        Some(0.0)
106    }
107
108    fn reset(&mut self) {
109        self.has_emitted = false;
110    }
111
112    fn warmup_period(&self) -> usize {
113        1
114    }
115
116    fn is_ready(&self) -> bool {
117        self.has_emitted
118    }
119
120    fn name(&self) -> &'static str {
121        "BeltHold"
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128    use crate::traits::BatchExt;
129
130    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
131        Candle::new(open, high, low, close, 1.0, ts).unwrap()
132    }
133
134    #[test]
135    fn rejects_invalid_tolerance() {
136        assert!(BeltHold::with_tolerance(-0.01).is_err());
137        assert!(BeltHold::with_tolerance(1.0).is_err());
138    }
139
140    #[test]
141    fn accepts_valid_tolerance() {
142        let t = BeltHold::with_tolerance(0.0).unwrap();
143        assert!((t.shadow_tolerance() - 0.0).abs() < 1e-12);
144    }
145
146    #[test]
147    fn accessors_and_metadata() {
148        let t = BeltHold::default();
149        assert_eq!(t.name(), "BeltHold");
150        assert_eq!(t.warmup_period(), 1);
151        assert!(!t.is_ready());
152        assert!((t.shadow_tolerance() - 0.05).abs() < 1e-12);
153    }
154
155    #[test]
156    fn bullish_belt_hold_is_plus_one() {
157        let mut t = BeltHold::new();
158        assert_eq!(t.update(c(10.0, 12.0, 10.0, 11.5, 0)), Some(1.0));
159    }
160
161    #[test]
162    fn bearish_belt_hold_is_minus_one() {
163        let mut t = BeltHold::new();
164        assert_eq!(t.update(c(12.0, 12.0, 10.0, 10.5, 0)), Some(-1.0));
165    }
166
167    #[test]
168    fn opening_shadow_yields_zero() {
169        let mut t = BeltHold::new();
170        // Opens 0.5 above the low -> lower shadow exceeds tolerance.
171        assert_eq!(t.update(c(10.5, 12.0, 10.0, 11.5, 0)), Some(0.0));
172    }
173
174    #[test]
175    fn short_body_yields_zero() {
176        let mut t = BeltHold::new();
177        // Body 0.5 < half the range (1.0) -> not a long belt-hold.
178        assert_eq!(t.update(c(10.0, 12.0, 10.0, 10.5, 0)), Some(0.0));
179    }
180
181    #[test]
182    fn zero_range_yields_zero() {
183        let mut t = BeltHold::new();
184        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
185    }
186
187    #[test]
188    fn batch_equals_streaming() {
189        let candles: Vec<Candle> = (0..40)
190            .map(|i| {
191                let base = 100.0 + i as f64;
192                c(base, base + 2.0, base, base + 1.8, i)
193            })
194            .collect();
195        let mut a = BeltHold::new();
196        let mut b = BeltHold::new();
197        assert_eq!(
198            a.batch(&candles),
199            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
200        );
201    }
202
203    #[test]
204    fn reset_clears_state() {
205        let mut t = BeltHold::new();
206        t.update(c(10.0, 12.0, 10.0, 11.5, 0));
207        assert!(t.is_ready());
208        t.reset();
209        assert!(!t.is_ready());
210    }
211}