| 1 |
package chart |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
const ( |
| 6 |
|
| 7 |
DefaultEMAPeriod = 12 |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
var ( |
| 12 |
_ Series = (*EMASeries)(nil) |
| 13 |
_ FirstValuesProvider = (*EMASeries)(nil) |
| 14 |
_ LastValuesProvider = (*EMASeries)(nil) |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type EMASeries struct { |
| 19 |
Name string |
| 20 |
Style Style |
| 21 |
YAxis YAxisType |
| 22 |
|
| 23 |
Period int |
| 24 |
InnerSeries ValuesProvider |
| 25 |
|
| 26 |
cache []float64 |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func (ema EMASeries) GetName() string { |
| 31 |
return ema.Name |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (ema EMASeries) GetStyle() Style { |
| 36 |
return ema.Style |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func (ema EMASeries) GetYAxis() YAxisType { |
| 41 |
return ema.YAxis |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (ema EMASeries) GetPeriod() int { |
| 46 |
if ema.Period == 0 { |
| 47 |
return DefaultEMAPeriod |
| 48 |
} |
| 49 |
return ema.Period |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (ema EMASeries) Len() int { |
| 54 |
return ema.InnerSeries.Len() |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (ema EMASeries) GetSigma() float64 { |
| 59 |
return 2.0 / (float64(ema.GetPeriod()) + 1) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func (ema *EMASeries) GetValues(index int) (x, y float64) { |
| 64 |
if ema.InnerSeries == nil { |
| 65 |
return |
| 66 |
} |
| 67 |
if len(ema.cache) == 0 { |
| 68 |
ema.ensureCachedValues() |
| 69 |
} |
| 70 |
vx, _ := ema.InnerSeries.GetValues(index) |
| 71 |
x = vx |
| 72 |
y = ema.cache[index] |
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (ema *EMASeries) GetFirstValues() (x, y float64) { |
| 78 |
if ema.InnerSeries == nil { |
| 79 |
return |
| 80 |
} |
| 81 |
if len(ema.cache) == 0 { |
| 82 |
ema.ensureCachedValues() |
| 83 |
} |
| 84 |
x, _ = ema.InnerSeries.GetValues(0) |
| 85 |
y = ema.cache[0] |
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func (ema *EMASeries) GetLastValues() (x, y float64) { |
| 92 |
if ema.InnerSeries == nil { |
| 93 |
return |
| 94 |
} |
| 95 |
if len(ema.cache) == 0 { |
| 96 |
ema.ensureCachedValues() |
| 97 |
} |
| 98 |
lastIndex := ema.InnerSeries.Len() - 1 |
| 99 |
x, _ = ema.InnerSeries.GetValues(lastIndex) |
| 100 |
y = ema.cache[lastIndex] |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
func (ema *EMASeries) ensureCachedValues() { |
| 105 |
seriesLength := ema.InnerSeries.Len() |
| 106 |
ema.cache = make([]float64, seriesLength) |
| 107 |
sigma := ema.GetSigma() |
| 108 |
for x := 0; x < seriesLength; x++ { |
| 109 |
_, y := ema.InnerSeries.GetValues(x) |
| 110 |
if x == 0 { |
| 111 |
ema.cache[x] = y |
| 112 |
continue |
| 113 |
} |
| 114 |
previousEMA := ema.cache[x-1] |
| 115 |
ema.cache[x] = ((y - previousEMA) * sigma) + previousEMA |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func (ema *EMASeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) { |
| 121 |
style := ema.Style.InheritFrom(defaults) |
| 122 |
Draw.LineSeries(r, canvasBox, xrange, yrange, style, ema) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (ema *EMASeries) Validate() error { |
| 127 |
if ema.InnerSeries == nil { |
| 128 |
return fmt.Errorf("ema series requires InnerSeries to be set") |
| 129 |
} |
| 130 |
return nil |
| 131 |
} |