Debian Codegraph @

bollinger_band_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 )
6
7 // Interface Assertions.
8 var (
9 _ Series = (*BollingerBandsSeries)(nil)
10 )
11
12 // BollingerBandsSeries draws bollinger bands for an inner series.
13 // Bollinger bands are defined by two lines, one at SMA+k*stddev, one at SMA-k*stdev.
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 // GetName returns the name of the time series.
27 func (bbs BollingerBandsSeries) GetName() string {
28 return bbs.Name
29 }
30
31 // GetStyle returns the line style.
32 func (bbs BollingerBandsSeries) GetStyle() Style {
33 return bbs.Style
34 }
35
36 // GetYAxis returns which YAxis the series draws on.
37 func (bbs BollingerBandsSeries) GetYAxis() YAxisType {
38 return bbs.YAxis
39 }
40
41 // GetPeriod returns the window size.
42 func (bbs BollingerBandsSeries) GetPeriod() int {
43 if bbs.Period == 0 {
44 return DefaultSimpleMovingAveragePeriod
45 }
46 return bbs.Period
47 }
48
49 // GetK returns the K value, or the number of standard deviations above and below
50 // to band the simple moving average with.
51 // Typical K value is 2.0.
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 // Len returns the number of elements in the series.
63 func (bbs BollingerBandsSeries) Len() int {
64 return bbs.InnerSeries.Len()
65 }
66
67 // GetBoundedValues gets the bounded value for the series.
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 // GetBoundedLastValues returns the last bounded value for the series.
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 // Render renders the series.
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 // Validate validates the series.
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 }