Skip to main content

wickra_core/indicators/
doji.rs

1//! Doji candlestick pattern.
2
3use crate::error::{Error, Result};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// Doji — a candle whose body is negligible relative to its range.
8///
9/// A Doji prints whenever the absolute distance between open and close is
10/// small compared to the total `high − low` range. It is the canonical
11/// indecision bar and a building block for many three-bar reversal patterns.
12///
13/// ```text
14/// body  = |close − open|
15/// range = high − low
16/// doji  = body <= body_threshold * range
17/// ```
18///
19/// # Signed ±1 encoding
20///
21/// By default the output is `+1.0` when a Doji is detected and `0.0`
22/// otherwise — a direction-less detection flag. For a drop-in machine-learning
23/// feature where every candlestick pattern shares the same sign convention
24/// (`+1.0` bullish, `−1.0` bearish, `0.0` none), switch the detector into
25/// signed mode with [`Doji::signed`]. A detected Doji is then classified by
26/// where its (negligible) body sits within the bar's range:
27///
28/// ```text
29/// pos = (0.5 * (open + close) − low) / (high − low)
30/// pos > 2/3  ->  +1.0   dragonfly  (long lower shadow, bullish)
31/// pos < 1/3  ->  −1.0   gravestone (long upper shadow, bearish)
32/// else       ->   0.0   long-legged / standard (neutral)
33/// ```
34///
35/// Pattern-shape check only — no trend filter is applied; combine with a trend
36/// indicator for actionable signals.
37///
38/// # Example
39///
40/// ```
41/// use wickra_core::{Candle, Doji, Indicator};
42///
43/// // Default: direction-less detection flag.
44/// let mut indicator = Doji::default();
45/// let candle = Candle::new(10.0, 11.0, 9.0, 10.0, 1.0, 0).unwrap();
46/// assert_eq!(indicator.update(candle), Some(1.0));
47///
48/// // Signed: a dragonfly Doji (body at the top, long lower shadow) is bullish.
49/// let mut signed = Doji::new().signed();
50/// let dragonfly = Candle::new(10.0, 10.05, 6.0, 10.0, 1.0, 0).unwrap();
51/// assert_eq!(signed.update(dragonfly), Some(1.0));
52/// ```
53#[derive(Debug, Clone)]
54pub struct Doji {
55    body_threshold: f64,
56    signed: bool,
57    has_emitted: bool,
58}
59
60impl Default for Doji {
61    fn default() -> Self {
62        Self::new()
63    }
64}
65
66impl Doji {
67    /// Construct a Doji detector with the default body threshold (`0.1`).
68    pub const fn new() -> Self {
69        Self {
70            body_threshold: 0.1,
71            signed: false,
72            has_emitted: false,
73        }
74    }
75
76    /// Construct a Doji detector with a custom body / range threshold.
77    ///
78    /// `body_threshold` must lie in `(0, 1]`.
79    pub fn with_threshold(body_threshold: f64) -> Result<Self> {
80        if !(body_threshold > 0.0 && body_threshold <= 1.0) {
81            return Err(Error::InvalidPeriod {
82                message: "doji body threshold must lie in (0, 1]",
83            });
84        }
85        Ok(Self {
86            body_threshold,
87            signed: false,
88            has_emitted: false,
89        })
90    }
91
92    /// Switch to the signed dragonfly / gravestone encoding (consuming builder).
93    ///
94    /// In signed mode a detected Doji emits `+1.0` (dragonfly, bullish),
95    /// `−1.0` (gravestone, bearish) or `0.0` (long-legged / neutral) instead of
96    /// the default direction-less `+1.0` detection flag. See the type-level
97    /// docs for the exact classification rule.
98    #[must_use]
99    pub fn signed(mut self) -> Self {
100        self.signed = true;
101        self
102    }
103
104    /// Configured body / range threshold.
105    pub fn body_threshold(&self) -> f64 {
106        self.body_threshold
107    }
108
109    /// Whether this detector emits the signed dragonfly / gravestone encoding.
110    pub fn is_signed(&self) -> bool {
111        self.signed
112    }
113}
114
115impl Indicator for Doji {
116    type Input = Candle;
117    type Output = f64;
118
119    #[inline]
120    fn update(&mut self, candle: Candle) -> Option<f64> {
121        self.has_emitted = true;
122        let range = candle.high - candle.low;
123        if range <= 0.0 {
124            return Some(0.0);
125        }
126        let body = (candle.close - candle.open).abs();
127        if body > self.body_threshold * range {
128            return Some(0.0);
129        }
130        if !self.signed {
131            return Some(1.0);
132        }
133        // Signed mode: classify the Doji by where its (negligible) body sits
134        // within the high–low range.
135        let body_mid = f64::midpoint(candle.open, candle.close);
136        let pos = (body_mid - candle.low) / range;
137        if pos > 2.0 / 3.0 {
138            Some(1.0)
139        } else if pos < 1.0 / 3.0 {
140            Some(-1.0)
141        } else {
142            Some(0.0)
143        }
144    }
145
146    fn reset(&mut self) {
147        self.has_emitted = false;
148    }
149
150    #[inline]
151    fn warmup_period(&self) -> usize {
152        1
153    }
154
155    #[inline]
156    fn is_ready(&self) -> bool {
157        self.has_emitted
158    }
159
160    #[inline]
161    fn name(&self) -> &'static str {
162        "Doji"
163    }
164}
165
166#[cfg(test)]
167mod tests {
168    use super::*;
169    use crate::traits::BatchExt;
170
171    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
172        Candle::new(open, high, low, close, 1.0, ts).unwrap()
173    }
174
175    #[test]
176    fn rejects_invalid_threshold() {
177        assert!(Doji::with_threshold(0.0).is_err());
178        assert!(Doji::with_threshold(-0.1).is_err());
179        assert!(Doji::with_threshold(1.5).is_err());
180    }
181
182    #[test]
183    fn accepts_valid_threshold() {
184        let d = Doji::with_threshold(0.05).unwrap();
185        assert!((d.body_threshold() - 0.05).abs() < 1e-12);
186    }
187
188    #[test]
189    fn accessors_and_metadata() {
190        let d = Doji::default();
191        assert_eq!(d.name(), "Doji");
192        assert_eq!(d.warmup_period(), 1);
193        assert!(!d.is_ready());
194        assert!(!d.is_signed());
195        assert!((d.body_threshold() - 0.1).abs() < 1e-12);
196    }
197
198    #[test]
199    fn obvious_doji_is_one() {
200        let mut d = Doji::new();
201        // open == close, full range -> body / range = 0.
202        assert_eq!(d.update(c(10.0, 11.0, 9.0, 10.0, 0)), Some(1.0));
203        assert!(d.is_ready());
204    }
205
206    #[test]
207    fn marubozu_is_not_doji() {
208        // Big body, no shadows -> body / range = 1.0 > 0.1.
209        let mut d = Doji::new();
210        assert_eq!(d.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(0.0));
211    }
212
213    #[test]
214    fn zero_range_yields_zero() {
215        let mut d = Doji::new();
216        assert_eq!(d.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
217    }
218
219    #[test]
220    fn batch_equals_streaming() {
221        let candles: Vec<Candle> = (0..40)
222            .map(|i| {
223                let base = 100.0 + i as f64;
224                c(base, base + 2.0, base - 2.0, base + 1.0, i)
225            })
226            .collect();
227        let mut a = Doji::new();
228        let mut b = Doji::new();
229        assert_eq!(
230            a.batch(&candles),
231            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
232        );
233    }
234
235    #[test]
236    fn reset_clears_state() {
237        let mut d = Doji::new();
238        d.update(c(10.0, 11.0, 9.0, 10.0, 0));
239        assert!(d.is_ready());
240        d.reset();
241        assert!(!d.is_ready());
242    }
243
244    #[test]
245    fn signed_accessor_and_builder() {
246        let d = Doji::new().signed();
247        assert!(d.is_signed());
248        // The consuming builder composes with `with_threshold`.
249        let t = Doji::with_threshold(0.05).unwrap().signed();
250        assert!(t.is_signed());
251        assert!((t.body_threshold() - 0.05).abs() < 1e-12);
252    }
253
254    #[test]
255    fn signed_dragonfly_is_plus_one() {
256        // Body at the top of the range, long lower shadow -> bullish.
257        let mut d = Doji::new().signed();
258        assert_eq!(d.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(1.0));
259    }
260
261    #[test]
262    fn signed_gravestone_is_minus_one() {
263        // Body at the bottom of the range, long upper shadow -> bearish.
264        let mut d = Doji::new().signed();
265        assert_eq!(d.update(c(10.0, 14.0, 9.95, 10.0, 0)), Some(-1.0));
266    }
267
268    #[test]
269    fn signed_long_legged_is_zero() {
270        // Body centred, symmetric shadows -> neutral.
271        let mut d = Doji::new().signed();
272        assert_eq!(d.update(c(10.0, 12.0, 8.0, 10.0, 0)), Some(0.0));
273    }
274
275    #[test]
276    fn signed_non_doji_is_zero() {
277        // A large body is not a Doji at all -> 0 regardless of position.
278        let mut d = Doji::new().signed();
279        assert_eq!(d.update(c(10.0, 12.0, 10.0, 12.0, 0)), Some(0.0));
280    }
281
282    #[test]
283    fn signed_zero_range_is_zero() {
284        let mut d = Doji::new().signed();
285        assert_eq!(d.update(c(10.0, 10.0, 10.0, 10.0, 0)), Some(0.0));
286    }
287
288    #[test]
289    fn signed_batch_equals_streaming() {
290        let candles: Vec<Candle> = (0..40)
291            .map(|i| {
292                let base = 100.0 + i as f64;
293                // Alternate dragonfly / gravestone / centred Doji shapes.
294                match i % 3 {
295                    0 => c(base, base + 0.05, base - 4.0, base, i),
296                    1 => c(base, base + 4.0, base - 0.05, base, i),
297                    _ => c(base, base + 2.0, base - 2.0, base, i),
298                }
299            })
300            .collect();
301        let mut a = Doji::new().signed();
302        let mut b = Doji::new().signed();
303        assert_eq!(
304            a.batch(&candles),
305            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
306        );
307    }
308
309    #[test]
310    fn signed_survives_reset() {
311        let mut d = Doji::new().signed();
312        d.update(c(10.0, 10.05, 6.0, 10.0, 0));
313        assert!(d.is_ready());
314        d.reset();
315        assert!(!d.is_ready());
316        // `reset` clears only the streaming state, not the signed configuration.
317        assert!(d.is_signed());
318        assert_eq!(d.update(c(10.0, 10.05, 6.0, 10.0, 1)), Some(1.0));
319    }
320}