indicator/sma.go

26 lines
422 B
Go
Raw Normal View History

2024-06-25 15:17:36 +00:00
package indicator
type SMA struct {
MABase
prices []float64
sum float64
age int
}
func NewSMA(winLen int) *SMA {
s := new(SMA)
s.winLen = winLen
s.prices = make([]float64, s.winLen)
s.age = 0
return s
}
func (s *SMA) Update(price float64) {
s.result = price
tail := s.prices[s.age]
s.prices[s.age] = price
s.sum += price - tail
s.result = s.sum / float64(s.winLen)
s.age = (s.age + 1) % s.winLen
}