-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdDev_test.go
162 lines (155 loc) · 3.58 KB
/
stdDev_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
package shingo
import (
"math"
"testing"
)
// 52.22
// 52.78
// 53.02
// 53.67
// 53.67
// 53.74
// 53.45
// 53.72
// 53.39
// 52.51 (STDEVP)
// 52.32 0.51
// 51.45 0.73
// 51.60 0.86
// 52.43 0.83
// 52.47 0.79
// 52.91 0.72
// 52.07 0.68
// 53.12 0.58
// 52.77 0.51
// 52.73 0.52
// 52.09 0.53
// 53.19 0.48
// 53.73 0.49
// 53.87 0.58
// 53.85 0.62
// 53.88 0.67
// 54.08 0.62
// 54.14 0.66
// 54.50 0.69
// 54.30 0.65
// 54.40 0.36
// 54.16 0.24
func TestStdDev(t *testing.T) {
sdTests := []struct {
title string
arg IndicatorInputArg
candles []*Candlestick
expected []*StdDevDelta
}{
{
title: "Standard Deviation test",
arg: IndicatorInputArg{
Type: IndicatorTypeStdDev,
Period: 10,
},
candles: []*Candlestick{
&Candlestick{Close: 52.22},
&Candlestick{Close: 52.78},
&Candlestick{Close: 53.02},
&Candlestick{Close: 53.67},
&Candlestick{Close: 53.67},
&Candlestick{Close: 53.74},
&Candlestick{Close: 53.45},
&Candlestick{Close: 53.72},
&Candlestick{Close: 53.39},
&Candlestick{Close: 52.51},
&Candlestick{Close: 52.32},
&Candlestick{Close: 51.45},
&Candlestick{Close: 51.60},
&Candlestick{Close: 52.43},
&Candlestick{Close: 52.47},
&Candlestick{Close: 52.91},
&Candlestick{Close: 52.07},
&Candlestick{Close: 53.12},
&Candlestick{Close: 52.77},
&Candlestick{Close: 52.73},
&Candlestick{Close: 52.09},
&Candlestick{Close: 53.19},
&Candlestick{Close: 53.73},
&Candlestick{Close: 53.87},
&Candlestick{Close: 53.85},
&Candlestick{Close: 53.88},
&Candlestick{Close: 54.08},
&Candlestick{Close: 54.14},
&Candlestick{Close: 54.50},
&Candlestick{Close: 54.30},
&Candlestick{Close: 54.40},
&Candlestick{Close: 54.16},
},
expected: []*StdDevDelta{
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
&StdDevDelta{Value: 0.523018},
&StdDevDelta{Value: 0.505411},
&StdDevDelta{Value: 0.730122},
&StdDevDelta{Value: 0.857364},
&StdDevDelta{Value: 0.833642},
&StdDevDelta{Value: 0.788707},
&StdDevDelta{Value: 0.716251},
&StdDevDelta{Value: 0.675498},
&StdDevDelta{Value: 0.584679},
&StdDevDelta{Value: 0.507870},
&StdDevDelta{Value: 0.518353},
&StdDevDelta{Value: 0.526061},
&StdDevDelta{Value: 0.480964},
&StdDevDelta{Value: 0.490176},
&StdDevDelta{Value: 0.578439},
&StdDevDelta{Value: 0.622905},
&StdDevDelta{Value: 0.670093},
&StdDevDelta{Value: 0.622025},
&StdDevDelta{Value: 0.661064},
&StdDevDelta{Value: 0.690358},
&StdDevDelta{Value: 0.651152},
&StdDevDelta{Value: 0.360466},
&StdDevDelta{Value: 0.242959},
},
},
}
for _, st := range sdTests {
cs, _ := NewCandlesticks(IntervalOneDay, 100)
for _, c := range st.candles {
cs.AppendCandlestick(c)
}
if err := cs.GenerateIndicator(IndicatorTypeStdDev, st.arg); err != nil {
t.Fatalf("Error appending stddev: %+v", err)
}
for i, e := range st.expected {
v := cs.ItemAtIndex(i)
delta := v.GetStdDev(st.arg.Period)
if e == nil && delta == nil {
continue
}
if e != nil && delta == nil {
t.Fatalf("Expected non-nil: %+v but got nil", e)
}
if e == nil && delta != nil {
t.Fatalf("Expected nil but got non nil %+v %d", delta, i)
}
if !equalWithinPct(e.Value, delta.Value, 0.005) {
t.Errorf("Expected value to be: %+v but got %+v", e.Value, delta.Value)
}
}
}
}
func equalWithinPct(x, y, d float64) bool {
if x == 0.0 && y == 0.0 {
return true
} else if x != 0.0 {
return math.Abs(y/x-1) < d
} else {
return math.Abs(x/y-1) < d
}
}