bbgo_origin/pkg/indicator/v2_ewma.go

29 lines
475 B
Go
Raw Normal View History

2023-05-29 13:42:22 +00:00
package indicator
type EWMAStream struct {
*Float64Series
2023-05-29 13:42:22 +00:00
window int
multiplier float64
}
func EWMA2(source Float64Source, window int) *EWMAStream {
s := &EWMAStream{
Float64Series: NewFloat64Series(),
window: window,
multiplier: 2.0 / float64(1+window),
2023-05-29 13:42:22 +00:00
}
s.Bind(source, s)
2023-05-29 13:42:22 +00:00
return s
}
func (s *EWMAStream) Calculate(v float64) float64 {
last := s.slice.Last(0)
2023-06-12 09:38:17 +00:00
if last == 0.0 {
return v
}
2023-05-29 13:42:22 +00:00
m := s.multiplier
return (1.0-m)*last + m*v
}