bbgo/pkg/indicator/v2/cma.go

29 lines
533 B
Go

package indicatorv2
import (
"git.qtrade.icu/lychiyu/bbgo/pkg/indicator"
"git.qtrade.icu/lychiyu/bbgo/pkg/types"
)
type CMAStream struct {
*types.Float64Series
}
func CMA2(source types.Float64Source) *CMAStream {
s := &CMAStream{
Float64Series: types.NewFloat64Series(),
}
s.Bind(source, s)
return s
}
func (s *CMAStream) Calculate(x float64) float64 {
l := float64(s.Slice.Length())
cma := (s.Slice.Last(0)*l + x) / (l + 1.)
return cma
}
func (s *CMAStream) Truncate() {
s.Slice.Truncate(indicator.MaxNumOfEWMA)
}