go-chart/bollinger_band_series.go

150 lines
3.5 KiB
Go
Raw Normal View History

2016-07-15 18:02:50 +02:00
package chart
import "math"
// 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-08-01 01:54:09 +02:00
YAxis YAxisType
2016-07-15 18:02:50 +02:00
2016-07-19 00:06:42 +02:00
Period int
2016-07-15 18:02:50 +02:00
K float64
InnerSeries ValueProvider
valueBuffer *RingBuffer
}
// 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-08-01 01:54:09 +02:00
func (bbs BollingerBandsSeries) GetYAxis() YAxisType {
2016-07-15 18:02:50 +02:00
return bbs.YAxis
}
2016-07-19 00:06:42 +02:00
// GetPeriod returns the window size.
func (bbs BollingerBandsSeries) GetPeriod() int {
if bbs.Period == 0 {
return DefaultSimpleMovingAveragePeriod
2016-07-15 18:02:50 +02:00
}
2016-07-19 00:06:42 +02:00
return bbs.Period
2016-07-15 18:02:50 +02:00
}
2016-07-15 18:17:51 +02:00
// GetK returns the K value.
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 18:02:50 +02:00
// Len returns the number of elements in the series.
func (bbs *BollingerBandsSeries) Len() int {
return bbs.InnerSeries.Len()
}
// GetBoundedValue gets the bounded value for the series.
func (bbs *BollingerBandsSeries) GetBoundedValue(index int) (x, y1, y2 float64) {
if bbs.InnerSeries == nil {
return
}
if bbs.valueBuffer == nil || index == 0 {
2016-07-19 00:06:42 +02:00
bbs.valueBuffer = NewRingBufferWithCapacity(bbs.GetPeriod())
2016-07-15 18:02:50 +02:00
}
2016-07-19 00:06:42 +02:00
if bbs.valueBuffer.Len() >= bbs.GetPeriod() {
2016-07-15 18:02:50 +02:00
bbs.valueBuffer.Dequeue()
}
px, py := bbs.InnerSeries.GetValue(index)
bbs.valueBuffer.Enqueue(py)
x = px
ay := bbs.getAverage(bbs.valueBuffer)
std := bbs.getStdDev(bbs.valueBuffer)
2016-07-15 18:17:51 +02:00
y1 = ay + (bbs.GetK() * std)
y2 = ay - (bbs.GetK() * std)
2016-07-15 18:02:50 +02:00
return
}
2016-07-19 00:47:33 +02:00
// GetBoundedLastValue returns the last bounded value for the series.
func (bbs *BollingerBandsSeries) GetBoundedLastValue() (x, y1, y2 float64) {
2016-07-15 22:27:45 +02:00
if bbs.InnerSeries == nil {
return
}
2016-07-19 00:06:42 +02:00
period := bbs.GetPeriod()
2016-07-15 22:27:45 +02:00
seriesLength := bbs.InnerSeries.Len()
2016-07-19 00:06:42 +02:00
startAt := seriesLength - period
2016-07-15 22:27:45 +02:00
if startAt < 0 {
startAt = 0
}
2016-07-19 00:06:42 +02:00
vb := NewRingBufferWithCapacity(period)
2016-07-15 22:27:45 +02:00
for index := startAt; index < seriesLength; index++ {
xn, yn := bbs.InnerSeries.GetValue(index)
vb.Enqueue(yn)
x = xn
}
ay := bbs.getAverage(vb)
std := bbs.getStdDev(vb)
y1 = ay + (bbs.GetK() * std)
y2 = ay - (bbs.GetK() * std)
return
}
2016-07-15 18:02:50 +02:00
// Render renders the series.
func (bbs *BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
s := bbs.Style.InheritFrom(defaults.InheritFrom(Style{
StrokeWidth: 1.0,
StrokeColor: DefaultAxisColor.WithAlpha(64),
FillColor: DefaultAxisColor.WithAlpha(32),
}))
2016-07-30 01:36:29 +02:00
Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod())
2016-07-15 18:02:50 +02:00
}
func (bbs BollingerBandsSeries) getAverage(valueBuffer *RingBuffer) float64 {
var accum float64
valueBuffer.Each(func(v interface{}) {
if typed, isTyped := v.(float64); isTyped {
accum += typed
}
})
return accum / float64(valueBuffer.Len())
}
func (bbs BollingerBandsSeries) getVariance(valueBuffer *RingBuffer) float64 {
if valueBuffer.Len() == 0 {
return 0
}
var variance float64
m := bbs.getAverage(valueBuffer)
valueBuffer.Each(func(v interface{}) {
if n, isTyped := v.(float64); isTyped {
variance += (float64(n) - m) * (float64(n) - m)
}
})
return variance / float64(valueBuffer.Len())
}
func (bbs BollingerBandsSeries) getStdDev(valueBuffer *RingBuffer) float64 {
return math.Pow(bbs.getVariance(valueBuffer), 0.5)
}