Debian Codegraph @

macd_series.go

raw symbols SCIP
1 package chart
2
3 import "fmt"
4
5 const (
6 // DefaultMACDPeriodPrimary is the long window.
7 DefaultMACDPeriodPrimary = 26
8 // DefaultMACDPeriodSecondary is the short window.
9 DefaultMACDPeriodSecondary = 12
10 // DefaultMACDSignalPeriod is the signal period to compute for the MACD.
11 DefaultMACDSignalPeriod = 9
12 )
13
14 // MACDSeries computes the difference between the MACD line and the MACD Signal line.
15 // It is used in technical analysis and gives a lagging indicator of momentum.
16 type MACDSeries struct {
17 Name string
18 Style Style
19 YAxis YAxisType
20 InnerSeries ValuesProvider
21
22 PrimaryPeriod int
23 SecondaryPeriod int
24 SignalPeriod int
25
26 signal *MACDSignalSeries
27 macdl *MACDLineSeries
28 }
29
30 // Validate validates the series.
31 func (macd MACDSeries) Validate() error {
32 var err error
33 if macd.signal != nil {
34 err = macd.signal.Validate()
35 }
36 if err != nil {
37 return err
38 }
39 if macd.macdl != nil {
40 err = macd.macdl.Validate()
41 }
42 if err != nil {
43 return err
44 }
45 return nil
46 }
47
48 // GetPeriods returns the primary and secondary periods.
49 func (macd MACDSeries) GetPeriods() (w1, w2, sig int) {
50 if macd.PrimaryPeriod == 0 {
51 w1 = DefaultMACDPeriodPrimary
52 } else {
53 w1 = macd.PrimaryPeriod
54 }
55 if macd.SecondaryPeriod == 0 {
56 w2 = DefaultMACDPeriodSecondary
57 } else {
58 w2 = macd.SecondaryPeriod
59 }
60 if macd.SignalPeriod == 0 {
61 sig = DefaultMACDSignalPeriod
62 } else {
63 sig = macd.SignalPeriod
64 }
65 return
66 }
67
68 // GetName returns the name of the time series.
69 func (macd MACDSeries) GetName() string {
70 return macd.Name
71 }
72
73 // GetStyle returns the line style.
74 func (macd MACDSeries) GetStyle() Style {
75 return macd.Style
76 }
77
78 // GetYAxis returns which YAxis the series draws on.
79 func (macd MACDSeries) GetYAxis() YAxisType {
80 return macd.YAxis
81 }
82
83 // Len returns the number of elements in the series.
84 func (macd MACDSeries) Len() int {
85 if macd.InnerSeries == nil {
86 return 0
87 }
88
89 return macd.InnerSeries.Len()
90 }
91
92 // GetValues gets a value at a given index. For MACD it is the signal value.
93 func (macd *MACDSeries) GetValues(index int) (x float64, y float64) {
94 if macd.InnerSeries == nil {
95 return
96 }
97
98 if macd.signal == nil || macd.macdl == nil {
99 macd.ensureChildSeries()
100 }
101
102 _, lv := macd.macdl.GetValues(index)
103 _, sv := macd.signal.GetValues(index)
104
105 x, _ = macd.InnerSeries.GetValues(index)
106 y = lv - sv
107
108 return
109 }
110
111 func (macd *MACDSeries) ensureChildSeries() {
112 w1, w2, sig := macd.GetPeriods()
113
114 macd.signal = &MACDSignalSeries{
115 InnerSeries: macd.InnerSeries,
116 PrimaryPeriod: w1,
117 SecondaryPeriod: w2,
118 SignalPeriod: sig,
119 }
120
121 macd.macdl = &MACDLineSeries{
122 InnerSeries: macd.InnerSeries,
123 PrimaryPeriod: w1,
124 SecondaryPeriod: w2,
125 }
126 }
127
128 // MACDSignalSeries computes the EMA of the MACDLineSeries.
129 type MACDSignalSeries struct {
130 Name string
131 Style Style
132 YAxis YAxisType
133 InnerSeries ValuesProvider
134
135 PrimaryPeriod int
136 SecondaryPeriod int
137 SignalPeriod int
138
139 signal *EMASeries
140 }
141
142 // Validate validates the series.
143 func (macds MACDSignalSeries) Validate() error {
144 if macds.signal != nil {
145 return macds.signal.Validate()
146 }
147 return nil
148 }
149
150 // GetPeriods returns the primary and secondary periods.
151 func (macds MACDSignalSeries) GetPeriods() (w1, w2, sig int) {
152 if macds.PrimaryPeriod == 0 {
153 w1 = DefaultMACDPeriodPrimary
154 } else {
155 w1 = macds.PrimaryPeriod
156 }
157 if macds.SecondaryPeriod == 0 {
158 w2 = DefaultMACDPeriodSecondary
159 } else {
160 w2 = macds.SecondaryPeriod
161 }
162 if macds.SignalPeriod == 0 {
163 sig = DefaultMACDSignalPeriod
164 } else {
165 sig = macds.SignalPeriod
166 }
167 return
168 }
169
170 // GetName returns the name of the time series.
171 func (macds MACDSignalSeries) GetName() string {
172 return macds.Name
173 }
174
175 // GetStyle returns the line style.
176 func (macds MACDSignalSeries) GetStyle() Style {
177 return macds.Style
178 }
179
180 // GetYAxis returns which YAxis the series draws on.
181 func (macds MACDSignalSeries) GetYAxis() YAxisType {
182 return macds.YAxis
183 }
184
185 // Len returns the number of elements in the series.
186 func (macds *MACDSignalSeries) Len() int {
187 if macds.InnerSeries == nil {
188 return 0
189 }
190
191 return macds.InnerSeries.Len()
192 }
193
194 // GetValues gets a value at a given index. For MACD it is the signal value.
195 func (macds *MACDSignalSeries) GetValues(index int) (x float64, y float64) {
196 if macds.InnerSeries == nil {
197 return
198 }
199
200 if macds.signal == nil {
201 macds.ensureSignal()
202 }
203 x, _ = macds.InnerSeries.GetValues(index)
204 _, y = macds.signal.GetValues(index)
205 return
206 }
207
208 func (macds *MACDSignalSeries) ensureSignal() {
209 w1, w2, sig := macds.GetPeriods()
210
211 macds.signal = &EMASeries{
212 InnerSeries: &MACDLineSeries{
213 InnerSeries: macds.InnerSeries,
214 PrimaryPeriod: w1,
215 SecondaryPeriod: w2,
216 },
217 Period: sig,
218 }
219 }
220
221 // Render renders the series.
222 func (macds *MACDSignalSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
223 style := macds.Style.InheritFrom(defaults)
224 Draw.LineSeries(r, canvasBox, xrange, yrange, style, macds)
225 }
226
227 // MACDLineSeries is a series that computes the inner ema1-ema2 value as a series.
228 type MACDLineSeries struct {
229 Name string
230 Style Style
231 YAxis YAxisType
232 InnerSeries ValuesProvider
233
234 PrimaryPeriod int
235 SecondaryPeriod int
236
237 ema1 *EMASeries
238 ema2 *EMASeries
239
240 Sigma float64
241 }
242
243 // Validate validates the series.
244 func (macdl MACDLineSeries) Validate() error {
245 var err error
246 if macdl.ema1 != nil {
247 err = macdl.ema1.Validate()
248 }
249 if err != nil {
250 return err
251 }
252 if macdl.ema2 != nil {
253 err = macdl.ema2.Validate()
254 }
255 if err != nil {
256 return err
257 }
258 if macdl.InnerSeries == nil {
259 return fmt.Errorf("MACDLineSeries: must provide an inner series")
260 }
261 return nil
262 }
263
264 // GetName returns the name of the time series.
265 func (macdl MACDLineSeries) GetName() string {
266 return macdl.Name
267 }
268
269 // GetStyle returns the line style.
270 func (macdl MACDLineSeries) GetStyle() Style {
271 return macdl.Style
272 }
273
274 // GetYAxis returns which YAxis the series draws on.
275 func (macdl MACDLineSeries) GetYAxis() YAxisType {
276 return macdl.YAxis
277 }
278
279 // GetPeriods returns the primary and secondary periods.
280 func (macdl MACDLineSeries) GetPeriods() (w1, w2 int) {
281 if macdl.PrimaryPeriod == 0 {
282 w1 = DefaultMACDPeriodPrimary
283 } else {
284 w1 = macdl.PrimaryPeriod
285 }
286 if macdl.SecondaryPeriod == 0 {
287 w2 = DefaultMACDPeriodSecondary
288 } else {
289 w2 = macdl.SecondaryPeriod
290 }
291 return
292 }
293
294 // Len returns the number of elements in the series.
295 func (macdl *MACDLineSeries) Len() int {
296 if macdl.InnerSeries == nil {
297 return 0
298 }
299
300 return macdl.InnerSeries.Len()
301 }
302
303 // GetValues gets a value at a given index. For MACD it is the signal value.
304 func (macdl *MACDLineSeries) GetValues(index int) (x float64, y float64) {
305 if macdl.InnerSeries == nil {
306 return
307 }
308 if macdl.ema1 == nil && macdl.ema2 == nil {
309 macdl.ensureEMASeries()
310 }
311
312 x, _ = macdl.InnerSeries.GetValues(index)
313
314 _, emav1 := macdl.ema1.GetValues(index)
315 _, emav2 := macdl.ema2.GetValues(index)
316
317 y = emav2 - emav1
318 return
319 }
320
321 func (macdl *MACDLineSeries) ensureEMASeries() {
322 w1, w2 := macdl.GetPeriods()
323
324 macdl.ema1 = &EMASeries{
325 InnerSeries: macdl.InnerSeries,
326 Period: w1,
327 }
328 macdl.ema2 = &EMASeries{
329 InnerSeries: macdl.InnerSeries,
330 Period: w2,
331 }
332 }
333
334 // Render renders the series.
335 func (macdl *MACDLineSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
336 style := macdl.Style.InheritFrom(defaults)
337 Draw.LineSeries(r, canvasBox, xrange, yrange, style, macdl)
338 }