2016-07-15 12:02:50 -04:00
|
|
|
package chart
|
|
|
|
|
2017-04-29 19:18:09 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-30 03:39:38 -04:00
|
|
|
"github.com/wcharczuk/go-chart/sequence"
|
2017-04-29 19:18:09 -04:00
|
|
|
)
|
2016-07-15 12:02:50 -04:00
|
|
|
|
|
|
|
// BollingerBandsSeries draws bollinger bands for an inner series.
|
|
|
|
// Bollinger bands are defined by two lines, one at SMA+k*stddev, one at SMA-k*stdev.
|
|
|
|
type BollingerBandsSeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
2016-07-31 19:54:09 -04:00
|
|
|
YAxis YAxisType
|
2016-07-15 12:02:50 -04:00
|
|
|
|
2016-07-18 18:06:42 -04:00
|
|
|
Period int
|
2016-07-15 12:02:50 -04:00
|
|
|
K float64
|
2017-04-28 19:07:36 -04:00
|
|
|
InnerSeries ValuesProvider
|
2016-07-15 12:02:50 -04:00
|
|
|
|
2017-04-30 03:39:38 -04:00
|
|
|
valueBuffer *sequence.Buffer
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the time series.
|
|
|
|
func (bbs BollingerBandsSeries) GetName() string {
|
|
|
|
return bbs.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
|
|
|
func (bbs BollingerBandsSeries) GetStyle() Style {
|
|
|
|
return bbs.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYAxis returns which YAxis the series draws on.
|
2016-07-31 19:54:09 -04:00
|
|
|
func (bbs BollingerBandsSeries) GetYAxis() YAxisType {
|
2016-07-15 12:02:50 -04:00
|
|
|
return bbs.YAxis
|
|
|
|
}
|
|
|
|
|
2016-07-18 18:06:42 -04:00
|
|
|
// GetPeriod returns the window size.
|
|
|
|
func (bbs BollingerBandsSeries) GetPeriod() int {
|
|
|
|
if bbs.Period == 0 {
|
2016-07-17 14:10:04 -04:00
|
|
|
return DefaultSimpleMovingAveragePeriod
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
2016-07-18 18:06:42 -04:00
|
|
|
return bbs.Period
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:07:36 -04:00
|
|
|
// GetK returns the K value, or the number of standard deviations above and below
|
|
|
|
// to band the simple moving average with.
|
|
|
|
// Typical K value is 2.0.
|
2016-07-15 12:17:51 -04:00
|
|
|
func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 {
|
|
|
|
if bbs.K == 0 {
|
|
|
|
if len(defaults) > 0 {
|
|
|
|
return defaults[0]
|
|
|
|
}
|
|
|
|
return 2.0
|
|
|
|
}
|
|
|
|
return bbs.K
|
|
|
|
}
|
|
|
|
|
2016-07-15 12:02:50 -04:00
|
|
|
// Len returns the number of elements in the series.
|
2017-04-28 19:07:36 -04:00
|
|
|
func (bbs BollingerBandsSeries) Len() int {
|
2016-07-15 12:02:50 -04:00
|
|
|
return bbs.InnerSeries.Len()
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:07:36 -04:00
|
|
|
// GetBoundedValues gets the bounded value for the series.
|
|
|
|
func (bbs *BollingerBandsSeries) GetBoundedValues(index int) (x, y1, y2 float64) {
|
2016-07-15 12:02:50 -04:00
|
|
|
if bbs.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bbs.valueBuffer == nil || index == 0 {
|
2017-04-30 03:39:38 -04:00
|
|
|
bbs.valueBuffer = sequence.NewBufferWithCapacity(bbs.GetPeriod())
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
2016-07-18 18:06:42 -04:00
|
|
|
if bbs.valueBuffer.Len() >= bbs.GetPeriod() {
|
2016-07-15 12:02:50 -04:00
|
|
|
bbs.valueBuffer.Dequeue()
|
|
|
|
}
|
2017-04-28 19:07:36 -04:00
|
|
|
px, py := bbs.InnerSeries.GetValues(index)
|
2016-07-15 12:02:50 -04:00
|
|
|
bbs.valueBuffer.Enqueue(py)
|
|
|
|
x = px
|
|
|
|
|
2017-04-30 03:39:38 -04:00
|
|
|
ay := sequence.Seq{Provider: bbs.valueBuffer}.Average()
|
|
|
|
std := sequence.Seq{Provider: bbs.valueBuffer}.StdDev()
|
2016-07-15 12:02:50 -04:00
|
|
|
|
2016-07-15 12:17:51 -04:00
|
|
|
y1 = ay + (bbs.GetK() * std)
|
|
|
|
y2 = ay - (bbs.GetK() * std)
|
2016-07-15 12:02:50 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:07:36 -04:00
|
|
|
// GetBoundedLastValues returns the last bounded value for the series.
|
|
|
|
func (bbs *BollingerBandsSeries) GetBoundedLastValues() (x, y1, y2 float64) {
|
2016-07-15 16:27:45 -04:00
|
|
|
if bbs.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-07-18 18:06:42 -04:00
|
|
|
period := bbs.GetPeriod()
|
2016-07-15 16:27:45 -04:00
|
|
|
seriesLength := bbs.InnerSeries.Len()
|
2016-07-18 18:06:42 -04:00
|
|
|
startAt := seriesLength - period
|
2016-07-15 16:27:45 -04:00
|
|
|
if startAt < 0 {
|
|
|
|
startAt = 0
|
|
|
|
}
|
|
|
|
|
2017-04-30 03:39:38 -04:00
|
|
|
vb := sequence.NewBufferWithCapacity(period)
|
2016-07-15 16:27:45 -04:00
|
|
|
for index := startAt; index < seriesLength; index++ {
|
2017-04-28 19:07:36 -04:00
|
|
|
xn, yn := bbs.InnerSeries.GetValues(index)
|
2016-07-15 16:27:45 -04:00
|
|
|
vb.Enqueue(yn)
|
|
|
|
x = xn
|
|
|
|
}
|
|
|
|
|
2017-04-30 03:39:38 -04:00
|
|
|
ay := sequence.Seq{Provider: vb}.Average()
|
|
|
|
std := sequence.Seq{Provider: vb}.StdDev()
|
2016-07-15 16:27:45 -04:00
|
|
|
|
|
|
|
y1 = ay + (bbs.GetK() * std)
|
|
|
|
y2 = ay - (bbs.GetK() * std)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-15 12:02:50 -04:00
|
|
|
// Render renders the series.
|
2016-07-15 20:15:06 -04:00
|
|
|
func (bbs *BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
2016-07-21 17:11:27 -04:00
|
|
|
s := bbs.Style.InheritFrom(defaults.InheritFrom(Style{
|
2016-07-15 20:15:06 -04:00
|
|
|
StrokeWidth: 1.0,
|
|
|
|
StrokeColor: DefaultAxisColor.WithAlpha(64),
|
|
|
|
FillColor: DefaultAxisColor.WithAlpha(32),
|
|
|
|
}))
|
|
|
|
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod())
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:26:53 -05:00
|
|
|
// Validate validates the series.
|
|
|
|
func (bbs BollingerBandsSeries) Validate() error {
|
|
|
|
if bbs.InnerSeries == nil {
|
|
|
|
return fmt.Errorf("bollinger bands series requires InnerSeries to be set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|