wickra_core/indicators/
inertia.rs1use crate::error::{Error, Result};
4use crate::indicators::linreg::LinearRegression;
5use crate::indicators::rvi::Rvi;
6use crate::ohlcv::Candle;
7use crate::traits::Indicator;
8
9#[derive(Debug, Clone)]
36pub struct Inertia {
37 rvi_period: usize,
38 linreg_period: usize,
39 rvi: Rvi,
40 linreg: LinearRegression,
41}
42
43impl Inertia {
44 pub fn new(rvi_period: usize, linreg_period: usize) -> Result<Self> {
47 if rvi_period == 0 || linreg_period == 0 {
48 return Err(Error::PeriodZero);
49 }
50 Ok(Self {
51 rvi_period,
52 linreg_period,
53 rvi: Rvi::new(rvi_period)?,
54 linreg: LinearRegression::new(linreg_period)?,
55 })
56 }
57
58 pub fn classic() -> Self {
60 Self::new(14, 20).expect("classic Inertia parameters are valid")
61 }
62
63 pub const fn periods(&self) -> (usize, usize) {
65 (self.rvi_period, self.linreg_period)
66 }
67}
68
69impl Indicator for Inertia {
70 type Input = Candle;
71 type Output = f64;
72
73 fn update(&mut self, candle: Candle) -> Option<f64> {
74 let rvi = self.rvi.update(candle)?;
75 self.linreg.update(rvi)
76 }
77
78 fn reset(&mut self) {
79 self.rvi.reset();
80 self.linreg.reset();
81 }
82
83 fn warmup_period(&self) -> usize {
84 self.rvi_period + self.linreg_period - 1
87 }
88
89 fn is_ready(&self) -> bool {
90 self.linreg.is_ready()
91 }
92
93 fn name(&self) -> &'static str {
94 "Inertia"
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101 use crate::traits::BatchExt;
102 use approx::assert_relative_eq;
103
104 fn candle(open: f64, high: f64, low: f64, close: f64, ts: i64) -> Candle {
105 Candle::new(open, high, low, close, 1.0, ts).unwrap()
106 }
107
108 #[test]
109 fn rejects_zero_period() {
110 assert!(matches!(Inertia::new(0, 20), Err(Error::PeriodZero)));
111 assert!(matches!(Inertia::new(14, 0), Err(Error::PeriodZero)));
112 }
113
114 #[test]
115 fn accessors_and_metadata() {
116 let inertia = Inertia::classic();
117 assert_eq!(inertia.periods(), (14, 20));
118 assert_eq!(inertia.warmup_period(), 33);
119 assert_eq!(inertia.name(), "Inertia");
120 }
121
122 #[test]
123 fn classic_factory() {
124 assert_eq!(Inertia::classic().periods(), (14, 20));
125 }
126
127 #[test]
128 fn warmup_emits_first_value_at_warmup_period() {
129 let mut inertia = Inertia::new(3, 4).unwrap();
132 assert_eq!(inertia.warmup_period(), 6);
133 for i in 0..5 {
134 assert_eq!(inertia.update(candle(10.0, 11.0, 9.0, 10.5, i)), None);
135 }
136 assert!(inertia.update(candle(10.0, 11.0, 9.0, 10.5, 5)).is_some());
137 }
138
139 #[test]
140 fn constant_rvi_yields_constant_inertia() {
141 let mut inertia = Inertia::new(3, 4).unwrap();
144 let mut last = None;
145 for i in 0..40 {
146 last = inertia.update(candle(10.0, 11.0, 9.0, 10.5, i));
147 }
148 let v = last.unwrap();
150 assert_relative_eq!(v, 0.25, epsilon = 1e-12);
151 }
152
153 #[test]
154 fn batch_equals_streaming() {
155 let candles: Vec<Candle> = (0..80_i64)
156 .map(|i| {
157 let o = 100.0 + (i as f64 * 0.3).sin() * 5.0;
158 let c = o + (i as f64 * 0.1).cos();
159 candle(o, o.max(c) + 0.5, o.min(c) - 0.5, c, i)
160 })
161 .collect();
162 let batch = Inertia::classic().batch(&candles);
163 let mut b = Inertia::classic();
164 let streamed: Vec<_> = candles.iter().map(|c| b.update(*c)).collect();
165 assert_eq!(batch, streamed);
166 }
167
168 #[test]
169 fn reset_clears_state() {
170 let mut inertia = Inertia::classic();
171 for i in 0..50 {
172 inertia.update(candle(10.0, 11.0, 9.0, 10.5, i));
173 }
174 assert!(inertia.is_ready());
175 inertia.reset();
176 assert!(!inertia.is_ready());
177 assert_eq!(inertia.update(candle(10.0, 11.0, 9.0, 10.5, 0)), None);
178 }
179}