wickra_core/indicators/
fib_arcs.rs1use crate::indicators::pattern_swing::{SwingTracker, SWING_THRESHOLD};
5use crate::ohlcv::Candle;
6use crate::traits::Indicator;
7
8const RATIOS: [f64; 3] = [0.382, 0.5, 0.618];
10
11#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct FibArcsOutput {
14 pub arc_382: f64,
16 pub arc_500: f64,
18 pub arc_618: f64,
20}
21
22#[derive(Debug, Clone)]
40pub struct FibArcs {
41 swing: SwingTracker,
42}
43
44impl FibArcs {
45 #[must_use]
47 pub const fn new() -> Self {
48 Self {
49 swing: SwingTracker::new(SWING_THRESHOLD, 2),
50 }
51 }
52
53 fn arcs(&self) -> Option<FibArcsOutput> {
54 let pivots = self.swing.pivots();
55 let start = pivots.first()?;
56 let end = pivots.get(1)?;
57 let span_bars = (end.bar - start.bar) as f64;
59 let u = (self.swing.current_bar() - end.bar) as f64 / span_bars;
60 let curve = (1.0 - u * u).max(0.0).sqrt();
61 let arc = |r: f64| end.price + (start.price - end.price) * r * curve;
62 Some(FibArcsOutput {
63 arc_382: arc(RATIOS[0]),
64 arc_500: arc(RATIOS[1]),
65 arc_618: arc(RATIOS[2]),
66 })
67 }
68}
69
70impl Default for FibArcs {
71 fn default() -> Self {
72 Self::new()
73 }
74}
75
76impl Indicator for FibArcs {
77 type Input = Candle;
78 type Output = FibArcsOutput;
79
80 fn update(&mut self, candle: Candle) -> Option<FibArcsOutput> {
81 self.swing.update(candle);
82 self.arcs()
83 }
84
85 fn reset(&mut self) {
86 self.swing.reset();
87 }
88
89 fn warmup_period(&self) -> usize {
90 2
91 }
92
93 fn is_ready(&self) -> bool {
94 self.swing.pivots().len() >= 2
95 }
96
97 fn name(&self) -> &'static str {
98 "FibArcs"
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105 use crate::traits::BatchExt;
106 use approx::assert_relative_eq;
107
108 fn c(high: f64, low: f64, ts: i64) -> Candle {
109 Candle::new(low, high, low, low, 1.0, ts).unwrap()
110 }
111
112 fn down_leg() -> Vec<Candle> {
115 vec![
116 c(200.0, 199.0, 0),
117 c(190.0, 160.0, 1), c(150.0, 100.0, 2), c(110.0, 105.0, 3), ]
121 }
122
123 #[test]
124 fn accessors_and_metadata() {
125 let indicator = FibArcs::new();
126 assert_eq!(indicator.name(), "FibArcs");
127 assert_eq!(indicator.warmup_period(), 2);
128 assert!(!indicator.is_ready());
129 assert!(!FibArcs::default().is_ready());
130 }
131
132 #[test]
133 fn no_output_before_two_pivots() {
134 let mut indicator = FibArcs::new();
135 let outputs: Vec<_> = [c(200.0, 199.0, 0), c(190.0, 150.0, 1)]
136 .into_iter()
137 .map(|x| indicator.update(x))
138 .collect();
139 assert!(outputs.iter().all(Option::is_none));
140 assert!(!indicator.is_ready());
141 }
142
143 #[test]
144 fn arcs_curve_back_toward_the_swing_end() {
145 let mut indicator = FibArcs::new();
146 let mut last = None;
147 for candle in down_leg() {
148 last = indicator.update(candle);
149 }
150 let v = last.unwrap();
151 assert!(indicator.is_ready());
152 let curve = 0.75_f64.sqrt();
154 assert_relative_eq!(v.arc_382, 100.0 + 100.0 * 0.382 * curve);
155 assert_relative_eq!(v.arc_500, 100.0 + 100.0 * 0.5 * curve);
156 assert_relative_eq!(v.arc_618, 100.0 + 100.0 * 0.618 * curve);
157 }
158
159 #[test]
160 fn arc_clamps_to_zero_beyond_one_leg_width() {
161 let mut indicator = FibArcs::new();
164 for candle in down_leg() {
165 let _ = indicator.update(candle);
166 }
167 let mut last = None;
169 for ts in 4..12 {
170 last = indicator.update(c(108.0, 106.0, ts));
171 }
172 let v = last.unwrap();
173 assert_relative_eq!(v.arc_382, 100.0);
174 assert_relative_eq!(v.arc_618, 100.0);
175 }
176
177 #[test]
178 fn reset_clears_state() {
179 let mut indicator = FibArcs::new();
180 for candle in down_leg() {
181 let _ = indicator.update(candle);
182 }
183 indicator.reset();
184 assert!(!indicator.is_ready());
185 assert!(indicator.update(c(100.0, 99.5, 0)).is_none());
186 }
187
188 #[test]
189 fn batch_equals_streaming() {
190 let candles = down_leg();
191 let mut a = FibArcs::new();
192 let mut b = FibArcs::new();
193 assert_eq!(
194 a.batch(&candles),
195 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
196 );
197 }
198}