Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: [indicator] add v2 stddev indicator #1189

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions pkg/indicator/tma.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,3 @@ var _ types.SeriesExtend = &TMA{}
func (inc *TMA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}

func (inc *TMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.s1 == nil {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
}

func (inc *TMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}

inc.CalculateAndUpdate(window)
}

func (inc *TMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
28 changes: 28 additions & 0 deletions pkg/indicator/v2_stddev.go
Original file line number Diff line number Diff line change
@@ -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
}