bbgo_origin/pkg/indicator/v2_cma.go

24 lines
405 B
Go
Raw Normal View History

2023-06-01 06:27:03 +00:00
package indicator
type CMAStream struct {
*Float64Series
2023-06-01 06:27:03 +00:00
}
func CMA2(source Float64Source) *CMAStream {
s := &CMAStream{
Float64Series: 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(MaxNumOfEWMA)
}