From b141ae3ece1d4c8938a014a51ec289a54fdfe749 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 1 Jun 2023 15:19:12 +0800 Subject: [PATCH] indicator: add stddev v2 --- pkg/indicator/v2_stddev.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pkg/indicator/v2_stddev.go diff --git a/pkg/indicator/v2_stddev.go b/pkg/indicator/v2_stddev.go new file mode 100644 index 000000000..28a5a4d07 --- /dev/null +++ b/pkg/indicator/v2_stddev.go @@ -0,0 +1,28 @@ +package indicator + +import "github.com/c9s/bbgo/pkg/types" + +type StdDevStream struct { + Float64Series + + rawValues *types.Queue + + window int + multiplier float64 +} + +func StdDev2(source Float64Source, window int) *StdDevStream { + s := &StdDevStream{ + Float64Series: NewFloat64Series(), + 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 +}