adding GetLastBoundedValue and tests

This commit is contained in:
Will Charczuk 2016-07-15 13:27:45 -07:00
parent d888440415
commit f8573f1123
2 changed files with 47 additions and 0 deletions

View File

@ -81,6 +81,34 @@ func (bbs *BollingerBandsSeries) GetBoundedValue(index int) (x, y1, y2 float64)
return return
} }
// GetLastBoundedValue returns the last bounded value for the series.
func (bbs *BollingerBandsSeries) GetLastBoundedValue() (x, y1, y2 float64) {
if bbs.InnerSeries == nil {
return
}
windowSize := bbs.GetWindowSize()
seriesLength := bbs.InnerSeries.Len()
startAt := seriesLength - windowSize
if startAt < 0 {
startAt = 0
}
vb := NewRingBufferWithCapacity(windowSize)
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
}
// Render renders the series. // Render renders the series.
func (bbs BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) { func (bbs BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
s := bbs.Style.WithDefaultsFrom(defaults) s := bbs.Style.WithDefaultsFrom(defaults)

View File

@ -1,6 +1,7 @@
package chart package chart
import ( import (
"math"
"testing" "testing"
"github.com/blendlabs/go-assert" "github.com/blendlabs/go-assert"
@ -30,3 +31,21 @@ func TestBollingerBandSeries(t *testing.T) {
assert.True(y1values[x] > y2values[x]) assert.True(y1values[x] > y2values[x])
} }
} }
func TestBollingerBandLastValue(t *testing.T) {
assert := assert.New(t)
s1 := mockValueProvider{
X: Seq(1.0, 100.0),
Y: Seq(1.0, 100.0),
}
bbs := &BollingerBandsSeries{
InnerSeries: s1,
}
x, y1, y2 := bbs.GetLastBoundedValue()
assert.Equal(100.0, x)
assert.Equal(100, math.Floor(y1))
assert.Equal(95, math.Floor(y2))
}