-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathema_test.go
202 lines (198 loc) · 4.27 KB
/
ema_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
package shingo
import (
"testing"
)
func TestAppendEMA(t *testing.T) {
emaTests := []struct {
title string
args IndicatorInputArg
candles []*Candlestick
expected []*EMADelta
}{
{
title: "It should not have any computed values",
args: IndicatorInputArg{
Limit: 1,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
},
},
{
title: "It should have as many computed values (one) as possible",
args: IndicatorInputArg{
Limit: 0,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
&EMADelta{Value: 5.875, Change: 0},
},
},
{
title: "It should have only one as limit is 1",
args: IndicatorInputArg{
Limit: 1,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
nil,
&EMADelta{Value: 5.31, Change: 0.86315789},
},
},
{
title: "It should have two as limit is 2",
args: IndicatorInputArg{
Limit: 2,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
&EMADelta{Value: 2.85, Change: 0},
&EMADelta{Value: 5.31, Change: 0.86315789},
},
},
{
title: "It should have only two as limit is 2",
args: IndicatorInputArg{
Limit: 2,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 1.1},
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
nil,
&EMADelta{Value: 2.4, Change: -0.07692308},
&EMADelta{Value: 5.04, Change: 1.1},
},
},
{
title: "It should have only two out of 4 possible as limit is 2",
args: IndicatorInputArg{
Limit: 2,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 1.1},
&Candlestick{Close: 1.1},
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
nil,
nil,
&EMADelta{Value: 2.385, Change: -0.07378641},
&EMADelta{Value: 5.031, Change: 1.10943396},
},
},
{
title: "It should calculate 5",
args: IndicatorInputArg{
Limit: 5,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*EMADelta{
nil,
nil,
nil,
&EMADelta{Value: 2.85, Change: 0},
&EMADelta{Value: 5.31, Change: 0.86315789},
},
},
}
for ti, v := range emaTests {
cs, _ := NewCandlesticks(IntervalOneDay, 100)
for _, c := range v.candles {
cs.AppendCandlestick(c)
}
if err := cs.GenerateIndicator(IndicatorTypeEMA, v.args); err != nil {
t.Fatalf("Expected ok but got error %+v for %s", err, v.title)
}
for i := range v.candles {
c := cs.ItemAtIndex(i)
ema := c.GetEMA(v.args.Period)
if v.expected[i] == nil {
if ema != nil {
t.Errorf("Expected nil but got %+v in Test Idx: %+v idx: %+v for %s", ema, ti, i, v.title)
}
continue
} else if v.expected[i] != nil && ema == nil {
t.Errorf("Expected non nil but got nil for %s", v.title)
continue
}
if !almostEqual(ema.Value, v.expected[i].Value, 0.0001) {
t.Errorf("Expected value %+v but got %+v for test %+v index %+v for %s",
v.expected[i].Value,
ema.Value,
ti,
i,
v.title)
}
if !almostEqual(ema.Change, v.expected[i].Change, 0.0001) {
t.Errorf("Expected change %+v but got %+v for test %+v index %+v for %s",
v.expected[i].Change,
ema.Change,
ti,
i,
v.title)
}
}
}
}