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/// # Example
26///
27/// ```
28/// use wickra_core::{Candle, Indicator, Marubozu};
29///
30/// let mut indicator = Marubozu::new();
31/// // Bullish marubozu: open == low, close == high.
32/// let candle = Candle::new(10.0, 12.0, 10.0, 12.0, 1.0, 0).unwrap();
33/// assert_eq!(indicator.update(candle), Some(1.0));
34/// ```
35#[derive(Debug, Clone)]
36pub struct Marubozu {
37    shadow_tolerance: f64,
38    has_emitted: bool,
39}
40
41impl Default for Marubozu {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl Marubozu {
48    /// Construct a Marubozu detector with the default 5 % shadow tolerance.
49    pub const fn new() -> Self {
50        Self {
51            shadow_tolerance: 0.05,
52            has_emitted: false,
53        }
54    }
55
56    /// Construct a Marubozu detector with a custom shadow tolerance.
57    ///
58    /// `shadow_tolerance` must lie in `[0, 1)`.
59    pub fn with_tolerance(shadow_tolerance: f64) -> Result<Self> {
60        if !(0.0..1.0).contains(&shadow_tolerance) {
61            return Err(Error::InvalidPeriod {
62                message: "marubozu shadow tolerance must lie in [0, 1)",
63            });
64        }
65        Ok(Self {
66            shadow_tolerance,
67            has_emitted: false,
68        })
69    }
70
71    /// Configured shadow tolerance.
72    pub fn shadow_tolerance(&self) -> f64 {
73        self.shadow_tolerance
74    }
75}
76
77impl Indicator for Marubozu {
78    type Input = Candle;
79    type Output = f64;
80
81    fn update(&mut self, candle: Candle) -> Option<f64> {
82        self.has_emitted = true;
83        let range = candle.high - candle.low;
84        if range <= 0.0 {
85            return Some(0.0);
86        }
87        let body = candle.close - candle.open;
88        if body == 0.0 {
89            return Some(0.0);
90        }
91        let upper = candle.high - candle.open.max(candle.close);
92        let lower = candle.open.min(candle.close) - candle.low;
93        let tol = self.shadow_tolerance * range;
94        if upper <= tol && lower <= tol {
95            Some(if body > 0.0 { 1.0 } else { -1.0 })
96        } else {
97            Some(0.0)
98        }
99    }
100
101    fn reset(&mut self) {
102        self.has_emitted = false;
103    }
104
105    fn warmup_period(&self) -> usize {
106        1
107    }
108
109    fn is_ready(&self) -> bool {
110        self.has_emitted
111    }
112
113    fn name(&self) -> &'static str {
114        "Marubozu"
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use crate::traits::BatchExt;
122
123    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
124        Candle::new(open, high, low, close, 1.0, ts).unwrap()
125    }
126
127    #[test]
128    fn rejects_invalid_tolerance() {
129        assert!(Marubozu::with_tolerance(-0.01).is_err());
130        assert!(Marubozu::with_tolerance(1.0).is_err());
131        assert!(Marubozu::with_tolerance(2.0).is_err());
132    }
133
134    #[test]
135    fn accepts_valid_tolerance() {
136        let m = Marubozu::with_tolerance(0.0).unwrap();
137        assert!((m.shadow_tolerance() - 0.0).abs() < 1e-12);
138        let m = Marubozu::with_tolerance(0.5).unwrap();
139        assert!((m.shadow_tolerance() - 0.5).abs() < 1e-12);
140    }
141
142    #[test]
143    fn accessors_and_metadata() {
144        let m = Marubozu::default();
145        assert_eq!(m.name(), "Marubozu");
146        assert_eq!(m.warmup_period(), 1);
147        assert!(!m.is_ready());
148        assert!((m.shadow_tolerance() - 0.05).abs() < 1e-12);
149    }
150
151    #[test]
152    fn bullish_marubozu_is_plus_one() {
153        let mut m = Marubozu::new();
154        assert_eq!(m.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(1.0));
155    }
156
157    #[test]
158    fn bearish_marubozu_is_minus_one() {
159        let mut m = Marubozu::new();
160        assert_eq!(m.update(c(12.0, 12.0, 10.0, 10.0, 0)), Some(-1.0));
161    }
162
163    #[test]
164    fn candle_with_long_shadows_is_zero() {
165        let mut m = Marubozu::new();
166        // Big upper shadow violates tolerance.
167        assert_eq!(m.update(c(10.0, 15.0, 10.0, 12.0, 0)), Some(0.0));
168    }
169
170    #[test]
171    fn doji_is_zero() {
172        let mut m = Marubozu::new();
173        // body == 0 -> not a marubozu.
174        assert_eq!(m.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(0.0));
175    }
176
177    #[test]
178    fn zero_range_yields_zero() {
179        let mut m = Marubozu::new();
180        assert_eq!(m.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
181    }
182
183    #[test]
184    fn batch_equals_streaming() {
185        let candles: Vec<Candle> = (0..40)
186            .map(|i| {
187                let base = 100.0 + i as f64;
188                c(base, base + 2.0, base, base + 2.0, i)
189            })
190            .collect();
191        let mut a = Marubozu::new();
192        let mut b = Marubozu::new();
193        assert_eq!(
194            a.batch(&candles),
195            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
196        );
197    }
198
199    #[test]
200    fn reset_clears_state() {
201        let mut m = Marubozu::new();
202        m.update(c(10.0, 12.0, 10.0, 12.0, 0));
203        assert!(m.is_ready());
204        m.reset();
205        assert!(!m.is_ready());
206    }
207}