2016-07-17 14:10:04 -04:00
|
|
|
package chart
|
|
|
|
|
2017-02-03 14:26:53 -05:00
|
|
|
import "fmt"
|
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
const (
|
2016-07-18 19:59:13 -04:00
|
|
|
// DefaultEMAPeriod is the default EMA period used in the sigma calculation.
|
|
|
|
DefaultEMAPeriod = 12
|
2016-07-17 14:10:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// EMASeries is a computed series.
|
|
|
|
type EMASeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
2016-07-31 19:54:09 -04:00
|
|
|
YAxis YAxisType
|
2016-07-17 14:10:04 -04:00
|
|
|
|
|
|
|
Period int
|
2017-05-12 20:12:23 -04:00
|
|
|
InnerSeries ValuesProvider
|
2016-07-18 21:29:08 -04:00
|
|
|
|
|
|
|
cache []float64
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the time series.
|
|
|
|
func (ema EMASeries) GetName() string {
|
|
|
|
return ema.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
|
|
|
func (ema EMASeries) GetStyle() Style {
|
|
|
|
return ema.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYAxis returns which YAxis the series draws on.
|
2016-07-31 19:54:09 -04:00
|
|
|
func (ema EMASeries) GetYAxis() YAxisType {
|
2016-07-17 14:10:04 -04:00
|
|
|
return ema.YAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeriod returns the window size.
|
2016-07-18 19:59:37 -04:00
|
|
|
func (ema EMASeries) GetPeriod() int {
|
2016-07-17 14:10:04 -04:00
|
|
|
if ema.Period == 0 {
|
2016-07-18 19:59:13 -04:00
|
|
|
return DefaultEMAPeriod
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
|
|
|
return ema.Period
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the series.
|
|
|
|
func (ema EMASeries) Len() int {
|
|
|
|
return ema.InnerSeries.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSigma returns the smoothing factor for the serise.
|
2016-07-17 21:54:50 -04:00
|
|
|
func (ema EMASeries) GetSigma() float64 {
|
2016-07-18 20:02:14 -04:00
|
|
|
return 2.0 / (float64(ema.GetPeriod()) + 1)
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetValues gets a value at a given index.
|
|
|
|
func (ema *EMASeries) GetValues(index int) (x, y float64) {
|
2016-07-17 14:10:04 -04:00
|
|
|
if ema.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-07-18 21:29:08 -04:00
|
|
|
if len(ema.cache) == 0 {
|
|
|
|
ema.ensureCachedValues()
|
|
|
|
}
|
2017-05-12 20:12:23 -04:00
|
|
|
vx, _ := ema.InnerSeries.GetValues(index)
|
2016-07-17 14:10:04 -04:00
|
|
|
x = vx
|
2016-07-18 21:29:08 -04:00
|
|
|
y = ema.cache[index]
|
2016-07-17 14:10:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetLastValues computes the last moving average value but walking back window size samples,
|
2016-07-17 14:10:04 -04:00
|
|
|
// and recomputing the last moving average chunk.
|
2017-05-12 20:12:23 -04:00
|
|
|
func (ema *EMASeries) GetLastValues() (x, y float64) {
|
2016-07-17 14:10:04 -04:00
|
|
|
if ema.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-07-18 21:29:08 -04:00
|
|
|
if len(ema.cache) == 0 {
|
|
|
|
ema.ensureCachedValues()
|
|
|
|
}
|
2016-07-17 14:10:04 -04:00
|
|
|
lastIndex := ema.InnerSeries.Len() - 1
|
2017-05-12 20:12:23 -04:00
|
|
|
x, _ = ema.InnerSeries.GetValues(lastIndex)
|
2016-07-18 21:29:08 -04:00
|
|
|
y = ema.cache[lastIndex]
|
2016-07-17 14:10:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-18 21:29:08 -04:00
|
|
|
func (ema *EMASeries) ensureCachedValues() {
|
|
|
|
seriesLength := ema.InnerSeries.Len()
|
|
|
|
ema.cache = make([]float64, seriesLength)
|
|
|
|
sigma := ema.GetSigma()
|
|
|
|
for x := 0; x < seriesLength; x++ {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, y := ema.InnerSeries.GetValues(x)
|
2016-07-18 21:29:08 -04:00
|
|
|
if x == 0 {
|
|
|
|
ema.cache[x] = y
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
previousEMA := ema.cache[x-1]
|
|
|
|
ema.cache[x] = ((y - previousEMA) * sigma) + previousEMA
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the series.
|
2016-07-18 21:29:08 -04:00
|
|
|
func (ema *EMASeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
2016-07-21 17:11:27 -04:00
|
|
|
style := ema.Style.InheritFrom(defaults)
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.LineSeries(r, canvasBox, xrange, yrange, style, ema)
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
2017-02-03 14:26:53 -05:00
|
|
|
|
|
|
|
// Validate validates the series.
|
|
|
|
func (ema *EMASeries) Validate() error {
|
|
|
|
if ema.InnerSeries == nil {
|
|
|
|
return fmt.Errorf("ema series requires InnerSeries to be set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|