Skip to content

Commit 9c684c1

Browse files
committed
feature: add G-H filter and Kalman filter
- implement G-H (alpha beta) filter and Kalman filter - compare the predict accurateness with other indicator
1 parent 843b81e commit 9c684c1

File tree

6 files changed

+12576
-0
lines changed

6 files changed

+12576
-0
lines changed

pkg/indicator/ghfilter.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
var _ types.SeriesExtend = &GHFilter{}
69+
70+
func (inc *GHFilter) PushK(k types.KLine) {
71+
inc.update(k.Close.Float64(), k.High.Float64()-k.Low.Float64())
72+
}
73+
74+
func (inc *GHFilter) CalculateAndUpdate(allKLines []types.KLine) {
75+
if inc.Values != nil {
76+
k := allKLines[len(allKLines)-1]
77+
inc.PushK(k)
78+
inc.EmitUpdate(inc.Last())
79+
return
80+
}
81+
for _, k := range allKLines {
82+
inc.PushK(k)
83+
inc.EmitUpdate(inc.Last())
84+
}
85+
}
86+
87+
func (inc *GHFilter) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
88+
if inc.Interval != interval {
89+
return
90+
}
91+
inc.CalculateAndUpdate(window)
92+
}
93+
94+
func (inc *GHFilter) Bind(updater KLineWindowUpdater) {
95+
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
96+
}

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)