-
Notifications
You must be signed in to change notification settings - Fork 0
/
macd_test.go
275 lines (268 loc) · 7.95 KB
/
macd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package shingo
import (
"fmt"
"testing"
)
func TestAppendMACD(t *testing.T) {
macdTests := []struct {
title string
limit int
candles []*Candlestick
expected []*MACDDelta
}{
{
title: "From 40 candles, it requires at least 34 candles so it returns none",
limit: 40,
candles: []*Candlestick{
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.6},
&Candlestick{Close: 7.3},
&Candlestick{Close: 7.1},
&Candlestick{Close: 6.8},
},
expected: []*MACDDelta{
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil,
},
},
{
title: "From 40 candles, it returns 8",
limit: 40,
candles: []*Candlestick{
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.6},
&Candlestick{Close: 7.3},
&Candlestick{Close: 7.1},
&Candlestick{Close: 6.8},
&Candlestick{Close: 6.3},
&Candlestick{Close: 6},
&Candlestick{Close: 7.1},
&Candlestick{Close: 6.1},
&Candlestick{Close: 5.9},
&Candlestick{Close: 5.1},
&Candlestick{Close: 4.8},
&Candlestick{Close: 5.3},
},
expected: []*MACDDelta{
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil,
&MACDDelta{
MACDValue: 6.338822758123295 - 5.827396793334697,
SignalValue: 0.4420618107841297,
MACDHistogram: 6.338822758123295 - 5.827396793334697 - 0.4420618107841297,
},
&MACDDelta{
MACDValue: 6.2867118179239325 - 5.840186690948596,
SignalValue: 0.44300102610456293,
MACDHistogram: 6.2867118179239325 - 5.840186690948596 - 0.44300102610456293,
},
&MACDDelta{
MACDValue: 6.4117955403272315 - 5.933538857149305,
SignalValue: 0.45010768419554115,
MACDHistogram: 6.4117955403272315 - 5.933538857149305 - 0.45010768419554115,
},
&MACDDelta{
MACDValue: 6.3638413862249035 - 5.945873627834541,
SignalValue: 0.4437271307574296,
MACDHistogram: 6.3638413862249035 - 5.945873627834541 - 0.4437271307574296,
},
&MACDDelta{
MACDValue: 6.292502581023514 - 5.942474392012002,
SignalValue: 0.4250253064995333,
MACDHistogram: 6.292502581023514 - 5.942474392012002 - 0.4250253064995333,
},
&MACDDelta{
MACDValue: 6.109095684062098 - 5.880047039563912,
SignalValue: 0.38584887751455155,
MACDHistogram: 6.109095684062098 - 5.880047039563912 - 0.38584887751455155,
},
&MACDDelta{
MACDValue: 5.907756767853347 - 5.8000155539322265,
SignalValue: 0.33022730366163033,
MACDHistogram: 5.907756767853347 - 5.8000155539322265 - 0.33022730366163033,
},
&MACDDelta{
MACDValue: 5.814283776957502 - 5.762964401385848,
SignalValue: 0.27443855956659446,
MACDHistogram: 5.814283776957502 - 5.762964401385848 - 0.27443855956659446,
},
},
},
{
title: "Requesting for 40 MACD values and returns 1 since that's the limit",
limit: 1,
candles: []*Candlestick{
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 1.8},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 6.6},
&Candlestick{Close: 7.5},
&Candlestick{Close: 7.5},
&Candlestick{Close: 4.9},
&Candlestick{Close: 3.2},
&Candlestick{Close: 7.8},
&Candlestick{Close: 4.9},
&Candlestick{Close: 1.8},
&Candlestick{Close: 7.8},
&Candlestick{Close: 7.6},
&Candlestick{Close: 7.3},
&Candlestick{Close: 7.1},
&Candlestick{Close: 6.8},
&Candlestick{Close: 6.3},
&Candlestick{Close: 6},
&Candlestick{Close: 7.1},
&Candlestick{Close: 6.1},
&Candlestick{Close: 5.9},
&Candlestick{Close: 5.1},
&Candlestick{Close: 4.8},
&Candlestick{Close: 5.3},
},
expected: []*MACDDelta{
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil, nil, nil,
&MACDDelta{
MACDValue: 5.814283776957502 - 5.762964401385848,
SignalValue: 0.27443855956659446,
MACDHistogram: 5.814283776957502 - 5.762964401385848 - 0.27443855956659446,
},
},
},
}
for _, v := range macdTests {
cs, _ := NewCandlesticks(IntervalOneDay, 100)
for _, c := range v.candles {
cs.AppendCandlestick(c)
}
args := IndicatorInputArg{
MacdLarge: 12,
MacdSmall: 26,
MacdSignal: 9,
Limit: v.limit,
}
if err := cs.GenerateIndicator(IndicatorTypeMACD, args); err != nil {
t.Errorf("Expected ok but got error appending EMA to period: 12, args: %+v for %s", args, v.title)
}
var str string
for i := range v.candles {
c := cs.ItemAtIndex(i)
str += fmt.Sprintf("%v,", c.Close)
macd := c.GetMACD(12, 26, 9)
if v.expected[i] == nil {
if macd != nil {
t.Errorf("Expected nil but got %+v for test %s index %d", macd, v.title, i)
}
continue
} else if v.expected[i] != nil && macd == nil {
t.Errorf("Expected non nil but got nil %+v for test %s index %d", macd, v.title, i)
continue
}
if !almostEqual(macd.MACDValue, v.expected[i].MACDValue, 0.001) {
t.Errorf("Expected MACDValue %+v but got %+v for test %s index %d",
v.expected[i].MACDValue,
macd.MACDValue,
v.title,
i)
}
if !almostEqual(macd.MACDHistogram, v.expected[i].MACDHistogram, 0.001) {
t.Errorf("Expected MACDHistogram %+v but got %+v for test %s index %d",
v.expected[i].MACDHistogram,
macd.MACDHistogram,
v.title,
i)
}
if !almostEqual(macd.SignalValue, v.expected[i].SignalValue, 0.001) {
t.Errorf("Expected SignalValue %+v but got %+v for test %s index %d",
v.expected[i].SignalValue,
macd.SignalValue,
v.title,
i)
}
}
}
}