@icgio/icg-utils 1.9.74 → 1.9.76
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 +6 -17
- package/lib/rates.js +66 -197
- package/lib/utils.js +58 -604
- 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,24 +62,15 @@ 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
|
}
|
|
@@ -196,146 +108,6 @@ function max_level_gap(levels, n) {
|
|
|
196
108
|
return gap
|
|
197
109
|
}
|
|
198
110
|
|
|
199
|
-
function sum_position(input_array, sum_target) {
|
|
200
|
-
if (input_array.length === 1) {
|
|
201
|
-
return {
|
|
202
|
-
price: input_array[0][0],
|
|
203
|
-
avg_price: input_array[0][0],
|
|
204
|
-
vol: input_array[0][1],
|
|
205
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
206
|
-
sum_original: input_array[0][0] * input_array[0][1],
|
|
207
|
-
pos: 0,
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
let vol = 0,
|
|
211
|
-
price = 0,
|
|
212
|
-
sum = 0,
|
|
213
|
-
pos = 0,
|
|
214
|
-
sum_original = 0
|
|
215
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
216
|
-
if (sum < sum_target) {
|
|
217
|
-
vol = vol + input_array[i][1]
|
|
218
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
219
|
-
price = input_array[i][0]
|
|
220
|
-
pos = i
|
|
221
|
-
sum_original = sum
|
|
222
|
-
} else if (sum >= sum_target) {
|
|
223
|
-
break
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, sum_original, pos }
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function vol_position(input_array, vol_target) {
|
|
230
|
-
if (input_array.length === 1) {
|
|
231
|
-
return {
|
|
232
|
-
price: input_array[0][0],
|
|
233
|
-
avg_price: input_array[0][0],
|
|
234
|
-
vol: input_array[0][1],
|
|
235
|
-
vol_original: input_array[0][1],
|
|
236
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
237
|
-
pos: 0,
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
let vol = 0,
|
|
241
|
-
price = 0,
|
|
242
|
-
sum = 0,
|
|
243
|
-
pos = 0,
|
|
244
|
-
vol_original = 0
|
|
245
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
246
|
-
if (vol < vol_target) {
|
|
247
|
-
vol = vol + input_array[i][1]
|
|
248
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
249
|
-
price = input_array[i][0]
|
|
250
|
-
pos = i
|
|
251
|
-
vol_original = vol
|
|
252
|
-
} else if (vol >= vol_target) {
|
|
253
|
-
sum = sum - input_array[i - 1][0] * (vol - vol_target)
|
|
254
|
-
vol = vol_target
|
|
255
|
-
break
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return { price, avg_price: sum / vol ? sum / vol : price, vol, vol_original, sum, pos }
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function sum_position_orderbook(input_array, sum_target) {
|
|
262
|
-
if (sum_target === 0) {
|
|
263
|
-
return []
|
|
264
|
-
}
|
|
265
|
-
let { sum_original, pos } = sum_position(input_array, sum_target)
|
|
266
|
-
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
267
|
-
result[pos][1] += (sum_target - sum_original) / result[pos][0]
|
|
268
|
-
return result
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function vol_position_orderbook(input_array, vol_target) {
|
|
272
|
-
if (vol_target === 0) {
|
|
273
|
-
return []
|
|
274
|
-
}
|
|
275
|
-
let { vol_original, pos } = vol_position(input_array, vol_target)
|
|
276
|
-
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
277
|
-
result[pos][1] += vol_target - vol_original
|
|
278
|
-
return result
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// console.log(
|
|
282
|
-
// sum_position_orderbook(
|
|
283
|
-
// [
|
|
284
|
-
// [2, 1],
|
|
285
|
-
// [6, 6],
|
|
286
|
-
// [7, 7],
|
|
287
|
-
// ],
|
|
288
|
-
// 0
|
|
289
|
-
// )
|
|
290
|
-
// )
|
|
291
|
-
// console.log(
|
|
292
|
-
// vol_position_orderbook(
|
|
293
|
-
// [
|
|
294
|
-
// [0.00004637, 14.12],
|
|
295
|
-
// [0.00004636, 19.37],
|
|
296
|
-
// [0.0000463, 77.76],
|
|
297
|
-
// [0.00004625, 17.93],
|
|
298
|
-
// [0.00004623, 14.89],
|
|
299
|
-
// [0.00004621, 34.25],
|
|
300
|
-
// [0.0000462, 131.77],
|
|
301
|
-
// [0.00004619, 24.79],
|
|
302
|
-
// [0.00004618, 36.71],
|
|
303
|
-
// [0.00004617, 18.36],
|
|
304
|
-
// [0.00004616, 9],
|
|
305
|
-
// [0.00004615, 67.22],
|
|
306
|
-
// [0.00004614, 74.74],
|
|
307
|
-
// [0.00004613, 31.89],
|
|
308
|
-
// [0.00004608, 31.25],
|
|
309
|
-
// [0.00004606, 72],
|
|
310
|
-
// [0.00004605, 11.69],
|
|
311
|
-
// [0.00004604, 36.68],
|
|
312
|
-
// [0.00004601, 29.77],
|
|
313
|
-
// [0.00004598, 11.17],
|
|
314
|
-
// [0.00004596, 25.19],
|
|
315
|
-
// [0.00004593, 9.01],
|
|
316
|
-
// [0.00004592, 15.56],
|
|
317
|
-
// [0.00004589, 15.21],
|
|
318
|
-
// [0.00004588, 21.25],
|
|
319
|
-
// [0.00004583, 25.19],
|
|
320
|
-
// [0.00004582, 81.02],
|
|
321
|
-
// [0.00004581, 24.55],
|
|
322
|
-
// [0.0000458, 0.26],
|
|
323
|
-
// [0.00004577, 71.32],
|
|
324
|
-
// [0.00004576, 43.39],
|
|
325
|
-
// [0.00004574, 41.3],
|
|
326
|
-
// [0.00004573, 0.75],
|
|
327
|
-
// [0.0000457, 10.84],
|
|
328
|
-
// [0.00004567, 0.26],
|
|
329
|
-
// [0.00004549, 239.91],
|
|
330
|
-
// [0.00004548, 115.89],
|
|
331
|
-
// [0.00004542, 192.91],
|
|
332
|
-
// [0.00004512, 0.64],
|
|
333
|
-
// [0.000045, 3.24],
|
|
334
|
-
// ],
|
|
335
|
-
// 1
|
|
336
|
-
// )
|
|
337
|
-
// )
|
|
338
|
-
|
|
339
111
|
function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
340
112
|
const exchanges = Object.keys(orderbooks_dict)
|
|
341
113
|
const numExchanges = exchanges.length
|
|
@@ -395,148 +167,6 @@ function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
|
395
167
|
}
|
|
396
168
|
}
|
|
397
169
|
|
|
398
|
-
// console.log(
|
|
399
|
-
// combine_orderbooks(
|
|
400
|
-
// {
|
|
401
|
-
// binance: {
|
|
402
|
-
// asks: [
|
|
403
|
-
// [1, 1],
|
|
404
|
-
// [2, 2],
|
|
405
|
-
// [3, 3],
|
|
406
|
-
// ],
|
|
407
|
-
// bids: [
|
|
408
|
-
// // [0.9, 0.9],
|
|
409
|
-
// // [0.5, 0.5],
|
|
410
|
-
// // [0.3, 0.2],
|
|
411
|
-
// ],
|
|
412
|
-
// },
|
|
413
|
-
// bittrex: {
|
|
414
|
-
// // asks: [
|
|
415
|
-
// // // [4, 4],
|
|
416
|
-
// // // [5, 5],
|
|
417
|
-
// // // [8, 8],
|
|
418
|
-
// // ],
|
|
419
|
-
// bids: [
|
|
420
|
-
// [0.6, 0.9],
|
|
421
|
-
// [0.5, 0.2],
|
|
422
|
-
// [0.2, 0.2],
|
|
423
|
-
// ],
|
|
424
|
-
// },
|
|
425
|
-
// poloniex: {
|
|
426
|
-
// asks: [
|
|
427
|
-
// [1, 1],
|
|
428
|
-
// [6, 6],
|
|
429
|
-
// [7, 7],
|
|
430
|
-
// ],
|
|
431
|
-
// bids: [
|
|
432
|
-
// [0.95, 0.9],
|
|
433
|
-
// [0.7, 0.5],
|
|
434
|
-
// [0.4, 0.2],
|
|
435
|
-
// ],
|
|
436
|
-
// },
|
|
437
|
-
// },
|
|
438
|
-
// 100
|
|
439
|
-
// )
|
|
440
|
-
// )
|
|
441
|
-
// console.log(combine_orderbooks({}, 100))
|
|
442
|
-
|
|
443
|
-
function vol_position_combined(input_array, vol_target) {
|
|
444
|
-
if (input_array.length === 1) {
|
|
445
|
-
return {
|
|
446
|
-
price: input_array[0][0],
|
|
447
|
-
price_exchange: { [input_array[0][2]]: input_array[0][0] },
|
|
448
|
-
avg_price: input_array[0][0],
|
|
449
|
-
vol: input_array[0][1],
|
|
450
|
-
vol_exchange: { [input_array[0][2]]: input_array[0][1] },
|
|
451
|
-
sum: input_array[0][0] * input_array[0][1],
|
|
452
|
-
pos: 0,
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
let vol = 0,
|
|
456
|
-
price = 0,
|
|
457
|
-
sum = 0,
|
|
458
|
-
pos = 0,
|
|
459
|
-
vol_exchange = {},
|
|
460
|
-
price_exchange = {},
|
|
461
|
-
sum_exchange = {},
|
|
462
|
-
pos_exchange = {}
|
|
463
|
-
for (let i = 0; i < input_array.length; i++) {
|
|
464
|
-
if (vol < vol_target) {
|
|
465
|
-
let exchange = input_array[i][2]
|
|
466
|
-
vol = vol + input_array[i][1]
|
|
467
|
-
vol_exchange[exchange] = vol_exchange[exchange] === undefined ? input_array[i][1] : vol_exchange[exchange] + input_array[i][1]
|
|
468
|
-
sum = sum + input_array[i][0] * input_array[i][1]
|
|
469
|
-
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]
|
|
470
|
-
price = input_array[i][0]
|
|
471
|
-
price_exchange[exchange] = price
|
|
472
|
-
pos = i
|
|
473
|
-
pos_exchange[exchange] = pos_exchange[exchange] === undefined ? 0 : pos_exchange[exchange] + 1
|
|
474
|
-
} else if (vol >= vol_target) {
|
|
475
|
-
sum_exchange[input_array[i - 1][2]] -= input_array[i - 1][0] * (vol - vol_target)
|
|
476
|
-
sum -= input_array[i - 1][0] * (vol - vol_target)
|
|
477
|
-
vol_exchange[input_array[i - 1][2]] -= vol - vol_target
|
|
478
|
-
vol = vol_target
|
|
479
|
-
break
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
let avg_price = sum / vol ? sum / vol : price
|
|
483
|
-
return { price, price_exchange, avg_price, vol, vol_exchange, sum, sum_exchange, pos, pos_exchange }
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
// console.log(vol_position_combined([ [ 1, 1, 'binance' ],
|
|
487
|
-
// [ 1, 1, 'poloniex' ],
|
|
488
|
-
// [ 2, 2, 'binance' ],
|
|
489
|
-
// [ 3, 3, 'binance' ],
|
|
490
|
-
// [ 4, 4, 'bittrex' ],
|
|
491
|
-
// [ 5, 5, 'bittrex' ],
|
|
492
|
-
// [ 6, 6, 'poloniex' ],
|
|
493
|
-
// [ 7, 7, 'poloniex' ],
|
|
494
|
-
// [ 8, 8, 'bittrex' ] ], 5))
|
|
495
|
-
|
|
496
|
-
function sort_dict(dict, mode) {
|
|
497
|
-
var items = Object.keys(dict).map(function (key) {
|
|
498
|
-
return [key, dict[key]]
|
|
499
|
-
})
|
|
500
|
-
items.sort(function (first, second) {
|
|
501
|
-
if (mode === 0) {
|
|
502
|
-
return second[1] - first[1]
|
|
503
|
-
} else {
|
|
504
|
-
return first[1] - second[1]
|
|
505
|
-
}
|
|
506
|
-
})
|
|
507
|
-
return items
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function merge_trades_with_same_time(array) {
|
|
511
|
-
if (array.length === 1) {
|
|
512
|
-
return array
|
|
513
|
-
} else {
|
|
514
|
-
let trades = []
|
|
515
|
-
let processed_index = []
|
|
516
|
-
for (let i = 0; i < array.length; i++) {
|
|
517
|
-
if (processed_index.indexOf(i) === -1) {
|
|
518
|
-
let trade = array[i]
|
|
519
|
-
processed_index.push(i)
|
|
520
|
-
for (let j = i + 1; j < array.length; j++) {
|
|
521
|
-
if (processed_index.indexOf(j) === -1) {
|
|
522
|
-
let to_compare_trade = array[j]
|
|
523
|
-
if (to_compare_trade.close_time.getTime() === trade.close_time.getTime() && to_compare_trade.market === trade.market && to_compare_trade.type === trade.type) {
|
|
524
|
-
trade.amount = trade.amount + to_compare_trade.amount
|
|
525
|
-
trade.total = trade.total + to_compare_trade.total
|
|
526
|
-
processed_index.push(j)
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
trade.amount = parseFloat(trade.amount)
|
|
531
|
-
trade.total = parseFloat(trade.total)
|
|
532
|
-
trade.price = trade.total / trade.amount
|
|
533
|
-
trades.push(trade)
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
return trades
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
170
|
// Groups fills by order_id into one row per order (name is historical): amounts/totals
|
|
541
171
|
// summed, fees merged, open_time earliest, close_time latest, per-fill detail kept in
|
|
542
172
|
// fill_history. O(n) single pass; does not mutate the input rows.
|
|
@@ -636,13 +266,6 @@ function merge_trades_with_same_id(array) {
|
|
|
636
266
|
}
|
|
637
267
|
}
|
|
638
268
|
|
|
639
|
-
function similar(a, b, scale) {
|
|
640
|
-
if (Math.abs(a - b) / a < scale) {
|
|
641
|
-
return true
|
|
642
|
-
}
|
|
643
|
-
return false
|
|
644
|
-
}
|
|
645
|
-
|
|
646
269
|
const quote_currencies = ['BTC', 'ETH', 'BNB', 'USD', 'USDC', 'USDT', 'HKD', 'KRW', 'THB']
|
|
647
270
|
function parse_pair(pair) {
|
|
648
271
|
pair = pair.toUpperCase()
|
|
@@ -661,18 +284,6 @@ function parse_pair(pair) {
|
|
|
661
284
|
return result
|
|
662
285
|
}
|
|
663
286
|
|
|
664
|
-
function reverse_pair(pair) {
|
|
665
|
-
let [cur, quote_cur] = parse_pair(pair)
|
|
666
|
-
return quote_cur + cur
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
function cur_dict_to_pair_list(cur_dict) {
|
|
670
|
-
return [].concat.apply(
|
|
671
|
-
[],
|
|
672
|
-
Object.entries(cur_dict).map((e) => e[1].map((j) => j + e[0])),
|
|
673
|
-
)
|
|
674
|
-
}
|
|
675
|
-
|
|
676
287
|
function number_with_commas(x) {
|
|
677
288
|
let parts = x.toString().split('.')
|
|
678
289
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
@@ -715,197 +326,57 @@ function is_exchange_account_key(exchange) {
|
|
|
715
326
|
return EXCHANGE_ACCOUNT_KEY_RE.test(normalize_exchange_key(exchange))
|
|
716
327
|
}
|
|
717
328
|
|
|
718
|
-
|
|
719
|
-
const key = normalize_exchange_key(exchange_account_key)
|
|
720
|
-
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
721
|
-
if (!m) {
|
|
722
|
-
throw new Error(`Invalid exchange account key "${exchange_account_key}" (expected "base_index", e.g. "mexc_1")`)
|
|
723
|
-
}
|
|
724
|
-
const base = m[1]
|
|
725
|
-
const index = Number(m[2])
|
|
726
|
-
if (!Number.isInteger(index) || index <= 0) {
|
|
727
|
-
throw new Error(`Invalid exchange account index in "${exchange_account_key}"`)
|
|
728
|
-
}
|
|
729
|
-
return { base, index }
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
function format_exchange_account_key(base, index) {
|
|
733
|
-
const b = normalize_exchange_key(base)
|
|
734
|
-
if (!b) throw new Error('format_exchange_account_key: base is required')
|
|
735
|
-
if (!Number.isInteger(index) || index <= 0) {
|
|
736
|
-
throw new Error(`format_exchange_account_key: index must be a positive integer (got ${index})`)
|
|
737
|
-
}
|
|
738
|
-
return `${b}_${index}`
|
|
739
|
-
}
|
|
740
|
-
|
|
329
|
+
// "mexc_1" -> "mexc"; legacy keys are cut at the first underscore
|
|
741
330
|
function exchange_base_from_key(exchange_key) {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
return
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
const base = exchange_base_from_key(key)
|
|
756
|
-
if (!grouped[base]) grouped[base] = []
|
|
757
|
-
grouped[base].push(key)
|
|
758
|
-
})
|
|
759
|
-
// Stable deterministic ordering.
|
|
760
|
-
for (const base in grouped) {
|
|
761
|
-
grouped[base] = _.uniq(grouped[base]).sort()
|
|
762
|
-
}
|
|
763
|
-
return grouped
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
function pick_primary_key_for_base(keys_for_base, exchange_roles) {
|
|
767
|
-
const keys = (keys_for_base || []).map(normalize_exchange_key).filter(Boolean)
|
|
768
|
-
if (keys.length === 0) return null
|
|
769
|
-
|
|
770
|
-
if (exchange_roles && typeof exchange_roles === 'object') {
|
|
771
|
-
const primaries = keys.filter((k) => exchange_roles[normalize_exchange_key(k)] === 'primary')
|
|
772
|
-
if (primaries.length === 1) return primaries[0]
|
|
773
|
-
if (primaries.length > 1) return primaries.sort()[0]
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
// Legacy heuristic: base-only key (no underscore) is primary if present.
|
|
777
|
-
const base_only = keys.find((k) => k.indexOf('_') === -1)
|
|
778
|
-
if (base_only) return base_only
|
|
779
|
-
|
|
780
|
-
// Otherwise, prefer lowest account index if possible.
|
|
781
|
-
const parsed = []
|
|
782
|
-
for (const k of keys) {
|
|
783
|
-
if (is_exchange_account_key(k)) {
|
|
784
|
-
const { index } = parse_exchange_account_key(k)
|
|
785
|
-
parsed.push({ k, index })
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
if (parsed.length > 0) {
|
|
789
|
-
return _.sortBy(parsed, ['index', 'k'])[0].k
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
// Fallback: stable first.
|
|
793
|
-
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
|
+
)
|
|
794
344
|
}
|
|
795
345
|
|
|
796
|
-
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur,
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
for (
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
for (let pair of pairs_for_quote_cur) {
|
|
811
|
-
const [cur] = parse_pair(pair)
|
|
812
|
-
// Add pair to cur (base currency)
|
|
813
|
-
if (!exchange_cur_pairs[exchange][cur]) {
|
|
814
|
-
exchange_cur_pairs[exchange][cur] = []
|
|
815
|
-
}
|
|
816
|
-
exchange_cur_pairs[exchange][cur].push(pair)
|
|
817
|
-
// Add pair to quote_cur
|
|
818
|
-
if (!exchange_cur_pairs[exchange][quote_cur]) {
|
|
819
|
-
exchange_cur_pairs[exchange][quote_cur] = []
|
|
820
|
-
}
|
|
821
|
-
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)
|
|
822
360
|
}
|
|
823
361
|
}
|
|
824
362
|
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
// Primary-only (aka "without affiliates") sets.
|
|
835
|
-
//
|
|
836
|
-
// Legacy behavior: keys without "_" are treated as primary.
|
|
837
|
-
// Canonical behavior: use exchange_roles to pick exactly 1 primary per base.
|
|
838
|
-
const exchange_roles = opts && opts.exchange_roles ? opts.exchange_roles : null
|
|
839
|
-
|
|
840
|
-
const exchange_keys = Object.keys(exchange_pairs)
|
|
841
|
-
const by_base = group_exchange_keys_by_base(exchange_keys)
|
|
842
|
-
|
|
843
|
-
const exchange_primary_by_base = {}
|
|
844
|
-
const primary_exchange_keys = []
|
|
845
|
-
for (const base in by_base) {
|
|
846
|
-
const primary = pick_primary_key_for_base(by_base[base], exchange_roles)
|
|
847
|
-
if (primary) {
|
|
848
|
-
exchange_primary_by_base[base] = primary
|
|
849
|
-
primary_exchange_keys.push(primary)
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
const primary_exchange_key_set = new Set(primary_exchange_keys)
|
|
853
|
-
|
|
854
|
-
let exchange_pairs_without_affiliates = {}
|
|
855
|
-
let exchange_curs_without_affiliates = {}
|
|
856
|
-
for (const exchange of primary_exchange_keys) {
|
|
857
|
-
if (exchange_pairs[exchange]) exchange_pairs_without_affiliates[exchange] = exchange_pairs[exchange]
|
|
858
|
-
if (exchange_curs[exchange]) exchange_curs_without_affiliates[exchange] = exchange_curs[exchange]
|
|
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]
|
|
859
372
|
}
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) => primary_exchange_key_set.has(exchange))
|
|
864
|
-
if (pair_exchanges_without_affiliates[pair].length === 0) {
|
|
865
|
-
delete pair_exchanges_without_affiliates[pair]
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
// Map primary exchange key -> affiliate exchange keys (same base, excluding primary).
|
|
870
|
-
const exchange_affiliates = {}
|
|
871
|
-
for (const base in by_base) {
|
|
872
|
-
const primary = exchange_primary_by_base[base]
|
|
873
|
-
if (!primary) continue
|
|
874
|
-
exchange_affiliates[primary] = by_base[base].filter((k) => k !== primary)
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
// Add exchange_pairs_without_2
|
|
878
|
-
return {
|
|
879
|
-
exchange_cur_quote_cur,
|
|
880
|
-
exchange_pairs,
|
|
881
|
-
exchange_pairs_without_affiliates,
|
|
882
|
-
exchange_curs,
|
|
883
|
-
exchange_curs_without_affiliates,
|
|
884
|
-
exchange_cur_pairs,
|
|
885
|
-
pair_exchanges,
|
|
886
|
-
pair_exchanges_without_affiliates,
|
|
887
|
-
exchange_affiliates,
|
|
888
|
-
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
|
|
889
376
|
}
|
|
377
|
+
return { exchange_pairs, exchange_pairs_without_affiliates, exchange_cur_pairs, pair_exchanges, pair_exchanges_without_affiliates, exchange_affiliates }
|
|
890
378
|
}
|
|
891
379
|
|
|
892
|
-
// console.log(
|
|
893
|
-
// exchange_cur_quote_cur_converter({
|
|
894
|
-
// bitget: {
|
|
895
|
-
// USDT: ['MAK'],
|
|
896
|
-
// },
|
|
897
|
-
// bybit: {
|
|
898
|
-
// USDT: ['MAK'],
|
|
899
|
-
// },
|
|
900
|
-
// bybit_2: {
|
|
901
|
-
// USDT: ['MAK'],
|
|
902
|
-
// },
|
|
903
|
-
// bybit_3: {
|
|
904
|
-
// USDT: ['MAK'],
|
|
905
|
-
// },
|
|
906
|
-
// })
|
|
907
|
-
// )
|
|
908
|
-
|
|
909
380
|
function sort_keys(dict) {
|
|
910
381
|
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
911
382
|
}
|
|
@@ -931,37 +402,20 @@ function convert_deposit_withdrawal_chain(chain) {
|
|
|
931
402
|
|
|
932
403
|
module.exports = {
|
|
933
404
|
ordered_dict,
|
|
934
|
-
random_id,
|
|
935
|
-
get_hhmmss,
|
|
936
405
|
round,
|
|
937
|
-
cut_orderbook,
|
|
938
406
|
level_position,
|
|
939
407
|
price_position,
|
|
940
408
|
orderbook_mid,
|
|
941
409
|
orderbook_spread,
|
|
942
410
|
depth_within_pct,
|
|
943
411
|
max_level_gap,
|
|
944
|
-
sum_position,
|
|
945
|
-
vol_position,
|
|
946
|
-
sum_position_orderbook,
|
|
947
|
-
vol_position_orderbook,
|
|
948
412
|
combine_orderbooks,
|
|
949
|
-
vol_position_combined,
|
|
950
|
-
sort_dict,
|
|
951
|
-
merge_trades_with_same_time,
|
|
952
413
|
merge_trades_with_same_id,
|
|
953
|
-
similar,
|
|
954
414
|
parse_pair,
|
|
955
|
-
reverse_pair,
|
|
956
|
-
cur_dict_to_pair_list,
|
|
957
415
|
number_with_commas,
|
|
958
416
|
format_amount,
|
|
959
|
-
normalize_exchange_key,
|
|
960
417
|
is_exchange_account_key,
|
|
961
|
-
parse_exchange_account_key,
|
|
962
|
-
format_exchange_account_key,
|
|
963
418
|
exchange_base_from_key,
|
|
964
|
-
group_exchange_keys_by_base,
|
|
965
419
|
exchange_cur_quote_cur_converter,
|
|
966
420
|
sort_keys,
|
|
967
421
|
convert_deposit_withdrawal_chain,
|