Skip to main content

wickra_core/indicators/
auto_fib.rs

1//! Auto-Fibonacci — retracement of the most significant recent swing leg.
2
3use crate::indicators::pattern_swing::{SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7/// How many recent pivots to consider when picking the dominant leg.
8const PIVOT_HISTORY: usize = 6;
9
10/// The seven canonical retracement ratios, in ascending order.
11const RATIOS: [f64; 7] = [0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0];
12
13/// Auto-Fibonacci retracement levels for the dominant recent swing leg.
14#[derive(Debug, Clone, Copy, PartialEq)]
15pub struct AutoFibOutput {
16    /// 0.0% — the dominant leg's end.
17    pub level_0: f64,
18    /// 23.6% retracement.
19    pub level_236: f64,
20    /// 38.2% retracement.
21    pub level_382: f64,
22    /// 50% retracement.
23    pub level_500: f64,
24    /// 61.8% retracement.
25    pub level_618: f64,
26    /// 78.6% retracement.
27    pub level_786: f64,
28    /// 100% — the dominant leg's start.
29    pub level_1000: f64,
30}
31
32/// Auto-Fibonacci (`AutoFib`).
33///
34/// Like [`crate::indicators::FibRetracement`], but instead of always using the
35/// immediate last leg it scans the last six confirmed pivots and anchors the
36/// retracement on the single largest-magnitude leg among them — the dominant
37/// swing the market is most likely respecting.
38///
39/// Parameter-free; construction is infallible. Returns `None` until two pivots
40/// have confirmed.
41///
42/// See `crates/wickra-core/src/indicators/auto_fib.rs`.
43/// # Example
44///
45/// ```
46/// use wickra_core::{AutoFib, Candle, Indicator};
47///
48/// let mut indicator = AutoFib::new();
49/// // `None` during warmup, then `Some(_)` once enough bars are seen.
50/// let mut out = None;
51/// for i in 0..40i64 {
52///     let p = 100.0 + (i as f64 * 0.4).sin() * 5.0;
53///     let candle = Candle::new(p, p + 1.5, p - 1.5, p + 0.3, 1_000.0, i).unwrap();
54///     out = indicator.update(candle);
55/// }
56/// let _ = out;
57/// ```
58#[derive(Debug, Clone)]
59pub struct AutoFib {
60    swing: SwingTracker,
61}
62
63impl AutoFib {
64    /// Construct a new Auto-Fibonacci tracker.
65    #[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    #[inline]
103    fn update(&mut self, candle: Candle) -> Option<AutoFibOutput> {
104        self.swing.update(candle);
105        self.levels()
106    }
107
108    fn reset(&mut self) {
109        self.swing.reset();
110    }
111
112    #[inline]
113    fn warmup_period(&self) -> usize {
114        2
115    }
116
117    #[inline]
118    fn is_ready(&self) -> bool {
119        self.swing.pivots().len() >= 2
120    }
121
122    #[inline]
123    fn name(&self) -> &'static str {
124        "AutoFib"
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    use crate::indicators::pattern_swing::candles_for_pivots;
132    use crate::traits::BatchExt;
133    use approx::assert_relative_eq;
134
135    #[test]
136    fn accessors_and_metadata() {
137        let indicator = AutoFib::new();
138        assert_eq!(indicator.name(), "AutoFib");
139        assert_eq!(indicator.warmup_period(), 2);
140        assert!(!indicator.is_ready());
141        assert!(!AutoFib::default().is_ready());
142    }
143
144    #[test]
145    fn no_output_before_two_pivots() {
146        let mut indicator = AutoFib::new();
147        let outputs: Vec<_> = candles_for_pivots(&[120.0])
148            .into_iter()
149            .map(|c| indicator.update(c))
150            .collect();
151        assert!(outputs.iter().all(Option::is_none));
152    }
153
154    #[test]
155    fn anchors_on_the_largest_leg() {
156        // Pivots: 130 -> 120 (small, 10) -> 220 (large, 100) -> 200 (small, 20).
157        // The dominant leg is 120 -> 220; its retracement spans [120, 220].
158        let mut indicator = AutoFib::new();
159        let mut last = None;
160        for candle in candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]) {
161            last = indicator.update(candle);
162        }
163        let v = last.unwrap();
164        assert!(indicator.is_ready());
165        // Largest leg 120 -> 220: 0% on 220 (end), 100% on 120 (start).
166        assert_relative_eq!(v.level_0, 220.0);
167        assert_relative_eq!(v.level_1000, 120.0);
168        assert_relative_eq!(v.level_500, 170.0);
169        assert_relative_eq!(v.level_618, 220.0 + 0.618 * (120.0 - 220.0));
170    }
171
172    #[test]
173    fn reset_clears_state() {
174        let mut indicator = AutoFib::new();
175        for candle in candles_for_pivots(&[200.0, 100.0]) {
176            let _ = indicator.update(candle);
177        }
178        assert!(indicator.is_ready());
179        indicator.reset();
180        assert!(!indicator.is_ready());
181        let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
182        assert!(indicator.update(c).is_none());
183    }
184
185    #[test]
186    fn batch_equals_streaming() {
187        let candles = candles_for_pivots(&[130.0, 120.0, 220.0, 200.0]);
188        let mut a = AutoFib::new();
189        let mut b = AutoFib::new();
190        assert_eq!(
191            a.batch(&candles),
192            candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
193        );
194    }
195}