Helper API refactor (#40)
* api cleaup * updates * wtf * updates * snapshot. * tweaks * snapshot * api tweaks. * updates * updates * updates * changes. * updates * updates * sequence => seq * dont need to use curl, just using wget * fixing examples
This commit is contained in:
parent
43212f871f
commit
03708a90ef
100 changed files with 1687 additions and 1055 deletions
|
|
@ -2,7 +2,8 @@ package chart
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/wcharczuk/go-chart/seq"
|
||||
)
|
||||
|
||||
// BollingerBandsSeries draws bollinger bands for an inner series.
|
||||
|
|
@ -14,9 +15,9 @@ type BollingerBandsSeries struct {
|
|||
|
||||
Period int
|
||||
K float64
|
||||
InnerSeries ValueProvider
|
||||
InnerSeries ValuesProvider
|
||||
|
||||
valueBuffer *RingBuffer
|
||||
valueBuffer *seq.Buffer
|
||||
}
|
||||
|
||||
// GetName returns the name of the time series.
|
||||
|
|
@ -42,7 +43,9 @@ func (bbs BollingerBandsSeries) GetPeriod() int {
|
|||
return bbs.Period
|
||||
}
|
||||
|
||||
// GetK returns the K value.
|
||||
// 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.
|
||||
func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 {
|
||||
if bbs.K == 0 {
|
||||
if len(defaults) > 0 {
|
||||
|
|
@ -54,35 +57,35 @@ func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 {
|
|||
}
|
||||
|
||||
// Len returns the number of elements in the series.
|
||||
func (bbs *BollingerBandsSeries) Len() int {
|
||||
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) {
|
||||
// GetBoundedValues gets the bounded value for the series.
|
||||
func (bbs *BollingerBandsSeries) GetBoundedValues(index int) (x, y1, y2 float64) {
|
||||
if bbs.InnerSeries == nil {
|
||||
return
|
||||
}
|
||||
if bbs.valueBuffer == nil || index == 0 {
|
||||
bbs.valueBuffer = NewRingBufferWithCapacity(bbs.GetPeriod())
|
||||
bbs.valueBuffer = seq.NewBufferWithCapacity(bbs.GetPeriod())
|
||||
}
|
||||
if bbs.valueBuffer.Len() >= bbs.GetPeriod() {
|
||||
bbs.valueBuffer.Dequeue()
|
||||
}
|
||||
px, py := bbs.InnerSeries.GetValue(index)
|
||||
px, py := bbs.InnerSeries.GetValues(index)
|
||||
bbs.valueBuffer.Enqueue(py)
|
||||
x = px
|
||||
|
||||
ay := bbs.getAverage(bbs.valueBuffer)
|
||||
std := bbs.getStdDev(bbs.valueBuffer)
|
||||
ay := seq.New(bbs.valueBuffer).Average()
|
||||
std := seq.New(bbs.valueBuffer).StdDev()
|
||||
|
||||
y1 = ay + (bbs.GetK() * std)
|
||||
y2 = ay - (bbs.GetK() * std)
|
||||
return
|
||||
}
|
||||
|
||||
// GetBoundedLastValue returns the last bounded value for the series.
|
||||
func (bbs *BollingerBandsSeries) GetBoundedLastValue() (x, y1, y2 float64) {
|
||||
// GetBoundedLastValues returns the last bounded value for the series.
|
||||
func (bbs *BollingerBandsSeries) GetBoundedLastValues() (x, y1, y2 float64) {
|
||||
if bbs.InnerSeries == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -93,15 +96,15 @@ func (bbs *BollingerBandsSeries) GetBoundedLastValue() (x, y1, y2 float64) {
|
|||
startAt = 0
|
||||
}
|
||||
|
||||
vb := NewRingBufferWithCapacity(period)
|
||||
vb := seq.NewBufferWithCapacity(period)
|
||||
for index := startAt; index < seriesLength; index++ {
|
||||
xn, yn := bbs.InnerSeries.GetValue(index)
|
||||
xn, yn := bbs.InnerSeries.GetValues(index)
|
||||
vb.Enqueue(yn)
|
||||
x = xn
|
||||
}
|
||||
|
||||
ay := bbs.getAverage(vb)
|
||||
std := bbs.getStdDev(vb)
|
||||
ay := seq.Seq{Provider: vb}.Average()
|
||||
std := seq.Seq{Provider: vb}.StdDev()
|
||||
|
||||
y1 = ay + (bbs.GetK() * std)
|
||||
y2 = ay - (bbs.GetK() * std)
|
||||
|
|
@ -120,37 +123,6 @@ func (bbs *BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrang
|
|||
Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Validate validates the series.
|
||||
func (bbs BollingerBandsSeries) Validate() error {
|
||||
if bbs.InnerSeries == nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue