Skip to content

Commit

Permalink
use change for rsi
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 5, 2023
1 parent 2ddecee commit 7c4c02c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions ta_lib/momentum/src/rsi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use overlap::smma::smma;
use utils::change::change;

pub fn rsi(source: &[f64], period: usize) -> Vec<Option<f64>> {
let len = source.len();
Expand All @@ -7,15 +8,10 @@ pub fn rsi(source: &[f64], period: usize) -> Vec<Option<f64>> {
return vec![None; len];
}

let mut gains = vec![0.0; len];
let mut losses = vec![0.0; len];
let changes = change(source, 1);

for i in 1..len {
let change = source[i] - source[i - 1];

gains[i] = change.max(0.0);
losses[i] = (-change).max(0.0);
}
let gains = changes.iter().map(|&x| x.max(0.0)).collect::<Vec<_>>();
let losses = changes.iter().map(|&x| (-x).max(0.0)).collect::<Vec<_>>();

let avg_gain = smma(&gains, period);
let avg_loss = smma(&losses, period);
Expand Down

0 comments on commit 7c4c02c

Please sign in to comment.