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)]
44pub struct AutoFib {
45 swing: SwingTracker,
46}
47
48impl AutoFib {
49 #[must_use]
51 pub const fn new() -> Self {
52 Self {
53 swing: SwingTracker::new(SWING_THRESHOLD, PIVOT_HISTORY),
54 }
55 }
56
57 fn levels(&self) -> Option<AutoFibOutput> {
58 let dominant = self.swing.pivots().windows(2).max_by(|x, y| {
59 (x[0].price - x[1].price)
60 .abs()
61 .total_cmp(&(y[0].price - y[1].price).abs())
62 })?;
63 let (start, end) = (dominant[0].price, dominant[1].price);
64 let level = |r: f64| end + r * (start - end);
65 Some(AutoFibOutput {
66 level_0: level(RATIOS[0]),
67 level_236: level(RATIOS[1]),
68 level_382: level(RATIOS[2]),
69 level_500: level(RATIOS[3]),
70 level_618: level(RATIOS[4]),
71 level_786: level(RATIOS[5]),
72 level_1000: level(RATIOS[6]),
73 })
74 }
75}
76
77impl Default for AutoFib {
78 fn default() -> Self {
79 Self::new()
80 }
81}
82
83impl Indicator for AutoFib {
84 type Input = Candle;
85 type Output = AutoFibOutput;
86
87 fn update(&mut self, candle: Candle) -> Option<AutoFibOutput> {
88 self.swing.update(candle);
89 self.levels()
90 }
91
92 fn reset(&mut self) {
93 self.swing.reset();
94 }
95
96 fn warmup_period(&self) -> usize {
97 2
98 }
99
100 fn is_ready(&self) -> bool {
101 self.swing.pivots().len() >= 2
102 }
103
104 fn name(&self) -> &'static str {
105 "AutoFib"
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use crate::indicators::pattern_swing::candles_for_pivots;
113 use crate::traits::BatchExt;
114 use approx::assert_relative_eq;
115
116 #[test]
117 fn accessors_and_metadata() {
118 let indicator = AutoFib::new();
119 assert_eq!(indicator.name(), "AutoFib");
120 assert_eq!(indicator.warmup_period(), 2);
121 assert!(!indicator.is_ready());
122 assert!(!AutoFib::default().is_ready());
123 }
124
125 #[test]
126 fn no_output_before_two_pivots() {
127 let mut indicator = AutoFib::new();
128 let outputs: Vec<_> = candles_for_pivots(&[120.0])
129 .into_iter()
130 .map(|c| indicator.update(c))
131 .collect();
132 assert!(outputs.iter().all(Option::is_none));
133 }
134
135 #[test]
136 fn anchors_on_the_largest_leg() {
137 let mut indicator = AutoFib::new();
140 let mut last = None;
141 for candle in candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]) {
142 last = indicator.update(candle);
143 }
144 let v = last.unwrap();
145 assert!(indicator.is_ready());
146 assert_relative_eq!(v.level_0, 220.0);
148 assert_relative_eq!(v.level_1000, 120.0);
149 assert_relative_eq!(v.level_500, 170.0);
150 assert_relative_eq!(v.level_618, 220.0 + 0.618 * (120.0 - 220.0));
151 }
152
153 #[test]
154 fn reset_clears_state() {
155 let mut indicator = AutoFib::new();
156 for candle in candles_for_pivots(&[200.0, 100.0]) {
157 let _ = indicator.update(candle);
158 }
159 assert!(indicator.is_ready());
160 indicator.reset();
161 assert!(!indicator.is_ready());
162 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
163 assert!(indicator.update(c).is_none());
164 }
165
166 #[test]
167 fn batch_equals_streaming() {
168 let candles = candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]);
169 let mut a = AutoFib::new();
170 let mut b = AutoFib::new();
171 assert_eq!(
172 a.batch(&candles),
173 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
174 );
175 }
176}