mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
24 lines
404 B
Go
24 lines
404 B
Go
package indicator
|
|
|
|
type CMAStream struct {
|
|
Float64Series
|
|
}
|
|
|
|
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)
|
|
}
|