@icgio/icg-utils 1.9.73 → 1.9.75
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/README.md +18 -177
- package/index.js +0 -3
- package/lib/influxdb.js +54 -76
- package/lib/logger.js +1 -19
- package/lib/market-history-buffer.js +2 -8
- package/lib/observable.js +0 -18
- package/lib/ohlcv.js +0 -7
- package/lib/ordered-dict.js +4 -31
- package/lib/rate-limiter.js +4 -16
- package/lib/rates.js +66 -197
- package/lib/utils.js +85 -594
- package/package.json +1 -4
- package/lib/ip.js +0 -19
- package/test/combine-orderbooks.test.js +0 -226
- package/test/merge-trades.test.js +0 -327
- package/test/ordered-dict.test.js +0 -343
- package/test/rate-limiter.test.js +0 -60
- package/test/sorted-book.test.js +0 -257
package/lib/utils.js
CHANGED
|
@@ -1,66 +1,18 @@
|
|
|
1
1
|
const _ = require('lodash')
|
|
2
2
|
|
|
3
|
-
// Number.prototype.toFixed_old = Number.prototype.toFixed
|
|
4
|
-
|
|
5
|
-
// Number.prototype.toFixed = function(s) {
|
|
6
|
-
// if (s >= 0 && s <= 20) {
|
|
7
|
-
// return this.toFixed_old(s)
|
|
8
|
-
// }
|
|
9
|
-
// let changenum = (parseInt(this * Math.pow(10, s) + 0.5) / Math.pow(10, s)).toString()
|
|
10
|
-
// index = changenum.indexOf('.')
|
|
11
|
-
// if (index < 0 && s > 0) {
|
|
12
|
-
// changenum = changenum + '.'
|
|
13
|
-
// for (i = 0; i < s; i++) {
|
|
14
|
-
// changenum = changenum + '0'
|
|
15
|
-
// }
|
|
16
|
-
// } else {
|
|
17
|
-
// index = changenum.length - index;
|
|
18
|
-
// for (i = 0; i < (s - index) + 1; i++) {
|
|
19
|
-
// changenum = changenum + '0'
|
|
20
|
-
// }
|
|
21
|
-
// }
|
|
22
|
-
// return changenum
|
|
23
|
-
// }
|
|
24
|
-
|
|
25
|
-
// console.log(0.000000000000000000000000001232312421.toFixed(30))
|
|
26
|
-
|
|
27
3
|
Number.prototype.toStringNoExponents = function () {
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
str = data[0].replace('.', ''),
|
|
35
|
-
mag = Number(data[1]) + 1
|
|
36
|
-
if (mag < 0) {
|
|
37
|
-
z = sign + '0.'
|
|
38
|
-
while (mag++) {
|
|
39
|
-
z += '0'
|
|
40
|
-
}
|
|
41
|
-
return z + str.replace(/^\-/, '')
|
|
42
|
-
}
|
|
43
|
-
mag -= str.length
|
|
44
|
-
while (mag--) {
|
|
45
|
-
z += '0'
|
|
46
|
-
}
|
|
47
|
-
return str + z
|
|
4
|
+
const [mantissa, exponent] = String(this).split(/[eE]/)
|
|
5
|
+
if (exponent === undefined) return mantissa
|
|
6
|
+
const sign = this < 0 ? '-' : ''
|
|
7
|
+
const digits = mantissa.replace('-', '').replace('.', '')
|
|
8
|
+
const point = Number(exponent) + 1 // decimal point position, counted from the first digit
|
|
9
|
+
return point < 0 ? sign + '0.' + '0'.repeat(-point) + digits : sign + digits + '0'.repeat(point - digits.length)
|
|
48
10
|
}
|
|
49
11
|
|
|
50
12
|
Date.prototype.addHours = function (h) {
|
|
51
13
|
return new Date(this.getTime() + h * 60 * 60 * 1000)
|
|
52
14
|
}
|
|
53
15
|
|
|
54
|
-
Date.prototype.roundHours = function (m = 1) {
|
|
55
|
-
let coeff = m * 60 * 60 * 1000
|
|
56
|
-
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
Date.prototype.ceilHours = function (m = 1) {
|
|
60
|
-
let coeff = m * 60 * 60 * 1000
|
|
61
|
-
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
16
|
Date.prototype.floorHours = function (m = 1) {
|
|
65
17
|
let coeff = m * 60 * 60 * 1000
|
|
66
18
|
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
@@ -70,21 +22,6 @@ Date.prototype.addMinutes = function (m) {
|
|
|
70
22
|
return new Date(this.getTime() + m * 60 * 1000)
|
|
71
23
|
}
|
|
72
24
|
|
|
73
|
-
Date.prototype.roundMinutes = function (m = 1) {
|
|
74
|
-
let coeff = m * 60 * 1000
|
|
75
|
-
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
Date.prototype.ceilMinutes = function (m = 1) {
|
|
79
|
-
let coeff = m * 60 * 1000
|
|
80
|
-
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
Date.prototype.floorMinutes = function (m = 1) {
|
|
84
|
-
let coeff = m * 60 * 1000
|
|
85
|
-
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
25
|
Date.prototype.addSeconds = function (s) {
|
|
89
26
|
return new Date(this.getTime() + s * 1000)
|
|
90
27
|
}
|
|
@@ -93,42 +30,26 @@ String.prototype.replaceAt = function (index, replacement) {
|
|
|
93
30
|
return this.substr(0, index) + replacement + this.substr(index + replacement.length)
|
|
94
31
|
}
|
|
95
32
|
|
|
96
|
-
const { ordered_dict
|
|
97
|
-
|
|
98
|
-
function random_id() {
|
|
99
|
-
return _.random(100000000, 999999999)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function get_hhmmss() {
|
|
103
|
-
return new Date().toTimeString().split(' ')[0]
|
|
104
|
-
}
|
|
33
|
+
const { ordered_dict } = require('./ordered-dict.js')
|
|
105
34
|
|
|
106
35
|
function round(number, precision) {
|
|
107
|
-
|
|
108
|
-
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function cut_orderbook(input_array, level_target) {
|
|
112
|
-
return input_array.slice(level_target)
|
|
36
|
+
return _.round(typeof number === 'string' ? parseFloat(number) : number, precision)
|
|
113
37
|
}
|
|
114
38
|
|
|
115
39
|
function level_position(input_array, level_target) {
|
|
116
|
-
|
|
117
|
-
level_target--
|
|
118
|
-
return level_position(input_array, level_target)
|
|
119
|
-
}
|
|
40
|
+
const level = Math.min(level_target, input_array.length)
|
|
120
41
|
let vol = 0,
|
|
121
42
|
price = 0,
|
|
122
43
|
sum = 0,
|
|
123
44
|
pos = 0
|
|
124
|
-
for (let i = 0; i <
|
|
45
|
+
for (let i = 0; i < level; i++) {
|
|
125
46
|
vol = vol + input_array[i][1]
|
|
126
47
|
sum = sum + input_array[i][0] * input_array[i][1]
|
|
127
48
|
price = input_array[i][0]
|
|
128
49
|
pos = i
|
|
129
50
|
}
|
|
130
51
|
const avg_price = sum / vol ? sum / vol : price
|
|
131
|
-
return { level
|
|
52
|
+
return { level, price, avg_price, vol, sum, pos }
|
|
132
53
|
}
|
|
133
54
|
|
|
134
55
|
function price_position(input_array, price) {
|
|
@@ -141,168 +62,52 @@ function price_position(input_array, price) {
|
|
|
141
62
|
pos: 0,
|
|
142
63
|
}
|
|
143
64
|
}
|
|
65
|
+
const is_bid = input_array[0][0] > input_array[1][0]
|
|
144
66
|
let vol = 0,
|
|
145
67
|
sum = 0,
|
|
146
|
-
pos = 0
|
|
147
|
-
type
|
|
148
|
-
if (input_array[0][0] > input_array[1][0]) type = 'bid'
|
|
149
|
-
else type = 'ask'
|
|
68
|
+
pos = 0
|
|
150
69
|
for (let i = 0; i < input_array.length; i++) {
|
|
151
|
-
if (input_array[i][0]
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
} else if (input_array[i][0] >= price && type === 'bid') {
|
|
156
|
-
vol = vol + input_array[i][1]
|
|
157
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
158
|
-
pos = i
|
|
159
|
-
} else {
|
|
160
|
-
break
|
|
161
|
-
}
|
|
70
|
+
if (!(is_bid ? input_array[i][0] >= price : input_array[i][0] <= price)) break
|
|
71
|
+
vol = vol + input_array[i][1]
|
|
72
|
+
sum = sum + input_array[i][0] * input_array[i][1]
|
|
73
|
+
pos = i
|
|
162
74
|
}
|
|
163
75
|
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, pos }
|
|
164
76
|
}
|
|
165
77
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
avg_price: input_array[0][0],
|
|
171
|
-
vol: input_array[0][1],
|
|
172
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
173
|
-
sum_original: input_array[0][0] * input_array[0][1],
|
|
174
|
-
pos: 0,
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
let vol = 0,
|
|
178
|
-
price = 0,
|
|
179
|
-
sum = 0,
|
|
180
|
-
pos = 0,
|
|
181
|
-
sum_original = 0
|
|
182
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
183
|
-
if (sum < sum_target) {
|
|
184
|
-
vol = vol + input_array[i][1]
|
|
185
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
186
|
-
price = input_array[i][0]
|
|
187
|
-
pos = i
|
|
188
|
-
sum_original = sum
|
|
189
|
-
} else if (sum >= sum_target) {
|
|
190
|
-
break
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, sum_original, pos }
|
|
78
|
+
// Mid of the best ask and best bid; null when either side is empty
|
|
79
|
+
function orderbook_mid(asks, bids) {
|
|
80
|
+
if (asks.length === 0 || bids.length === 0) return null
|
|
81
|
+
return (asks[0][0] + bids[0][0]) / 2
|
|
194
82
|
}
|
|
195
83
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
avg_price: input_array[0][0],
|
|
201
|
-
vol: input_array[0][1],
|
|
202
|
-
vol_original: input_array[0][1],
|
|
203
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
204
|
-
pos: 0,
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
let vol = 0,
|
|
208
|
-
price = 0,
|
|
209
|
-
sum = 0,
|
|
210
|
-
pos = 0,
|
|
211
|
-
vol_original = 0
|
|
212
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
213
|
-
if (vol < vol_target) {
|
|
214
|
-
vol = vol + input_array[i][1]
|
|
215
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
216
|
-
price = input_array[i][0]
|
|
217
|
-
pos = i
|
|
218
|
-
vol_original = vol
|
|
219
|
-
} else if (vol >= vol_target) {
|
|
220
|
-
sum = sum - input_array[i - 1][0] * (vol - vol_target)
|
|
221
|
-
vol = vol_target
|
|
222
|
-
break
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return { price, avg_price: sum / vol ? sum / vol : price, vol, vol_original, sum, pos }
|
|
84
|
+
// Best ask minus best bid as a fraction of the mid (0.01 = 1%); null when either side is empty
|
|
85
|
+
function orderbook_spread(asks, bids) {
|
|
86
|
+
const mid = orderbook_mid(asks, bids)
|
|
87
|
+
return mid === null ? null : (asks[0][0] - bids[0][0]) / mid
|
|
226
88
|
}
|
|
227
89
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
90
|
+
// Quote value of one side's levels ([price, amount], best first) within ±pct% of any ref_price, edge included
|
|
91
|
+
function depth_within_pct(levels, ref_price, pct) {
|
|
92
|
+
const band = ref_price * (pct / 100) * (1 + 1e-9)
|
|
93
|
+
let sum = 0
|
|
94
|
+
for (const [price, amount] of levels) {
|
|
95
|
+
if (Math.abs(price - ref_price) > band) break
|
|
96
|
+
sum += price * amount
|
|
231
97
|
}
|
|
232
|
-
|
|
233
|
-
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
234
|
-
result[pos][1] += (sum_target - sum_original) / result[pos][0]
|
|
235
|
-
return result
|
|
98
|
+
return sum
|
|
236
99
|
}
|
|
237
100
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
101
|
+
// Largest gap between adjacent prices in one side's first n levels, as a fraction of the price nearer the touch; null below two levels
|
|
102
|
+
function max_level_gap(levels, n) {
|
|
103
|
+
let gap = null
|
|
104
|
+
for (let i = 0; i + 1 < Math.min(n, levels.length); i++) {
|
|
105
|
+
const level_gap = Math.abs(levels[i + 1][0] - levels[i][0]) / levels[i][0]
|
|
106
|
+
if (gap === null || level_gap > gap) gap = level_gap
|
|
241
107
|
}
|
|
242
|
-
|
|
243
|
-
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
244
|
-
result[pos][1] += vol_target - vol_original
|
|
245
|
-
return result
|
|
108
|
+
return gap
|
|
246
109
|
}
|
|
247
110
|
|
|
248
|
-
// console.log(
|
|
249
|
-
// sum_position_orderbook(
|
|
250
|
-
// [
|
|
251
|
-
// [2, 1],
|
|
252
|
-
// [6, 6],
|
|
253
|
-
// [7, 7],
|
|
254
|
-
// ],
|
|
255
|
-
// 0
|
|
256
|
-
// )
|
|
257
|
-
// )
|
|
258
|
-
// console.log(
|
|
259
|
-
// vol_position_orderbook(
|
|
260
|
-
// [
|
|
261
|
-
// [0.00004637, 14.12],
|
|
262
|
-
// [0.00004636, 19.37],
|
|
263
|
-
// [0.0000463, 77.76],
|
|
264
|
-
// [0.00004625, 17.93],
|
|
265
|
-
// [0.00004623, 14.89],
|
|
266
|
-
// [0.00004621, 34.25],
|
|
267
|
-
// [0.0000462, 131.77],
|
|
268
|
-
// [0.00004619, 24.79],
|
|
269
|
-
// [0.00004618, 36.71],
|
|
270
|
-
// [0.00004617, 18.36],
|
|
271
|
-
// [0.00004616, 9],
|
|
272
|
-
// [0.00004615, 67.22],
|
|
273
|
-
// [0.00004614, 74.74],
|
|
274
|
-
// [0.00004613, 31.89],
|
|
275
|
-
// [0.00004608, 31.25],
|
|
276
|
-
// [0.00004606, 72],
|
|
277
|
-
// [0.00004605, 11.69],
|
|
278
|
-
// [0.00004604, 36.68],
|
|
279
|
-
// [0.00004601, 29.77],
|
|
280
|
-
// [0.00004598, 11.17],
|
|
281
|
-
// [0.00004596, 25.19],
|
|
282
|
-
// [0.00004593, 9.01],
|
|
283
|
-
// [0.00004592, 15.56],
|
|
284
|
-
// [0.00004589, 15.21],
|
|
285
|
-
// [0.00004588, 21.25],
|
|
286
|
-
// [0.00004583, 25.19],
|
|
287
|
-
// [0.00004582, 81.02],
|
|
288
|
-
// [0.00004581, 24.55],
|
|
289
|
-
// [0.0000458, 0.26],
|
|
290
|
-
// [0.00004577, 71.32],
|
|
291
|
-
// [0.00004576, 43.39],
|
|
292
|
-
// [0.00004574, 41.3],
|
|
293
|
-
// [0.00004573, 0.75],
|
|
294
|
-
// [0.0000457, 10.84],
|
|
295
|
-
// [0.00004567, 0.26],
|
|
296
|
-
// [0.00004549, 239.91],
|
|
297
|
-
// [0.00004548, 115.89],
|
|
298
|
-
// [0.00004542, 192.91],
|
|
299
|
-
// [0.00004512, 0.64],
|
|
300
|
-
// [0.000045, 3.24],
|
|
301
|
-
// ],
|
|
302
|
-
// 1
|
|
303
|
-
// )
|
|
304
|
-
// )
|
|
305
|
-
|
|
306
111
|
function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
307
112
|
const exchanges = Object.keys(orderbooks_dict)
|
|
308
113
|
const numExchanges = exchanges.length
|
|
@@ -362,148 +167,6 @@ function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
|
362
167
|
}
|
|
363
168
|
}
|
|
364
169
|
|
|
365
|
-
// console.log(
|
|
366
|
-
// combine_orderbooks(
|
|
367
|
-
// {
|
|
368
|
-
// binance: {
|
|
369
|
-
// asks: [
|
|
370
|
-
// [1, 1],
|
|
371
|
-
// [2, 2],
|
|
372
|
-
// [3, 3],
|
|
373
|
-
// ],
|
|
374
|
-
// bids: [
|
|
375
|
-
// // [0.9, 0.9],
|
|
376
|
-
// // [0.5, 0.5],
|
|
377
|
-
// // [0.3, 0.2],
|
|
378
|
-
// ],
|
|
379
|
-
// },
|
|
380
|
-
// bittrex: {
|
|
381
|
-
// // asks: [
|
|
382
|
-
// // // [4, 4],
|
|
383
|
-
// // // [5, 5],
|
|
384
|
-
// // // [8, 8],
|
|
385
|
-
// // ],
|
|
386
|
-
// bids: [
|
|
387
|
-
// [0.6, 0.9],
|
|
388
|
-
// [0.5, 0.2],
|
|
389
|
-
// [0.2, 0.2],
|
|
390
|
-
// ],
|
|
391
|
-
// },
|
|
392
|
-
// poloniex: {
|
|
393
|
-
// asks: [
|
|
394
|
-
// [1, 1],
|
|
395
|
-
// [6, 6],
|
|
396
|
-
// [7, 7],
|
|
397
|
-
// ],
|
|
398
|
-
// bids: [
|
|
399
|
-
// [0.95, 0.9],
|
|
400
|
-
// [0.7, 0.5],
|
|
401
|
-
// [0.4, 0.2],
|
|
402
|
-
// ],
|
|
403
|
-
// },
|
|
404
|
-
// },
|
|
405
|
-
// 100
|
|
406
|
-
// )
|
|
407
|
-
// )
|
|
408
|
-
// console.log(combine_orderbooks({}, 100))
|
|
409
|
-
|
|
410
|
-
function vol_position_combined(input_array, vol_target) {
|
|
411
|
-
if (input_array.length === 1) {
|
|
412
|
-
return {
|
|
413
|
-
price: input_array[0][0],
|
|
414
|
-
price_exchange: { [input_array[0][2]]: input_array[0][0] },
|
|
415
|
-
avg_price: input_array[0][0],
|
|
416
|
-
vol: input_array[0][1],
|
|
417
|
-
vol_exchange: { [input_array[0][2]]: input_array[0][1] },
|
|
418
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
419
|
-
pos: 0,
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
let vol = 0,
|
|
423
|
-
price = 0,
|
|
424
|
-
sum = 0,
|
|
425
|
-
pos = 0,
|
|
426
|
-
vol_exchange = {},
|
|
427
|
-
price_exchange = {},
|
|
428
|
-
sum_exchange = {},
|
|
429
|
-
pos_exchange = {}
|
|
430
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
431
|
-
if (vol < vol_target) {
|
|
432
|
-
let exchange = input_array[i][2]
|
|
433
|
-
vol = vol + input_array[i][1]
|
|
434
|
-
vol_exchange[exchange] = vol_exchange[exchange] === undefined ? input_array[i][1] : vol_exchange[exchange] + input_array[i][1]
|
|
435
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
436
|
-
sum_exchange[exchange] = sum_exchange[exchange] === undefined ? input_array[i][0] * input_array[i][1] : sum_exchange[exchange] + input_array[i][0] * input_array[i][1]
|
|
437
|
-
price = input_array[i][0]
|
|
438
|
-
price_exchange[exchange] = price
|
|
439
|
-
pos = i
|
|
440
|
-
pos_exchange[exchange] = pos_exchange[exchange] === undefined ? 0 : pos_exchange[exchange] + 1
|
|
441
|
-
} else if (vol >= vol_target) {
|
|
442
|
-
sum_exchange[input_array[i - 1][2]] -= input_array[i - 1][0] * (vol - vol_target)
|
|
443
|
-
sum -= input_array[i - 1][0] * (vol - vol_target)
|
|
444
|
-
vol_exchange[input_array[i - 1][2]] -= vol - vol_target
|
|
445
|
-
vol = vol_target
|
|
446
|
-
break
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
let avg_price = sum / vol ? sum / vol : price
|
|
450
|
-
return { price, price_exchange, avg_price, vol, vol_exchange, sum, sum_exchange, pos, pos_exchange }
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
// console.log(vol_position_combined([ [ 1, 1, 'binance' ],
|
|
454
|
-
// [ 1, 1, 'poloniex' ],
|
|
455
|
-
// [ 2, 2, 'binance' ],
|
|
456
|
-
// [ 3, 3, 'binance' ],
|
|
457
|
-
// [ 4, 4, 'bittrex' ],
|
|
458
|
-
// [ 5, 5, 'bittrex' ],
|
|
459
|
-
// [ 6, 6, 'poloniex' ],
|
|
460
|
-
// [ 7, 7, 'poloniex' ],
|
|
461
|
-
// [ 8, 8, 'bittrex' ] ], 5))
|
|
462
|
-
|
|
463
|
-
function sort_dict(dict, mode) {
|
|
464
|
-
var items = Object.keys(dict).map(function (key) {
|
|
465
|
-
return [key, dict[key]]
|
|
466
|
-
})
|
|
467
|
-
items.sort(function (first, second) {
|
|
468
|
-
if (mode === 0) {
|
|
469
|
-
return second[1] - first[1]
|
|
470
|
-
} else {
|
|
471
|
-
return first[1] - second[1]
|
|
472
|
-
}
|
|
473
|
-
})
|
|
474
|
-
return items
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function merge_trades_with_same_time(array) {
|
|
478
|
-
if (array.length === 1) {
|
|
479
|
-
return array
|
|
480
|
-
} else {
|
|
481
|
-
let trades = []
|
|
482
|
-
let processed_index = []
|
|
483
|
-
for (let i = 0; i < array.length; i++) {
|
|
484
|
-
if (processed_index.indexOf(i) === -1) {
|
|
485
|
-
let trade = array[i]
|
|
486
|
-
processed_index.push(i)
|
|
487
|
-
for (let j = i + 1; j < array.length; j++) {
|
|
488
|
-
if (processed_index.indexOf(j) === -1) {
|
|
489
|
-
let to_compare_trade = array[j]
|
|
490
|
-
if (to_compare_trade.close_time.getTime() === trade.close_time.getTime() && to_compare_trade.market === trade.market && to_compare_trade.type === trade.type) {
|
|
491
|
-
trade.amount = trade.amount + to_compare_trade.amount
|
|
492
|
-
trade.total = trade.total + to_compare_trade.total
|
|
493
|
-
processed_index.push(j)
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
trade.amount = parseFloat(trade.amount)
|
|
498
|
-
trade.total = parseFloat(trade.total)
|
|
499
|
-
trade.price = trade.total / trade.amount
|
|
500
|
-
trades.push(trade)
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
return trades
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
170
|
// Groups fills by order_id into one row per order (name is historical): amounts/totals
|
|
508
171
|
// summed, fees merged, open_time earliest, close_time latest, per-fill detail kept in
|
|
509
172
|
// fill_history. O(n) single pass; does not mutate the input rows.
|
|
@@ -603,13 +266,6 @@ function merge_trades_with_same_id(array) {
|
|
|
603
266
|
}
|
|
604
267
|
}
|
|
605
268
|
|
|
606
|
-
function similar(a, b, scale) {
|
|
607
|
-
if (Math.abs(a - b) / a < scale) {
|
|
608
|
-
return true
|
|
609
|
-
}
|
|
610
|
-
return false
|
|
611
|
-
}
|
|
612
|
-
|
|
613
269
|
const quote_currencies = ['BTC', 'ETH', 'BNB', 'USD', 'USDC', 'USDT', 'HKD', 'KRW', 'THB']
|
|
614
270
|
function parse_pair(pair) {
|
|
615
271
|
pair = pair.toUpperCase()
|
|
@@ -628,18 +284,6 @@ function parse_pair(pair) {
|
|
|
628
284
|
return result
|
|
629
285
|
}
|
|
630
286
|
|
|
631
|
-
function reverse_pair(pair) {
|
|
632
|
-
let [cur, quote_cur] = parse_pair(pair)
|
|
633
|
-
return quote_cur + cur
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function cur_dict_to_pair_list(cur_dict) {
|
|
637
|
-
return [].concat.apply(
|
|
638
|
-
[],
|
|
639
|
-
Object.entries(cur_dict).map((e) => e[1].map((j) => j + e[0])),
|
|
640
|
-
)
|
|
641
|
-
}
|
|
642
|
-
|
|
643
287
|
function number_with_commas(x) {
|
|
644
288
|
let parts = x.toString().split('.')
|
|
645
289
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
@@ -682,197 +326,57 @@ function is_exchange_account_key(exchange) {
|
|
|
682
326
|
return EXCHANGE_ACCOUNT_KEY_RE.test(normalize_exchange_key(exchange))
|
|
683
327
|
}
|
|
684
328
|
|
|
685
|
-
|
|
686
|
-
const key = normalize_exchange_key(exchange_account_key)
|
|
687
|
-
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
688
|
-
if (!m) {
|
|
689
|
-
throw new Error(`Invalid exchange account key "${exchange_account_key}" (expected "base_index", e.g. "mexc_1")`)
|
|
690
|
-
}
|
|
691
|
-
const base = m[1]
|
|
692
|
-
const index = Number(m[2])
|
|
693
|
-
if (!Number.isInteger(index) || index <= 0) {
|
|
694
|
-
throw new Error(`Invalid exchange account index in "${exchange_account_key}"`)
|
|
695
|
-
}
|
|
696
|
-
return { base, index }
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
function format_exchange_account_key(base, index) {
|
|
700
|
-
const b = normalize_exchange_key(base)
|
|
701
|
-
if (!b) throw new Error('format_exchange_account_key: base is required')
|
|
702
|
-
if (!Number.isInteger(index) || index <= 0) {
|
|
703
|
-
throw new Error(`format_exchange_account_key: index must be a positive integer (got ${index})`)
|
|
704
|
-
}
|
|
705
|
-
return `${b}_${index}`
|
|
706
|
-
}
|
|
707
|
-
|
|
329
|
+
// "mexc_1" -> "mexc"; legacy keys are cut at the first underscore
|
|
708
330
|
function exchange_base_from_key(exchange_key) {
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
return
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
const base = exchange_base_from_key(key)
|
|
723
|
-
if (!grouped[base]) grouped[base] = []
|
|
724
|
-
grouped[base].push(key)
|
|
725
|
-
})
|
|
726
|
-
// Stable deterministic ordering.
|
|
727
|
-
for (const base in grouped) {
|
|
728
|
-
grouped[base] = _.uniq(grouped[base]).sort()
|
|
729
|
-
}
|
|
730
|
-
return grouped
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
function pick_primary_key_for_base(keys_for_base, exchange_roles) {
|
|
734
|
-
const keys = (keys_for_base || []).map(normalize_exchange_key).filter(Boolean)
|
|
735
|
-
if (keys.length === 0) return null
|
|
736
|
-
|
|
737
|
-
if (exchange_roles && typeof exchange_roles === 'object') {
|
|
738
|
-
const primaries = keys.filter((k) => exchange_roles[normalize_exchange_key(k)] === 'primary')
|
|
739
|
-
if (primaries.length === 1) return primaries[0]
|
|
740
|
-
if (primaries.length > 1) return primaries.sort()[0]
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// Legacy heuristic: base-only key (no underscore) is primary if present.
|
|
744
|
-
const base_only = keys.find((k) => k.indexOf('_') === -1)
|
|
745
|
-
if (base_only) return base_only
|
|
746
|
-
|
|
747
|
-
// Otherwise, prefer lowest account index if possible.
|
|
748
|
-
const parsed = []
|
|
749
|
-
for (const k of keys) {
|
|
750
|
-
if (is_exchange_account_key(k)) {
|
|
751
|
-
const { index } = parse_exchange_account_key(k)
|
|
752
|
-
parsed.push({ k, index })
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
if (parsed.length > 0) {
|
|
756
|
-
return _.sortBy(parsed, ['index', 'k'])[0].k
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
// Fallback: stable first.
|
|
760
|
-
return keys.sort()[0]
|
|
331
|
+
return normalize_exchange_key(exchange_key).split('_')[0]
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// keys: normalized, unique and sorted, all of one base
|
|
335
|
+
function pick_primary_key_for_base(keys, exchange_roles) {
|
|
336
|
+
return (
|
|
337
|
+
keys.find((k) => exchange_roles?.[k] === 'primary') ||
|
|
338
|
+
// Legacy heuristic: base-only key (no underscore) is primary if present.
|
|
339
|
+
keys.find((k) => !k.includes('_')) ||
|
|
340
|
+
// Otherwise, prefer lowest account index.
|
|
341
|
+
_.minBy(keys.filter(is_exchange_account_key), (k) => Number(k.match(EXCHANGE_ACCOUNT_KEY_RE)[2])) ||
|
|
342
|
+
keys[0]
|
|
343
|
+
)
|
|
761
344
|
}
|
|
762
345
|
|
|
763
|
-
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur,
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
for (
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
for (let pair of pairs_for_quote_cur) {
|
|
778
|
-
const [cur] = parse_pair(pair)
|
|
779
|
-
// Add pair to cur (base currency)
|
|
780
|
-
if (!exchange_cur_pairs[exchange][cur]) {
|
|
781
|
-
exchange_cur_pairs[exchange][cur] = []
|
|
782
|
-
}
|
|
783
|
-
exchange_cur_pairs[exchange][cur].push(pair)
|
|
784
|
-
// Add pair to quote_cur
|
|
785
|
-
if (!exchange_cur_pairs[exchange][quote_cur]) {
|
|
786
|
-
exchange_cur_pairs[exchange][quote_cur] = []
|
|
787
|
-
}
|
|
788
|
-
exchange_cur_pairs[exchange][quote_cur].push(pair)
|
|
346
|
+
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur, { exchange_roles } = {}) {
|
|
347
|
+
const exchange_pairs = {},
|
|
348
|
+
exchange_cur_pairs = {},
|
|
349
|
+
pair_exchanges = {}
|
|
350
|
+
for (const exchange in exchange_cur_quote_cur) {
|
|
351
|
+
exchange_pairs[exchange] = []
|
|
352
|
+
exchange_cur_pairs[exchange] = {}
|
|
353
|
+
for (const quote_cur in exchange_cur_quote_cur[exchange]) {
|
|
354
|
+
for (const cur of exchange_cur_quote_cur[exchange][quote_cur] || []) {
|
|
355
|
+
const pair = cur + quote_cur
|
|
356
|
+
exchange_pairs[exchange].push(pair)
|
|
357
|
+
// Map each currency (base and quote) to the pairs containing it
|
|
358
|
+
for (const c of [cur, quote_cur]) (exchange_cur_pairs[exchange][c] ||= []).push(pair)
|
|
359
|
+
;(pair_exchanges[pair] ||= []).push(exchange)
|
|
789
360
|
}
|
|
790
361
|
}
|
|
791
362
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
363
|
+
// One primary account per base exchange; the base's other accounts are its affiliates
|
|
364
|
+
const exchange_affiliates = {},
|
|
365
|
+
exchange_pairs_without_affiliates = {},
|
|
366
|
+
pair_exchanges_without_affiliates = {}
|
|
367
|
+
const keys_by_base = _.groupBy(_.uniq(Object.keys(exchange_pairs).map(normalize_exchange_key).filter(Boolean)), exchange_base_from_key)
|
|
368
|
+
for (const keys of Object.values(keys_by_base)) {
|
|
369
|
+
const primary = pick_primary_key_for_base(keys.sort(), exchange_roles)
|
|
370
|
+
exchange_affiliates[primary] = keys.filter((k) => k !== primary)
|
|
371
|
+
if (exchange_pairs[primary]) exchange_pairs_without_affiliates[primary] = exchange_pairs[primary]
|
|
800
372
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
// Canonical behavior: use exchange_roles to pick exactly 1 primary per base.
|
|
805
|
-
const exchange_roles = opts && opts.exchange_roles ? opts.exchange_roles : null
|
|
806
|
-
|
|
807
|
-
const exchange_keys = Object.keys(exchange_pairs)
|
|
808
|
-
const by_base = group_exchange_keys_by_base(exchange_keys)
|
|
809
|
-
|
|
810
|
-
const exchange_primary_by_base = {}
|
|
811
|
-
const primary_exchange_keys = []
|
|
812
|
-
for (const base in by_base) {
|
|
813
|
-
const primary = pick_primary_key_for_base(by_base[base], exchange_roles)
|
|
814
|
-
if (primary) {
|
|
815
|
-
exchange_primary_by_base[base] = primary
|
|
816
|
-
primary_exchange_keys.push(primary)
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
const primary_exchange_key_set = new Set(primary_exchange_keys)
|
|
820
|
-
|
|
821
|
-
let exchange_pairs_without_affiliates = {}
|
|
822
|
-
let exchange_curs_without_affiliates = {}
|
|
823
|
-
for (const exchange of primary_exchange_keys) {
|
|
824
|
-
if (exchange_pairs[exchange]) exchange_pairs_without_affiliates[exchange] = exchange_pairs[exchange]
|
|
825
|
-
if (exchange_curs[exchange]) exchange_curs_without_affiliates[exchange] = exchange_curs[exchange]
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
let pair_exchanges_without_affiliates = {}
|
|
829
|
-
for (let pair in pair_exchanges) {
|
|
830
|
-
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) => primary_exchange_key_set.has(exchange))
|
|
831
|
-
if (pair_exchanges_without_affiliates[pair].length === 0) {
|
|
832
|
-
delete pair_exchanges_without_affiliates[pair]
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
// Map primary exchange key -> affiliate exchange keys (same base, excluding primary).
|
|
837
|
-
const exchange_affiliates = {}
|
|
838
|
-
for (const base in by_base) {
|
|
839
|
-
const primary = exchange_primary_by_base[base]
|
|
840
|
-
if (!primary) continue
|
|
841
|
-
exchange_affiliates[primary] = by_base[base].filter((k) => k !== primary)
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
// Add exchange_pairs_without_2
|
|
845
|
-
return {
|
|
846
|
-
exchange_cur_quote_cur,
|
|
847
|
-
exchange_pairs,
|
|
848
|
-
exchange_pairs_without_affiliates,
|
|
849
|
-
exchange_curs,
|
|
850
|
-
exchange_curs_without_affiliates,
|
|
851
|
-
exchange_cur_pairs,
|
|
852
|
-
pair_exchanges,
|
|
853
|
-
pair_exchanges_without_affiliates,
|
|
854
|
-
exchange_affiliates,
|
|
855
|
-
exchange_primary_by_base,
|
|
373
|
+
for (const pair in pair_exchanges) {
|
|
374
|
+
const primaries = pair_exchanges[pair].filter((exchange) => Object.hasOwn(exchange_affiliates, exchange))
|
|
375
|
+
if (primaries.length > 0) pair_exchanges_without_affiliates[pair] = primaries
|
|
856
376
|
}
|
|
377
|
+
return { exchange_pairs, exchange_pairs_without_affiliates, exchange_cur_pairs, pair_exchanges, pair_exchanges_without_affiliates, exchange_affiliates }
|
|
857
378
|
}
|
|
858
379
|
|
|
859
|
-
// console.log(
|
|
860
|
-
// exchange_cur_quote_cur_converter({
|
|
861
|
-
// bitget: {
|
|
862
|
-
// USDT: ['MAK'],
|
|
863
|
-
// },
|
|
864
|
-
// bybit: {
|
|
865
|
-
// USDT: ['MAK'],
|
|
866
|
-
// },
|
|
867
|
-
// bybit_2: {
|
|
868
|
-
// USDT: ['MAK'],
|
|
869
|
-
// },
|
|
870
|
-
// bybit_3: {
|
|
871
|
-
// USDT: ['MAK'],
|
|
872
|
-
// },
|
|
873
|
-
// })
|
|
874
|
-
// )
|
|
875
|
-
|
|
876
380
|
function sort_keys(dict) {
|
|
877
381
|
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
878
382
|
}
|
|
@@ -898,33 +402,20 @@ function convert_deposit_withdrawal_chain(chain) {
|
|
|
898
402
|
|
|
899
403
|
module.exports = {
|
|
900
404
|
ordered_dict,
|
|
901
|
-
random_id,
|
|
902
|
-
get_hhmmss,
|
|
903
405
|
round,
|
|
904
|
-
cut_orderbook,
|
|
905
406
|
level_position,
|
|
906
407
|
price_position,
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
408
|
+
orderbook_mid,
|
|
409
|
+
orderbook_spread,
|
|
410
|
+
depth_within_pct,
|
|
411
|
+
max_level_gap,
|
|
911
412
|
combine_orderbooks,
|
|
912
|
-
vol_position_combined,
|
|
913
|
-
sort_dict,
|
|
914
|
-
merge_trades_with_same_time,
|
|
915
413
|
merge_trades_with_same_id,
|
|
916
|
-
similar,
|
|
917
414
|
parse_pair,
|
|
918
|
-
reverse_pair,
|
|
919
|
-
cur_dict_to_pair_list,
|
|
920
415
|
number_with_commas,
|
|
921
416
|
format_amount,
|
|
922
|
-
normalize_exchange_key,
|
|
923
417
|
is_exchange_account_key,
|
|
924
|
-
parse_exchange_account_key,
|
|
925
|
-
format_exchange_account_key,
|
|
926
418
|
exchange_base_from_key,
|
|
927
|
-
group_exchange_keys_by_base,
|
|
928
419
|
exchange_cur_quote_cur_converter,
|
|
929
420
|
sort_keys,
|
|
930
421
|
convert_deposit_withdrawal_chain,
|