Skip to main content

wickra_core/indicators/
gravestone_doji.rs

1//! Gravestone Doji candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Gravestone Doji — a single-bar bearish reversal. Open, close, and low sit at
7/// the bottom of the bar while a long upper shadow shows price was pushed up hard
8/// and then sold all the way back to the open — sellers rejecting the highs.
9///
10/// ```text
11/// range = high − low
12/// doji          = |close − open| <= 0.1 * range
13/// no lower wick = min(open, close) − low   <= 0.1 * range
14/// long upper    = high − max(open, close)  >= 0.5 * range
15/// ```
16///
17/// Output is `−1.0` when the gravestone prints and `0.0` otherwise. Gravestone
18/// Doji is a single-direction (bearish-only) shape, so it never emits `+1.0`.
19/// Body and shadow thresholds follow the geometric house style (fixed fractions
20/// of the bar range) rather than TA-Lib's rolling averages. Pattern-shape check
21/// only — no trend filter is applied; combine with a trend indicator for
22/// actionable signals.
23///
24/// # Signed ±1 encoding
25///
26/// This detector emits the uniform candlestick sign convention shared across the
27/// pattern family — `−1.0` bearish, `0.0` no pattern — so it drops straight into
28/// a machine-learning feature matrix as a single dimension.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{Candle, GravestoneDoji, Indicator};
34///
35/// let mut indicator = GravestoneDoji::new();
36/// // Body at the bottom, long upper shadow.
37/// let candle = Candle::new(10.0, 14.0, 9.95, 10.0, 1.0, 0).unwrap();
38/// assert_eq!(indicator.update(candle), Some(-1.0));
39/// ```
40#[derive(Debug, Clone, Default)]
41pub struct GravestoneDoji {
42    has_emitted: bool,
43}
44
45impl GravestoneDoji {
46    /// Construct a new Gravestone Doji detector.
47    pub const fn new() -> Self {
48        Self { has_emitted: false }
49    }
50}
51
52impl Indicator for GravestoneDoji {
53    type Input = Candle;
54    type Output = f64;
55
56    #[inline]
57    fn update(&mut self, candle: Candle) -> Option<f64> {
58        self.has_emitted = true;
59        let range = candle.high - candle.low;
60        if range <= 0.0 {
61            return Some(0.0);
62        }
63        if (candle.close - candle.open).abs() > 0.1 * range {
64            return Some(0.0);
65        }
66        let upper = candle.high - candle.open.max(candle.close);
67        let lower = candle.open.min(candle.close) - candle.low;
68        if lower <= 0.1 * range && upper >= 0.5 * range {
69            return Some(-1.0);
70        }
71        Some(0.0)
72    }
73
74    fn reset(&mut self) {
75        self.has_emitted = false;
76    }
77
78    #[inline]
79    fn warmup_period(&self) -> usize {
80        1
81    }
82
83    #[inline]
84    fn is_ready(&self) -> bool {
85        self.has_emitted
86    }
87
88    #[inline]
89    fn name(&self) -> &'static str {
90        "GravestoneDoji"
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use crate::traits::BatchExt;
98
99    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
100        Candle::new(open, high, low, close, 1.0, ts).unwrap()
101    }
102
103    #[test]
104    fn accessors_and_metadata() {
105        let t = GravestoneDoji::new();
106        assert_eq!(t.name(), "GravestoneDoji");
107        assert_eq!(t.warmup_period(), 1);
108        assert!(!t.is_ready());
109    }
110
111    #[test]
112    fn gravestone_is_minus_one() {
113        let mut t = GravestoneDoji::new();
114        assert_eq!(t.update(c(10.0, 14.0, 9.95, 10.0, 0)), Some(-1.0));
115    }
116
117    #[test]
118    fn lower_shadow_yields_zero() {
119        let mut t = GravestoneDoji::new();
120        // Long lower shadow -> not a gravestone (this is a dragonfly shape).
121        assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
122    }
123
124    #[test]
125    fn short_upper_shadow_yields_zero() {
126        let mut t = GravestoneDoji::new();
127        // Body at the bottom but the upper shadow is too short.
128        assert_eq!(t.update(c(10.0, 10.4, 9.95, 10.0, 0)), Some(0.0));
129    }
130
131    #[test]
132    fn non_doji_yields_zero() {
133        let mut t = GravestoneDoji::new();
134        assert_eq!(t.update(c(10.0, 14.0, 9.5, 13.5, 0)), Some(0.0));
135    }
136
137    #[test]
138    fn zero_range_yields_zero() {
139        let mut t = GravestoneDoji::new();
140        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
141    }
142
143    #[test]
144    fn batch_equals_streaming() {
145        let candles: Vec<Candle> = (0..40)
146            .map(|i| {
147                let base = 100.0 + i as f64;
148                c(base, base + 4.0, base - 0.05, base, i)
149            })
150            .collect();
151        let mut a = GravestoneDoji::new();
152        let mut b = GravestoneDoji::new();
153        assert_eq!(
154            a.batch(&candles),
155            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
156        );
157    }
158
159    #[test]
160    fn reset_clears_state() {
161        let mut t = GravestoneDoji::new();
162        t.update(c(10.0, 14.0, 9.95, 10.0, 0));
163        assert!(t.is_ready());
164        t.reset();
165        assert!(!t.is_ready());
166    }
167}