-
Notifications
You must be signed in to change notification settings - Fork 1
/
bollingerbands.go
77 lines (65 loc) · 1.63 KB
/
bollingerbands.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
package indicator
import (
"fmt"
"time"
"github.com/ezquant/azbot/azbot/model"
"github.com/ezquant/azbot/azbot/plot"
"github.com/markcheno/go-talib"
)
func BollingerBands(period int, stdDeviation float64, upDnBandColor, midBandColor string) plot.Indicator {
return &bollingerBands{
Period: period,
StdDeviation: stdDeviation,
UpDnBandColor: upDnBandColor,
MidBandColor: midBandColor,
}
}
type bollingerBands struct {
Period int
StdDeviation float64
UpDnBandColor string
MidBandColor string
UpperBand model.Series[float64]
MiddleBand model.Series[float64]
LowerBand model.Series[float64]
Time []time.Time
}
func (bb bollingerBands) Warmup() int {
return bb.Period
}
func (bb bollingerBands) Name() string {
return fmt.Sprintf("BB(%d, %.2f)", bb.Period, bb.StdDeviation)
}
func (bb bollingerBands) Overlay() bool {
return true
}
func (bb *bollingerBands) Load(dataframe *model.Dataframe) {
if len(dataframe.Time) < bb.Period {
return
}
upper, mid, lower := talib.BBands(dataframe.Close, bb.Period, bb.StdDeviation, bb.StdDeviation, talib.EMA)
bb.UpperBand, bb.MiddleBand, bb.LowerBand = upper[bb.Period:], mid[bb.Period:], lower[bb.Period:]
bb.Time = dataframe.Time[bb.Period:]
}
func (bb bollingerBands) Metrics() []plot.IndicatorMetric {
return []plot.IndicatorMetric{
{
Style: "line",
Color: bb.UpDnBandColor,
Values: bb.UpperBand,
Time: bb.Time,
},
{
Style: "line",
Color: bb.MidBandColor,
Values: bb.MiddleBand,
Time: bb.Time,
},
{
Style: "line",
Color: bb.UpDnBandColor,
Values: bb.LowerBand,
Time: bb.Time,
},
}
}