Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 16, 2023
1 parent f32ced9 commit bd0dc2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions ta_lib/core/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ impl Series<f64> {
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);
}
}

Expand Down Expand Up @@ -66,7 +62,7 @@ impl Series<f64> {
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| {
Expand All @@ -77,9 +73,13 @@ impl Series<f64> {
.map(|v| (v - ma_val).powi(2))
.sum::<f64>()
/ 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)]
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/trend/src/zlema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub fn zlema(source: &[f64], period: usize) -> Vec<f64> {
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()
}
Expand Down

0 comments on commit bd0dc2c

Please sign in to comment.