mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-14 19:13:52 +00:00
24 lines
405 B
Go
24 lines
405 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)
|
|
}
|