Skip to content

Commit 7416f10

Browse files
authored
Merge pull request #915 from COLDTURNIP/feature/observer_filters
feature: add G-H filter and Kalman filter
2 parents 067f127 + 65cd17d commit 7416f10

7 files changed

+12545
-0
lines changed

pkg/bbgo/standard_indicator_set.go

+13
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ func (s *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64)
147147

148148
return inc
149149
}
150+
151+
// GHFilter is a helper function that returns the G-H (alpha beta) digital filter of the given interval and the window size.
152+
func (s *StandardIndicatorSet) GHFilter(iw types.IntervalWindow) *indicator.GHFilter {
153+
inc := s.allocateSimpleIndicator(&indicator.GHFilter{IntervalWindow: iw}, iw, "ghfilter")
154+
return inc.(*indicator.GHFilter)
155+
}
156+
157+
// KalmanFilter is a helper function that returns the Kalman digital filter of the given interval and the window size.
158+
// 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.
159+
func (s *StandardIndicatorSet) KalmanFilter(iw types.IntervalWindow) *indicator.KalmanFilter {
160+
inc := s.allocateSimpleIndicator(&indicator.KalmanFilter{IntervalWindow: iw, AdditionalSmoothWindow: 0}, iw, "kalmanfilter")
161+
return inc.(*indicator.KalmanFilter)
162+
}

pkg/indicator/ghfilter.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package indicator
2+
3+
import (
4+
"github.com/c9s/bbgo/pkg/datatype/floats"
5+
"github.com/c9s/bbgo/pkg/types"
6+
"math"
7+
)
8+
9+
// Refer: https://jamesgoulding.com/Research_II/Ehlers/Ehlers%20(Optimal%20Tracking%20Filters).doc
10+
// Ehler's Optimal Tracking Filter, an alpha-beta filter, also called g-h filter
11+
12+
//go:generate callbackgen -type GHFilter
13+
type GHFilter struct {
14+
types.SeriesBase
15+
types.IntervalWindow
16+
a float64 // maneuverability uncertainty
17+
b float64 // measurement uncertainty
18+
lastMeasurement float64
19+
Values floats.Slice
20+
21+
UpdateCallbacks []func(value float64)
22+
}
23+
24+
func (inc *GHFilter) Update(value float64) {
25+
inc.update(value, math.Abs(value-inc.lastMeasurement))
26+
}
27+
28+
func (inc *GHFilter) update(value, uncertainty float64) {
29+
if len(inc.Values) == 0 {
30+
inc.a = 0
31+
inc.b = uncertainty / 2
32+
inc.lastMeasurement = value
33+
inc.Values.Push(value)
34+
return
35+
}
36+
multiplier := 2.0 / float64(1+inc.Window) // EMA multiplier
37+
inc.a = multiplier*(value-inc.lastMeasurement) + (1-multiplier)*inc.a
38+
inc.b = multiplier*uncertainty/2 + (1-multiplier)*inc.b
39+
lambda := inc.a / inc.b
40+
lambda2 := lambda * lambda
41+
alpha := (-lambda2 + math.Sqrt(lambda2*lambda2+16*lambda2)) / 8
42+
filtered := alpha*value + (1-alpha)*inc.Values.Last()
43+
inc.Values.Push(filtered)
44+
inc.lastMeasurement = value
45+
}
46+
47+
func (inc *GHFilter) Index(i int) float64 {
48+
if inc.Values == nil {
49+
return 0.0
50+
}
51+
return inc.Values.Index(i)
52+
}
53+
54+
func (inc *GHFilter) Length() int {
55+
if inc.Values == nil {
56+
return 0
57+
}
58+
return inc.Values.Length()
59+
}
60+
61+
func (inc *GHFilter) Last() float64 {
62+
if inc.Values == nil {
63+
return 0.0
64+
}
65+
return inc.Values.Last()
66+
}
67+
68+
// interfaces implementation check
69+
var _ Simple = &GHFilter{}
70+
var _ types.SeriesExtend = &GHFilter{}
71+
72+
func (inc *GHFilter) PushK(k types.KLine) {
73+
inc.update(k.Close.Float64(), k.High.Float64()-k.Low.Float64())
74+
}

pkg/indicator/ghfilter_callbacks.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)