forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlema.go
93 lines (78 loc) · 2.43 KB
/
zlema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package indicator
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// Refer: Zero Lag Exponential Moving Average
// Refer URL: https://en.wikipedia.org/wiki/Zero_lag_exponential_moving_average
//
// 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.
//go:generate callbackgen -type ZLEMA
type ZLEMA struct {
types.SeriesBase
types.IntervalWindow
data floats.Slice
zlema *EWMA
lag int
updateCallbacks []func(value float64)
}
func (inc *ZLEMA) Index(i int) float64 {
return inc.Last(i)
}
func (inc *ZLEMA) Last(i int) float64 {
if inc.zlema == nil {
return 0
}
return inc.zlema.Last(i)
}
func (inc *ZLEMA) Length() int {
if inc.zlema == nil {
return 0
}
return inc.zlema.Length()
}
func (inc *ZLEMA) Update(value float64) {
if inc.lag == 0 || inc.zlema == nil {
inc.SeriesBase.Series = inc
inc.zlema = &EWMA{IntervalWindow: inc.IntervalWindow}
inc.lag = int((float64(inc.Window)-1.)/2. + 0.5)
}
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]
inc.zlema.Update(emaData)
}
var _ types.SeriesExtend = &ZLEMA{}
func (inc *ZLEMA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *ZLEMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.zlema == nil {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
}
func (inc *ZLEMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *ZLEMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}