indicator/smma.go

34 lines
595 B
Go
Raw Permalink Normal View History

2024-06-25 15:17:36 +00:00
package indicator
// SMMA Smoothed Moving Average (SMMA)
type SMMA struct {
MABase
sma *SMA
age int
bFirst bool
}
func NewSMMA(winLen int) *SMMA {
sm := new(SMMA)
sm.winLen = winLen
sm.sma = NewSMA(sm.winLen)
sm.bFirst = true
return sm
}
func (sm *SMMA) Update(price float64) {
if sm.bFirst {
nLen := sm.age + 1
if nLen < sm.winLen {
sm.sma.Update(price)
} else if nLen == sm.winLen {
sm.sma.Update(price)
sm.result = sm.sma.Result()
sm.bFirst = false
}
sm.age++
} else {
sm.result = (sm.result*float64(sm.winLen-1) + price) / float64(sm.winLen)
}
}