forked from cinar/indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
279 lines (213 loc) · 5.5 KB
/
helper.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
276
277
278
279
// Copyright (c) 2021 Onur Cinar. All Rights Reserved.
// The source code is provided under MIT License.
//
// https://github.com/cinar/indicator
package indicator
import (
"math"
"testing"
)
// Check values same size.
func checkSameSize(values ...[]float64) {
if len(values) < 2 {
return
}
n := len(values[0])
for i := 1; i < len(values); i++ {
if len(values[i]) != n {
panic("not all same size")
}
}
}
// Multiply values by multipler.
func multiplyBy(values []float64, multiplier float64) []float64 {
result := make([]float64, len(values))
for i, value := range values {
result[i] = value * multiplier
}
return result
}
// Multiply values1 and values2.
func multiply(values1, values2 []float64) []float64 {
checkSameSize(values1, values2)
result := make([]float64, len(values1))
for i := 0; i < len(result); i++ {
result[i] = values1[i] * values2[i]
}
return result
}
// Divide values by divider.
func divideBy(values []float64, divider float64) []float64 {
return multiplyBy(values, float64(1)/divider)
}
// Divide values1 by values2.
func divide(values1, values2 []float64) []float64 {
checkSameSize(values1, values2)
result := make([]float64, len(values1))
for i := 0; i < len(result); i++ {
result[i] = values1[i] / values2[i]
}
return result
}
// Add values1 and values2.
func add(values1, values2 []float64) []float64 {
checkSameSize(values1, values2)
result := make([]float64, len(values1))
for i := 0; i < len(result); i++ {
result[i] = values1[i] + values2[i]
}
return result
}
// Add addition to values.
func addBy(values []float64, addition float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(result); i++ {
result[i] = values[i] + addition
}
return result
}
// Substract values2 from values1.
func substract(values1, values2 []float64) []float64 {
substract := multiplyBy(values2, float64(-1))
return add(values1, substract)
}
// Difference between current and before values.
func diff(values []float64, before int) []float64 {
return substract(values, shiftRight(before, values))
}
// Percent difference between current and before values.
func percentDiff(values []float64, before int) []float64 {
result := make([]float64, len(values))
for i := before; i < len(values); i++ {
result[i] = (values[i] - values[i-before]) / values[i-before]
}
return result
}
// Shift right for period and fills with value.
func shiftRightAndFillBy(period int, fill float64, values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(result); i++ {
if i < period {
result[i] = fill
} else {
result[i] = values[i-period]
}
}
return result
}
// Shift right for period.
func shiftRight(period int, values []float64) []float64 {
return shiftRightAndFillBy(period, 0, values)
}
// Round value to digits.
func roundDigits(value float64, digits int) float64 {
n := math.Pow(10, float64(digits))
return math.Round(value*n) / n
}
// Round values to digits.
func roundDigitsAll(values []float64, digits int) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(result); i++ {
result[i] = roundDigits(values[i], digits)
}
return result
}
// Generate numbers.
func generateNumbers(begin, end, step float64) []float64 {
n := int(math.Round((end - begin) / step))
numbers := make([]float64, n)
for i := 0; i < n; i++ {
numbers[i] = begin + (step * float64(i))
}
return numbers
}
// Convets the []int64 to []float64.
func asFloat64(values []int64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(values); i++ {
result[i] = float64(values[i])
}
return result
}
// Calculate power of base with exponent.
func pow(base []float64, exponent float64) []float64 {
result := make([]float64, len(base))
for i := 0; i < len(result); i++ {
result[i] = math.Pow(base[i], exponent)
}
return result
}
// Extact sign.
func extractSign(values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(result); i++ {
if values[i] >= 0 {
result[i] = 1
} else {
result[i] = -1
}
}
return result
}
// Keep positives.
func keepPositives(values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(values); i++ {
if values[i] > 0 {
result[i] = values[i]
} else {
result[i] = 0
}
}
return result
}
// Keep negatives.
func keepNegatives(values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(values); i++ {
if values[i] < 0 {
result[i] = values[i]
} else {
result[i] = 0
}
}
return result
}
// Test equals.
func testEquals(t *testing.T, actual, expected []float64) {
if len(actual) != len(expected) {
t.Fatal("not the same size")
}
for i := 0; i < len(expected); i++ {
if actual[i] != expected[i] {
t.Fatalf("at %d actual %f expected %f", i, actual[i], expected[i])
}
}
}
// Test equals int array.
func testEqualsInt(t *testing.T, actual, expected []int) {
if len(actual) != len(expected) {
t.Fatal("not the same size")
}
for i := 0; i < len(expected); i++ {
if actual[i] != expected[i] {
t.Fatalf("at %d actual %d expected %d", i, actual[i], expected[i])
}
}
}
// Sqrt of given values.
func sqrt(values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(values); i++ {
result[i] = math.Sqrt(values[i])
}
return result
}
// Abs of given values.
func abs(values []float64) []float64 {
result := make([]float64, len(values))
for i := 0; i < len(values); i++ {
result[i] = math.Abs(values[i])
}
return result
}