@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.
@@ -1,219 +1,228 @@
1
1
  'use strict'
2
2
 
3
- const { expect } = require('chai')
3
+ const test = require('brittle')
4
4
  const { nBN } = require('../src/bn')
5
5
  const impactMidPrice = require('../src/impact-mid-price')
6
6
 
7
- module.exports = () => {
8
- describe('# impact-mid-price-tests', () => {
9
- it('it should fail with incompatible type', () => {
10
- expect(
11
- impactMidPrice.bind(null, {}, {}, 10)
12
- ).to.throw('ERR_INVALID_BID_ASK_TYPE')
13
-
14
- expect(
15
- impactMidPrice.bind(null, [[1000, 1], { price: 10000, amount: 33 }], [[1000, 13], [1000, 33]], 10)
16
- ).to.throw('ERR_INVALID_BID_ASK_ELEM_TYPE')
17
-
18
- expect(
19
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], { price: 10000, amount: 33 }], 10)
20
- ).to.throw('ERR_INVALID_BID_ASK_ELEM_TYPE')
21
- })
22
-
23
- it('it should fail with invalid array lengths', () => {
24
- expect(
25
- impactMidPrice.bind(null, [], [], 10)
26
- ).to.throw('ERR_INVALID_BID_ASK_LENGTH')
27
-
28
- expect(
29
- impactMidPrice.bind(null, [[1000, 3], [1000, 13]], [[1000, 13], [1000]], 10)
30
- ).to.throw('ERR_INVALID_BID_ASK_ELEM_LENGTH')
31
- })
32
-
33
- it('it should fail with NaN values', () => {
34
- expect(
35
- impactMidPrice.bind(null, [[1000, 1], [null, 13]], [[1000, 13], [1000, 33]], 10)
36
- ).to.throw('ERR_NUM_NAN')
37
-
38
- expect(
39
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [null, 33]], 10)
40
- ).to.throw('ERR_NUM_NAN')
41
- })
42
-
43
- it('it should fail with negative amounts', () => {
44
- expect(
45
- impactMidPrice.bind(null, [[1000, 1], ['-1000', 13]], [[1000, 13], [1000, 33]], 10)
46
- ).to.throw('ERR_NUM_NOT_POS')
47
-
48
- expect(
49
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [-1000, 33]], 10)
50
- ).to.throw('ERR_NUM_NOT_POS')
51
- })
52
-
53
- it('it should fail with invalid depth param', () => {
54
- expect(
55
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [1000, 33]], 'a123')
56
- ).to.throw('ERR_NUM_NAN')
57
-
58
- expect(
59
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [1000, 33]], null)
60
- ).to.throw('ERR_NUM_NAN')
61
-
62
- expect(
63
- impactMidPrice.bind(null, [[1000, 1], [null, 13]], [[1000, 13], [1000, 33]], '-10')
64
- ).to.throw('ERR_NUM_NOT_POS')
65
-
66
- expect(
67
- impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [null, 33]], -10.33)
68
- ).to.throw('ERR_NUM_NOT_POS')
69
- })
70
-
71
- it('it should calc expected value', () => {
72
- const bids = [
73
- [7897.0, 0.002],
74
- [7894.8, 1.0620],
75
- [7893.6, 1.2660],
76
- [7893.5, 0.0142],
77
- [7893.2, 1.4150],
78
- [7892.8, 0.2359],
79
- [7892.2, 0.0212],
80
- [7890.8, 0.2110],
81
- [7890.2, 0.0283],
82
- [7889.5, 1.6990],
83
- [7888.8, 0.0354],
84
- [7888.4, 2.1230],
85
- [7887.5, 0.0425],
86
- [7886.8, 2.8310]
87
- ]
88
- const asks = [
89
- [7900.9, 4.246],
90
- [7901.0, 0.3538],
91
- [7901.3, 1.062],
92
- [7902.9, 1.416],
93
- [7903.0, 0.002],
94
- [7903.6, 1.699],
95
- [7903.7, 0.0142],
96
- [7904.8, 2.124],
97
- [7905.0, 0.0212],
98
- [7906.4, 2.832],
99
- [7907.0, 0.0283],
100
- [7908.3, 0.0354],
101
- [7908.8, 3.54],
102
- [7909.7, 0.0425]
103
- ]
104
- const res = impactMidPrice(bids, asks, 10)
105
- expect(nBN(res).toFixed(2)).to.be.equal('7896.32')
106
- })
107
-
108
- it('it should work also with complex types', () => {
109
- const bids = [
110
- [7897.0, 0.002],
111
- [7894.8, 1.0620],
112
- [7893.6, 1.2660],
113
- ['7893.5', 0.0142],
114
- [7893.2, 1.4150],
115
- [7892.8, 0.2359],
116
- [7892.2, 0.0212],
117
- [7890.8, 0.2110],
118
- ['7890.2', 0.0283],
119
- ['7889.5', 1.6990],
120
- ['7888.8', 0.0354],
121
- [7888.4, nBN(2.1230)],
122
- [7887.5, nBN('0.0425')],
123
- [7886.8, 2.8310]
124
- ]
125
- const asks = [
126
- [7900.9, 4.246],
127
- [7901.0, 0.3538],
128
- [7901.3, nBN(1.062)],
129
- [7902.9, nBN('1.416')],
130
- [7903.0, 0.002],
131
- ['7903.6', 1.699],
132
- ['7903.7', 0.0142],
133
- [7904.8, 2.124],
134
- [7905.0, 0.0212],
135
- [7906.4, 2.832],
136
- [7907.0, 0.0283],
137
- [7908.3, 0.0354],
138
- [7908.8, 3.54],
139
- [7909.7, 0.0425]
140
- ]
141
- const res = impactMidPrice(bids, asks, 10)
142
- expect(nBN(res).toFixed(2)).to.be.equal('7896.32')
143
- })
144
-
145
- it('it should work with decimal depth', () => {
146
- const bids = [
147
- [7897.0, 0.002],
148
- [7894.8, 1.0620],
149
- [7893.6, 1.2660],
150
- ['7893.5', 0.0142],
151
- [7893.2, 1.4150],
152
- [7892.8, 0.2359],
153
- [7892.2, 0.0212],
154
- [7890.8, 0.2110],
155
- ['7890.2', 0.0283],
156
- ['7889.5', 1.6990],
157
- ['7888.8', 0.0354],
158
- [7888.4, nBN(2.1230)],
159
- [7887.5, nBN('0.0425')],
160
- [7886.8, 2.8310]
161
- ]
162
- const asks = [
163
- [7900.9, 4.246],
164
- [7901.0, 0.3538],
165
- [7901.3, nBN(1.062)],
166
- [7902.9, nBN('1.416')],
167
- [7903.0, 0.002],
168
- ['7903.6', 1.699],
169
- ['7903.7', 0.0142],
170
- [7904.8, 2.124],
171
- [7905.0, 0.0212],
172
- [7906.4, 2.832],
173
- [7907.0, 0.0283],
174
- [7908.3, 0.0354],
175
- [7908.8, 3.54],
176
- [7909.7, 0.0425]
177
- ]
178
- const res = impactMidPrice(bids, asks, 10.917)
179
- expect(nBN(res).toFixed(2)).to.be.equal('7896.28')
180
- })
181
-
182
- it('it should return null in insufficient depth', () => {
183
- const bids = [
184
- [7897.0, 0.002],
185
- [7894.8, 1.0620],
186
- [7893.6, 1.2660],
187
- ['7893.5', 0.0142],
188
- [7893.2, 1.4150],
189
- [7892.8, 0.2359],
190
- [7892.2, 0.0212],
191
- [7890.8, 0.2110],
192
- ['7890.2', 0.0283],
193
- ['7889.5', 1.6990],
194
- ['7888.8', 0.0354],
195
- [7888.4, nBN(2.1230)],
196
- [7887.5, nBN('0.0425')],
197
- [7886.8, 2.8310]
198
- ]
199
- const asks = [
200
- [7900.9, 4.246],
201
- [7901.0, 0.3538],
202
- [7901.3, nBN(1.062)],
203
- [7902.9, nBN('1.416')],
204
- [7903.0, 0.002],
205
- ['7903.6', 1.699],
206
- ['7903.7', 0.0142],
207
- [7904.8, 2.124],
208
- [7905.0, 0.0212],
209
- [7906.4, 2.832],
210
- [7907.0, 0.0283],
211
- [7908.3, 0.0354],
212
- [7908.8, 3.54],
213
- [7909.7, 0.0425]
214
- ]
215
- expect(impactMidPrice(bids, asks.slice(0, 4), 10.917)).to.be.equal(null)
216
- expect(impactMidPrice(bids.slice(0, 5), asks, 10.917)).to.be.equal(null)
217
- })
218
- })
219
- }
7
+ test('it should fail with incompatible type', (t) => {
8
+ t.exception(
9
+ impactMidPrice.bind(null, {}, {}, 10),
10
+ /ERR_INVALID_BID_ASK_TYPE/
11
+ )
12
+
13
+ t.exception(
14
+ impactMidPrice.bind(null, [[1000, 1], { price: 10000, amount: 33 }], [[1000, 13], [1000, 33]], 10),
15
+ /ERR_INVALID_BID_ASK_ELEM_TYPE/
16
+ )
17
+
18
+ t.exception(
19
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], { price: 10000, amount: 33 }], 10),
20
+ /ERR_INVALID_BID_ASK_ELEM_TYPE/
21
+ )
22
+ })
23
+
24
+ test('it should fail with invalid array lengths', (t) => {
25
+ t.exception(
26
+ impactMidPrice.bind(null, [], [], 10),
27
+ /ERR_INVALID_BID_ASK_LENGTH/
28
+ )
29
+
30
+ t.exception(
31
+ impactMidPrice.bind(null, [[1000, 3], [1000, 13]], [[1000, 13], [1000]], 10),
32
+ /ERR_INVALID_BID_ASK_ELEM_LENGTH/
33
+ )
34
+ })
35
+
36
+ test('it should fail with NaN values', (t) => {
37
+ t.exception(
38
+ impactMidPrice.bind(null, [[1000, 1], [null, 13]], [[1000, 13], [1000, 33]], 10),
39
+ /ERR_NUM_NAN/
40
+ )
41
+
42
+ t.exception(
43
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [null, 33]], 10),
44
+ /ERR_NUM_NAN/
45
+ )
46
+ })
47
+
48
+ test('it should fail with negative amounts', (t) => {
49
+ t.exception(
50
+ impactMidPrice.bind(null, [[1000, 1], ['-1000', 13]], [[1000, 13], [1000, 33]], 10),
51
+ /ERR_NUM_NOT_POS/
52
+ )
53
+
54
+ t.exception(
55
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [-1000, 33]], 10),
56
+ /ERR_NUM_NOT_POS/
57
+ )
58
+ })
59
+
60
+ test('it should fail with invalid depth param', (t) => {
61
+ t.exception(
62
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [1000, 33]], 'a123'),
63
+ /ERR_NUM_NAN/
64
+ )
65
+
66
+ t.exception(
67
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [1000, 33]], null),
68
+ /ERR_NUM_NAN/
69
+ )
70
+
71
+ t.exception(
72
+ impactMidPrice.bind(null, [[1000, 1], [null, 13]], [[1000, 13], [1000, 33]], '-10'),
73
+ /ERR_NUM_NOT_POS/
74
+ )
75
+
76
+ t.exception(
77
+ impactMidPrice.bind(null, [[1000, 1], [1000, 13]], [[1000, 13], [null, 33]], -10.33),
78
+ /ERR_NUM_NOT_POS/
79
+ )
80
+ })
81
+
82
+ test('it should calc expected value', (t) => {
83
+ const bids = [
84
+ [7897.0, 0.002],
85
+ [7894.8, 1.0620],
86
+ [7893.6, 1.2660],
87
+ [7893.5, 0.0142],
88
+ [7893.2, 1.4150],
89
+ [7892.8, 0.2359],
90
+ [7892.2, 0.0212],
91
+ [7890.8, 0.2110],
92
+ [7890.2, 0.0283],
93
+ [7889.5, 1.6990],
94
+ [7888.8, 0.0354],
95
+ [7888.4, 2.1230],
96
+ [7887.5, 0.0425],
97
+ [7886.8, 2.8310]
98
+ ]
99
+ const asks = [
100
+ [7900.9, 4.246],
101
+ [7901.0, 0.3538],
102
+ [7901.3, 1.062],
103
+ [7902.9, 1.416],
104
+ [7903.0, 0.002],
105
+ [7903.6, 1.699],
106
+ [7903.7, 0.0142],
107
+ [7904.8, 2.124],
108
+ [7905.0, 0.0212],
109
+ [7906.4, 2.832],
110
+ [7907.0, 0.0283],
111
+ [7908.3, 0.0354],
112
+ [7908.8, 3.54],
113
+ [7909.7, 0.0425]
114
+ ]
115
+ const res = impactMidPrice(bids, asks, 10)
116
+ t.is(nBN(res).toFixed(2), '7896.32')
117
+ })
118
+
119
+ test('it should work also with complex types', (t) => {
120
+ const bids = [
121
+ [7897.0, 0.002],
122
+ [7894.8, 1.0620],
123
+ [7893.6, 1.2660],
124
+ ['7893.5', 0.0142],
125
+ [7893.2, 1.4150],
126
+ [7892.8, 0.2359],
127
+ [7892.2, 0.0212],
128
+ [7890.8, 0.2110],
129
+ ['7890.2', 0.0283],
130
+ ['7889.5', 1.6990],
131
+ ['7888.8', 0.0354],
132
+ [7888.4, nBN(2.1230)],
133
+ [7887.5, nBN('0.0425')],
134
+ [7886.8, 2.8310]
135
+ ]
136
+ const asks = [
137
+ [7900.9, 4.246],
138
+ [7901.0, 0.3538],
139
+ [7901.3, nBN(1.062)],
140
+ [7902.9, nBN('1.416')],
141
+ [7903.0, 0.002],
142
+ ['7903.6', 1.699],
143
+ ['7903.7', 0.0142],
144
+ [7904.8, 2.124],
145
+ [7905.0, 0.0212],
146
+ [7906.4, 2.832],
147
+ [7907.0, 0.0283],
148
+ [7908.3, 0.0354],
149
+ [7908.8, 3.54],
150
+ [7909.7, 0.0425]
151
+ ]
152
+ const res = impactMidPrice(bids, asks, 10)
153
+ t.is(nBN(res).toFixed(2), '7896.32')
154
+ })
155
+
156
+ test('it should work with decimal depth', (t) => {
157
+ const bids = [
158
+ [7897.0, 0.002],
159
+ [7894.8, 1.0620],
160
+ [7893.6, 1.2660],
161
+ ['7893.5', 0.0142],
162
+ [7893.2, 1.4150],
163
+ [7892.8, 0.2359],
164
+ [7892.2, 0.0212],
165
+ [7890.8, 0.2110],
166
+ ['7890.2', 0.0283],
167
+ ['7889.5', 1.6990],
168
+ ['7888.8', 0.0354],
169
+ [7888.4, nBN(2.1230)],
170
+ [7887.5, nBN('0.0425')],
171
+ [7886.8, 2.8310]
172
+ ]
173
+ const asks = [
174
+ [7900.9, 4.246],
175
+ [7901.0, 0.3538],
176
+ [7901.3, nBN(1.062)],
177
+ [7902.9, nBN('1.416')],
178
+ [7903.0, 0.002],
179
+ ['7903.6', 1.699],
180
+ ['7903.7', 0.0142],
181
+ [7904.8, 2.124],
182
+ [7905.0, 0.0212],
183
+ [7906.4, 2.832],
184
+ [7907.0, 0.0283],
185
+ [7908.3, 0.0354],
186
+ [7908.8, 3.54],
187
+ [7909.7, 0.0425]
188
+ ]
189
+ const res = impactMidPrice(bids, asks, 10.917)
190
+ t.is(nBN(res).toFixed(2), '7896.28')
191
+ })
192
+
193
+ test('it should return null in insufficient depth', (t) => {
194
+ const bids = [
195
+ [7897.0, 0.002],
196
+ [7894.8, 1.0620],
197
+ [7893.6, 1.2660],
198
+ ['7893.5', 0.0142],
199
+ [7893.2, 1.4150],
200
+ [7892.8, 0.2359],
201
+ [7892.2, 0.0212],
202
+ [7890.8, 0.2110],
203
+ ['7890.2', 0.0283],
204
+ ['7889.5', 1.6990],
205
+ ['7888.8', 0.0354],
206
+ [7888.4, nBN(2.1230)],
207
+ [7887.5, nBN('0.0425')],
208
+ [7886.8, 2.8310]
209
+ ]
210
+ const asks = [
211
+ [7900.9, 4.246],
212
+ [7901.0, 0.3538],
213
+ [7901.3, nBN(1.062)],
214
+ [7902.9, nBN('1.416')],
215
+ [7903.0, 0.002],
216
+ ['7903.6', 1.699],
217
+ ['7903.7', 0.0142],
218
+ [7904.8, 2.124],
219
+ [7905.0, 0.0212],
220
+ [7906.4, 2.832],
221
+ [7907.0, 0.0283],
222
+ [7908.3, 0.0354],
223
+ [7908.8, 3.54],
224
+ [7909.7, 0.0425]
225
+ ]
226
+ t.is(impactMidPrice(bids, asks.slice(0, 4), 10.917), null)
227
+ t.is(impactMidPrice(bids.slice(0, 5), asks, 10.917), null)
228
+ })