2023-07-10 08:54:22 +00:00
|
|
|
package indicatorv2
|
|
|
|
|
|
|
|
import "github.com/c9s/bbgo/pkg/types"
|
2023-05-29 13:42:22 +00:00
|
|
|
|
|
|
|
type EWMAStream struct {
|
2023-07-10 08:54:22 +00:00
|
|
|
*types.Float64Series
|
2023-05-29 13:42:22 +00:00
|
|
|
|
|
|
|
window int
|
|
|
|
multiplier float64
|
|
|
|
}
|
|
|
|
|
2023-07-10 08:54:22 +00:00
|
|
|
func EWMA2(source types.Float64Source, window int) *EWMAStream {
|
2023-05-29 13:42:22 +00:00
|
|
|
s := &EWMAStream{
|
2023-07-10 08:54:22 +00:00
|
|
|
Float64Series: types.NewFloat64Series(),
|
2023-05-30 04:13:55 +00:00
|
|
|
window: window,
|
|
|
|
multiplier: 2.0 / float64(1+window),
|
2023-05-29 13:42:22 +00:00
|
|
|
}
|
2023-05-31 23:58:58 +00:00
|
|
|
s.Bind(source, s)
|
2023-05-29 13:42:22 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:58:58 +00:00
|
|
|
func (s *EWMAStream) Calculate(v float64) float64 {
|
2023-07-10 08:54:22 +00:00
|
|
|
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
|
|
|
|
}
|