-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
ewma.go
92 lines (72 loc) · 2.02 KB
/
ewma.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
package indicator
import (
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// These numbers should be aligned with bbgo MaxNumOfKLines and MaxNumOfKLinesTruncate
const MaxNumOfEWMA = 1_000
const MaxNumOfEWMATruncateSize = 500
//go:generate callbackgen -type EWMA
type EWMA struct {
types.IntervalWindow
types.SeriesBase
Values floats.Slice
EndTime time.Time
updateCallbacks []func(value float64)
}
var _ types.SeriesExtend = &EWMA{}
func (inc *EWMA) Clone() *EWMA {
out := &EWMA{
IntervalWindow: inc.IntervalWindow,
Values: inc.Values[:],
}
out.SeriesBase.Series = out
return out
}
func (inc *EWMA) TestUpdate(value float64) *EWMA {
out := inc.Clone()
out.Update(value)
return out
}
func (inc *EWMA) Update(value float64) {
var multiplier = 2.0 / float64(1+inc.Window)
if len(inc.Values) == 0 {
inc.SeriesBase.Series = inc
inc.Values.Push(value)
return
} else if len(inc.Values) > MaxNumOfEWMA {
inc.Values = inc.Values[MaxNumOfEWMATruncateSize-1:]
}
ema := (1-multiplier)*inc.Last(0) + multiplier*value
inc.Values.Push(ema)
}
func (inc *EWMA) Last(i int) float64 {
return inc.Values.Last(i)
}
func (inc *EWMA) Index(i int) float64 {
return inc.Last(i)
}
func (inc *EWMA) Length() int {
return len(inc.Values)
}
func (inc *EWMA) PushK(k types.KLine) {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
return
}
inc.Update(k.Close.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last(0))
}
func CalculateKLinesEMA(allKLines []types.KLine, priceF types.KLineValueMapper, window int) float64 {
var multiplier = 2.0 / (float64(window) + 1)
return ewma(types.MapKLinePrice(allKLines, priceF), multiplier)
}
// see https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
func ewma(prices []float64, multiplier float64) float64 {
var end = len(prices) - 1
if end == 0 {
return prices[0]
}
return prices[end]*multiplier + (1-multiplier)*ewma(prices[:end], multiplier)
}