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: add G-H filter and Kalman filter #915

Merged
merged 3 commits into from
Sep 5, 2022
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
13 changes: 13 additions & 0 deletions pkg/bbgo/standard_indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ func (s *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64)

return inc
}

// GHFilter is a helper function that returns the G-H (alpha beta) digital filter of the given interval and the window size.
func (s *StandardIndicatorSet) GHFilter(iw types.IntervalWindow) *indicator.GHFilter {
inc := s.allocateSimpleIndicator(&indicator.GHFilter{IntervalWindow: iw}, iw, "ghfilter")
return inc.(*indicator.GHFilter)
}

// KalmanFilter is a helper function that returns the Kalman digital filter of the given interval and the window size.
// Note that the additional smooth window is set to zero in standard indicator set. Users have to create their own instance and push K-lines if a smoother filter is needed.
func (s *StandardIndicatorSet) KalmanFilter(iw types.IntervalWindow) *indicator.KalmanFilter {
inc := s.allocateSimpleIndicator(&indicator.KalmanFilter{IntervalWindow: iw, AdditionalSmoothWindow: 0}, iw, "kalmanfilter")
return inc.(*indicator.KalmanFilter)
}
74 changes: 74 additions & 0 deletions pkg/indicator/ghfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package indicator

import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
"math"
)

// Refer: https://jamesgoulding.com/Research_II/Ehlers/Ehlers%20(Optimal%20Tracking%20Filters).doc
// Ehler's Optimal Tracking Filter, an alpha-beta filter, also called g-h filter

//go:generate callbackgen -type GHFilter
type GHFilter struct {
types.SeriesBase
types.IntervalWindow
a float64 // maneuverability uncertainty
b float64 // measurement uncertainty
lastMeasurement float64
Values floats.Slice

UpdateCallbacks []func(value float64)
}

func (inc *GHFilter) Update(value float64) {
inc.update(value, math.Abs(value-inc.lastMeasurement))
}

func (inc *GHFilter) update(value, uncertainty float64) {
if len(inc.Values) == 0 {
inc.a = 0
inc.b = uncertainty / 2
inc.lastMeasurement = value
inc.Values.Push(value)
return
}
multiplier := 2.0 / float64(1+inc.Window) // EMA multiplier
inc.a = multiplier*(value-inc.lastMeasurement) + (1-multiplier)*inc.a
inc.b = multiplier*uncertainty/2 + (1-multiplier)*inc.b
lambda := inc.a / inc.b
lambda2 := lambda * lambda
alpha := (-lambda2 + math.Sqrt(lambda2*lambda2+16*lambda2)) / 8
filtered := alpha*value + (1-alpha)*inc.Values.Last()
inc.Values.Push(filtered)
inc.lastMeasurement = value
}

func (inc *GHFilter) Index(i int) float64 {
if inc.Values == nil {
return 0.0
}
return inc.Values.Index(i)
}

func (inc *GHFilter) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}

func (inc *GHFilter) Last() float64 {
if inc.Values == nil {
return 0.0
}
return inc.Values.Last()
}

// interfaces implementation check
var _ Simple = &GHFilter{}
var _ types.SeriesExtend = &GHFilter{}

func (inc *GHFilter) PushK(k types.KLine) {
inc.update(k.Close.Float64(), k.High.Float64()-k.Low.Float64())
}
15 changes: 15 additions & 0 deletions pkg/indicator/ghfilter_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading