@bitfinex/lib-js-util-math 0.7.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -29
- package/src/bn.js +4 -0
- package/test/array.test.js +268 -269
- package/test/bn.test.js +169 -167
- package/test/ema.test.js +23 -27
- package/test/impact-mid-price.test.js +223 -214
- package/test/std-deviation.test.js +161 -166
- package/test/twap.test.js +209 -215
- package/test/vwap.test.js +115 -117
- package/test/index.test.js +0 -19
|
@@ -1,172 +1,167 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
.use(require('dirty-chai'))
|
|
5
|
-
const { expect } = chai
|
|
3
|
+
const test = require('brittle')
|
|
6
4
|
const { BN } = require('../src/bn')
|
|
7
5
|
const { stdDeviation, filterStdDeviated, zScore } = require('../src/std-deviation')
|
|
8
6
|
const BigNumber = BN()
|
|
9
7
|
|
|
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
|
-
}
|
|
8
|
+
test('stdDeviation - it should fail with invalid input', (t) => {
|
|
9
|
+
const values = ['10161', { price: '10261.235', amount: '1' }]
|
|
10
|
+
|
|
11
|
+
t.exception.all(
|
|
12
|
+
stdDeviation.bind(null, values)
|
|
13
|
+
)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('stdDeviation - it should fail with non numeric value', (t) => {
|
|
17
|
+
const values = ['10152.235', 'a10261.235']
|
|
18
|
+
|
|
19
|
+
t.exception.all(
|
|
20
|
+
stdDeviation.bind(null, values)
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('stdDeviation - it should fail with NaN value', (t) => {
|
|
25
|
+
const values = [10500, 10700, 11500, '12300', 5000, 5100, NaN, NaN]
|
|
26
|
+
|
|
27
|
+
t.exception(
|
|
28
|
+
stdDeviation.bind(null, values),
|
|
29
|
+
/ERR_NUM_NAN/
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('stdDeviation - it should return expected value with valid config', (t) => {
|
|
34
|
+
const values = [10500, 10700, 11500, '12300', 5000, 5100]
|
|
35
|
+
|
|
36
|
+
const dev = stdDeviation(values)
|
|
37
|
+
t.is(dev instanceof BigNumber, true)
|
|
38
|
+
t.is(dev.toString(), '2980.16591633568094682823')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('stdDeviation - it should work with single item in array', (t) => {
|
|
42
|
+
const values = [10500]
|
|
43
|
+
|
|
44
|
+
const dev = stdDeviation(values)
|
|
45
|
+
t.is(dev instanceof BigNumber, true)
|
|
46
|
+
t.is(dev.toString(), '0')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('stdDeviation - it should return expected value with negative values', (t) => {
|
|
50
|
+
const values = [-2, -3, -4, -5]
|
|
51
|
+
|
|
52
|
+
const dev = stdDeviation(values)
|
|
53
|
+
t.is(dev instanceof BigNumber, true)
|
|
54
|
+
t.is(dev.toString(), '1.1180339887498948482')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('stdDeviation - it should return expected value with mixed positive/negative values', (t) => {
|
|
58
|
+
const values = [2, 3, -4, 5]
|
|
59
|
+
|
|
60
|
+
const dev = stdDeviation(values)
|
|
61
|
+
t.is(dev instanceof BigNumber, true)
|
|
62
|
+
t.is(dev.toString(), '3.35410196624968454461')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('stdDeviation - it should return expected value with selector arg', (t) => {
|
|
66
|
+
const values = [
|
|
67
|
+
{ price: 10500, amount: '1' },
|
|
68
|
+
{ price: 10700, amount: '1' },
|
|
69
|
+
{ price: 11500, amount: '1' },
|
|
70
|
+
{ price: '12300', amount: '1' },
|
|
71
|
+
{ price: 5000, amount: '1' },
|
|
72
|
+
{ price: 5100, amount: '1' }
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
const dev = stdDeviation(values, (a) => a.price)
|
|
76
|
+
t.is(dev instanceof BigNumber, true)
|
|
77
|
+
t.is(dev.toString(), '2980.16591633568094682823')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('zScore - it should throw on null value', (t) => {
|
|
81
|
+
t.exception.all(zScore.bind(null, 33, null, 25))
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('zScore - it should throw on NaN values', (t) => {
|
|
85
|
+
t.exception.all(zScore.bind(null, 190, NaN, 25))
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('zScore - it should throw on divide by zero', (t) => {
|
|
89
|
+
t.exception.all(zScore.bind(null, 190, 150, 0))
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('zScore - it should calculate the expected value', (t) => {
|
|
93
|
+
t.is(zScore(190, 150, 25).toString(), '1.6')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('filterStdDeviated - it should fail with invalid input', (t) => {
|
|
97
|
+
const values = ['10161', { price: '10261.235', amount: '1' }]
|
|
98
|
+
|
|
99
|
+
t.exception.all(
|
|
100
|
+
filterStdDeviated.bind(null, values, 1)
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('filterStdDeviated - it should fail with non numeric price', (t) => {
|
|
105
|
+
const values = ['10152.235', 'a10261.235']
|
|
106
|
+
|
|
107
|
+
t.exception.all(
|
|
108
|
+
filterStdDeviated.bind(null, values, 1)
|
|
109
|
+
)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('filterStdDeviated - it should fail with non numeric threshold', (t) => {
|
|
113
|
+
const values = ['10152.235', 'a10261.235']
|
|
114
|
+
|
|
115
|
+
t.exception.all(
|
|
116
|
+
filterStdDeviated.bind(null, values, 'a232')
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('filterStdDeviated - it should return expected value with valid config', (t) => {
|
|
121
|
+
const values = [10500, 10700, 11500, '12300', 5000, 5100]
|
|
122
|
+
|
|
123
|
+
const res = filterStdDeviated(values, '1')
|
|
124
|
+
const remaining = [10500, 10700, 11500]
|
|
125
|
+
t.ok(res.every(n => remaining.includes(n)))
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('filterStdDeviated - it should work with single item in array', (t) => {
|
|
129
|
+
const values = [10500]
|
|
130
|
+
|
|
131
|
+
const res = filterStdDeviated(values, 1)
|
|
132
|
+
t.is(res.length, 1)
|
|
133
|
+
t.is(res[0], 10500)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('filterStdDeviated - it should work with negative array', (t) => {
|
|
137
|
+
const values = [-2, -3, '-4', -5]
|
|
138
|
+
|
|
139
|
+
const res = filterStdDeviated(values, 1)
|
|
140
|
+
t.is(res.length, 2)
|
|
141
|
+
const remaining = [-3, '-4']
|
|
142
|
+
t.ok(res.every(n => remaining.includes(n)))
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('filterStdDeviated - it should work with mixed positive/negative array', (t) => {
|
|
146
|
+
const values = [2, 3, '-4', 5]
|
|
147
|
+
|
|
148
|
+
const res = filterStdDeviated(values, 1)
|
|
149
|
+
t.is(res.length, 2)
|
|
150
|
+
const remaining = [2, 3]
|
|
151
|
+
t.ok(res.every(n => remaining.includes(n)))
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('filterDeviated - it should return expected value with selector arg', (t) => {
|
|
155
|
+
const values = [
|
|
156
|
+
{ price: 10500, amount: '1' },
|
|
157
|
+
{ price: 10700, amount: '1' },
|
|
158
|
+
{ price: 11500, amount: '1' },
|
|
159
|
+
{ price: '12300', amount: '1' },
|
|
160
|
+
{ price: 5000, amount: '1' },
|
|
161
|
+
{ price: 5100, amount: '1' }
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
const res = filterStdDeviated(values, '1', (a) => a.price)
|
|
165
|
+
const remaining = [10500, 10700, 11500]
|
|
166
|
+
t.ok(res.every(a => remaining.includes(a.price)))
|
|
167
|
+
})
|