-
Notifications
You must be signed in to change notification settings - Fork 0
/
sma_test.go
201 lines (191 loc) · 4.83 KB
/
sma_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
package shingo
import (
"math"
"testing"
)
func TestAppendSMA(t *testing.T) {
smaTests := []struct {
title string
arg IndicatorInputArg
candles []*Candlestick
expected []*SMADelta
}{
{
title: "When limit is zero, generate on as many candlestick as possible",
arg: IndicatorInputArg{
Limit: 0,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*SMADelta{
nil,
nil,
nil,
&SMADelta{Value: 4.325, Change: 0},
},
},
{
title: "When candlestick is less than period, return none as it can't be computed",
arg: IndicatorInputArg{
Limit: 1,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*SMADelta{
nil,
nil,
nil,
},
},
// requesting the last one to have
// SMADelta indicator of 4 periods
// there are 5 candles so only have
// the last one set
{
title: "When limit is set to 1, it should return that many count",
arg: IndicatorInputArg{
Limit: 1,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 1.9},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*SMADelta{
nil,
nil,
nil,
nil,
&SMADelta{Value: 4.325, Change: (4.325 - 2.55) / 2.55},
},
},
// requesting the last two to have
// SMADelta indicator of 4 periods
{
title: "When limit is set to > 1, it should return that many count",
arg: IndicatorInputArg{
Limit: 2,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 8.9}, // out of limit range
&Candlestick{Close: 10.2}, // out of limit range
&Candlestick{Close: 8}, // does not have 4 periods total including previous
&Candlestick{Close: 3.9},
&Candlestick{Close: 4.2},
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.2},
&Candlestick{Close: 1.9},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*SMADelta{
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
&SMADelta{Value: 2.55, Change: 2.55/2.575 - 1},
&SMADelta{Value: 4.325, Change: (4.325 - 2.55) / 2.55},
},
},
// requesting the last 10 to have
// SMADelta indicator of 4 periods
{
title: "It should return less than number that can be generated",
arg: IndicatorInputArg{
Limit: 10,
Period: 4,
},
candles: []*Candlestick{
&Candlestick{Close: 8.9}, // out of limit range
&Candlestick{Close: 10.2}, // out of limit range
&Candlestick{Close: 8}, // does not have 4 periods total including previous
&Candlestick{Close: 3.9},
&Candlestick{Close: 4.2},
&Candlestick{Close: 3.1},
&Candlestick{Close: 2.2},
&Candlestick{Close: 1.9},
&Candlestick{Close: 2.8},
&Candlestick{Close: 3.4},
&Candlestick{Close: 2.1},
&Candlestick{Close: 9},
},
expected: []*SMADelta{
nil,
nil,
nil,
&SMADelta{Value: 7.75, Change: 0},
&SMADelta{Value: 6.575, Change: 6.575/7.75 - 1},
&SMADelta{Value: 4.8, Change: 4.8/6.575 - 1},
&SMADelta{Value: 3.35, Change: 3.35/4.8 - 1},
&SMADelta{Value: 2.85, Change: 2.85/3.35 - 1},
&SMADelta{Value: 2.5, Change: 2.5/2.85 - 1},
&SMADelta{Value: 2.575, Change: (2.575 - 2.5) / 2.5},
&SMADelta{Value: 2.55, Change: 2.55/2.575 - 1},
&SMADelta{Value: 4.325, Change: (4.325 - 2.55) / 2.55},
},
},
}
for ti, v := range smaTests {
cs, _ := NewCandlesticks(IntervalOneDay, 100)
for _, c := range v.candles {
cs.AppendCandlestick(c)
}
period := v.arg.Period
if err := cs.GenerateIndicator(IndicatorTypeSMA, v.arg); err != nil {
t.Fatalf("Expected ok but got error %+v for %s", err, v.title)
}
for i := range v.candles {
c := cs.ItemAtIndex(i)
sma := c.GetSMA(period)
if v.expected[i] == nil {
if sma != nil {
t.Fatalf("Expected nil but got %+v for %s", sma, v.title)
}
continue
} else if v.expected[i] != nil && sma == nil {
t.Fatalf("Expected indicators to be non nil but got nil for %s", v.title)
}
if !almostEqual(sma.Value, v.expected[i].Value, 0.0001) {
t.Errorf("Expected value %+v but got %+v for test %+v index %+v for %s",
sma.Value,
v.expected[i].Value,
ti,
i,
v.title)
}
if !almostEqual(sma.Change, v.expected[i].Change, 0.0001) {
t.Errorf("Expected change %+v but got %+v for test %+v index %+v for %s",
sma.Change,
v.expected[i].Change,
ti,
i,
v.title)
}
}
}
}
func almostEqual(x, y, epsilon float64) bool {
return math.Nextafter(x, y) == y ||
math.Abs(x-y) < epsilon // epislon hack
}