mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
feature: add G-H filter and Kalman filter
- implement G-H (alpha beta) filter and Kalman filter - compare the predict accurateness with other indicator
This commit is contained in:
parent
843b81e132
commit
9c684c124c
96
pkg/indicator/ghfilter.go
Normal file
96
pkg/indicator/ghfilter.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
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()
|
||||
}
|
||||
|
||||
var _ types.SeriesExtend = &GHFilter{}
|
||||
|
||||
func (inc *GHFilter) PushK(k types.KLine) {
|
||||
inc.update(k.Close.Float64(), k.High.Float64()-k.Low.Float64())
|
||||
}
|
||||
|
||||
func (inc *GHFilter) CalculateAndUpdate(allKLines []types.KLine) {
|
||||
if inc.Values != nil {
|
||||
k := allKLines[len(allKLines)-1]
|
||||
inc.PushK(k)
|
||||
inc.EmitUpdate(inc.Last())
|
||||
return
|
||||
}
|
||||
for _, k := range allKLines {
|
||||
inc.PushK(k)
|
||||
inc.EmitUpdate(inc.Last())
|
||||
}
|
||||
}
|
||||
|
||||
func (inc *GHFilter) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
||||
if inc.Interval != interval {
|
||||
return
|
||||
}
|
||||
inc.CalculateAndUpdate(window)
|
||||
}
|
||||
|
||||
func (inc *GHFilter) Bind(updater KLineWindowUpdater) {
|
||||
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
||||
}
|
15
pkg/indicator/ghfilter_callbacks.go
Normal file
15
pkg/indicator/ghfilter_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type GHFilter"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *GHFilter) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *GHFilter) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
6151
pkg/indicator/ghfilter_test.go
Normal file
6151
pkg/indicator/ghfilter_test.go
Normal file
File diff suppressed because it is too large
Load Diff
108
pkg/indicator/kalmanfilter.go
Normal file
108
pkg/indicator/kalmanfilter.go
Normal file
|
@ -0,0 +1,108 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/datatype/floats"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"math"
|
||||
)
|
||||
|
||||
// Refer: https://www.kalmanfilter.net/kalman1d.html
|
||||
// One-dimensional Kalman filter
|
||||
|
||||
//go:generate callbackgen -type KalmanFilter
|
||||
type KalmanFilter struct {
|
||||
types.SeriesBase
|
||||
types.IntervalWindow
|
||||
AdditionalSmoothWindow uint
|
||||
amp2 *types.Queue // measurement uncertainty
|
||||
k float64 // Kalman gain
|
||||
measurements *types.Queue
|
||||
Values floats.Slice
|
||||
|
||||
UpdateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) Update(value float64) {
|
||||
var measureMove = value
|
||||
if inc.measurements != nil {
|
||||
measureMove = value - inc.measurements.Last()
|
||||
}
|
||||
inc.update(value, math.Abs(measureMove))
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) update(value, amp float64) {
|
||||
if len(inc.Values) == 0 {
|
||||
inc.amp2 = types.NewQueue(inc.Window)
|
||||
inc.amp2.Update(amp * amp)
|
||||
inc.measurements = types.NewQueue(inc.Window)
|
||||
inc.measurements.Update(value)
|
||||
inc.Values.Push(value)
|
||||
return
|
||||
}
|
||||
|
||||
// measurement
|
||||
inc.measurements.Update(value)
|
||||
inc.amp2.Update(amp * amp)
|
||||
q := math.Sqrt(types.Mean(inc.amp2)) * float64(1+inc.AdditionalSmoothWindow)
|
||||
|
||||
// update
|
||||
lastPredict := inc.Values.Last()
|
||||
curState := value + (value - lastPredict)
|
||||
estimated := lastPredict + inc.k*(curState-lastPredict)
|
||||
|
||||
// predict
|
||||
inc.Values.Push(estimated)
|
||||
p := math.Abs(curState - estimated)
|
||||
inc.k = p / (p + q)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) Index(i int) float64 {
|
||||
if inc.Values == nil {
|
||||
return 0.0
|
||||
}
|
||||
return inc.Values.Index(i)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) Length() int {
|
||||
if inc.Values == nil {
|
||||
return 0
|
||||
}
|
||||
return inc.Values.Length()
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) Last() float64 {
|
||||
if inc.Values == nil {
|
||||
return 0.0
|
||||
}
|
||||
return inc.Values.Last()
|
||||
}
|
||||
|
||||
var _ types.SeriesExtend = &KalmanFilter{}
|
||||
|
||||
func (inc *KalmanFilter) PushK(k types.KLine) {
|
||||
inc.update(k.Close.Float64(), (k.High.Float64()-k.Low.Float64())/2)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) CalculateAndUpdate(allKLines []types.KLine) {
|
||||
if inc.Values != nil {
|
||||
k := allKLines[len(allKLines)-1]
|
||||
inc.PushK(k)
|
||||
inc.EmitUpdate(inc.Last())
|
||||
return
|
||||
}
|
||||
for _, k := range allKLines {
|
||||
inc.PushK(k)
|
||||
inc.EmitUpdate(inc.Last())
|
||||
}
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
||||
if inc.Interval != interval {
|
||||
return
|
||||
}
|
||||
inc.CalculateAndUpdate(window)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) Bind(updater KLineWindowUpdater) {
|
||||
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
||||
}
|
15
pkg/indicator/kalmanfilter_callbacks.go
Normal file
15
pkg/indicator/kalmanfilter_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type KalmanFilter"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *KalmanFilter) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *KalmanFilter) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
6191
pkg/indicator/kalmanfilter_test.go
Normal file
6191
pkg/indicator/kalmanfilter_test.go
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user