Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Second/subsequent iterations of EfficiencyRatio is NaN when price is stationary. #73

Open
nathanfranke opened this issue Aug 29, 2023 · 0 comments

Comments

@nathanfranke
Copy link

nathanfranke commented Aug 29, 2023

let mut ker = EfficiencyRatio::new(14)?;
println!("1  {}", ker.next(1.69));
println!("2  {}", ker.next(1.69));
println!("3  {}", ker.next(1.70));
println!("4  {}", ker.next(1.70));
println!("5  {}", ker.next(1.69));

Output:

1  1
2  NaN
3  1
4  1
5  0

If you keep passing 1.69, it subsequently outputs NaN.

ad hoc workaround

// https://github.com/greyblake/ta-rs/issues/73
struct FixEfficiencyRatio {
    er: EfficiencyRatio,
}
impl FixEfficiencyRatio {
    fn new(period: usize) -> ta::errors::Result<Self> {
        Ok(Self {
            er: EfficiencyRatio::new(period)?,
        })
    }
}
impl<T: Close> ta::Next<&T> for FixEfficiencyRatio {
    type Output = f64;
    fn next(&mut self, input: &T) -> f64 {
        match self.er.next(input) {
            v if v.is_nan() => 1.0,
            v => v,
        }
    }
}

KER implementation could be simplified with VecDeque.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant