diff --git a/ta_lib/core/src/math.rs b/ta_lib/core/src/math.rs index 1ee08a67..f3e2db18 100644 --- a/ta_lib/core/src/math.rs +++ b/ta_lib/core/src/math.rs @@ -12,15 +12,11 @@ impl Series { let one_minus_alpha = 1.0 - alpha; let mut ew = Series::empty(len); - - if let Some(val) = self[0] { - ew[0] = Some(val); - } + ew[0] = self[0]; for i in 1..len { - let ew_prev = ew[i - 1].unwrap_or(0.0); - if let Some(val) = self[i] { - ew[i] = Some(alpha * val + one_minus_alpha * ew_prev); + if let (Some(ew_prev), Some(current)) = (ew[i - 1], self[i]) { + ew[i] = Some(alpha * current + one_minus_alpha * ew_prev); } } @@ -66,7 +62,7 @@ impl Series { self.ew(period, |period| 1.0 / (period as f64)) } - pub fn std(&self, period: usize) -> Self { + pub fn variance(&self, period: usize) -> Self { let ma = self.ma(period); self.sliding_map(period, |window, size, i| { @@ -77,9 +73,13 @@ impl Series { .map(|v| (v - ma_val).powi(2)) .sum::() / size as f64; - Some(variance.sqrt()) + Some(variance) }) } + + pub fn std(&self, period: usize) -> Self { + self.variance(period).fmap(|val| val.map(|v| v.sqrt())) + } } #[cfg(test)] diff --git a/ta_lib/trend/src/zlema.rs b/ta_lib/trend/src/zlema.rs index 8720c7ec..5ab035d6 100644 --- a/ta_lib/trend/src/zlema.rs +++ b/ta_lib/trend/src/zlema.rs @@ -6,9 +6,9 @@ pub fn zlema(source: &[f64], period: usize) -> Vec { let ema_first = source.ema(period); let ema_second = ema_first.ema(period); - let diff = &ema_first - &ema_second; + let macd_line = &ema_first - &ema_second; - let zlema = ema_first + &diff; + let zlema = ema_first + &macd_line; zlema.into() }