wickra_core/indicators/
takuri.rs1use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6#[derive(Debug, Clone, Default)]
43pub struct Takuri {
44 has_emitted: bool,
45}
46
47impl Takuri {
48 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 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 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 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}