diff --git a/ta_lib/indicators/momentum/src/rsi.rs b/ta_lib/indicators/momentum/src/rsi.rs index fa89816d..59d136e4 100644 --- a/ta_lib/indicators/momentum/src/rsi.rs +++ b/ta_lib/indicators/momentum/src/rsi.rs @@ -1,17 +1,22 @@ -use core::Series; +use core::{iff, Series}; pub fn rsi(source: &[f32], period: usize) -> Series { 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)]