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 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 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 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 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}