@icgio/icg-utils 1.9.12 → 1.9.14

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 +27 -9
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -544,7 +544,7 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
544
544
  // )
545
545
  // console.log(module.exports.combine_orderbooks({}, 100))
546
546
 
547
- module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0) => {
547
+ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0, quote_amount_cap = 0) => {
548
548
  let i = 0,
549
549
  j = 0,
550
550
  ask_amount = 0,
@@ -614,15 +614,33 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
614
614
  opportunities[bid[2]].amount += bid[1]
615
615
  opportunities[bid[2]].total = opportunities[bid[2]].avg_price * opportunities[bid[2]].amount
616
616
  })
617
+
618
+ // Apply amount caps if needed
617
619
  let amount_total = _.sumBy(_.values(opportunities), 'amount')
620
+ let quote_amount_total = _.sumBy(_.values(opportunities), 'total')
621
+ let amount_coeff = 1
622
+ let quote_amount_coeff = 1
623
+
618
624
  if (amount_total > amount_cap && amount_cap !== 0) {
619
- let coeff = amount_cap / amount_total
625
+ amount_coeff = amount_cap / amount_total
626
+ }
627
+
628
+ if (quote_amount_total > quote_amount_cap && quote_amount_cap !== 0) {
629
+ quote_amount_coeff = quote_amount_cap / quote_amount_total
630
+ }
631
+
632
+ // Use the more restrictive coefficient
633
+ let final_coeff = Math.min(amount_coeff, quote_amount_coeff)
634
+
635
+ if (final_coeff < 1) {
620
636
  for (let exchange in opportunities) {
621
637
  let opportunity = opportunities[exchange]
622
- opportunity.reduced_amount = opportunity.amount * coeff
638
+ opportunity.reduced_amount = opportunity.amount * final_coeff
639
+ opportunity.reduced_total = opportunity.total * final_coeff
623
640
  opportunities[exchange] = opportunity
624
641
  }
625
642
  }
643
+
626
644
  return opportunities
627
645
  }
628
646
 
@@ -808,15 +826,15 @@ module.exports.number_with_commas = (x) => {
808
826
  return parts.join('.')
809
827
  }
810
828
 
811
- module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
829
+ module.exports.exchange_cur_quote_cur_converter = (exchange_cur_quote_cur) => {
812
830
  let exchange_pairs = {},
813
831
  exchange_curs = {}
814
- for (let exchange in exchange_basecur_curs) {
832
+ for (let exchange in exchange_cur_quote_cur) {
815
833
  _.set(exchange_pairs, [exchange], [])
816
834
  _.set(exchange_curs, [exchange], [])
817
- for (let quote_cur in exchange_basecur_curs[exchange]) {
818
- exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][quote_cur], (cur) => cur + quote_cur))
819
- exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_basecur_curs[exchange][quote_cur], [quote_cur]))
835
+ for (let quote_cur in exchange_cur_quote_cur[exchange]) {
836
+ exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_cur_quote_cur[exchange][quote_cur], (cur) => cur + quote_cur))
837
+ exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_cur_quote_cur[exchange][quote_cur], [quote_cur]))
820
838
  }
821
839
  }
822
840
  let pair_exchanges = {}
@@ -829,7 +847,7 @@ module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
829
847
  })
830
848
  }
831
849
  return {
832
- exchange_basecur_curs,
850
+ exchange_cur_quote_cur,
833
851
  exchange_pairs,
834
852
  exchange_curs,
835
853
  pair_exchanges,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.12",
3
+ "version": "1.9.14",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {