| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
var ( |
| 9 |
_ Series = (*BollingerBandsSeries)(nil) |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type BollingerBandsSeries struct { |
| 15 |
Name string |
| 16 |
Style Style |
| 17 |
YAxis YAxisType |
| 18 |
|
| 19 |
Period int |
| 20 |
K float64 |
| 21 |
InnerSeries ValuesProvider |
| 22 |
|
| 23 |
valueBuffer *ValueBuffer |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (bbs BollingerBandsSeries) GetName() string { |
| 28 |
return bbs.Name |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (bbs BollingerBandsSeries) GetStyle() Style { |
| 33 |
return bbs.Style |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (bbs BollingerBandsSeries) GetYAxis() YAxisType { |
| 38 |
return bbs.YAxis |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (bbs BollingerBandsSeries) GetPeriod() int { |
| 43 |
if bbs.Period == 0 { |
| 44 |
return DefaultSimpleMovingAveragePeriod |
| 45 |
} |
| 46 |
return bbs.Period |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 { |
| 53 |
if bbs.K == 0 { |
| 54 |
if len(defaults) > 0 { |
| 55 |
return defaults[0] |
| 56 |
} |
| 57 |
return 2.0 |
| 58 |
} |
| 59 |
return bbs.K |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func (bbs BollingerBandsSeries) Len() int { |
| 64 |
return bbs.InnerSeries.Len() |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (bbs *BollingerBandsSeries) GetBoundedValues(index int) (x, y1, y2 float64) { |
| 69 |
if bbs.InnerSeries == nil { |
| 70 |
return |
| 71 |
} |
| 72 |
if bbs.valueBuffer == nil || index == 0 { |
| 73 |
bbs.valueBuffer = NewValueBufferWithCapacity(bbs.GetPeriod()) |
| 74 |
} |
| 75 |
if bbs.valueBuffer.Len() >= bbs.GetPeriod() { |
| 76 |
bbs.valueBuffer.Dequeue() |
| 77 |
} |
| 78 |
px, py := bbs.InnerSeries.GetValues(index) |
| 79 |
bbs.valueBuffer.Enqueue(py) |
| 80 |
x = px |
| 81 |
|
| 82 |
ay := Seq{bbs.valueBuffer}.Average() |
| 83 |
std := Seq{bbs.valueBuffer}.StdDev() |
| 84 |
|
| 85 |
y1 = ay + (bbs.GetK() * std) |
| 86 |
y2 = ay - (bbs.GetK() * std) |
| 87 |
return |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (bbs *BollingerBandsSeries) GetBoundedLastValues() (x, y1, y2 float64) { |
| 92 |
if bbs.InnerSeries == nil { |
| 93 |
return |
| 94 |
} |
| 95 |
period := bbs.GetPeriod() |
| 96 |
seriesLength := bbs.InnerSeries.Len() |
| 97 |
startAt := seriesLength - period |
| 98 |
if startAt < 0 { |
| 99 |
startAt = 0 |
| 100 |
} |
| 101 |
|
| 102 |
vb := NewValueBufferWithCapacity(period) |
| 103 |
for index := startAt; index < seriesLength; index++ { |
| 104 |
xn, yn := bbs.InnerSeries.GetValues(index) |
| 105 |
vb.Enqueue(yn) |
| 106 |
x = xn |
| 107 |
} |
| 108 |
|
| 109 |
ay := Seq{vb}.Average() |
| 110 |
std := Seq{vb}.StdDev() |
| 111 |
|
| 112 |
y1 = ay + (bbs.GetK() * std) |
| 113 |
y2 = ay - (bbs.GetK() * std) |
| 114 |
|
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (bbs *BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) { |
| 120 |
s := bbs.Style.InheritFrom(defaults.InheritFrom(Style{ |
| 121 |
StrokeWidth: 1.0, |
| 122 |
StrokeColor: DefaultAxisColor.WithAlpha(64), |
| 123 |
FillColor: DefaultAxisColor.WithAlpha(32), |
| 124 |
})) |
| 125 |
|
| 126 |
Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod()) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func (bbs BollingerBandsSeries) Validate() error { |
| 131 |
if bbs.InnerSeries == nil { |
| 132 |
return fmt.Errorf("bollinger bands series requires InnerSeries to be set") |
| 133 |
} |
| 134 |
return nil |
| 135 |
} |