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    #[inline]
58    fn update(&mut self, candle: Candle) -> Option<f64> {
59        self.has_emitted = true;
60        let range = candle.high - candle.low;
61        if range <= 0.0 {
62            return Some(0.0);
63        }
64        if (candle.close - candle.open).abs() > 0.1 * range {
65            return Some(0.0);
66        }
67        let upper = candle.high - candle.open.max(candle.close);
68        let lower = candle.open.min(candle.close) - candle.low;
69        if upper >= 0.3 * range && lower >= 0.3 * range {
70            return Some(1.0);
71        }
72        Some(0.0)
73    }
74
75    fn reset(&mut self) {
76        self.has_emitted = false;
77    }
78
79    #[inline]
80    fn warmup_period(&self) -> usize {
81        1
82    }
83
84    #[inline]
85    fn is_ready(&self) -> bool {
86        self.has_emitted
87    }
88
89    #[inline]
90    fn name(&self) -> &'static str {
91        "LongLeggedDoji"
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::traits::BatchExt;
99
100    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
101        Candle::new(open, high, low, close, 1.0, ts).unwrap()
102    }
103
104    #[test]
105    fn accessors_and_metadata() {
106        let t = LongLeggedDoji::new();
107        assert_eq!(t.name(), "LongLeggedDoji");
108        assert_eq!(t.warmup_period(), 1);
109        assert!(!t.is_ready());
110    }
111
112    #[test]
113    fn long_legged_is_plus_one() {
114        let mut t = LongLeggedDoji::new();
115        assert_eq!(t.update(c(10.0, 12.0, 8.0, 10.05, 0)), Some(1.0));
116    }
117
118    #[test]
119    fn one_sided_shadow_yields_zero() {
120        let mut t = LongLeggedDoji::new();
121        // Dragonfly shape: long lower shadow but no upper -> not long-legged.
122        assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
123    }
124
125    #[test]
126    fn non_doji_yields_zero() {
127        let mut t = LongLeggedDoji::new();
128        assert_eq!(t.update(c(10.0, 12.0, 8.0, 11.5, 0)), Some(0.0));
129    }
130
131    #[test]
132    fn zero_range_yields_zero() {
133        let mut t = LongLeggedDoji::new();
134        assert_eq!(t.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
135    }
136
137    #[test]
138    fn batch_equals_streaming() {
139        let candles: Vec<Candle> = (0..40)
140            .map(|i| {
141                let base = 100.0 + i as f64;
142                c(base, base + 3.0, base - 3.0, base + 0.05, i)
143            })
144            .collect();
145        let mut a = LongLeggedDoji::new();
146        let mut b = LongLeggedDoji::new();
147        assert_eq!(
148            a.batch(&candles),
149            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
150        );
151    }
152
153    #[test]
154    fn reset_clears_state() {
155        let mut t = LongLeggedDoji::new();
156        t.update(c(10.0, 12.0, 8.0, 10.05, 0));
157        assert!(t.is_ready());
158        t.reset();
159        assert!(!t.is_ready());
160    }
161}