Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@nathanfranke

Description

@nathanfranke
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions