wickra_core/indicators/
auto_fib.rs1use crate::indicators::pattern_swing::{SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7const PIVOT_HISTORY: usize = 6;
9
10const RATIOS: [f64; 7] = [0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0];
12
13#[derive(Debug, Clone, Copy, PartialEq)]
15pub struct AutoFibOutput {
16 pub level_0: f64,
18 pub level_236: f64,
20 pub level_382: f64,
22 pub level_500: f64,
24 pub level_618: f64,
26 pub level_786: f64,
28 pub level_1000: f64,
30}
31
32#[derive(Debug, Clone)]
59pub struct AutoFib {
60 swing: SwingTracker,
61}
62
63impl AutoFib {
64 #[must_use]
66 pub const fn new() -> Self {
67 Self {
68 swing: SwingTracker::new(SWING_THRESHOLD, PIVOT_HISTORY),
69 }
70 }
71
72 fn levels(&self) -> Option<AutoFibOutput> {
73 let dominant = self.swing.pivots().windows(2).max_by(|x, y| {
74 (x[0].price - x[1].price)
75 .abs()
76 .total_cmp(&(y[0].price - y[1].price).abs())
77 })?;
78 let (start, end) = (dominant[0].price, dominant[1].price);
79 let level = |r: f64| end + r * (start - end);
80 Some(AutoFibOutput {
81 level_0: level(RATIOS[0]),
82 level_236: level(RATIOS[1]),
83 level_382: level(RATIOS[2]),
84 level_500: level(RATIOS[3]),
85 level_618: level(RATIOS[4]),
86 level_786: level(RATIOS[5]),
87 level_1000: level(RATIOS[6]),
88 })
89 }
90}
91
92impl Default for AutoFib {
93 fn default() -> Self {
94 Self::new()
95 }
96}
97
98impl Indicator for AutoFib {
99 type Input = Candle;
100 type Output = AutoFibOutput;
101
102 fn update(&mut self, candle: Candle) -> Option<AutoFibOutput> {
103 self.swing.update(candle);
104 self.levels()
105 }
106
107 fn reset(&mut self) {
108 self.swing.reset();
109 }
110
111 fn warmup_period(&self) -> usize {
112 2
113 }
114
115 fn is_ready(&self) -> bool {
116 self.swing.pivots().len() >= 2
117 }
118
119 fn name(&self) -> &'static str {
120 "AutoFib"
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127 use crate::indicators::pattern_swing::candles_for_pivots;
128 use crate::traits::BatchExt;
129 use approx::assert_relative_eq;
130
131 #[test]
132 fn accessors_and_metadata() {
133 let indicator = AutoFib::new();
134 assert_eq!(indicator.name(), "AutoFib");
135 assert_eq!(indicator.warmup_period(), 2);
136 assert!(!indicator.is_ready());
137 assert!(!AutoFib::default().is_ready());
138 }
139
140 #[test]
141 fn no_output_before_two_pivots() {
142 let mut indicator = AutoFib::new();
143 let outputs: Vec<_> = candles_for_pivots(&[120.0])
144 .into_iter()
145 .map(|c| indicator.update(c))
146 .collect();
147 assert!(outputs.iter().all(Option::is_none));
148 }
149
150 #[test]
151 fn anchors_on_the_largest_leg() {
152 let mut indicator = AutoFib::new();
155 let mut last = None;
156 for candle in candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]) {
157 last = indicator.update(candle);
158 }
159 let v = last.unwrap();
160 assert!(indicator.is_ready());
161 assert_relative_eq!(v.level_0, 220.0);
163 assert_relative_eq!(v.level_1000, 120.0);
164 assert_relative_eq!(v.level_500, 170.0);
165 assert_relative_eq!(v.level_618, 220.0 + 0.618 * (120.0 - 220.0));
166 }
167
168 #[test]
169 fn reset_clears_state() {
170 let mut indicator = AutoFib::new();
171 for candle in candles_for_pivots(&[200.0, 100.0]) {
172 let _ = indicator.update(candle);
173 }
174 assert!(indicator.is_ready());
175 indicator.reset();
176 assert!(!indicator.is_ready());
177 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
178 assert!(indicator.update(c).is_none());
179 }
180
181 #[test]
182 fn batch_equals_streaming() {
183 let candles = candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]);
184 let mut a = AutoFib::new();
185 let mut b = AutoFib::new();
186 assert_eq!(
187 a.batch(&candles),
188 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
189 );
190 }
191}