qbtrade/pkg/indicator/v2/cma.go

29 lines
539 B
Go
Raw Normal View History

2024-06-27 14:42:38 +00:00
package indicatorv2
import (
"git.qtrade.icu/lychiyu/qbtrade/pkg/indicator"
"git.qtrade.icu/lychiyu/qbtrade/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)
}