@icgio/icg-utils 1.9.56 → 1.9.58
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/lib/utils.js +49 -7
- package/package.json +2 -2
- package/test/merge-trades.test.js +57 -0
package/lib/utils.js
CHANGED
|
@@ -505,7 +505,35 @@ function merge_trades_with_same_time(array) {
|
|
|
505
505
|
}
|
|
506
506
|
|
|
507
507
|
function merge_trades_with_same_id(array) {
|
|
508
|
+
let sum_numeric_fee_amount = (left, right) => {
|
|
509
|
+
const left_num = parseFloat(left || 0)
|
|
510
|
+
const right_num = parseFloat(right || 0)
|
|
511
|
+
return (Number.isFinite(left_num) ? left_num : 0) + (Number.isFinite(right_num) ? right_num : 0)
|
|
512
|
+
}
|
|
513
|
+
let get_fill_history = (trade) => {
|
|
514
|
+
if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
|
|
515
|
+
return trade.fill_history.map((fill) => Object.assign({}, fill))
|
|
516
|
+
}
|
|
517
|
+
let fill = {
|
|
518
|
+
price: parseFloat(trade.price),
|
|
519
|
+
amount: parseFloat(trade.amount),
|
|
520
|
+
total: parseFloat(trade.total),
|
|
521
|
+
}
|
|
522
|
+
let time = trade.close_time || trade.open_time
|
|
523
|
+
if (time) fill.time = time
|
|
524
|
+
if (trade.trade_id != null) fill.trade_id = String(trade.trade_id)
|
|
525
|
+
if (trade.fees && typeof trade.fees === 'object') fill.fees = Object.assign({}, trade.fees)
|
|
526
|
+
if (trade.role != null) fill.role = trade.role
|
|
527
|
+
return [fill]
|
|
528
|
+
}
|
|
529
|
+
let strip_trade_fill_id = (trade) => {
|
|
530
|
+
delete trade.trade_id
|
|
531
|
+
delete trade.id
|
|
532
|
+
}
|
|
533
|
+
|
|
508
534
|
if (array.length === 1) {
|
|
535
|
+
array[0].fill_history = get_fill_history(array[0])
|
|
536
|
+
strip_trade_fill_id(array[0])
|
|
509
537
|
return array
|
|
510
538
|
} else {
|
|
511
539
|
let trades = []
|
|
@@ -513,21 +541,29 @@ function merge_trades_with_same_id(array) {
|
|
|
513
541
|
for (let i = 0; i < array.length; i++) {
|
|
514
542
|
if (!processed_index_set.has(i)) {
|
|
515
543
|
let trade = array[i]
|
|
544
|
+
trade.fill_history = get_fill_history(trade)
|
|
516
545
|
processed_index_set.add(i)
|
|
517
546
|
for (let j = i + 1; j < array.length; j++) {
|
|
518
547
|
if (!processed_index_set.has(j)) {
|
|
519
548
|
let to_compare_trade = array[j]
|
|
520
|
-
if (to_compare_trade.order_id === trade.order_id) {
|
|
549
|
+
if (trade.order_id != null && to_compare_trade.order_id === trade.order_id) {
|
|
521
550
|
trade.amount = trade.amount + to_compare_trade.amount
|
|
522
551
|
trade.total = trade.total + to_compare_trade.total
|
|
523
|
-
|
|
524
|
-
if (
|
|
525
|
-
|
|
526
|
-
trade.fees
|
|
552
|
+
trade.fill_history = trade.fill_history.concat(get_fill_history(to_compare_trade))
|
|
553
|
+
if (to_compare_trade.fees && typeof to_compare_trade.fees === 'object') {
|
|
554
|
+
if (!trade.fees || typeof trade.fees !== 'object') {
|
|
555
|
+
trade.fees = Object.assign({}, to_compare_trade.fees)
|
|
556
|
+
} else {
|
|
557
|
+
for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
|
|
558
|
+
trade.fees[cur] = sum_numeric_fee_amount(trade.fees[cur], amount)
|
|
559
|
+
}
|
|
527
560
|
}
|
|
528
561
|
}
|
|
529
|
-
|
|
530
|
-
|
|
562
|
+
if (to_compare_trade.open_time && (!trade.open_time || to_compare_trade.open_time < trade.open_time)) {
|
|
563
|
+
trade.open_time = to_compare_trade.open_time
|
|
564
|
+
}
|
|
565
|
+
// Use latest close_time so grouped trades remain visible when later fills land near reconciliation cutoffs.
|
|
566
|
+
if (to_compare_trade.close_time && (!trade.close_time || to_compare_trade.close_time > trade.close_time)) {
|
|
531
567
|
trade.close_time = to_compare_trade.close_time
|
|
532
568
|
}
|
|
533
569
|
processed_index_set.add(j)
|
|
@@ -537,6 +573,12 @@ function merge_trades_with_same_id(array) {
|
|
|
537
573
|
trade.amount = parseFloat(trade.amount)
|
|
538
574
|
trade.total = parseFloat(trade.total)
|
|
539
575
|
trade.price = trade.total / trade.amount
|
|
576
|
+
trade.fill_history = trade.fill_history.sort((a, b) => {
|
|
577
|
+
let a_time = a.time ? new Date(a.time).getTime() : 0
|
|
578
|
+
let b_time = b.time ? new Date(b.time).getTime() : 0
|
|
579
|
+
return a_time - b_time
|
|
580
|
+
})
|
|
581
|
+
strip_trade_fill_id(trade)
|
|
540
582
|
trades.push(trade)
|
|
541
583
|
}
|
|
542
584
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.58",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js"
|
|
10
|
+
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js && node test/merge-trades.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { merge_trades_with_same_id } = require('../lib/utils.js')
|
|
3
|
+
|
|
4
|
+
let pass = 0, fail = 0
|
|
5
|
+
function assert(condition, msg) {
|
|
6
|
+
if (condition) { pass++ }
|
|
7
|
+
else { fail++; console.log(' FAIL:', msg) }
|
|
8
|
+
}
|
|
9
|
+
function eq(a, b, msg) {
|
|
10
|
+
const as = JSON.stringify(a), bs = JSON.stringify(b)
|
|
11
|
+
if (as === bs) { pass++ }
|
|
12
|
+
else { fail++; console.log(' FAIL:', msg, '— got', as, 'expected', bs) }
|
|
13
|
+
}
|
|
14
|
+
function approx(a, b, epsilon, msg) {
|
|
15
|
+
if (Math.abs(a - b) <= epsilon) { pass++ }
|
|
16
|
+
else { fail++; console.log(' FAIL:', msg, '— got', a, 'expected', b) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log('--- merge_trades_with_same_id ---')
|
|
20
|
+
|
|
21
|
+
const merged = merge_trades_with_same_id([
|
|
22
|
+
{
|
|
23
|
+
order_id: 'order-1',
|
|
24
|
+
trade_id: 'fill-1',
|
|
25
|
+
pair: 'SWFTCUSDT',
|
|
26
|
+
type: 'buy',
|
|
27
|
+
price: 0.0033818,
|
|
28
|
+
amount: 4430.6295,
|
|
29
|
+
total: 14.9835028431,
|
|
30
|
+
open_time: new Date('2026-04-12T11:57:17.154Z'),
|
|
31
|
+
close_time: new Date('2026-04-12T11:57:17.154Z'),
|
|
32
|
+
fees: { USDT: '0.0299670056862' },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
order_id: 'order-1',
|
|
36
|
+
trade_id: 'fill-2',
|
|
37
|
+
pair: 'SWFTCUSDT',
|
|
38
|
+
type: 'buy',
|
|
39
|
+
price: 0.0033818,
|
|
40
|
+
amount: 1955.1223,
|
|
41
|
+
total: 6.61163708291,
|
|
42
|
+
open_time: new Date('2026-04-12T11:57:17.537Z'),
|
|
43
|
+
close_time: new Date('2026-04-12T11:57:31.380Z'),
|
|
44
|
+
fees: { USDT: '0.01322327416382' },
|
|
45
|
+
},
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
eq(merged.length, 1, 'merges trades with same order_id')
|
|
49
|
+
approx(merged[0].amount, 6385.7518, 1e-10, 'sums amount numerically')
|
|
50
|
+
approx(merged[0].total, 21.59513992601, 1e-10, 'sums total numerically')
|
|
51
|
+
approx(merged[0].fees.USDT, 0.04319027985002, 1e-14, 'sums string fees numerically')
|
|
52
|
+
eq(merged[0].fill_history.length, 2, 'preserves both fills in fill_history')
|
|
53
|
+
eq(merged[0].close_time.toISOString(), '2026-04-12T11:57:31.380Z', 'keeps latest close_time')
|
|
54
|
+
eq(merged[0].trade_id, undefined, 'strips fill ids from merged trade')
|
|
55
|
+
|
|
56
|
+
console.log(`pass=${pass} fail=${fail}`)
|
|
57
|
+
if (fail > 0) process.exit(1)
|