2023-06-01 00:11:30 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import "github.com/c9s/bbgo/pkg/types"
|
|
|
|
|
|
|
|
type SMAStream struct {
|
2023-06-09 05:18:08 +00:00
|
|
|
*Float64Series
|
2023-06-01 00:11:30 +00:00
|
|
|
window int
|
|
|
|
rawValues *types.Queue
|
|
|
|
}
|
|
|
|
|
|
|
|
func SMA2(source Float64Source, window int) *SMAStream {
|
|
|
|
s := &SMAStream{
|
|
|
|
Float64Series: NewFloat64Series(),
|
|
|
|
window: window,
|
|
|
|
rawValues: types.NewQueue(window),
|
|
|
|
}
|
|
|
|
s.Bind(source, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SMAStream) Calculate(v float64) float64 {
|
|
|
|
s.rawValues.Update(v)
|
|
|
|
sma := s.rawValues.Mean(s.window)
|
|
|
|
return sma
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SMAStream) Truncate() {
|
2023-06-01 00:33:14 +00:00
|
|
|
s.slice = s.slice.Truncate(MaxNumOfSMA)
|
2023-06-01 00:11:30 +00:00
|
|
|
}
|