2022-04-19 10:22:22 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: Hull Moving Average
|
|
|
|
// Refer URL: https://fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/hull-moving-average
|
|
|
|
//go:generate callbackgen -type HULL
|
|
|
|
type HULL struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-04-19 10:22:22 +00:00
|
|
|
types.IntervalWindow
|
2022-04-21 10:28:11 +00:00
|
|
|
ma1 *EWMA
|
|
|
|
ma2 *EWMA
|
2022-04-19 10:22:22 +00:00
|
|
|
result *EWMA
|
|
|
|
|
2022-07-13 17:16:39 +00:00
|
|
|
updateCallbacks []func(value float64)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 10:26:52 +00:00
|
|
|
var _ types.SeriesExtend = &HULL{}
|
|
|
|
|
2022-04-19 10:22:22 +00:00
|
|
|
func (inc *HULL) Update(value float64) {
|
2022-04-22 10:02:26 +00:00
|
|
|
if inc.result == nil {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2022-08-24 10:17:37 +00:00
|
|
|
inc.ma1 = &EWMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: inc.Window / 2}}
|
|
|
|
inc.ma2 = &EWMA{IntervalWindow: inc.IntervalWindow}
|
|
|
|
inc.result = &EWMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: int(math.Sqrt(float64(inc.Window)))}}
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
inc.ma1.Update(value)
|
|
|
|
inc.ma2.Update(value)
|
2022-04-21 10:28:11 +00:00
|
|
|
inc.result.Update(2*inc.ma1.Last() - inc.ma2.Last())
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *HULL) Last() float64 {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.result == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.result.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *HULL) Index(i int) float64 {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.result == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.result.Index(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *HULL) Length() int {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.result == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.result.Length()
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:26:52 +00:00
|
|
|
func (inc *HULL) PushK(k types.KLine) {
|
2022-07-26 10:35:50 +00:00
|
|
|
if inc.ma1 != nil && inc.ma1.Length() > 0 && k.EndTime.Before(inc.ma1.EndTime) {
|
2022-04-21 10:28:11 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
|
2022-07-26 10:26:52 +00:00
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
inc.EmitUpdate(inc.Last())
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|