amv-dev / yata

Yet Another Technical Analysis library [for Rust]
Apache License 2.0
334 stars 52 forks source link

RelativeStrengthIndex indicator is inverted #15

Closed lephyrius closed 2 years ago

lephyrius commented 3 years ago

I think the RelativeStrengthIndex indicator is reversed. When it should send SELL it sends BUY and vice versa. Where are the tests covering this indicator? I cannot find them.

amv-dev commented 3 years ago

Hello. I will check you issue in a few days. Currently there are no written tests for indicators.

amv-dev commented 3 years ago

I don't see any mistakes in RelativeStrengthIndex.

Here is my code for debugging:

let candles: Vec<_> = yata::helpers::RandomCandles::new().take(60).collect();
let mut rsi = yata::indicators::RelativeStrengthIndex::default();
rsi.period = 4;

println!("{:?}", &rsi);

let instance = rsi.init_fn(candles.first().unwrap()).unwrap();

candles 
  .iter()
  .map(instance)
  .zip(candles.iter())
  .skip(30)
  .for_each(|(r, c)| println!("PRICE: {:.1} | RSI: {:.3} => S1: {:>5} S2: {}", c.close(), r.value(0), format!("{}", r.signal(0)), r.signal(1)));

Here is output I have with my comments:

RelativeStrengthIndex { period: 4, zone: 0.3, source: Close, method: EMA }
PRICE: 0.7 | RSI: 0.407 => S1:     N S2: +1.00
PRICE: 1.2 | RSI: 0.726 => S1: -1.00 S2: N // crossed upper bound 0.7 upwards => overbought, S1 correct
PRICE: 1.5 | RSI: 0.829 => S1:     N S2: N
PRICE: 1.4 | RSI: 0.658 => S1:     N S2: -1.00 // crossed upper bound 0.7 downwards => overbought, S2 correct
PRICE: 0.9 | RSI: 0.300 => S1: +1.00 S2: N // crossed lower bound 0.3 downwards => oversold, S1 correct
PRICE: 0.5 | RSI: 0.174 => S1:     N S2: N
PRICE: 0.6 | RSI: 0.266 => S1:     N S2: N
PRICE: 1.0 | RSI: 0.671 => S1:     N S2: +1.00 // crossed lower bound 0.3 upwards => oversold, S2 correct
PRICE: 1.4 | RSI: 0.823 => S1: -1.00 S2: N // crossed upper bound 0.7 upwards => overbought, S1 correct
PRICE: 1.4 | RSI: 0.824 => S1:     N S2: N
PRICE: 1.0 | RSI: 0.363 => S1:     N S2: -1.00 // crossed upper bound 0.7 downwards => overbought, S2 correct
PRICE: 0.6 | RSI: 0.181 => S1: +1.00 S2: N // crossed lower bound 0.3 downwards => oversold, S1 correct
PRICE: 0.5 | RSI: 0.159 => S1:     N S2: N
PRICE: 0.9 | RSI: 0.583 => S1:     N S2: +1.00 // crossed lower bound 0.3 upwards => oversold, S2 correct
PRICE: 1.4 | RSI: 0.799 => S1: -1.00 S2: N // crossed upper bound 0.7 upwards => overbought, S1 correct
PRICE: 1.5 | RSI: 0.840 => S1:     N S2: N
PRICE: 1.2 | RSI: 0.472 => S1:     N S2: -1.00 // crossed upper bound 0.7 downwards => overbought, S2 correct
...

The only thing I found wrong is that at the very first step RSI returns 0.0 instead of 0.5. It is a mistake, but not so important.

amv-dev commented 3 years ago

Issue with wrong RSI value at the very first step is fixed in 7bb8ae7540574b530457574befe90d13f1645ad9

amv-dev commented 2 years ago

Closed due lack of information