@icgio/icg-utils 1.9.56 → 1.9.57

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.
Files changed (2) hide show
  1. package/lib/utils.js +44 -7
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -505,7 +505,30 @@ function merge_trades_with_same_time(array) {
505
505
  }
506
506
 
507
507
  function merge_trades_with_same_id(array) {
508
+ let get_fill_history = (trade) => {
509
+ if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
510
+ return trade.fill_history.map((fill) => Object.assign({}, fill))
511
+ }
512
+ let fill = {
513
+ price: parseFloat(trade.price),
514
+ amount: parseFloat(trade.amount),
515
+ total: parseFloat(trade.total),
516
+ }
517
+ let time = trade.close_time || trade.open_time
518
+ if (time) fill.time = time
519
+ if (trade.trade_id != null) fill.trade_id = String(trade.trade_id)
520
+ if (trade.fees && typeof trade.fees === 'object') fill.fees = Object.assign({}, trade.fees)
521
+ if (trade.role != null) fill.role = trade.role
522
+ return [fill]
523
+ }
524
+ let strip_trade_fill_id = (trade) => {
525
+ delete trade.trade_id
526
+ delete trade.id
527
+ }
528
+
508
529
  if (array.length === 1) {
530
+ array[0].fill_history = get_fill_history(array[0])
531
+ strip_trade_fill_id(array[0])
509
532
  return array
510
533
  } else {
511
534
  let trades = []
@@ -513,21 +536,29 @@ function merge_trades_with_same_id(array) {
513
536
  for (let i = 0; i < array.length; i++) {
514
537
  if (!processed_index_set.has(i)) {
515
538
  let trade = array[i]
539
+ trade.fill_history = get_fill_history(trade)
516
540
  processed_index_set.add(i)
517
541
  for (let j = i + 1; j < array.length; j++) {
518
542
  if (!processed_index_set.has(j)) {
519
543
  let to_compare_trade = array[j]
520
- if (to_compare_trade.order_id === trade.order_id) {
544
+ if (trade.order_id != null && to_compare_trade.order_id === trade.order_id) {
521
545
  trade.amount = trade.amount + to_compare_trade.amount
522
546
  trade.total = trade.total + to_compare_trade.total
523
- // Merge fees from partial fills (previously only first fill's fees were kept)
524
- if (trade.fees && to_compare_trade.fees) {
525
- for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
526
- trade.fees[cur] = (trade.fees[cur] || 0) + amount
547
+ trade.fill_history = trade.fill_history.concat(get_fill_history(to_compare_trade))
548
+ if (to_compare_trade.fees && typeof to_compare_trade.fees === 'object') {
549
+ if (!trade.fees || typeof trade.fees !== 'object') {
550
+ trade.fees = Object.assign({}, to_compare_trade.fees)
551
+ } else {
552
+ for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
553
+ trade.fees[cur] = (trade.fees[cur] || 0) + amount
554
+ }
527
555
  }
528
556
  }
529
- // Use earliest close_time so timeframe filters include the merged trade
530
- if (to_compare_trade.close_time && trade.close_time && to_compare_trade.close_time < trade.close_time) {
557
+ if (to_compare_trade.open_time && (!trade.open_time || to_compare_trade.open_time < trade.open_time)) {
558
+ trade.open_time = to_compare_trade.open_time
559
+ }
560
+ // Use latest close_time so grouped trades remain visible when later fills land near reconciliation cutoffs.
561
+ if (to_compare_trade.close_time && (!trade.close_time || to_compare_trade.close_time > trade.close_time)) {
531
562
  trade.close_time = to_compare_trade.close_time
532
563
  }
533
564
  processed_index_set.add(j)
@@ -537,6 +568,12 @@ function merge_trades_with_same_id(array) {
537
568
  trade.amount = parseFloat(trade.amount)
538
569
  trade.total = parseFloat(trade.total)
539
570
  trade.price = trade.total / trade.amount
571
+ trade.fill_history = trade.fill_history.sort((a, b) => {
572
+ let a_time = a.time ? new Date(a.time).getTime() : 0
573
+ let b_time = b.time ? new Date(b.time).getTime() : 0
574
+ return a_time - b_time
575
+ })
576
+ strip_trade_fill_id(trade)
540
577
  trades.push(trade)
541
578
  }
542
579
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.56",
3
+ "version": "1.9.57",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {