@icgio/icg-utils 1.9.52 → 1.9.54

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 CHANGED
@@ -543,119 +543,6 @@ function combine_orderbooks(orderbooks_dict, length = undefined) {
543
543
  // )
544
544
  // console.log(combine_orderbooks({}, 100))
545
545
 
546
- function arb_opportunities_from_combined_orderbook(orderbook_combined, profit_percentage = 0, amount_cap = 0, quote_amount_cap = 0) {
547
- let i = 0,
548
- j = 0,
549
- ask_amount = 0,
550
- bid_amount = 0
551
- let asks = orderbook_combined.asks,
552
- bids = orderbook_combined.bids
553
- if (asks[0][0] * (1 + profit_percentage) >= bids[0][0]) return {}
554
- let last_step = ''
555
- while (i < asks.length && j < bids.length) {
556
- if (asks[i][0] * (1 + profit_percentage) < bids[j][0]) {
557
- if (ask_amount === 0 && bid_amount === 0) {
558
- ;((ask_amount = asks[i][1]), (bid_amount = bids[j][1]))
559
- } else if (ask_amount <= bid_amount) {
560
- i++
561
- ask_amount += asks[i][1]
562
- last_step = 'ask'
563
- } else {
564
- j++
565
- bid_amount += bids[j][1]
566
- last_step = 'bid'
567
- }
568
- } else {
569
- if (last_step === 'ask') {
570
- ask_amount -= asks[i][1]
571
- i--
572
- } else if (last_step === 'bid') {
573
- bid_amount -= bids[j][1]
574
- j--
575
- }
576
- break
577
- }
578
- }
579
- let opportunities = {},
580
- diff = ask_amount - bid_amount
581
- asks = asks.slice(0, i + 1)
582
- bids = bids.slice(0, j + 1)
583
- if (diff > 0) {
584
- asks[i][1] -= diff
585
- } else {
586
- bids[j][1] += diff
587
- }
588
- asks.map((ask) => {
589
- if (!opportunities[ask[2]]) {
590
- opportunities[ask[2]] = {
591
- price: 0,
592
- amount: 0,
593
- exchange: ask[2],
594
- method: 'buy',
595
- }
596
- }
597
- opportunities[ask[2]].avg_price = (opportunities[ask[2]].price * opportunities[ask[2]].amount + ask[0] * ask[1]) / (opportunities[ask[2]].amount + ask[1])
598
- opportunities[ask[2]].price = ask[0]
599
- opportunities[ask[2]].amount += ask[1]
600
- opportunities[ask[2]].total = opportunities[ask[2]].avg_price * opportunities[ask[2]].amount
601
- })
602
- bids.map((bid) => {
603
- if (!opportunities[bid[2]]) {
604
- opportunities[bid[2]] = {
605
- price: 0,
606
- amount: 0,
607
- exchange: bid[2],
608
- method: 'sell',
609
- }
610
- }
611
- opportunities[bid[2]].avg_price = (opportunities[bid[2]].price * opportunities[bid[2]].amount + bid[0] * bid[1]) / (opportunities[bid[2]].amount + bid[1])
612
- opportunities[bid[2]].price = bid[0]
613
- opportunities[bid[2]].amount += bid[1]
614
- opportunities[bid[2]].total = opportunities[bid[2]].avg_price * opportunities[bid[2]].amount
615
- })
616
-
617
- // Apply amount caps if needed
618
- let amount_total = _.sumBy(_.values(opportunities), 'amount')
619
- let quote_amount_total = _.sumBy(_.values(opportunities), 'total')
620
- let amount_coeff = 1
621
- let quote_amount_coeff = 1
622
-
623
- if (amount_total > amount_cap && amount_cap !== 0) {
624
- amount_coeff = amount_cap / amount_total
625
- }
626
-
627
- if (quote_amount_total > quote_amount_cap && quote_amount_cap !== 0) {
628
- quote_amount_coeff = quote_amount_cap / quote_amount_total
629
- }
630
-
631
- // Use the more restrictive coefficient
632
- let final_coeff = Math.min(amount_coeff, quote_amount_coeff)
633
-
634
- if (final_coeff < 1) {
635
- for (let exchange in opportunities) {
636
- let opportunity = opportunities[exchange]
637
- opportunity.reduced_amount = opportunity.amount * final_coeff
638
- opportunity.reduced_total = opportunity.total * final_coeff
639
- opportunities[exchange] = opportunity
640
- }
641
- }
642
-
643
- return opportunities
644
- }
645
-
646
- // console.log(arb_opportunities_from_combined_orderbook({
647
- // asks: [ [ 0.3049, 150, 'bitget' ],
648
- // [ 0.30791, 50.5556, 'bitget' ],
649
- // [ 0.30792, 175.49623901, 'bitfinex' ],
650
- // [ 0.30793, 86.89887994, 'bitfinex' ],
651
- // [ 0.3081, 2.4526, 'huobi' ] ],
652
- // bids: [ [ 0.3079, 100, 'huobi' ],
653
- // [ 0.305, 100, 'huobi' ],
654
- // [ 0.3049, 5956.5837, 'huobi' ],
655
- // [ 0.3038, 4373.4668, 'huobi' ],
656
- // [ 0.3027, 867.8638, 'huobi' ] ]
657
- // }, 0.0004, amount_cap=1))
658
-
659
546
  function vol_position_combined(input_array, vol_target) {
660
547
  if (input_array.length === 1) {
661
548
  return {
@@ -769,6 +656,16 @@ function merge_trades_with_same_id(array) {
769
656
  if (to_compare_trade.order_id === trade.order_id) {
770
657
  trade.amount = trade.amount + to_compare_trade.amount
771
658
  trade.total = trade.total + to_compare_trade.total
659
+ // Merge fees from partial fills (previously only first fill's fees were kept)
660
+ if (trade.fees && to_compare_trade.fees) {
661
+ for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
662
+ trade.fees[cur] = (trade.fees[cur] || 0) + amount
663
+ }
664
+ }
665
+ // Use earliest close_time so timeframe filters include the merged trade
666
+ if (to_compare_trade.close_time && trade.close_time && to_compare_trade.close_time < trade.close_time) {
667
+ trade.close_time = to_compare_trade.close_time
668
+ }
772
669
  processed_index_set.add(j)
773
670
  }
774
671
  }
@@ -1073,7 +970,6 @@ module.exports = {
1073
970
  sum_position_orderbook,
1074
971
  vol_position_orderbook,
1075
972
  combine_orderbooks,
1076
- arb_opportunities_from_combined_orderbook,
1077
973
  vol_position_combined,
1078
974
  sort_dict,
1079
975
  merge_trades_with_same_time,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.52",
3
+ "version": "1.9.54",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
package/patches/README.md DELETED
@@ -1,18 +0,0 @@
1
- These patches target sibling repos that are read-only in this session.
2
-
3
- Apply them from the repo they target:
4
-
5
- ```bash
6
- cd /Users/lm/icg-exchanges-wrapper
7
- git apply /Users/lm/icg-utils/patches/icg-exchanges-wrapper-strategy-priority.patch
8
-
9
- cd /Users/lm/icg-market-maker
10
- git apply /Users/lm/icg-utils/patches/icg-market-maker-dependency-bump.patch
11
- ```
12
-
13
- After publishing `@icgio/icg-utils@1.9.52` and `@icgio/icg-exchanges-wrapper@1.15.36`, regenerate the market maker lockfile with:
14
-
15
- ```bash
16
- cd /Users/lm/icg-market-maker
17
- npm install
18
- ```
@@ -1,75 +0,0 @@
1
- diff --git a/middleware/orders.js b/middleware/orders.js
2
- --- a/middleware/orders.js
3
- +++ b/middleware/orders.js
4
- @@ -73,7 +73,23 @@
5
- const TRADES_DEFAULT_MS = 7 * 24 * 60 * 60 * 1000
6
- const NO_CALLBACK_TIMEOUT_MS = 2 * 60 * 1000
7
- const PROCESS_RATES_THROTTLE_MS = 75
8
- +const ORDER_TYPE_PRIORITY = {
9
- + arb: 1,
10
- + self: 2,
11
- + maker_1: 3,
12
- + maker_1_taker: 3,
13
- + maker_2: 4,
14
- + maker_3: 5,
15
- +}
16
-
17
- +const getOrderPriority = (order_type, is_cancel = false) => {
18
- + const priority = ORDER_TYPE_PRIORITY[order_type]
19
- + if (priority == null) {
20
- + return Infinity
21
- + }
22
- + return is_cancel ? priority - 0.5 : priority
23
- +}
24
- +
25
- module.exports = class Orders {
26
- constructor(exchange_cur_quote_cur_dict, cd, settings) {
27
- let { exchange_cur_quote_cur, exchange_cur_quote_cur_rates_only, pair_map_rates_only } = exchange_cur_quote_cur_dict
28
- @@ -332,11 +348,17 @@
29
- // }
30
- }
31
- if (routes) {
32
- + if (this.Exchanges[exchange].private_limiter) {
33
- + this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
34
- + }
35
- this.Exchanges[exchange].limit_trade_with_routes(pair, method, price, amount, routes, cb)
36
- } else {
37
- console.error('dex no routes:', rates_with_routes, price)
38
- }
39
- } else if (limiter_on) {
40
- + if (this.Exchanges[exchange].private_limiter) {
41
- + this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
42
- + }
43
- this.Exchanges[exchange].limit_trade(pair, method, price, amount, cb)
44
- } else {
45
- this.Exchanges[exchange].limit_trade_base(pair, method, price, amount, cb)
46
- @@ -403,6 +425,9 @@
47
- order.order_type + ' canceling',
48
- )
49
- ;((order.exchange = exchange), (order.pair = pair))
50
- + if (this.Exchanges[exchange].private_limiter) {
51
- + this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order.order_type, true)
52
- + }
53
- this.Exchanges[exchange].cancel_order_by_order(order, (res) => {
54
- if (res.success || (!res.success && res.error === 'order-not-open' && original_status === 'CANCELING')) {
55
- this.update_order_info(
56
- diff --git a/package.json b/package.json
57
- --- a/package.json
58
- +++ b/package.json
59
- @@ -1,6 +1,6 @@
60
- {
61
- "name": "@icgio/icg-exchanges-wrapper",
62
- - "version": "1.15.35",
63
- + "version": "1.15.36",
64
- "description": "",
65
- "main": "index.js",
66
- "scripts": {
67
- @@ -18,7 +18,7 @@
68
- "homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
69
- "dependencies": {
70
- "@icgio/icg-exchanges": "^1.32.187",
71
- - "@icgio/icg-utils": "^1.9.51",
72
- + "@icgio/icg-utils": "^1.9.52",
73
- "lodash": "^4.17.23",
74
- "pg": "^8.17.2"
75
- }
@@ -1,14 +0,0 @@
1
- diff --git a/package.json b/package.json
2
- --- a/package.json
3
- +++ b/package.json
4
- @@ -3,8 +3,8 @@
5
- "version": "1.0.0",
6
- "description": "ICG Market Maker",
7
- "dependencies": {
8
- - "@icgio/icg-exchanges-wrapper": "^1.15.35",
9
- - "@icgio/icg-utils": "^1.9.51",
10
- + "@icgio/icg-exchanges-wrapper": "^1.15.36",
11
- + "@icgio/icg-utils": "^1.9.52",
12
- "form-data": "^4.0.5",
13
- "lodash": "^4.17.23",
14
- "mailgun.js": "^12.7.0",