Skip to main content

quantwave_core/indicators/
cyber_cycle.rs

1use crate::indicators::metadata::{IndicatorMetadata, ParamDef};
2use crate::traits::Next;
3
4/// John Ehlers' Cyber Cycle
5/// As described in "Cybernetic Analysis for Stocks and Futures" (2004), Chapter 4, Page 33-34.
6///
7/// The Cyber Cycle is an indicator that models the cyclical component of price movement.
8/// It uses a 4-bar symmetrical finite impulse response (FIR) filter for smoothing
9/// and an alpha calculation to isolate the cycle.
10#[derive(Debug, Clone)]
11pub struct CyberCycle {
12    alpha: f64,
13    x: [f64; 4],   // X[t], X[t-1], X[t-2], X[t-3]
14    x_s: [f64; 3], // X_S[t], X_S[t-1], X_S[t-2]
15    cc: [f64; 3],  // CC[t], CC[t-1], CC[t-2]
16    trigger: f64,
17    t: usize,
18}
19
20impl CyberCycle {
21    pub fn new(length: usize) -> Self {
22        let alpha = 2.0 / ((length as f64) + 1.0);
23        Self {
24            alpha,
25            x: [0.0; 4],
26            x_s: [0.0; 3],
27            cc: [0.0; 3],
28            trigger: 0.0,
29            t: 0,
30        }
31    }
32}
33
34impl Next<f64> for CyberCycle {
35    type Output = (f64, f64); // (CyberCycle, Trigger)
36
37    fn next(&mut self, input: f64) -> Self::Output {
38        self.x[3] = self.x[2];
39        self.x[2] = self.x[1];
40        self.x[1] = self.x[0];
41        self.x[0] = input;
42
43        let smooth = (self.x[0] + 2.0 * self.x[1] + 2.0 * self.x[2] + self.x[3]) / 6.0;
44
45        self.x_s[2] = self.x_s[1];
46        self.x_s[1] = self.x_s[0];
47        self.x_s[0] = smooth;
48
49        self.cc[2] = self.cc[1];
50        self.cc[1] = self.cc[0];
51
52        // Ehlers' typical trigger is CC delayed by 1 bar
53        self.trigger = self.cc[1];
54
55        if self.t < 6 {
56            self.cc[0] = (self.x[0] - 2.0 * self.x[1] + self.x[2]) / 4.0;
57        } else {
58            let part1 =
59                (1.0 - 0.5 * self.alpha).powi(2) * (self.x_s[0] - 2.0 * self.x_s[1] + self.x_s[2]);
60            let part2 = 2.0 * (1.0 - self.alpha) * self.cc[1];
61            let part3 = (1.0 - self.alpha).powi(2) * self.cc[2];
62            self.cc[0] = part1 + part2 - part3;
63        }
64
65        self.t += 1;
66
67        (self.cc[0], self.trigger)
68    }
69}
70
71pub const CYBER_CYCLE_METADATA: IndicatorMetadata = IndicatorMetadata {
72    name: "Cyber Cycle",
73    description: "An oscillator introduced by John Ehlers that models the cyclical component of a time series using FIR smoothing.",
74    params: &[ParamDef {
75        name: "length",
76        default: "14",
77        description: "Alpha smoothing length parameter",
78    }],
79    formula_source: "Cybernetic Analysis for Stocks and Futures, John Ehlers, 2004, Chapter 4",
80    formula_latex: r#"
81\[
82\alpha = \frac{2}{\text{Length} + 1}
83\]
84\[
85\text{Smooth} = \frac{X_t + 2X_{t-1} + 2X_{t-2} + X_{t-3}}{6}
86\]
87\[
88CC_t = \left(1 - \frac{\alpha}{2}\right)^2 (\text{Smooth}_t - 2\text{Smooth}_{t-1} + \text{Smooth}_{t-2}) + 2(1 - \alpha)CC_{t-1} - (1 - \alpha)^2 CC_{t-2}
89\]
90"#,
91    gold_standard_file: "cyber_cycle.json",
92    category: "Ehlers DSP",
93};
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use proptest::prelude::*;
99
100    fn cyber_cycle_batch(data: &[f64], length: usize) -> Vec<(f64, f64)> {
101        let mut cc = CyberCycle::new(length);
102        data.iter().map(|&x| cc.next(x)).collect()
103    }
104
105    proptest! {
106        #[test]
107        fn test_cyber_cycle_parity(input in prop::collection::vec(0.1..100.0, 1..100)) {
108            let length = 14;
109            let mut streaming_cc = CyberCycle::new(length);
110            let streaming_results: Vec<(f64, f64)> = input.iter().map(|&x| streaming_cc.next(x)).collect();
111            let batch_results = cyber_cycle_batch(&input, length);
112
113            for (s, b) in streaming_results.iter().zip(batch_results.iter()) {
114                approx::assert_relative_eq!(s.0, b.0, epsilon = 1e-6);
115                approx::assert_relative_eq!(s.1, b.1, epsilon = 1e-6);
116            }
117        }
118    }
119}