2023-07-10 08:54:22 +00:00
|
|
|
package indicatorv2
|
2023-06-01 07:19:12 +00:00
|
|
|
|
|
|
|
import "github.com/c9s/bbgo/pkg/types"
|
|
|
|
|
|
|
|
type StdDevStream struct {
|
2023-07-10 08:54:22 +00:00
|
|
|
*types.Float64Series
|
2023-06-01 07:19:12 +00:00
|
|
|
|
|
|
|
rawValues *types.Queue
|
|
|
|
|
|
|
|
window int
|
|
|
|
multiplier float64
|
|
|
|
}
|
|
|
|
|
2023-07-10 08:54:22 +00:00
|
|
|
func StdDev(source types.Float64Source, window int) *StdDevStream {
|
2023-06-01 07:19:12 +00:00
|
|
|
s := &StdDevStream{
|
2023-07-10 08:54:22 +00:00
|
|
|
Float64Series: types.NewFloat64Series(),
|
2023-06-01 07:19:12 +00:00
|
|
|
rawValues: types.NewQueue(window),
|
|
|
|
window: window,
|
|
|
|
}
|
|
|
|
s.Bind(source, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StdDevStream) Calculate(x float64) float64 {
|
|
|
|
s.rawValues.Update(x)
|
|
|
|
var std = s.rawValues.Stdev()
|
|
|
|
return std
|
|
|
|
}
|