wickra_core/indicators/
triangle.rs1use crate::indicators::pattern_swing::{
4 approx_equal, recent_legs, SwingTracker, LEVEL_TOLERANCE, SWING_THRESHOLD,
5};
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9#[derive(Debug, Clone)]
28pub struct Triangle {
29 swing: SwingTracker,
30 has_emitted: bool,
31}
32
33impl Triangle {
34 pub const fn new() -> Self {
36 Self {
37 swing: SwingTracker::new(SWING_THRESHOLD, 4),
38 has_emitted: false,
39 }
40 }
41}
42
43impl Default for Triangle {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49impl Indicator for Triangle {
50 type Input = Candle;
51 type Output = f64;
52
53 fn update(&mut self, candle: Candle) -> Option<f64> {
54 self.has_emitted = true;
55 if !self.swing.update(candle) {
56 return Some(0.0);
57 }
58 let pivots = self.swing.pivots();
59 if pivots.len() < 4 {
60 return Some(0.0);
61 }
62 let (high_old, high_new, low_old, low_new) = recent_legs(pivots);
63 let flat_highs = approx_equal(high_old, high_new, LEVEL_TOLERANCE);
64 let flat_lows = approx_equal(low_old, low_new, LEVEL_TOLERANCE);
65 let rising_lows = low_new > low_old * (1.0 + LEVEL_TOLERANCE);
66 let falling_highs = high_new < high_old * (1.0 - LEVEL_TOLERANCE);
67 let last_is_high = pivots[pivots.len() - 1].direction > 0.0;
68
69 if flat_highs && rising_lows {
70 return Some(1.0); }
72 if falling_highs && flat_lows {
73 return Some(-1.0); }
75 if falling_highs && rising_lows {
76 return Some(if last_is_high { -1.0 } else { 1.0 });
78 }
79 Some(0.0)
80 }
81
82 fn reset(&mut self) {
83 self.swing.reset();
84 self.has_emitted = false;
85 }
86
87 fn warmup_period(&self) -> usize {
88 5
90 }
91
92 fn is_ready(&self) -> bool {
93 self.has_emitted
94 }
95
96 fn name(&self) -> &'static str {
97 "Triangle"
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104 use crate::indicators::pattern_swing::candles_for_pivots;
105 use crate::traits::BatchExt;
106
107 fn run(pivots: &[f64]) -> Vec<f64> {
108 let mut indicator = Triangle::new();
109 candles_for_pivots(pivots)
110 .into_iter()
111 .map(|c| indicator.update(c).unwrap())
112 .collect()
113 }
114
115 #[test]
116 fn accessors_and_metadata() {
117 let indicator = Triangle::new();
118 assert_eq!(indicator.name(), "Triangle");
119 assert_eq!(indicator.warmup_period(), 5);
120 assert!(!indicator.is_ready());
121 assert!(!Triangle::default().is_ready());
122 }
123
124 #[test]
125 fn ascending_triangle_is_plus_one() {
126 let out = run(&[130.0, 100.0, 120.0, 110.0, 120.0]);
128 assert_eq!(*out.last().unwrap(), 1.0);
129 }
130
131 #[test]
132 fn descending_triangle_is_minus_one() {
133 let out = run(&[120.0, 100.0, 110.0, 99.0]);
135 assert_eq!(*out.last().unwrap(), -1.0);
136 }
137
138 #[test]
139 fn symmetrical_triangle_ending_low_is_plus_one() {
140 let out = run(&[120.0, 100.0, 113.0, 106.0]);
142 assert_eq!(*out.last().unwrap(), 1.0);
143 }
144
145 #[test]
146 fn symmetrical_triangle_ending_high_is_minus_one() {
147 let out = run(&[130.0, 100.0, 120.0, 106.0, 113.0]);
149 assert_eq!(*out.last().unwrap(), -1.0);
150 }
151
152 #[test]
153 fn expanding_swings_are_not_a_triangle() {
154 let out = run(&[110.0, 100.0, 130.0, 80.0]);
156 assert_eq!(*out.last().unwrap(), 0.0);
157 }
158
159 #[test]
160 fn reset_clears_state() {
161 let mut indicator = Triangle::new();
162 for c in candles_for_pivots(&[130.0, 100.0, 120.0]) {
163 let _ = indicator.update(c);
164 }
165 indicator.reset();
166 assert!(!indicator.is_ready());
167 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
168 assert_eq!(indicator.update(c), Some(0.0));
169 }
170
171 #[test]
172 fn batch_equals_streaming() {
173 let candles = candles_for_pivots(&[130.0, 100.0, 120.0, 110.0, 120.0]);
174 let mut a = Triangle::new();
175 let mut b = Triangle::new();
176 assert_eq!(
177 a.batch(&candles),
178 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
179 );
180 }
181}