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