forked from cinar/indicator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
momentum_indicators_test.go
159 lines (143 loc) · 7.22 KB
/
momentum_indicators_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
// Copyright (c) 2021 Onur Cinar. All Rights Reserved.
// The source code is provided under MIT License.
//
// https://github.com/cinar/indicator
package indicator
import (
"testing"
)
func TestChaikinOscillator(t *testing.T) {
high := []float64{10, 11, 12, 13, 14, 15, 16, 17}
low := []float64{1, 2, 3, 4, 5, 6, 7, 8}
closing := []float64{5, 6, 7, 8, 9, 10, 11, 12}
volume := []int64{100, 200, 300, 400, 500, 600, 700, 800}
expected := []float64{0, -7.41, -18.52, -31.69, -46.09, -61.27, -76.95, -92.97}
actual, _ := ChaikinOscillator(2, 5, low, high, closing, volume)
testEquals(t, roundDigitsAll(actual, 2), expected)
}
func TestPercentagePriceOscillator(t *testing.T) {
price := []float64{
3674.84, 3666.77, 3789.99, 3735.48, 3749.63, 3900.86, 4017.82, 4115.77,
4160.68, 4121.43, 4108.54, 4176.82, 4101.23, 4132.15, 4158.24, 4057.84,
3978.73, 3941.48, 3973.75, 3901.36, 3900.79, 3923.68, 4088.85, 4008.01,
4023.89, 3930.08, 3935.18, 4001.05, 3991.24, 4123.34, 4146.87, 4300.17,
4175.48, 4155.38, 4131.93, 4287.5, 4183.96, 4175.2, 4296.12, 4271.78,
4393.66, 4459.45, 4462.21, 4391.69, 4392.59, 4446.59, 4397.45, 4412.53,
4488.28, 4500.21, 4481.15, 4525.12, 4582.64, 4545.86, 4530.41, 4602.45,
4631.6, 4575.52, 4543.06, 4520.16, 4456.24, 4511.61, 4461.18, 4463.12,
4411.67, 4357.86, 4262.45, 4173.11, 4204.31, 4259.52, 4277.88, 4170.7,
4201.09, 4328.87, 4363.49, 4386.54, 4306.26, 4373.94, 4384.65, 4288.7,
4225.5, 4304.76, 4348.87, 4380.26, 4475.01, 4471.07, 4401.67, 4418.64,
4504.08, 4587.18,
}
expectedPpo := []float64{
0, -0.02, 0.24, 0.31, 0.4, 0.79, 1.33, 1.93, 2.47, 2.77, 2.95, 3.19, 3.18,
3.2, 3.22, 3.01, 2.65, 2.26, 2, 1.63, 1.31, 1.1, 1.25, 1.19, 1.16, 0.94,
0.77, 0.75, 0.71, 0.94, 1.15, 1.6, 1.69, 1.7, 1.64, 1.88, 1.84, 1.77,
1.93, 1.98, 2.23, 2.52, 2.72, 2.71, 2.68, 2.71, 2.62, 2.55, 2.6, 2.62,
2.58, 2.6, 2.68, 2.65, 2.56, 2.6, 2.64, 2.55, 2.39, 2.19, 1.9, 1.75, 1.52,
1.33, 1.07, 0.77, 0.34, -0.16, -0.49, -0.65, -0.73, -0.99, -1.12, -0.98,
-0.79, -0.59, -0.58, -0.43, -0.3, -0.36, -0.53, -0.5, -0.4, -0.25, 0.04,
0.25, 0.3, 0.36, 0.55, 0.85,
}
expectedSignal := []float64{
0, -0, 0.04, 0.1, 0.16, 0.28, 0.49, 0.78, 1.12, 1.45, 1.75, 2.04, 2.27,
2.45, 2.61, 2.69, 2.68, 2.6, 2.48, 2.31, 2.11, 1.91, 1.77, 1.66, 1.56,
1.44, 1.3, 1.19, 1.1, 1.06, 1.08, 1.19, 1.29, 1.37, 1.42, 1.51, 1.58,
1.62, 1.68, 1.74, 1.84, 1.98, 2.13, 2.24, 2.33, 2.41, 2.45, 2.47, 2.49,
2.52, 2.53, 2.55, 2.57, 2.59, 2.58, 2.59, 2.6, 2.59, 2.55, 2.48, 2.36,
2.24, 2.1, 1.94, 1.77, 1.57, 1.32, 1.03, 0.72, 0.45, 0.21, -0.03, -0.25,
-0.39, -0.47, -0.5, -0.51, -0.5, -0.46, -0.44, -0.46, -0.47, -0.45, -0.41,
-0.32, -0.21, -0.11, -0.01, 0.1, 0.25,
}
expectedHistogram := []float64{
0, -0.01, 0.19, 0.22, 0.24, 0.5, 0.83, 1.15, 1.35, 1.32, 1.2, 1.15, 0.91,
0.75, 0.62, 0.32, -0.03, -0.33, -0.48, -0.68, -0.79, -0.81, -0.52, -0.47,
-0.4, -0.49, -0.54, -0.44, -0.38, -0.13, 0.07, 0.42, 0.4, 0.33, 0.22,
0.36, 0.26, 0.15, 0.25, 0.24, 0.39, 0.55, 0.6, 0.47, 0.35, 0.31, 0.17,
0.08, 0.1, 0.1, 0.05, 0.05, 0.11, 0.06, -0.02, 0.01, 0.05, -0.04, -0.16,
-0.28, -0.46, -0.49, -0.57, -0.61, -0.69, -0.8, -0.98, -1.18, -1.22, -1.1,
-0.95, -0.96, -0.88, -0.58, -0.32, -0.09, -0.06, 0.06, 0.16, 0.07, -0.07,
-0.04, 0.05, 0.16, 0.36, 0.46, 0.4, 0.37, 0.45, 0.6,
}
actualPpo, actualSignal, actualHistogram := DefaultPercentagePriceOscillator(price)
testEquals(t, roundDigitsAll(actualPpo, 2), expectedPpo)
testEquals(t, roundDigitsAll(actualSignal, 2), expectedSignal)
testEquals(t, roundDigitsAll(actualHistogram, 2), expectedHistogram)
}
func TestPercentageVolumeOscillator(t *testing.T) {
volume := []int64{
6954, 4511, 4474, 4126, 4572, 3936, 3192, 3090, 3476, 3852, 3107, 3604,
4145, 5192, 3560, 3961, 4322, 3901, 3392, 4278, 4212, 4428, 3846, 3824,
4142, 4964, 4683, 4630, 4746, 4254, 4197, 4236, 3877, 4474, 3943, 3969,
3876, 3760, 4061, 3930, 3833, 3678, 3197, 3509, 3634, 3273, 3451, 3452,
3453, 4054, 4137, 3906, 3833, 3828, 3782, 3665, 4239, 3696, 3577, 3573,
4014, 3962, 3961, 6681, 4174, 5002, 4331, 4757, 3877, 4008, 4220, 6237,
5506, 4558, 4062, 4409, 4679, 4594, 3941, 5070, 3814, 4007, 3871, 3596,
3478, 3363, 3466, 4164, 4490, 3662,
}
expectedPvo := []float64{
0, -2.88, -5.28, -7.67, -8.98, -10.88, -13.49, -15.76, -16.92, -17.12,
-18.45, -18.51, -17.41, -14.45, -14.91, -14.4, -13.19, -12.87, -13.46,
-12.11, -11.04, -9.66, -9.57, -9.45, -8.64, -6.34, -5.01, -4.01, -2.97,
-3.01, -3.12, -3.1, -3.72, -3.05, -3.49, -3.75, -4.09, -4.55, -4.28,
-4.28, -4.42, -4.79, -6.03, -6.32, -6.22, -6.85, -6.9, -6.88, -6.78,
-5.32, -3.95, -3.32, -2.94, -2.63, -2.44, -2.52, -1.35, -1.55, -1.94,
-2.23, -1.51, -1.04, -0.66, 4.99, 4.41, 5.45, 4.94, 5.27, 3.86, 2.94,
2.59, 5.92, 7.04, 6.21, 4.63, 3.94, 3.82, 3.54, 2.13, 2.99, 1.42, 0.49,
-0.5, -1.8, -3.06, -4.27, -5, -4.11, -2.72, -3.24,
}
expectedSignal := []float64{
0, -0.58, -1.52, -2.75, -3.99, -5.37, -6.99, -8.75, -10.38, -11.73,
-13.07, -14.16, -14.81, -14.74, -14.77, -14.7, -14.4, -14.09, -13.97,
-13.59, -13.08, -12.4, -11.83, -11.36, -10.81, -9.92, -8.94, -7.95, -6.96,
-6.17, -5.56, -5.07, -4.8, -4.45, -4.26, -4.15, -4.14, -4.22, -4.23,
-4.24, -4.28, -4.38, -4.71, -5.03, -5.27, -5.59, -5.85, -6.05, -6.2,
-6.02, -5.61, -5.15, -4.71, -4.29, -3.92, -3.64, -3.18, -2.86, -2.67,
-2.58, -2.37, -2.1, -1.81, -0.45, 0.52, 1.51, 2.19, 2.81, 3.02, 3, 2.92,
3.52, 4.22, 4.62, 4.62, 4.49, 4.35, 4.19, 3.78, 3.62, 3.18, 2.64, 2.02,
1.25, 0.39, -0.54, -1.43, -1.97, -2.12, -2.34,
}
expectedHistogram := []float64{
0, -2.3, -3.76, -4.92, -4.98, -5.51, -6.5, -7.02, -6.54, -5.39, -5.38,
-4.35, -2.6, 0.29, -0.13, 0.3, 1.21, 1.22, 0.5, 1.48, 2.05, 2.74, 2.26,
1.91, 2.17, 3.58, 3.93, 3.94, 3.99, 3.15, 2.44, 1.97, 1.08, 1.39, 0.77,
0.41, 0.05, -0.33, -0.05, -0.03, -0.14, -0.41, -1.32, -1.28, -0.95, -1.26,
-1.06, -0.82, -0.58, 0.7, 1.66, 1.83, 1.77, 1.67, 1.48, 1.12, 1.84, 1.31,
0.74, 0.35, 0.86, 1.07, 1.16, 5.44, 3.89, 3.94, 2.75, 2.46, 0.84, -0.06,
-0.33, 2.4, 2.82, 1.59, 0.01, -0.55, -0.53, -0.65, -1.65, -0.63, -1.76,
-2.15, -2.51, -3.05, -3.45, -3.73, -3.56, -2.14, -0.6, -0.9,
}
actualPvo, actualSignal, actualHistogram := DefaultPercentageVolumeOscillator(volume)
testEquals(t, roundDigitsAll(actualPvo, 2), expectedPvo)
testEquals(t, roundDigitsAll(actualSignal, 2), expectedSignal)
testEquals(t, roundDigitsAll(actualHistogram, 2), expectedHistogram)
}
func TestStochasticOscillator(t *testing.T) {
high := []float64{
127.01, 127.62, 126.59, 127.35, 128.17,
128.43, 127.37, 126.42, 126.90, 126.85,
125.65, 125.72, 127.16, 127.72, 127.69,
128.22, 128.27, 128.09, 128.27, 127.74,
}
low := []float64{
125.36, 126.16, 124.93, 126.09, 126.82,
126.48, 126.03, 124.83, 126.39, 125.72,
124.56, 124.57, 125.07, 126.86, 126.63,
126.80, 126.71, 126.80, 126.13, 125.92,
}
closing := []float64{
126.00, 126.60, 127.10, 127.20, 128.10,
128.20, 126.30, 126.00, 126.60, 127.00,
127.50, 128.00, 128.10, 127.29, 127.18,
128.01, 127.11, 127.73, 127.06, 127.33,
}
expectedK := []float64{
38.79, 54.87, 80.67, 84.39, 97.84,
93.43, 39.14, 32.5, 49.17, 60.28,
75.97, 88.89, 91.47, 70.54, 67.7,
89.15, 65.89, 81.91, 64.60, 74.66,
}
actualK, _ := StochasticOscillator(high, low, closing)
testEquals(t, roundDigitsAll(actualK, 2), expectedK)
}