amv-dev / yata

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

understand MA #49

Closed cometta closed 3 months ago

cometta commented 4 months ago

i saw the example look like below, what is the purpose to repeating inputing the first value?

// SMA of length=3
let mut sma = SMA::new(3, &1.0).unwrap(); // may i know is &1.0 the first value same as next line?

sma.next(&1.0);  // in SMA::new(), aren't we already input first value?
sma.next(&2.0);

assert_eq!(sma.next(&3.0), 2.0);
assert_eq!(sma.next(&4.0), 3.0);
amv-dev commented 4 months ago

Hello. You are totally correct. First value in SMA::new(3, &1.0) is the same value as in the next line. Yes, there is no reason to provide first input again. It will not change internal state of a MA. The only reason to provide first input again is to get resulting value (and use it somewhere else).

6qat commented 3 months ago

What's the second argument of SMA::new function?

amv-dev commented 3 months ago

What's the second argument of SMA::new function?

Again, it's just the very first value of time series you want to process.