From 73413b1937a96d032fb784ac39ac20782c109cc2 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Wed, 28 Jun 2023 18:18:55 +0400 Subject: [PATCH] zlema --- ta_lib/overlap/src/lib.rs | 1 + ta_lib/overlap/src/zlema.rs | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 ta_lib/overlap/src/zlema.rs diff --git a/ta_lib/overlap/src/lib.rs b/ta_lib/overlap/src/lib.rs index 1a8ffebf..2933e247 100644 --- a/ta_lib/overlap/src/lib.rs +++ b/ta_lib/overlap/src/lib.rs @@ -2,3 +2,4 @@ pub mod ema; pub mod sma; pub mod smma; pub mod vwap; +pub mod zlema; diff --git a/ta_lib/overlap/src/zlema.rs b/ta_lib/overlap/src/zlema.rs new file mode 100644 index 00000000..429c3544 --- /dev/null +++ b/ta_lib/overlap/src/zlema.rs @@ -0,0 +1,42 @@ +use crate::ema::ema; + +pub fn zlema(close: &[f64], period: usize) -> Vec> { + let len = close.len(); + + let lag = (period - 1) / 2; + + let mut ema_data = vec![0.0; len]; + let mut zlema = vec![None; len]; + + for i in 0..len { + if i >= lag { + let price = close[i]; + let prev_price = close[i - lag]; + ema_data[i] = price + (price - prev_price); + } + } + + let ema_result = ema(&ema_data, period); + + for i in 0..len { + if i >= lag { + zlema[i] = ema_result[i]; + } + } + + zlema +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_zlema() { + let close = vec![100.0, 105.0, 110.0, 115.0, 120.0]; + let period = 3; + let expected = vec![None, None, Some(85.0), Some(102.5), Some(113.75)]; + let result = zlema(&close, period); + assert_eq!(result, expected); + } +}