amv-dev / yata

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

HeikinAshi may have something wrong... #42

Closed CarlosLagarto closed 9 months ago

CarlosLagarto commented 9 months ago

Hi ,

I am implementing a thing for displaying a candlestick chart with HeikinAshi candles.

But from what I have seen in the code,

fn new((): Self::Params, value: &Self::Input) -> Result<Self, Error> {
  Ok(Self {
      prev: Candle::from(value),
  })
}

#[inline]
fn next(&mut self, value: &Self::Input) -> Self::Output {
  let open = (self.prev.open() + self.prev.close()) * 0.5;
  let close = value.ohlc4();

  Candle {
      open,
      high: value.high().max(open),
      low: value.low().min(open),
      close,
      volume: value.volume(),
  }
}

prev is updated in fn new(), but never ever gets updated again, so it always have the initial value that result in what it seems to me incorrect value:

image

Or am I interpreting something wrong?

amv-dev commented 9 months ago

Yes, you are totally right. Thank you. Will fix soon

amv-dev commented 9 months ago

Fixed in https://github.com/amv-dev/yata/commit/e7ba686d23bd2c2245351a0979b851bc6c8348b0

CarlosLagarto commented 8 months ago

Thanks amd-dev.