-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbbands.go
89 lines (79 loc) · 3.04 KB
/
bbands.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
package tart
// Developed by John Bollinger, Bollinger Bands are volatility
// bands placed above and below a moving average. Volatility
// is based on the standard deviation, which changes as volatility
// increases and decreases. The bands automatically widen when
// volatility increases and contract when volatility decreases.
// Their dynamic nature allows them to be used on different
// securities with the standard settings. Bollinger Bands can be
// used to identify M-Tops and W-Bottoms or to determine the
// strength of the trend. Signals based on the distance between
// the upper and lower band, including the popular Bollinger Band
// Squeeze, are identified using the related Bollinger BandWidth
// indicator.
// https://school.stockcharts.com/doku.php?id=technical_indicators:bollinger_bands
// https://www.investopedia.com/terms/b/bollingerbands.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/bollinger-bands
type BBands struct {
initPeriod int64
ma *Ma
stdDev *StdDev
upNStdDev float64
dnNStdDev float64
sz int64
}
func NewBBands(t MaType, n int64, upNStdDev, dnNStdDev float64) *BBands {
ma := NewMa(t, n)
stdDev := NewStdDev(n)
a := ma.InitPeriod()
b := stdDev.InitPeriod()
if a < b {
a = b
}
return &BBands{
initPeriod: a,
ma: ma,
stdDev: stdDev,
upNStdDev: upNStdDev,
dnNStdDev: dnNStdDev,
sz: 0,
}
}
// upper, middle, lower
func (b *BBands) Update(v float64) (float64, float64, float64) {
b.sz++
m := b.ma.Update(v)
stddev := b.stdDev.Update(v)
return m + b.upNStdDev*stddev, m, m - b.upNStdDev*stddev
}
func (b *BBands) InitPeriod() int64 {
return b.initPeriod
}
func (b *BBands) Valid() bool {
return b.sz > b.initPeriod
}
// Developed by John Bollinger, Bollinger Bands are volatility
// bands placed above and below a moving average. Volatility
// is based on the standard deviation, which changes as volatility
// increases and decreases. The bands automatically widen when
// volatility increases and contract when volatility decreases.
// Their dynamic nature allows them to be used on different
// securities with the standard settings. Bollinger Bands can be
// used to identify M-Tops and W-Bottoms or to determine the
// strength of the trend. Signals based on the distance between
// the upper and lower band, including the popular Bollinger Band
// Squeeze, are identified using the related Bollinger BandWidth
// indicator.
// https://school.stockcharts.com/doku.php?id=technical_indicators:bollinger_bands
// https://www.investopedia.com/terms/b/bollingerbands.asp
// https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/bollinger-bands
func BBandsArr(t MaType, in []float64, n int64, upNStdDev, dnNStdDev float64) ([]float64, []float64, []float64) {
m := make([]float64, len(in))
u := make([]float64, len(in))
l := make([]float64, len(in))
b := NewBBands(t, n, upNStdDev, dnNStdDev)
for i, v := range in {
u[i], m[i], l[i] = b.Update(v)
}
return u, m, l
}