mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
Merge pull request #915 from COLDTURNIP/feature/observer_filters
feature: add G-H filter and Kalman filter
This commit is contained in:
commit
7416f1074a
|
@ -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
pkg/indicator/ghfilter.go
Normal file
74
pkg/indicator/ghfilter.go
Normal file
|
@ -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
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
86
pkg/indicator/kalmanfilter.go
Normal file
86
pkg/indicator/kalmanfilter.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
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()
|
||||
}
|
||||
|
||||
// interfaces implementation check
|
||||
var _ Simple = &KalmanFilter{}
|
||||
var _ types.SeriesExtend = &KalmanFilter{}
|
||||
|
||||
func (inc *KalmanFilter) PushK(k types.KLine) {
|
||||
inc.update(k.Close.Float64(), (k.High.Float64()-k.Low.Float64())/2)
|
||||
}
|
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