Skip to main content

wickra_core/indicators/
takuri.rs

1//! Takuri candlestick pattern.
2
3use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6/// Takuri — a single-bar bullish reversal, a stricter Dragonfly Doji. Open, close,
7/// and high sit at the very top of the bar with a negligible upper shadow, while an
8/// exceptionally long lower shadow shows price was driven sharply down and then bid
9/// all the way back — an emphatic rejection of the lows.
10///
11/// ```text
12/// range = high − low
13/// doji            = |close − open| <= 0.1 * range
14/// negligible upper = high − max(open, close) <= 0.05 * range
15/// very long lower  = min(open, close) − low   >= 0.7  * range
16/// ```
17///
18/// Output is `+1.0` when the Takuri prints and `0.0` otherwise. Takuri is a
19/// single-direction (bullish-only) shape, so it never emits `−1.0`. Its tighter
20/// upper-shadow and longer lower-shadow thresholds make it a strict subset of
21/// [`crate::DragonflyDoji`]. Body and shadow thresholds follow the geometric house
22/// style (fixed fractions of the bar range) rather than TA-Lib's rolling averages.
23/// Pattern-shape check only — no trend filter is applied; combine with a trend
24/// indicator for actionable signals.
25///
26/// # Signed ±1 encoding
27///
28/// This detector emits the uniform candlestick sign convention shared across the
29/// pattern family — `+1.0` bullish, `0.0` no pattern — so it drops straight into
30/// a machine-learning feature matrix as a single dimension.
31///
32/// # Example
33///
34/// ```
35/// use wickra_core::{Candle, Indicator, Takuri};
36///
37/// let mut indicator = Takuri::new();
38/// // Body at the top, very long lower shadow.
39/// let candle = Candle::new(10.0, 10.05, 7.0, 10.0, 1.0, 0).unwrap();
40/// assert_eq!(indicator.update(candle), Some(1.0));
41/// ```
42#[derive(Debug, Clone, Default)]
43pub struct Takuri {
44    has_emitted: bool,
45}
46
47impl Takuri {
48    /// Construct a new Takuri detector.
49    pub const fn new() -> Self {
50        Self { has_emitted: false }
51    }
52}
53
54impl Indicator for Takuri {
55    type Input = Candle;
56    type Output = f64;
57
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.05 * range && lower >= 0.7 * 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    fn warmup_period(&self) -> usize {
80        1
81    }
82
83    fn is_ready(&self) -> bool {
84        self.has_emitted
85    }
86
87    fn name(&self) -> &'static str {
88        "Takuri"
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use crate::traits::BatchExt;
96
97    fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
98        Candle::new(open, high, low, close, 1.0, ts).unwrap()
99    }
100
101    #[test]
102    fn accessors_and_metadata() {
103        let t = Takuri::new();
104        assert_eq!(t.name(), "Takuri");
105        assert_eq!(t.warmup_period(), 1);
106        assert!(!t.is_ready());
107    }
108
109    #[test]
110    fn takuri_is_plus_one() {
111        let mut t = Takuri::new();
112        assert_eq!(t.update(c(10.0, 10.05, 7.0, 10.0, 0)), Some(1.0));
113    }
114
115    #[test]
116    fn non_doji_body_yields_zero() {
117        let mut t = Takuri::new();
118        // Large body -> not a doji.
119        assert_eq!(t.update(c(10.0, 12.0, 7.0, 11.5, 0)), Some(0.0));
120    }
121
122    #[test]
123    fn upper_shadow_yields_zero() {
124        let mut t = Takuri::new();
125        // Long upper shadow -> not a Takuri.
126        assert_eq!(t.update(c(10.0, 14.0, 7.0, 10.0, 0)), Some(0.0));
127    }
128
129    #[test]
130    fn dragonfly_but_not_takuri_yields_zero() {
131        let mut t = Takuri::new();
132        // Upper shadow ~0.07 of range: a Dragonfly Doji, but exceeds Takuri's
133        // tighter 0.05 ceiling.
134        assert_eq!(t.update(c(10.0, 10.24, 7.0, 10.0, 0)), Some(0.0));
135    }
136
137    #[test]
138    fn zero_range_yields_zero() {
139        let mut t = Takuri::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 + 0.02, base - 4.0, base, i)
149            })
150            .collect();
151        let mut a = Takuri::new();
152        let mut b = Takuri::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 = Takuri::new();
162        t.update(c(10.0, 10.05, 7.0, 10.0, 0));
163        assert!(t.is_ready());
164        t.reset();
165        assert!(!t.is_ready());
166    }
167}