Skip to main content

wickra_core/indicators/
marubozu.rs

1//! Marubozu candlestick pattern.
2
3use crate::error::{Error, Result};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Marubozu — a single-bar strong-continuation candle with body equal to range
8/// and (almost) no shadows.
9///
10/// ```text
11/// range        = high − low
12/// upper_shadow = high − max(open, close)
13/// lower_shadow = min(open, close) − low
14/// shadows OK   = upper_shadow <= tol * range && lower_shadow <= tol * range
15/// ```
16///
17/// When the shadow tolerance is satisfied the output is `+1.0` for a bullish
18/// Marubozu (close > open) and `−1.0` for a bearish one (close < open). Any
19/// candle whose shadows exceed the tolerance — or whose body is zero — yields
20/// `0.0`.
21///
22/// `shadow_tolerance` defaults to `0.05` (5 % of the bar range allowed on each
23/// side) and must lie in `[0, 1)`.
24///
25/// # Signed ±1 encoding
26///
27/// This detector already emits the uniform candlestick sign convention shared
28/// across the pattern family — `+1.0` bullish, `−1.0` bearish, `0.0` no
29/// pattern — so it drops straight into a machine-learning feature matrix where
30/// the bullish and bearish variants of the pattern occupy a single dimension.
31///
32/// # Example
33///
34/// ```
35/// use wickra_core::{Candle, Indicator, Marubozu};
36///
37/// let mut indicator = Marubozu::new();
38/// // Bullish marubozu: open == low, close == high.
39/// let candle = Candle::new(10.0, 12.0, 10.0, 12.0, 1.0, 0).unwrap();
40/// assert_eq!(indicator.update(candle), Some(1.0));
41/// ```
42#[derive(Debug, Clone)]
43pub struct Marubozu {
44    shadow_tolerance: f64,
45    has_emitted: bool,
46}
47
48impl Default for Marubozu {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl Marubozu {
55    /// Construct a Marubozu detector with the default 5 % shadow tolerance.
56    pub const fn new() -> Self {
57        Self {
58            shadow_tolerance: 0.05,
59            has_emitted: false,
60        }
61    }
62
63    /// Construct a Marubozu detector with a custom shadow tolerance.
64    ///
65    /// `shadow_tolerance` must lie in `[0, 1)`.
66    pub fn with_tolerance(shadow_tolerance: f64) -> Result<Self> {
67        if !(0.0..1.0).contains(&shadow_tolerance) {
68            return Err(Error::InvalidPeriod {
69                message: "marubozu shadow tolerance must lie in [0, 1)",
70            });
71        }
72        Ok(Self {
73            shadow_tolerance,
74            has_emitted: false,
75        })
76    }
77
78    /// Configured shadow tolerance.
79    pub fn shadow_tolerance(&self) -> f64 {
80        self.shadow_tolerance
81    }
82}
83
84impl Indicator for Marubozu {
85    type Input = Candle;
86    type Output = f64;
87
88    fn update(&mut self, candle: Candle) -> Option<f64> {
89        self.has_emitted = true;
90        let range = candle.high - candle.low;
91        if range <= 0.0 {
92            return Some(0.0);
93        }
94        let body = candle.close - candle.open;
95        if body == 0.0 {
96            return Some(0.0);
97        }
98        let upper = candle.high - candle.open.max(candle.close);
99        let lower = candle.open.min(candle.close) - candle.low;
100        let tol = self.shadow_tolerance * range;
101        if upper <= tol && lower <= tol {
102            Some(if body > 0.0 { 1.0 } else { -1.0 })
103        } else {
104            Some(0.0)
105        }
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        "Marubozu"
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!(Marubozu::with_tolerance(-0.01).is_err());
137        assert!(Marubozu::with_tolerance(1.0).is_err());
138        assert!(Marubozu::with_tolerance(2.0).is_err());
139    }
140
141    #[test]
142    fn accepts_valid_tolerance() {
143        let m = Marubozu::with_tolerance(0.0).unwrap();
144        assert!((m.shadow_tolerance() - 0.0).abs() < 1e-12);
145        let m = Marubozu::with_tolerance(0.5).unwrap();
146        assert!((m.shadow_tolerance() - 0.5).abs() < 1e-12);
147    }
148
149    #[test]
150    fn accessors_and_metadata() {
151        let m = Marubozu::default();
152        assert_eq!(m.name(), "Marubozu");
153        assert_eq!(m.warmup_period(), 1);
154        assert!(!m.is_ready());
155        assert!((m.shadow_tolerance() - 0.05).abs() < 1e-12);
156    }
157
158    #[test]
159    fn bullish_marubozu_is_plus_one() {
160        let mut m = Marubozu::new();
161        assert_eq!(m.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(1.0));
162    }
163
164    #[test]
165    fn bearish_marubozu_is_minus_one() {
166        let mut m = Marubozu::new();
167        assert_eq!(m.update(c(12.0, 12.0, 10.0, 10.0, 0)), Some(-1.0));
168    }
169
170    #[test]
171    fn candle_with_long_shadows_is_zero() {
172        let mut m = Marubozu::new();
173        // Big upper shadow violates tolerance.
174        assert_eq!(m.update(c(10.0, 15.0, 10.0, 12.0, 0)), Some(0.0));
175    }
176
177    #[test]
178    fn doji_is_zero() {
179        let mut m = Marubozu::new();
180        // body == 0 -> not a marubozu.
181        assert_eq!(m.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
182    }
183
184    #[test]
185    fn zero_range_yields_zero() {
186        let mut m = Marubozu::new();
187        assert_eq!(m.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
188    }
189
190    #[test]
191    fn batch_equals_streaming() {
192        let candles: Vec<Candle> = (0..40)
193            .map(|i| {
194                let base = 100.0 + i as f64;
195                c(base, base + 2.0, base, base + 2.0, i)
196            })
197            .collect();
198        let mut a = Marubozu::new();
199        let mut b = Marubozu::new();
200        assert_eq!(
201            a.batch(&candles),
202            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
203        );
204    }
205
206    #[test]
207    fn reset_clears_state() {
208        let mut m = Marubozu::new();
209        m.update(c(10.0, 12.0, 10.0, 12.0, 0));
210        assert!(m.is_ready());
211        m.reset();
212        assert!(!m.is_ready());
213    }
214}