2022-04-19 10:22:22 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
2022-08-25 09:31:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
2022-04-19 10:22:22 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: Zero Lag Exponential Moving Average
|
|
|
|
// Refer URL: https://en.wikipedia.org/wiki/Zero_lag_exponential_moving_average
|
2022-12-13 05:02:38 +00:00
|
|
|
//
|
|
|
|
// The Zero Lag Exponential Moving Average (ZLEMA) is a technical analysis indicator that is used to smooth price data and reduce the
|
|
|
|
// lag associated with traditional moving averages. It is calculated by taking the exponentially weighted moving average of the input
|
|
|
|
// data, and then applying a digital filter to the resulting average to eliminate any remaining lag. This filtered average is then
|
|
|
|
// plotted on the price chart as a line, which can be used to make predictions about future price movements. The ZLEMA is typically more
|
|
|
|
// responsive to changes in the underlying data than a simple moving average, but may be less reliable in trending markets.
|
2022-04-19 10:22:22 +00:00
|
|
|
|
|
|
|
//go:generate callbackgen -type ZLEMA
|
|
|
|
type ZLEMA struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-04-19 10:22:22 +00:00
|
|
|
types.IntervalWindow
|
|
|
|
|
2022-08-25 09:31:42 +00:00
|
|
|
data floats.Slice
|
2022-04-19 10:22:22 +00:00
|
|
|
zlema *EWMA
|
|
|
|
lag int
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
updateCallbacks []func(value float64)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) Index(i int) float64 {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.zlema == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.zlema.Index(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) Last() float64 {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.zlema == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.zlema.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) Length() int {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.zlema == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-04-19 10:22:22 +00:00
|
|
|
return inc.zlema.Length()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) Update(value float64) {
|
|
|
|
if inc.lag == 0 || inc.zlema == nil {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2022-08-24 10:17:37 +00:00
|
|
|
inc.zlema = &EWMA{IntervalWindow: inc.IntervalWindow}
|
2022-04-22 10:02:26 +00:00
|
|
|
inc.lag = int((float64(inc.Window)-1.)/2. + 0.5)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
2022-04-22 10:02:26 +00:00
|
|
|
inc.data.Push(value)
|
|
|
|
if len(inc.data) > MaxNumOfEWMA {
|
|
|
|
inc.data = inc.data[MaxNumOfEWMATruncateSize-1:]
|
|
|
|
}
|
|
|
|
if inc.lag >= inc.data.Length() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
emaData := 2.*value - inc.data[len(inc.data)-1-inc.lag]
|
2022-04-19 10:22:22 +00:00
|
|
|
inc.zlema.Update(emaData)
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &ZLEMA{}
|
2022-04-19 10:22:22 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *ZLEMA) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) CalculateAndUpdate(allKLines []types.KLine) {
|
2022-04-26 08:32:31 +00:00
|
|
|
if inc.zlema == nil {
|
|
|
|
for _, k := range allKLines {
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.PushK(k)
|
2022-04-26 08:32:31 +00:00
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 17:12:36 +00:00
|
|
|
k := allKLines[len(allKLines)-1]
|
|
|
|
inc.PushK(k)
|
2022-04-19 10:22:22 +00:00
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2022-04-19 10:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *ZLEMA) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|