Skip to main content

wickra_core/indicators/
long_legged_doji.rs

1//! Long-Legged Doji candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Long-Legged Doji — a single-bar indecision signal. A doji with long shadows on
7/// *both* sides: price ranged widely up and down yet closed essentially where it
8/// opened, a tug-of-war that often precedes a turn.
9///
10/// ```text
11/// range = high − low
12/// doji        = |close − open| <= 0.1 * range
13/// long upper  = high − max(open, close) >= 0.3 * range
14/// long lower  = min(open, close) − low  >= 0.3 * range
15/// ```
16///
17/// Output is `+1.0` when the long-legged doji prints and `0.0` otherwise. This is
18/// a non-directional indecision flag — it never emits `−1.0` (use
19/// `DragonflyDoji` / `GravestoneDoji` for the directional single-shadow variants).
20/// Body and shadow thresholds follow the geometric house style (fixed fractions
21/// of the bar range) rather than TA-Lib's rolling averages. Pattern-shape check
22/// only — no trend filter is applied; combine with a trend indicator for
23/// actionable signals.
24///
25/// # Signed ±1 encoding
26///
27/// This detector emits the uniform candlestick sign convention shared across the
28/// pattern family — `+1.0` detected, `0.0` no pattern — so it drops straight into
29/// a machine-learning feature matrix as a single dimension.
30///
31/// # Example
32///
33/// ```
34/// use wickra_core::{Candle, LongLeggedDoji, Indicator};
35///
36/// let mut indicator = LongLeggedDoji::new();
37/// // Tiny body, long shadows on both sides.
38/// let candle = Candle::new(10.0, 12.0, 8.0, 10.05, 1.0, 0).unwrap();
39/// assert_eq!(indicator.update(candle), Some(1.0));
40/// ```
41#[derive(Debug, Clone, Default)]
42pub struct LongLeggedDoji {
43    has_emitted: bool,
44}
45
46impl LongLeggedDoji {
47    /// Construct a new Long-Legged Doji detector.
48    pub const fn new() -> Self {
49        Self { has_emitted: false }
50    }
51}
52
53impl Indicator for LongLeggedDoji {
54    type Input = Candle;
55    type Output = f64;
56
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 upper >= 0.3 * range && lower >= 0.3 * 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    fn warmup_period(&self) -> usize {
79        1
80    }
81
82    fn is_ready(&self) -> bool {
83        self.has_emitted
84    }
85
86    fn name(&self) -> &'static str {
87        "LongLeggedDoji"
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use crate::traits::BatchExt;
95
96    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
97        Candle::new(open, high, low, close, 1.0, ts).unwrap()
98    }
99
100    #[test]
101    fn accessors_and_metadata() {
102        let t = LongLeggedDoji::new();
103        assert_eq!(t.name(), "LongLeggedDoji");
104        assert_eq!(t.warmup_period(), 1);
105        assert!(!t.is_ready());
106    }
107
108    #[test]
109    fn long_legged_is_plus_one() {
110        let mut t = LongLeggedDoji::new();
111        assert_eq!(t.update(c(10.0, 12.0, 8.0, 10.05, 0)), Some(1.0));
112    }
113
114    #[test]
115    fn one_sided_shadow_yields_zero() {
116        let mut t = LongLeggedDoji::new();
117        // Dragonfly shape: long lower shadow but no upper -> not long-legged.
118        assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
119    }
120
121    #[test]
122    fn non_doji_yields_zero() {
123        let mut t = LongLeggedDoji::new();
124        assert_eq!(t.update(c(10.0, 12.0, 8.0, 11.5, 0)), Some(0.0));
125    }
126
127    #[test]
128    fn zero_range_yields_zero() {
129        let mut t = LongLeggedDoji::new();
130        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
131    }
132
133    #[test]
134    fn batch_equals_streaming() {
135        let candles: Vec<Candle> = (0..40)
136            .map(|i| {
137                let base = 100.0 + i as f64;
138                c(base, base + 3.0, base - 3.0, base + 0.05, i)
139            })
140            .collect();
141        let mut a = LongLeggedDoji::new();
142        let mut b = LongLeggedDoji::new();
143        assert_eq!(
144            a.batch(&candles),
145            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
146        );
147    }
148
149    #[test]
150    fn reset_clears_state() {
151        let mut t = LongLeggedDoji::new();
152        t.update(c(10.0, 12.0, 8.0, 10.05, 0));
153        assert!(t.is_ready());
154        t.reset();
155        assert!(!t.is_ready());
156    }
157}