Debian Codegraph @

ema_series.go

raw symbols SCIP
1 package chart
2
3 import "fmt"
4
5 const (
6 // DefaultEMAPeriod is the default EMA period used in the sigma calculation.
7 DefaultEMAPeriod = 12
8 )
9
10 // Interface Assertions.
11 var (
12 _ Series = (*EMASeries)(nil)
13 _ FirstValuesProvider = (*EMASeries)(nil)
14 _ LastValuesProvider = (*EMASeries)(nil)
15 )
16
17 // EMASeries is a computed series.
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 // GetName returns the name of the time series.
30 func (ema EMASeries) GetName() string {
31 return ema.Name
32 }
33
34 // GetStyle returns the line style.
35 func (ema EMASeries) GetStyle() Style {
36 return ema.Style
37 }
38
39 // GetYAxis returns which YAxis the series draws on.
40 func (ema EMASeries) GetYAxis() YAxisType {
41 return ema.YAxis
42 }
43
44 // GetPeriod returns the window size.
45 func (ema EMASeries) GetPeriod() int {
46 if ema.Period == 0 {
47 return DefaultEMAPeriod
48 }
49 return ema.Period
50 }
51
52 // Len returns the number of elements in the series.
53 func (ema EMASeries) Len() int {
54 return ema.InnerSeries.Len()
55 }
56
57 // GetSigma returns the smoothing factor for the serise.
58 func (ema EMASeries) GetSigma() float64 {
59 return 2.0 / (float64(ema.GetPeriod()) + 1)
60 }
61
62 // GetValues gets a value at a given index.
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 // GetFirstValues computes the first moving average value.
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 // GetLastValues computes the last moving average value but walking back window size samples,
90 // and recomputing the last moving average chunk.
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 // Render renders the series.
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 // Validate validates the series.
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 }