wickra_core/indicators/
cypher.rs1use crate::indicators::pattern_swing::{ratios_in, xabcd, SwingTracker, SWING_THRESHOLD};
4use crate::ohlcv::Candle;
5use crate::traits::Indicator;
6
7#[derive(Debug, Clone)]
19pub struct Cypher {
20 swing: SwingTracker,
21 has_emitted: bool,
22}
23
24impl Cypher {
25 pub const fn new() -> Self {
27 Self {
28 swing: SwingTracker::new(SWING_THRESHOLD, 5),
29 has_emitted: false,
30 }
31 }
32}
33
34impl Default for Cypher {
35 fn default() -> Self {
36 Self::new()
37 }
38}
39
40impl Indicator for Cypher {
41 type Input = Candle;
42 type Output = f64;
43
44 #[inline]
45 fn update(&mut self, candle: Candle) -> Option<f64> {
46 let advanced = self.swing.update(candle);
47 let pivots = self.swing.pivots();
48 if pivots.len() < 5 {
51 return None;
52 }
53 self.has_emitted = true;
54 if !advanced {
57 return Some(0.0);
58 }
59 let p = xabcd(pivots);
60 let xa = (p.a - p.x).abs();
61 let ab = (p.b - p.a).abs();
62 let bc = (p.c - p.b).abs();
63 let xc = (p.c - p.x).abs();
64 let cd = (p.d - p.c).abs();
65 let matched = ratios_in(&[
66 (ab / xa, 0.382, 0.618),
67 (bc / xa, 1.13, 1.414),
68 (cd / xc, 0.74, 0.83),
69 ]);
70 if matched {
71 return Some(if p.bullish { 1.0 } else { -1.0 });
72 }
73 Some(0.0)
74 }
75
76 fn reset(&mut self) {
77 self.swing.reset();
78 self.has_emitted = false;
79 }
80
81 #[inline]
82 fn warmup_period(&self) -> usize {
83 6
84 }
85
86 #[inline]
87 fn is_ready(&self) -> bool {
88 self.has_emitted
89 }
90
91 #[inline]
92 fn name(&self) -> &'static str {
93 "Cypher"
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use crate::indicators::pattern_swing::candles_for_pivots;
101 use crate::traits::BatchExt;
102
103 fn run(pivots: &[f64]) -> Vec<f64> {
104 let mut indicator = Cypher::new();
105 candles_for_pivots(pivots)
106 .into_iter()
107 .filter_map(|c| indicator.update(c))
108 .collect()
109 }
110
111 #[test]
112 fn accessors_and_metadata() {
113 let indicator = Cypher::new();
114 assert_eq!(indicator.name(), "Cypher");
115 assert_eq!(indicator.warmup_period(), 6);
116 assert!(!indicator.is_ready());
117 assert!(!Cypher::default().is_ready());
118 }
119
120 #[test]
121 fn bullish_cypher_is_plus_one() {
122 let out = run(&[150.0, 100.0, 140.0, 120.0, 168.0, 114.55]);
123 assert_eq!(*out.last().unwrap(), 1.0);
124 assert!(out[..out.len() - 1].iter().all(|&x| x == 0.0));
125 }
126
127 #[test]
128 fn bearish_cypher_is_minus_one() {
129 let out = run(&[150.0, 110.0, 130.0, 82.0, 135.45]);
130 assert_eq!(*out.last().unwrap(), -1.0);
131 }
132
133 #[test]
134 fn out_of_ratio_does_not_trigger() {
135 let out = run(&[150.0, 100.0, 140.0, 110.0, 135.0, 105.0]);
136 assert_eq!(*out.last().unwrap(), 0.0);
137 }
138
139 #[test]
140 fn reset_clears_state() {
141 let mut indicator = Cypher::new();
142 for c in candles_for_pivots(&[150.0, 100.0, 140.0]) {
143 let _ = indicator.update(c);
144 }
145 indicator.reset();
146 assert!(!indicator.is_ready());
147 let c = Candle::new(99.5, 100.0, 99.5, 99.5, 1.0, 0).unwrap();
148 assert_eq!(indicator.update(c), None);
149 }
150
151 #[test]
152 fn batch_equals_streaming() {
153 let candles = candles_for_pivots(&[150.0, 100.0, 140.0, 120.0, 168.0, 114.55]);
154 let mut a = Cypher::new();
155 let mut b = Cypher::new();
156 assert_eq!(
157 a.batch(&candles),
158 candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
159 );
160 }
161}