wickra_core/indicators/
gravestone_doji.rs1use crate::ohlcv::Candle;
4use crate::traits::Indicator;
5
6#[derive(Debug, Clone, Default)]
41pub struct GravestoneDoji {
42 has_emitted: bool,
43}
44
45impl GravestoneDoji {
46 pub const fn new() -> Self {
48 Self { has_emitted: false }
49 }
50}
51
52impl Indicator for GravestoneDoji {
53 type Input = Candle;
54 type Output = f64;
55
56 #[inline]
57 fn update(&mut self, candle: Candle) -> Option<f64> {
58 self.has_emitted = true;
59 let range = candle.high - candle.low;
60 if range <= 0.0 {
61 return Some(0.0);
62 }
63 if (candle.close - candle.open).abs() > 0.1 * range {
64 return Some(0.0);
65 }
66 let upper = candle.high - candle.open.max(candle.close);
67 let lower = candle.open.min(candle.close) - candle.low;
68 if lower <= 0.1 * range && upper >= 0.5 * range {
69 return Some(-1.0);
70 }
71 Some(0.0)
72 }
73
74 fn reset(&mut self) {
75 self.has_emitted = false;
76 }
77
78 #[inline]
79 fn warmup_period(&self) -> usize {
80 1
81 }
82
83 #[inline]
84 fn is_ready(&self) -> bool {
85 self.has_emitted
86 }
87
88 #[inline]
89 fn name(&self) -> &'static str {
90 "GravestoneDoji"
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::traits::BatchExt;
98
99 fn c(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
100 Candle::new(open, high, low, close, 1.0, ts).unwrap()
101 }
102
103 #[test]
104 fn accessors_and_metadata() {
105 let t = GravestoneDoji::new();
106 assert_eq!(t.name(), "GravestoneDoji");
107 assert_eq!(t.warmup_period(), 1);
108 assert!(!t.is_ready());
109 }
110
111 #[test]
112 fn gravestone_is_minus_one() {
113 let mut t = GravestoneDoji::new();
114 assert_eq!(t.update(c(10.0, 14.0, 9.95, 10.0, 0)), Some(-1.0));
115 }
116
117 #[test]
118 fn lower_shadow_yields_zero() {
119 let mut t = GravestoneDoji::new();
120 assert_eq!(t.update(c(10.0, 10.05, 6.0, 10.0, 0)), Some(0.0));
122 }
123
124 #[test]
125 fn short_upper_shadow_yields_zero() {
126 let mut t = GravestoneDoji::new();
127 assert_eq!(t.update(c(10.0, 10.4, 9.95, 10.0, 0)), Some(0.0));
129 }
130
131 #[test]
132 fn non_doji_yields_zero() {
133 let mut t = GravestoneDoji::new();
134 assert_eq!(t.update(c(10.0, 14.0, 9.5, 13.5, 0)), Some(0.0));
135 }
136
137 #[test]
138 fn zero_range_yields_zero() {
139 let mut t = GravestoneDoji::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 + 4.0, base - 0.05, base, i)
149 })
150 .collect();
151 let mut a = GravestoneDoji::new();
152 let mut b = GravestoneDoji::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 = GravestoneDoji::new();
162 t.update(c(10.0, 14.0, 9.95, 10.0, 0));
163 assert!(t.is_ready());
164 t.reset();
165 assert!(!t.is_ready());
166 }
167}