@icgio/icg-exchanges-wrapper 1.21.4 → 1.21.6
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/middleware/orders.js +69 -20
- package/package.json +1 -1
package/middleware/orders.js
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
751
|
-
|
|
752
|
-
.
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
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
|
-
|
|
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) {
|
|
@@ -808,12 +851,17 @@ module.exports = class Orders {
|
|
|
808
851
|
}
|
|
809
852
|
for (let exchange in open_orders) {
|
|
810
853
|
for (let pair in open_orders[exchange]) {
|
|
854
|
+
const snapshot_update_time = open_orders[exchange][pair].update_time ? new Date(open_orders[exchange][pair].update_time).getTime() : 0
|
|
811
855
|
open_orders[exchange][pair].open_orders.map((order) => {
|
|
812
856
|
if (_.get(this.order_info, [exchange, pair, order.type, order.order_id])) {
|
|
813
857
|
let order_info = this.order_info[exchange][pair][order.type][order.order_id]
|
|
814
858
|
if (!order_info) {
|
|
815
859
|
return
|
|
816
860
|
}
|
|
861
|
+
const cancel_after_snapshot = order_info.close_time && order_info.close_time > snapshot_update_time
|
|
862
|
+
if (!cancel_after_snapshot) {
|
|
863
|
+
this.log_terminal_divergence('open_orders', exchange, pair, order.order_id, order_info.status, 'N-FILLED')
|
|
864
|
+
}
|
|
817
865
|
const order_f_amount = round_amount(exchange, pair, order.f_amount)
|
|
818
866
|
const order_info_f_amount = round_amount(exchange, pair, order_info.f_amount)
|
|
819
867
|
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 +1490,10 @@ module.exports = class Orders {
|
|
|
1442
1490
|
}
|
|
1443
1491
|
get_trades_all(this.Exchanges, this.exchange_pairs, this.interval_dict)
|
|
1444
1492
|
}
|
|
1445
|
-
check_query_order(exchange) {
|
|
1493
|
+
check_query_order(exchange, order_types) {
|
|
1446
1494
|
// const QUERY_ORDER_TIME_IN_MS = 30 * 60 * 1000
|
|
1447
1495
|
const QUERY_ORDER_TIME_IN_MS = 0
|
|
1496
|
+
const types = order_types && order_types.length > 0 ? order_types : ['maker_1']
|
|
1448
1497
|
let query_order_handler = (exchange, pair, res) => {
|
|
1449
1498
|
if (res.success) {
|
|
1450
1499
|
_.set(this.query_order, [exchange, pair], { query_order: res.body, update_time: new Date() })
|
|
@@ -1457,7 +1506,7 @@ module.exports = class Orders {
|
|
|
1457
1506
|
}
|
|
1458
1507
|
let get_query_order_all = (Exchanges, exchange_pairs, interval_dict) => {
|
|
1459
1508
|
let query_order = (exchange, pair) => {
|
|
1460
|
-
let order_info = _.get(this.get_order_info_by_order_type(
|
|
1509
|
+
let order_info = _.get(this.get_order_info_by_order_type(types), [exchange, pair], {})
|
|
1461
1510
|
let timeout = setTimeout(() => {
|
|
1462
1511
|
query_order(exchange, pair)
|
|
1463
1512
|
}, interval_dict[exchange].query_order)
|