wickra_core/indicators/
wedge.rs1use crate::indicators::pattern_swing::{recent_legs, SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7#[derive(Debug, Clone)]
23pub struct Wedge {
24 swing: SwingTracker,
25 has_emitted: bool,
26}
27
28impl Wedge {
29 pub const fn new() -> Self {
31 Self {
32 swing: SwingTracker::new(SWING_THRESHOLD, 4),
33 has_emitted: false,
34 }
35 }
36}
37
38impl Default for Wedge {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl Indicator for Wedge {
45 type Input = Candle;
46 type Output = f64;
47
48 fn update(&mut self, candle: Candle) -> Option<f64> {
49 self.has_emitted = true;
50 if !self.swing.update(candle) {
51 return Some(0.0);
52 }
53 let pivots = self.swing.pivots();
54 if pivots.len() < 4 {
55 return Some(0.0);
56 }
57 let (high_old, high_new, low_old, low_new) = recent_legs(pivots);
58 let high_slope = high_new - high_old;
59 let low_slope = low_new - low_old;
60
61 if high_slope > 0.0 && low_slope > 0.0 && low_slope > high_slope {
63 return Some(-1.0);
64 }
65 if high_slope < 0.0 && low_slope < 0.0 && high_slope < low_slope {
67 return Some(1.0);
68 }
69 Some(0.0)
70 }
71
72 fn reset(&mut self) {
73 self.swing.reset();
74 self.has_emitted = false;
75 }
76
77 fn warmup_period(&self) -> usize {
78 5
80 }
81
82 fn is_ready(&self) -> bool {
83 self.has_emitted
84 }
85
86 fn name(&self) -> &'static str {
87 "Wedge"
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use crate::indicators::pattern_swing::candles_for_pivots;
95 use crate::traits::BatchExt;
96
97 fn run(pivots: &[f64]) -> Vec<f64> {
98 let mut indicator = Wedge::new();
99 candles_for_pivots(pivots)
100 .into_iter()
101 .map(|c| indicator.update(c).unwrap())
102 .collect()
103 }
104
105 #[test]
106 fn accessors_and_metadata() {
107 let indicator = Wedge::new();
108 assert_eq!(indicator.name(), "Wedge");
109 assert_eq!(indicator.warmup_period(), 5);
110 assert!(!indicator.is_ready());
111 assert!(!Wedge::default().is_ready());
112 }
113
114 #[test]
115 fn rising_wedge_is_minus_one() {
116 let out = run(&[110.0, 90.0, 100.0, 94.0, 103.0]);
118 assert_eq!(*out.last().unwrap(), -1.0);
119 }
120
121 #[test]
122 fn falling_wedge_is_plus_one() {
123 let out = run(&[120.0, 100.0, 106.0, 99.0]);
125 assert_eq!(*out.last().unwrap(), 1.0);
126 }
127
128 #[test]
129 fn diverging_swings_are_not_a_wedge() {
130 let out = run(&[110.0, 100.0, 130.0, 80.0]);
132 assert_eq!(*out.last().unwrap(), 0.0);
133 }
134
135 #[test]
136 fn reset_clears_state() {
137 let mut indicator = Wedge::new();
138 for c in candles_for_pivots(&[110.0, 90.0, 100.0]) {
139 let _ = indicator.update(c);
140 }
141 indicator.reset();
142 assert!(!indicator.is_ready());
143 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
144 assert_eq!(indicator.update(c), Some(0.0));
145 }
146
147 #[test]
148 fn batch_equals_streaming() {
149 let candles = candles_for_pivots(&[110.0, 90.0, 100.0, 94.0, 103.0]);
150 let mut a = Wedge::new();
151 let mut b = Wedge::new();
152 assert_eq!(
153 a.batch(&candles),
154 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
155 );
156 }
157}