Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Sep 24, 2023
1 parent 06a74bd commit 65421bc
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ta_lib/indicators/momentum/src/rsi.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use core::Series;
use core::{iff, Series};

pub fn rsi(source: &[f32], period: usize) -> Series<f32> {
let source = Series::from(source);
let len = source.len();

let mom = source.change(1);

let up = mom.smax(0.0).smma(period);
let down = mom.smin(0.0).neg().smma(period);

let rs = up / down;

let rsi = 100.0 - 100.0 / (1.0 + rs);
let oneh = Series::fill(len, 100.0);
let zero = Series::fill(len, 0.0);

rsi.nz(Some(100.0))
iff!(
down.seq(0.0),
oneh,
iff!(up.seq(0.0), zero, 100.0 - 100.0 / (1.0 + up / down))
)
}

#[cfg(test)]
Expand Down

0 comments on commit 65421bc

Please sign in to comment.