-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dema.go
103 lines (86 loc) · 2.75 KB
/
dema.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
94
95
96
97
98
99
100
101
102
103
package indicator
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// Refer: Double Exponential Moving Average
// Refer URL: https://investopedia.com/terms/d/double-exponential-moving-average.asp
//
// The Double Exponential Moving Average (DEMA) 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 taking the exponentially weighted moving average of that result. This double-smoothing process helps to eliminate much of the noise
// in the original data and provides a more accurate representation of the underlying trend. The DEMA line is then plotted on the price chart,
// which can be used to make predictions about future price movements. The DEMA 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 DEMA
type DEMA struct {
types.IntervalWindow
types.SeriesBase
Values floats.Slice
a1 *EWMA
a2 *EWMA
UpdateCallbacks []func(value float64)
}
func (inc *DEMA) Clone() *DEMA {
out := &DEMA{
IntervalWindow: inc.IntervalWindow,
Values: inc.Values[:],
a1: inc.a1.Clone(),
a2: inc.a2.Clone(),
}
out.SeriesBase.Series = out
return out
}
func (inc *DEMA) TestUpdate(value float64) *DEMA {
out := inc.Clone()
out.Update(value)
return out
}
func (inc *DEMA) Update(value float64) {
if len(inc.Values) == 0 {
inc.SeriesBase.Series = inc
inc.a1 = &EWMA{IntervalWindow: inc.IntervalWindow}
inc.a2 = &EWMA{IntervalWindow: inc.IntervalWindow}
}
inc.a1.Update(value)
inc.a2.Update(inc.a1.Last(0))
inc.Values.Push(2*inc.a1.Last(0) - inc.a2.Last(0))
if len(inc.Values) > MaxNumOfEWMA {
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
}
}
func (inc *DEMA) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *DEMA) Index(i int) float64 {
return inc.Last(i)
}
func (inc *DEMA) Length() int {
return len(inc.Values)
}
var _ types.SeriesExtend = &DEMA{}
func (inc *DEMA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *DEMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.a1 == nil {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
} else {
// last k
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last(0))
}
}
func (inc *DEMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *DEMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}