@icgio/icg-exchanges-wrapper 1.21.4 → 1.21.5

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.
@@ -247,7 +247,7 @@ module.exports = class Orders {
247
247
  positions_check: false,
248
248
  open_orders_check: true,
249
249
  trades_check: true,
250
- query_order_check_maker_1: true,
250
+ query_order_check_maker_1_self: false,
251
251
  query_order_check_exchange: {},
252
252
  rates_check: true,
253
253
  market_history_check: true,
@@ -267,10 +267,10 @@ module.exports = class Orders {
267
267
  this.trades_amount_past_hour = {}
268
268
  this.check_trades()
269
269
  }
270
- if (this.function_enabled.query_order_check_maker_1) {
270
+ if (this.function_enabled.query_order_check_maker_1_self) {
271
271
  for (let exchange in this.Exchanges) {
272
272
  if (this.function_enabled.query_order_check_exchange[exchange]) {
273
- this.check_query_order(exchange)
273
+ this.check_query_order(exchange, ['maker_1', 'self'])
274
274
  }
275
275
  }
276
276
  }
@@ -292,7 +292,7 @@ module.exports = class Orders {
292
292
  this.process_open_orders(this.open_orders)
293
293
  }, PROCESS_OPEN_ORDERS_TRADERS_INTERVAL_MS)
294
294
  }
295
- if (this.function_enabled.query_order_check_maker_1) {
295
+ if (this.function_enabled.query_order_check_maker_1_self) {
296
296
  setInterval(() => {
297
297
  this.process_query_order(this.query_order)
298
298
  }, PROCESS_OPEN_ORDERS_TRADERS_INTERVAL_MS)
@@ -653,6 +653,7 @@ module.exports = class Orders {
653
653
  const order_info_f_amount = round_amount(exchange, pair, order_info.f_amount)
654
654
  if (_.includes(['CANCELED', 'CANCELING'], order_info.status) && trade_amount > order_info_f_amount) {
655
655
  // console.log('TRADES: %s ORDER FILLED', order_info.status, trade, order_info)
656
+ this.log_terminal_divergence('trades', exchange, pair, order_id, order_info.status, 'P-FILLED')
656
657
  this.update_order_info(
657
658
  exchange,
658
659
  pair,
@@ -743,26 +744,68 @@ module.exports = class Orders {
743
744
  }
744
745
  }
745
746
  }
747
+ log_terminal_divergence(source, exchange, pair, order_id, local_status, source_status) {
748
+ if ((local_status === 'F-FILLED' || local_status === 'CANCELED') && local_status !== source_status) {
749
+ console.warn('terminal divergence (%s): %s %s %s %s -> %s', source, exchange, pair, order_id, local_status, source_status)
750
+ }
751
+ }
746
752
  process_query_order(query_order) {
747
- let trades = {}
748
753
  for (let exchange in query_order) {
749
754
  for (let pair in query_order[exchange]) {
750
- let temp = query_order[exchange][pair].query_order
751
- .filter((o) => _.includes(['F-FILLED', 'P-FILLED'], o.status))
752
- .map((o) => {
753
- return {
754
- order_id: o.order_id,
755
- pair: o.pair,
756
- type: o.type,
757
- price: o.price,
758
- amount: o.f_amount,
759
- total: o.price * o.f_amount,
755
+ const polled_orders = query_order[exchange][pair].query_order || []
756
+ for (const polled_order of polled_orders) {
757
+ if (!polled_order || !polled_order.order_id || !polled_order.type) continue
758
+ const local_order = _.get(this.order_info, [exchange, pair, polled_order.type, polled_order.order_id])
759
+ if (!local_order) continue
760
+ let exchange_status
761
+ if (polled_order.status === 'F-FILLED' || polled_order.status === 'P-FILLED' || polled_order.status === 'N-FILLED' || polled_order.status === 'CANCELED') {
762
+ exchange_status = polled_order.status
763
+ } else if (typeof polled_order.status === 'string' && polled_order.status.startsWith('OTHER-')) {
764
+ exchange_status = 'CANCELED'
765
+ } else {
766
+ continue
767
+ }
768
+ if (exchange_status === 'N-FILLED') continue
769
+ const local_filled_amount = round_amount(exchange, pair, local_order.f_amount || 0)
770
+ const polled_filled_amount = round_amount(exchange, pair, polled_order.f_amount || 0)
771
+ if (local_order.status === exchange_status && local_filled_amount === polled_filled_amount) continue
772
+ this.log_terminal_divergence('query_order', exchange, pair, polled_order.order_id, local_order.status, exchange_status)
773
+ const merged_filled_amount = Math.max(polled_filled_amount, local_filled_amount)
774
+ const total_amount = round_amount(exchange, pair, local_order.t_amount || polled_order.t_amount || 0)
775
+ const tag = polled_order.status !== exchange_status ? ' (' + polled_order.status + ')' : ''
776
+ const note = (local_order.order_type || 'unknown') + ' query_order ' + exchange_status + tag
777
+ if (exchange_status === 'F-FILLED' || exchange_status === 'P-FILLED') {
778
+ const update_dict = {
779
+ status: exchange_status,
780
+ price: polled_order.price != null ? polled_order.price : local_order.price,
781
+ amount: round_amount(exchange, pair, Math.max(total_amount - merged_filled_amount, 0)),
782
+ f_amount: merged_filled_amount,
783
+ f_percentage: total_amount > 0 ? merged_filled_amount / total_amount : 0,
784
+ event_hrtime: process.hrtime(),
760
785
  }
761
- })
762
- _.set(trades, [exchange, pair, 'trades'], temp)
786
+ if (exchange_status === 'F-FILLED') update_dict.close_time = Date.now()
787
+ this.update_order_info(exchange, pair, polled_order.type, polled_order.order_id, update_dict, note)
788
+ if (exchange_status === 'F-FILLED') {
789
+ setTimeout(() => {
790
+ this.delete_order_info(exchange, pair, polled_order.type, polled_order.order_id, note)
791
+ }, this.delete_finished_orders_timeout)
792
+ }
793
+ } else {
794
+ const update_dict = {
795
+ status: 'CANCELED',
796
+ f_amount: merged_filled_amount,
797
+ f_percentage: total_amount > 0 ? merged_filled_amount / total_amount : 0,
798
+ close_time: Date.now(),
799
+ event_hrtime: process.hrtime(),
800
+ }
801
+ this.update_order_info(exchange, pair, polled_order.type, polled_order.order_id, update_dict, note)
802
+ setTimeout(() => {
803
+ this.delete_order_info(exchange, pair, polled_order.type, polled_order.order_id, note)
804
+ }, this.delete_finished_orders_timeout)
805
+ }
806
+ }
763
807
  }
764
808
  }
765
- this.process_trades(trades)
766
809
  }
767
810
  process_open_orders(open_orders) {
768
811
  for (let exchange of this.no_trades_function_exchanges) {
@@ -814,6 +857,7 @@ module.exports = class Orders {
814
857
  if (!order_info) {
815
858
  return
816
859
  }
860
+ this.log_terminal_divergence('open_orders', exchange, pair, order.order_id, order_info.status, 'N-FILLED')
817
861
  const order_f_amount = round_amount(exchange, pair, order.f_amount)
818
862
  const order_info_f_amount = round_amount(exchange, pair, order_info.f_amount)
819
863
  if (_.includes(['N-FILLED', 'P-FILLED'], order_info.status) && order_f_amount > order_info_f_amount && order_f_amount / order_info.t_amount > this.f_filled_threshold) {
@@ -1442,9 +1486,10 @@ module.exports = class Orders {
1442
1486
  }
1443
1487
  get_trades_all(this.Exchanges, this.exchange_pairs, this.interval_dict)
1444
1488
  }
1445
- check_query_order(exchange) {
1489
+ check_query_order(exchange, order_types) {
1446
1490
  // const QUERY_ORDER_TIME_IN_MS = 30 * 60 * 1000
1447
1491
  const QUERY_ORDER_TIME_IN_MS = 0
1492
+ const types = order_types && order_types.length > 0 ? order_types : ['maker_1']
1448
1493
  let query_order_handler = (exchange, pair, res) => {
1449
1494
  if (res.success) {
1450
1495
  _.set(this.query_order, [exchange, pair], { query_order: res.body, update_time: new Date() })
@@ -1457,7 +1502,7 @@ module.exports = class Orders {
1457
1502
  }
1458
1503
  let get_query_order_all = (Exchanges, exchange_pairs, interval_dict) => {
1459
1504
  let query_order = (exchange, pair) => {
1460
- let order_info = _.get(this.get_order_info_by_order_type(['maker_1']), [exchange, pair], {})
1505
+ let order_info = _.get(this.get_order_info_by_order_type(types), [exchange, pair], {})
1461
1506
  let timeout = setTimeout(() => {
1462
1507
  query_order(exchange, pair)
1463
1508
  }, interval_dict[exchange].query_order)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.21.4",
3
+ "version": "1.21.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {