-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbollinger.go
366 lines (297 loc) · 9.35 KB
/
bollinger.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package technical
/*
* A Bollinger Band is a techinical analysis volatility indicator developed by John Bollinger in the 1980s.
*
* They provide a relative indication of high and low prices in the market for a given time period.
*
* Bollinger bands consist of a center line (usually a EMA of a stock price) and two price channels (bands)
* above and below the center line scaled by some standard deviation.
*/
// Bound64 represents a pair of lower and upper values and a midpoint
type Bound64 struct {
Lower float64 `json:"lower"`
Midpoint float64 `json:"midpoint"`
Upper float64 `json:"upper"`
}
// Bound32 is a 32 bit version of Bound54
type Bound32 struct {
Lower float32 `json:"lower"`
Midpoint float32 `json:"midpoint"`
Upper float32 `json:"upper"`
}
// RoundBoundToNearestCent64 ceils a Bollinger Bound upper bound and floors the lower bound to near cent
func RoundBoundToNearestCent64(b *Bound64) {
// ceil upper
b.Upper = RoundUp64(b.Upper, 2)
// floor lower
b.Lower = RoundDown64(b.Lower, 2)
}
// RoundBoundtoNearestCent32 is 32 bit version of RoundBoundtoNearestCent64
func RoundBoundToNearestCent32(b *Bound32) {
// ceil upper
b.Upper = RoundUp32(b.Upper, 2)
// floor lower
b.Lower = RoundDown32(b.Lower, 2)
}
// CompareBound64 checks if the first bound is wider than the second bound
func CompareBound64(first *Bound64, second *Bound64) bool {
firstBoundRange := first.Upper - first.Lower
secondBoundRange := second.Upper - second.Lower
if firstBoundRange > secondBoundRange {
return true
}
return false
}
// CompareBound32 is 32 bit version of CompareBound64
func CompareBound32(first *Bound32, second *Bound32) bool {
firstBoundRange := first.Upper - first.Lower
secondBoundRange := second.Upper - second.Lower
if firstBoundRange > secondBoundRange {
return true
}
return false
}
// BollBound64 creates a float64 Bollinger Bound for the given period
// A period is a predetermined segment of a data series
//
// Parameters:
// period: list of data values
// k: midpoint of the bound in the given period
// a (alpha): multiplier on the standard deviation of the period
func BollBound64(period []float64, k float64, a float64) Bound64 {
var b Bound64
leg := StdDev64(period) * a
b.Midpoint = k
b.Lower = k - leg
b.Upper = k + leg
return b
}
// BollBound32 is 32 bit version of BollBound64
func BollBound32(period []float32, k float32, a float32) Bound32 {
var b Bound32
leg := StdDev32(period) * a
b.Midpoint = k
b.Lower = k - leg
b.Upper = k + leg
return b
}
// RollingerBollingerConst64 computes a float64 Bollinger Bound for a given period in a series
// Uses a constant specified midpoint for the Bollinger Bound
// Usage: Call while iterating over a series of values to build a full Bollinger Band.
//
// Parameters:
// period: list of float values
// k: static midpoint of the bound for a period
// a (alpha): multiplier on the standard deviation of the period
func RollingBollingerConst64(period []float64, k float64, a float64) Bound64 {
return BollBound64(period, k, a)
}
// RollingBollingerConst32 is 32 bit version of RollingBollingerConst64
func RollingBollingerConst32(period []float32, k float32, a float32) Bound32 {
return BollBound32(period, k, a)
}
// RollingerBollingerSMA64 computes a float64 Bollinger Bound for a given period in a series
// Uses a rolling Simple Moving Average midpoint for the Bollinger Bound
// Usage: Call while iterating over a series of values to build a full Bollinger Band.
//
// Parameters:
// period: list of float values
// a (alpha): multiplier on the standard deviation of the period
func RollingBollingerSMA64(period []float64, a float64) Bound64 {
if period == nil || len(period) == 0 {
return Bound64{}
}
return BollBound64(period, SimpleAvg64(period), a)
}
// RollingBollingerSMA32 is 32 bit version of ROllingBollingerSMA64
func RollingBollingerSMA32(period []float32, a float32) Bound32 {
if period == nil || len(period) == 0 {
return Bound32{}
}
return BollBound32(period, SimpleAvg32(period), a)
}
// RollingBollingerEMA64 computes a float64 Bollinger Bound for a given period in a series
// Uses a rolling Exponentially Weighted Moving Average midpoint for the Bollinger Bound
// Usage: Call while iterating over a series of values to build a full Bollinger Band.
//
// Parameters:
// period: list of float values
// y (lambda): smoothing factor for EWMA
// v: current underlying value in the series
// last: last EMA midpoint of the prior bound. The first bound midpoint should be computed using a Simple Average
// a (alpha): multiplier on the standard deviation of the period
// CONSTAINT: 0.0 < y < 1.0
func RollingBollingerEMA64(period []float64, y float64, v float64, last float64, a float64) Bound64 {
var (
b Bound64
k float64
)
if period == nil || len(period) == 0 {
return b
}
k = RollingEMA64(v, last, y)
return BollBound64(period, k, a)
}
// RollingBollingerEMA32 is 32 bit version of RollingBollingerEMA64
func RollingBollingerEMA32(period []float32, y float32, v float32, last float32, a float32) Bound32 {
var (
b Bound32
k float32
)
if period == nil || len(period) == 0 {
return b
}
k = RollingEMA32(v, last, y)
return BollBound32(period, k, a)
}
// StaticBollingerConst64 creates a float64 Bollinger Band using a static standard deviation multiplier and period lookback
// If time series data, assumes ascending order.
// Uses a constant specified midpoint for the Bollinger Bound
//
// Parameters:
// series: data series
// lb: lookback to derive a period
// k: static midpoint of the bound for a period
// a (alpha): multiplier on the standard deviation of the period
func StaticBollingerConst64(series []float64, lb int, k float64, a float64) []Bound64 {
if series == nil || len(series) == 0 {
return nil
}
band := make([]Bound64, len(series))
for i := range series {
j := i + 1 // offset by 1 bc of idx
if j < lb {
band[i] = Bound64{}
continue
}
band[i] = BollBound64(series[j-lb:j], k, a)
}
return band
}
// StaticBollingerConst32 is 32 bit version of StaticBollingerConst64
func StaticBollingerConst32(series []float32, lb int, k float32, a float32) []Bound32 {
if series == nil || len(series) == 0 {
return nil
}
band := make([]Bound32, len(series))
for i := range series {
j := i + 1 // offset by 1 bc of idx
if j < lb {
band[i] = Bound32{}
continue
}
band[i] = BollBound32(series[j-lb:j], k, a)
}
return band
}
// StaticBollingerSMA64 creates a float64 Bollinger Band using a static standard deviation multiplier and period lookback
// If time series data, assumes ascending order.
// Uses a Simple Moving Average midpoint for the Bollinger Bound
//
// Parameters:
// series: data series
// lb: lookback to derive a period
// a (alpha): multiplier on the standard deviation of the period
func StaticBollingerSMA64(series []float64, lb int, a float64) []Bound64 {
if series == nil || len(series) == 0 {
return nil
}
band := make([]Bound64, len(series))
for i := range series {
j := i + 1 // offset by 1 bc of idx
if j < lb {
band[i] = Bound64{}
continue
}
period := series[j-lb : j]
band[i] = BollBound64(period, SimpleAvg64(period), a)
}
return band
}
// StaticBollingerSMA32 is 32 bit version of StaticBollingerSMA64
func StaticBollingerSMA32(series []float32, lb int, a float32) []Bound32 {
if series == nil || len(series) == 0 {
return nil
}
band := make([]Bound32, len(series))
for i := range series {
j := i + 1
if j < lb {
band[i] = Bound32{}
continue
}
period := series[j-lb : j]
band[i] = BollBound32(period, SimpleAvg32(period), a)
}
return band
}
// StaticBollingerEMA64 creates a float64 Bollinger Band using a static standard deviation multiplier and period lookback
// If time series data, assumes ascending order.
// Uses an Exponentially Weighted Moving Average midpoint for the Bollinger Bound
//
// Parameters:
// series: data series
// lb: lookback to derive a period
// y (lambda): smoothing factor for rolling EWMA. If 0.0 use default formulaic calculation.
// a (alpha): multiplier on the standard deviation of the period
func StaticBollingerEMA64(series []float64, lb int, y float64, a float64) []Bound64 {
if series == nil || len(series) == 0 {
return nil
}
var (
k float64
last float64
)
if y == 0.0 { // use default smoothing
y = 2.0 / float64(lb+1)
}
band := make([]Bound64, len(series))
for i, v := range series {
j := i + 1
if j < lb {
band[i] = Bound64{}
continue
}
period := series[j-lb : j]
if j == lb { // first bound is simple avg
k = SimpleAvg64(period)
last = k
} else {
k = RollingEMA64(v, last, y)
last = k
}
band[i] = BollBound64(period, k, a)
}
return band
}
// StaticBollingerEMA32 is 32 bit version of StaticBollingerEMA64
func StaticBollingerEMA32(series []float32, lb int, y float32, a float32) []Bound32 {
if series == nil || len(series) == 0 {
return nil
}
var (
k float32
last float32
)
if y == 0.0 { // use default smoothing
y = 2.0 / float32(lb+1)
}
band := make([]Bound32, len(series))
for i, v := range series {
j := i + 1 // offset by 1 bc of index
if j < lb {
band[i] = Bound32{} // empty bound
continue
}
period := series[j-lb : j] // inclusive of current bound
if j == lb { // first bound is simple avg
k = SimpleAvg32(period)
last = k
} else {
k = RollingEMA32(v, last, y)
last = k
}
band[i] = BollBound32(period, k, a)
}
return band
}