-
Notifications
You must be signed in to change notification settings - Fork 17
/
ma.go
76 lines (64 loc) · 1.05 KB
/
ma.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
package tart
type MaType int
// TODO: support Mama & T3
const (
SMA MaType = iota
EMA
WMA
DEMA
TEMA
TRIMA
KAMA
)
// Convenient wrapper for different moving average types
type Ma struct {
mu maUpdater
}
func NewMa(t MaType, n int64) *Ma {
k := 2.0 / float64(n+1)
var mu maUpdater
switch t {
case SMA:
mu = NewSma(n)
case EMA:
mu = NewEma(n, k)
case WMA:
mu = NewWma(n)
case DEMA:
mu = NewDema(n, k)
case TEMA:
mu = NewTema(n, k)
case TRIMA:
mu = NewTrima(n)
case KAMA:
mu = NewKama(n)
default:
return nil
}
return &Ma{
mu: mu,
}
}
func (m *Ma) Update(v float64) float64 {
return m.mu.Update(v)
}
func (m *Ma) InitPeriod() int64 {
return m.mu.InitPeriod()
}
func (m *Ma) Valid() bool {
return m.mu.Valid()
}
// Convenient wrapper for different moving average types
func MaArr(t MaType, in []float64, n int64) []float64 {
out := make([]float64, len(in))
m := NewMa(t, n)
for i, v := range in {
out[i] = m.Update(v)
}
return out
}
type maUpdater interface {
Update(v float64) float64
InitPeriod() int64
Valid() bool
}