-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
ema.go
53 lines (43 loc) · 852 Bytes
/
ema.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
package indicator
import (
"fmt"
"time"
"github.com/rodrigo-brito/ninjabot/model"
"github.com/rodrigo-brito/ninjabot/plot"
"github.com/markcheno/go-talib"
)
func EMA(period int, color string) plot.Indicator {
return &ema{
Period: period,
Color: color,
}
}
type ema struct {
Period int
Color string
Values model.Series
Time []time.Time
}
func (e ema) Name() string {
return fmt.Sprintf("EMA(%d)", e.Period)
}
func (e ema) Overlay() bool {
return true
}
func (e *ema) Load(dataframe *model.Dataframe) {
if len(dataframe.Time) < e.Period {
return
}
e.Values = talib.Ema(dataframe.Close, e.Period)[e.Period:]
e.Time = dataframe.Time[e.Period:]
}
func (e ema) Metrics() []plot.IndicatorMetric {
return []plot.IndicatorMetric{
{
Style: "line",
Color: e.Color,
Values: e.Values,
Time: e.Time,
},
}
}