wickra_core/indicators/
fib_confluence.rs1use crate::indicators::pattern_swing::{
4 approx_equal, SwingTracker, LEVEL_TOLERANCE, SWING_THRESHOLD,
5};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9const PIVOT_HISTORY: usize = 6;
11
12const RATIOS: [f64; 3] = [0.382, 0.5, 0.618];
14
15#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct FibConfluenceOutput {
18 pub price: f64,
20 pub strength: f64,
22}
23
24#[derive(Debug, Clone)]
52pub struct FibConfluence {
53 swing: SwingTracker,
54}
55
56impl FibConfluence {
57 #[must_use]
59 pub const fn new() -> Self {
60 Self {
61 swing: SwingTracker::new(SWING_THRESHOLD, PIVOT_HISTORY),
62 }
63 }
64
65 fn confluence(&self) -> Option<FibConfluenceOutput> {
66 let pivots = self.swing.pivots();
67 if pivots.len() < 3 {
68 return None;
69 }
70 let levels: Vec<f64> = pivots
71 .windows(2)
72 .flat_map(|leg| {
73 let (start, end) = (leg[0].price, leg[1].price);
74 RATIOS.map(|r| end + r * (start - end))
75 })
76 .collect();
77 let (count, total) = levels
80 .iter()
81 .map(|¢er| {
82 let members: Vec<f64> = levels
83 .iter()
84 .copied()
85 .filter(|&x| approx_equal(x, center, LEVEL_TOLERANCE))
86 .collect();
87 (members.len(), members.iter().sum::<f64>())
88 })
89 .max_by(|a, b| a.0.cmp(&b.0))
90 .expect("at least two legs guarantee a non-empty level set");
91 Some(FibConfluenceOutput {
92 price: total / count as f64,
93 strength: count as f64,
94 })
95 }
96}
97
98impl Default for FibConfluence {
99 fn default() -> Self {
100 Self::new()
101 }
102}
103
104impl Indicator for FibConfluence {
105 type Input = Candle;
106 type Output = FibConfluenceOutput;
107
108 fn update(&mut self, candle: Candle) -> Option<FibConfluenceOutput> {
109 self.swing.update(candle);
110 self.confluence()
111 }
112
113 fn reset(&mut self) {
114 self.swing.reset();
115 }
116
117 fn warmup_period(&self) -> usize {
118 3
119 }
120
121 fn is_ready(&self) -> bool {
122 self.swing.pivots().len() >= 3
123 }
124
125 fn name(&self) -> &'static str {
126 "FibConfluence"
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133 use crate::indicators::pattern_swing::candles_for_pivots;
134 use crate::traits::BatchExt;
135 use approx::assert_relative_eq;
136
137 #[test]
138 fn accessors_and_metadata() {
139 let indicator = FibConfluence::new();
140 assert_eq!(indicator.name(), "FibConfluence");
141 assert_eq!(indicator.warmup_period(), 3);
142 assert!(!indicator.is_ready());
143 assert!(!FibConfluence::default().is_ready());
144 }
145
146 #[test]
147 fn no_output_before_two_legs() {
148 let mut indicator = FibConfluence::new();
149 let outputs: Vec<_> = candles_for_pivots(&[200.0, 100.0])
150 .into_iter()
151 .map(|c| indicator.update(c))
152 .collect();
153 assert!(outputs.iter().all(Option::is_none));
154 assert!(!indicator.is_ready());
155 }
156
157 #[test]
158 fn picks_the_densest_cluster() {
159 let mut indicator = FibConfluence::new();
162 let mut last = None;
163 for candle in candles_for_pivots(&[200.0, 100.0, 160.0]) {
164 last = indicator.update(candle);
165 }
166 let v = last.unwrap();
167 assert!(indicator.is_ready());
168 assert_relative_eq!(v.strength, 2.0);
169 let want = (138.2 + (160.0 + 0.382 * (100.0 - 160.0))) / 2.0;
170 assert_relative_eq!(v.price, want, epsilon = 1e-9);
171 }
172
173 #[test]
174 fn reset_clears_state() {
175 let mut indicator = FibConfluence::new();
176 for candle in candles_for_pivots(&[200.0, 100.0, 160.0]) {
177 let _ = indicator.update(candle);
178 }
179 assert!(indicator.is_ready());
180 indicator.reset();
181 assert!(!indicator.is_ready());
182 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
183 assert!(indicator.update(c).is_none());
184 }
185
186 #[test]
187 fn batch_equals_streaming() {
188 let candles = candles_for_pivots(&[200.0, 100.0, 160.0, 120.0]);
189 let mut a = FibConfluence::new();
190 let mut b = FibConfluence::new();
191 assert_eq!(
192 a.batch(&candles),
193 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
194 );
195 }
196}