@icgio/icg-exchanges-wrapper 1.14.113 → 1.14.114
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 +40 -48
- package/package.json +1 -1
package/middleware/orders.js
CHANGED
|
@@ -107,7 +107,7 @@ module.exports = class Orders {
|
|
|
107
107
|
this.f_filled_threshold = 0.999
|
|
108
108
|
this.previous_orders_canceled = false
|
|
109
109
|
this.previous_orders_canceled_exchange_pair = {}
|
|
110
|
-
this.
|
|
110
|
+
this.check_balance_amount_init = false
|
|
111
111
|
this.cmc_curs = settings.cmc_curs || []
|
|
112
112
|
this.accounting_benchmark = settings.accounting_benchmark
|
|
113
113
|
this.balance_benchmark = settings.balance_benchmark || {}
|
|
@@ -176,8 +176,6 @@ module.exports = class Orders {
|
|
|
176
176
|
this.order_info = {}
|
|
177
177
|
this.order_info_status = {}
|
|
178
178
|
this.function_enabled = settings.function_enabled || {
|
|
179
|
-
info_log: true,
|
|
180
|
-
error_log: true,
|
|
181
179
|
balances_check: true,
|
|
182
180
|
positions_check: false,
|
|
183
181
|
open_orders_check: true,
|
|
@@ -189,12 +187,6 @@ module.exports = class Orders {
|
|
|
189
187
|
ohlcv_check: false,
|
|
190
188
|
cmc_rates_check: true,
|
|
191
189
|
}
|
|
192
|
-
this.info_log = (...args) => {
|
|
193
|
-
if (this.function_enabled.info_log) console.log(...args)
|
|
194
|
-
}
|
|
195
|
-
this.error_log = (...args) => {
|
|
196
|
-
if (this.function_enabled.info_log) console.error(...args)
|
|
197
|
-
}
|
|
198
190
|
if (this.function_enabled.balances_check) {
|
|
199
191
|
this.check_balances()
|
|
200
192
|
}
|
|
@@ -248,7 +240,7 @@ module.exports = class Orders {
|
|
|
248
240
|
limit_trade(exchange, pair, method, price, amount, id = uuid(), order_type = '', limiter_on = true, api_key_index) {
|
|
249
241
|
price = round_price(_.upperFirst(exchange.replace('_2', '')), pair, price)
|
|
250
242
|
amount = round_amount(_.upperFirst(exchange.replace('_2', '')), pair, amount)
|
|
251
|
-
|
|
243
|
+
// console.log('%s %s %s %s %s %s at %s', limiter_on, order_type, exchange, pair, method, amount, price)
|
|
252
244
|
this.update_order_info(
|
|
253
245
|
exchange,
|
|
254
246
|
pair,
|
|
@@ -313,7 +305,7 @@ module.exports = class Orders {
|
|
|
313
305
|
if (routes) {
|
|
314
306
|
this.Exchanges[exchange].limit_trade_with_routes(pair, method, price, amount, routes, cb)
|
|
315
307
|
} else {
|
|
316
|
-
|
|
308
|
+
console.error('dex no routes:', rates_with_routes, price)
|
|
317
309
|
}
|
|
318
310
|
} else if (limiter_on) {
|
|
319
311
|
this.Exchanges[exchange].limit_trade(pair, method, price, amount, cb)
|
|
@@ -364,7 +356,7 @@ module.exports = class Orders {
|
|
|
364
356
|
// status: 'N-OPENED-TIMEOUT'
|
|
365
357
|
// }, order_type + ' n-opened-timeout-callback')
|
|
366
358
|
// }
|
|
367
|
-
|
|
359
|
+
console.error('limit trade error: %s %s %s %s %s %s %s %j', order_type, id, exchange, pair, method, price, amount, res)
|
|
368
360
|
}
|
|
369
361
|
clearTimeout(no_callback_timeout)
|
|
370
362
|
})
|
|
@@ -399,7 +391,7 @@ module.exports = class Orders {
|
|
|
399
391
|
this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + ' cancel-order-success')
|
|
400
392
|
}, this.delete_finished_orders_timeout)
|
|
401
393
|
} else if (!res.success && res.error === 'order-not-open' && original_status !== 'CANCELED') {
|
|
402
|
-
//
|
|
394
|
+
// console.log('%s ORDER CANNOT BE CANCELED: %j', original_status, order)
|
|
403
395
|
this.update_order_info(
|
|
404
396
|
exchange,
|
|
405
397
|
pair,
|
|
@@ -493,7 +485,7 @@ module.exports = class Orders {
|
|
|
493
485
|
if (_.includes(['CANCELED', 'CANCELING'], order_info.status) && trade.amount > order_info.f_amount) {
|
|
494
486
|
let type = trade.type,
|
|
495
487
|
order_id = trade.order_id
|
|
496
|
-
//
|
|
488
|
+
// console.log('TRADES: %s ORDER FILLED', order_info.status, trade, order_info)
|
|
497
489
|
this.update_order_info(
|
|
498
490
|
exchange,
|
|
499
491
|
pair,
|
|
@@ -514,7 +506,7 @@ module.exports = class Orders {
|
|
|
514
506
|
this.delete_order_info(exchange, pair, type, order_id, order_info.order_type + ' canceled-order-filled(-more)')
|
|
515
507
|
}, this.delete_finished_orders_timeout)
|
|
516
508
|
} else if (order_info.status === 'F-FILLED' && trade.amount > order_info.f_amount) {
|
|
517
|
-
//
|
|
509
|
+
// console.log('TRADES: FILLED ORDER FILLED MORE', trade, order_info)
|
|
518
510
|
this.update_order_info(
|
|
519
511
|
exchange,
|
|
520
512
|
pair,
|
|
@@ -533,7 +525,7 @@ module.exports = class Orders {
|
|
|
533
525
|
trade.amount > order_info.f_amount &&
|
|
534
526
|
trade.amount / order_info.t_amount > this.f_filled_threshold
|
|
535
527
|
) {
|
|
536
|
-
//
|
|
528
|
+
// console.log('TRADES: %s %s ORDER F-FILLED', exchange, pair, trade)
|
|
537
529
|
this.update_order_info(
|
|
538
530
|
exchange,
|
|
539
531
|
pair,
|
|
@@ -558,7 +550,7 @@ module.exports = class Orders {
|
|
|
558
550
|
trade.amount > order_info.f_amount &&
|
|
559
551
|
trade.amount / order_info.t_amount > this.p_filled_threshold
|
|
560
552
|
) {
|
|
561
|
-
//
|
|
553
|
+
// console.log('TRADES: %s %s ORDER P-FILLED', exchange, pair, trade)
|
|
562
554
|
this.update_order_info(
|
|
563
555
|
exchange,
|
|
564
556
|
pair,
|
|
@@ -576,7 +568,7 @@ module.exports = class Orders {
|
|
|
576
568
|
} else if (_.includes(['F-FILLED', 'P-FILLED'], order_info.status)) {
|
|
577
569
|
} else if (_.includes(['CANCELED', 'CANCELING'], order_info.status) && trade.amount === order_info.f_amount) {
|
|
578
570
|
} else {
|
|
579
|
-
|
|
571
|
+
console.log('TRADES: UNKNOWN %j %j', order_info, trade)
|
|
580
572
|
}
|
|
581
573
|
}
|
|
582
574
|
})
|
|
@@ -621,7 +613,7 @@ module.exports = class Orders {
|
|
|
621
613
|
}
|
|
622
614
|
})
|
|
623
615
|
if (!exist) {
|
|
624
|
-
//
|
|
616
|
+
// console.log('ORDERS: %s %s ORDER F-FILLED NO TRADES FUNCTION EXCHANGES', exchange, pair)
|
|
625
617
|
this.update_order_info(
|
|
626
618
|
exchange,
|
|
627
619
|
pair,
|
|
@@ -652,7 +644,7 @@ module.exports = class Orders {
|
|
|
652
644
|
let order_info = this.order_info[exchange][pair][order.type][order.order_id]
|
|
653
645
|
order.f_amount = round_amount(_.upperFirst(exchange.replace('_2', '')), pair, order.f_amount)
|
|
654
646
|
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) {
|
|
655
|
-
//
|
|
647
|
+
// console.log('ORDERS: %s %s ORDER F-FILLED', exchange, pair)
|
|
656
648
|
this.update_order_info(
|
|
657
649
|
exchange,
|
|
658
650
|
pair,
|
|
@@ -675,7 +667,7 @@ module.exports = class Orders {
|
|
|
675
667
|
order.f_amount > order_info.f_amount &&
|
|
676
668
|
order.f_amount / order_info.t_amount > this.p_filled_threshold
|
|
677
669
|
) {
|
|
678
|
-
//
|
|
670
|
+
// console.log('ORDERS: %s %s ORDER P-FILLED', exchange, pair)
|
|
679
671
|
this.update_order_info(
|
|
680
672
|
exchange,
|
|
681
673
|
pair,
|
|
@@ -702,7 +694,7 @@ module.exports = class Orders {
|
|
|
702
694
|
order_time - init_order_time > 0 &&
|
|
703
695
|
order_time - init_order_time < this.check_init_order_opened_timeout
|
|
704
696
|
) {
|
|
705
|
-
//
|
|
697
|
+
// console.log('ORDERS: %s %s %s %s INIT ORDER OPENED', init_order.order_id, order.order_id, exchange, pair)
|
|
706
698
|
this.delete_order_info(exchange, pair, init_order.type, init_order.order_id, init_order.order_type + ' init-order-opened')
|
|
707
699
|
this.update_order_info(
|
|
708
700
|
exchange,
|
|
@@ -731,7 +723,7 @@ module.exports = class Orders {
|
|
|
731
723
|
}
|
|
732
724
|
update_order_info(exchange, pair, type, order_id, dict, note) {
|
|
733
725
|
if (!order_id) {
|
|
734
|
-
|
|
726
|
+
console.error('UPDATE %s: order_id invalid %s %s %s %s %j', note, exchange, pair, type, order_id, dict)
|
|
735
727
|
return
|
|
736
728
|
}
|
|
737
729
|
for (let key in dict) {
|
|
@@ -740,7 +732,7 @@ module.exports = class Orders {
|
|
|
740
732
|
}
|
|
741
733
|
_.setWith(this.order_info, `${exchange}.${pair}.${type}.${order_id}.${key}`, dict[key], Object)
|
|
742
734
|
}
|
|
743
|
-
|
|
735
|
+
console.log('UPDATE %s: %s %s %s %s %j', note, exchange, pair, type, order_id, dict)
|
|
744
736
|
// start: log every f-filled trade for future analysis
|
|
745
737
|
if (dict.status === 'F-FILLED') {
|
|
746
738
|
let { price, amount, f_amount, t_amount, fees, close_time } = _.get(this.order_info, [exchange, pair, type, order_id])
|
|
@@ -751,7 +743,7 @@ module.exports = class Orders {
|
|
|
751
743
|
}
|
|
752
744
|
delete_order_info(exchange, pair, type, order_id, note) {
|
|
753
745
|
this.order_info[exchange][pair][type] = _.omit(this.order_info[exchange][pair][type], [order_id])
|
|
754
|
-
|
|
746
|
+
console.log('DELETE %s: %s %s %s %s', note, exchange, pair, type, order_id)
|
|
755
747
|
}
|
|
756
748
|
check_balances() {
|
|
757
749
|
let balances_handler = (exchange, res) => {
|
|
@@ -786,12 +778,12 @@ module.exports = class Orders {
|
|
|
786
778
|
})
|
|
787
779
|
}
|
|
788
780
|
}
|
|
789
|
-
if (!this.
|
|
790
|
-
this.
|
|
781
|
+
if (!this.check_balance_amount_init && _.keys(this.balances).length === _.keys(this.exchange_pairs).length) {
|
|
782
|
+
this.check_balance_amount_init = true
|
|
791
783
|
this.init_account(this.balances, this.accounting_benchmark || this.balance_benchmark) // use accounting_benchmark if it exists
|
|
792
784
|
}
|
|
793
785
|
} else if (_.get(this.balances, [exchange, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].balances) {
|
|
794
|
-
|
|
786
|
+
console.log('%s balances expired: %j', exchange, res)
|
|
795
787
|
this.balances = _.omit(this.balances, [exchange])
|
|
796
788
|
this.buy_sell_limits_against_reserve_balance = _.omit(this.buy_sell_limits_against_reserve_balance, [exchange])
|
|
797
789
|
}
|
|
@@ -822,7 +814,7 @@ module.exports = class Orders {
|
|
|
822
814
|
if (res.success) {
|
|
823
815
|
_.set(this.positions, [exchange], Object.assign(res.body, { update_time: new Date() }))
|
|
824
816
|
} else if (_.get(this.positions, [exchange, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].positions) {
|
|
825
|
-
|
|
817
|
+
console.log('%s positions expired: %j', exchange, res)
|
|
826
818
|
this.positions = _.omit(this.positions, [exchange])
|
|
827
819
|
}
|
|
828
820
|
}
|
|
@@ -872,21 +864,21 @@ module.exports = class Orders {
|
|
|
872
864
|
// if (buy_orders.length > 0) {
|
|
873
865
|
// buy_orders = _.initial(_.sortBy(buy_orders, 'price'))
|
|
874
866
|
// buy_orders.map((order) => {
|
|
875
|
-
//
|
|
867
|
+
// console.log('cancel original order: %s %s %j', exchange, pair, order)
|
|
876
868
|
// this.Exchanges[exchange].cancel_order_by_order(order, (res) => {})
|
|
877
869
|
// })
|
|
878
870
|
// }
|
|
879
871
|
// if (sell_orders.length > 0) {
|
|
880
872
|
// sell_orders = _.initial(_.sortBy(sell_orders, 'price').reverse())
|
|
881
873
|
// sell_orders.map((order) => {
|
|
882
|
-
//
|
|
874
|
+
// console.log('cancel original order: %s %s %j', exchange, pair, order)
|
|
883
875
|
// this.Exchanges[exchange].cancel_order_by_order(order, (res) => {})
|
|
884
876
|
// })
|
|
885
877
|
// }
|
|
886
878
|
_.set(this.previous_orders_canceled_exchange_pair, [exchange, pair], true)
|
|
887
879
|
} else if (this.cancel_previous_orders && !this.previous_orders_canceled) {
|
|
888
880
|
orders.map((order) => {
|
|
889
|
-
|
|
881
|
+
console.log('cancel original order: %s %s %j', exchange, pair, order)
|
|
890
882
|
this.Exchanges[exchange].cancel_order_by_order(order, (res) => {})
|
|
891
883
|
})
|
|
892
884
|
if (orders.length === 0) {
|
|
@@ -902,7 +894,7 @@ module.exports = class Orders {
|
|
|
902
894
|
})
|
|
903
895
|
}
|
|
904
896
|
if (previous_orders_canceled) {
|
|
905
|
-
|
|
897
|
+
console.log('original order canceled')
|
|
906
898
|
}
|
|
907
899
|
this.previous_orders_canceled = previous_orders_canceled
|
|
908
900
|
}
|
|
@@ -912,12 +904,12 @@ module.exports = class Orders {
|
|
|
912
904
|
// console.log(exchange, pair, rate_major, orders)
|
|
913
905
|
orders.map((order) => {
|
|
914
906
|
if (order.type === 'sell' && order.price < rate_major.asks[0][0] && order.order_status !== 'F-FILLED' && order.order_type !== 'arb') {
|
|
915
|
-
|
|
907
|
+
console.log('cancel range order: %s %s %j', exchange, pair, order)
|
|
916
908
|
order.order_type = 'range-order'
|
|
917
909
|
this.cancel_order(exchange, pair, order)
|
|
918
910
|
}
|
|
919
911
|
if (order.type === 'buy' && order.price > rate_major.bids[0][0] && order.order_status !== 'F-FILLED' && order.order_type !== 'arb') {
|
|
920
|
-
|
|
912
|
+
console.log('cancel range order: %s %s %j', exchange, pair, order)
|
|
921
913
|
order.order_type = 'range-order'
|
|
922
914
|
this.cancel_order(exchange, pair, order)
|
|
923
915
|
}
|
|
@@ -929,10 +921,10 @@ module.exports = class Orders {
|
|
|
929
921
|
_.set(this.open_orders, [exchange, pair], { open_orders: res.body, update_time: new Date() })
|
|
930
922
|
previous_orders_processer(exchange, pair, res.body)
|
|
931
923
|
} else if (_.get(this.open_orders, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].open_orders) {
|
|
932
|
-
|
|
924
|
+
console.log('%s %s open orders expired: %j', exchange, pair, res)
|
|
933
925
|
this.open_orders[exchange] = _.omit(this.open_orders[exchange], [pair])
|
|
934
926
|
} else if (res.error !== 'opening') {
|
|
935
|
-
|
|
927
|
+
console.error('%s %s open orders: %j', exchange, pair, res)
|
|
936
928
|
}
|
|
937
929
|
}
|
|
938
930
|
let all_open_orders_handler = (exchange, res) => {
|
|
@@ -947,12 +939,12 @@ module.exports = class Orders {
|
|
|
947
939
|
for (let pair of this.exchange_pairs[exchange]) {
|
|
948
940
|
if (_.get(this.open_orders, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].open_orders) {
|
|
949
941
|
if (!error_logged) {
|
|
950
|
-
|
|
942
|
+
console.log('%s all open orders expired: %j', exchange, res)
|
|
951
943
|
error_logged = true
|
|
952
944
|
}
|
|
953
945
|
this.open_orders[exchange] = _.omit(this.open_orders[exchange], [pair])
|
|
954
946
|
} else if (res.error !== 'opening' && !error_logged) {
|
|
955
|
-
|
|
947
|
+
console.error('%s all open orders: %j', exchange, res)
|
|
956
948
|
error_logged = true
|
|
957
949
|
}
|
|
958
950
|
}
|
|
@@ -1031,10 +1023,10 @@ module.exports = class Orders {
|
|
|
1031
1023
|
) || 0
|
|
1032
1024
|
)
|
|
1033
1025
|
} else if (_.get(this.trades, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].trades) {
|
|
1034
|
-
|
|
1026
|
+
console.log('%s %s trades expired: %j', exchange, pair, res)
|
|
1035
1027
|
this.trades[exchange] = _.omit(this.trades[exchange], [pair])
|
|
1036
1028
|
} else if (res.error !== 'opening') {
|
|
1037
|
-
|
|
1029
|
+
console.error('%s %s trades: %j', exchange, pair, res)
|
|
1038
1030
|
}
|
|
1039
1031
|
}
|
|
1040
1032
|
let all_trades_handler = (exchange, res) => {
|
|
@@ -1056,12 +1048,12 @@ module.exports = class Orders {
|
|
|
1056
1048
|
for (let pair of this.exchange_pairs[exchange]) {
|
|
1057
1049
|
if (_.get(this.trades, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].trades) {
|
|
1058
1050
|
if (!error_logged) {
|
|
1059
|
-
|
|
1051
|
+
console.log('%s all trades expired: %j', exchange, res)
|
|
1060
1052
|
error_logged = true
|
|
1061
1053
|
}
|
|
1062
1054
|
this.trades[exchange] = _.omit(this.trades[exchange], [pair])
|
|
1063
1055
|
} else if (res.error !== 'opening' && !error_logged) {
|
|
1064
|
-
|
|
1056
|
+
console.error('%s all trades: %j', exchange, res)
|
|
1065
1057
|
error_logged = true
|
|
1066
1058
|
}
|
|
1067
1059
|
}
|
|
@@ -1159,10 +1151,10 @@ module.exports = class Orders {
|
|
|
1159
1151
|
if (res.success) {
|
|
1160
1152
|
_.set(this.query_order, [exchange, pair], { query_order: res.body, update_time: new Date() })
|
|
1161
1153
|
} else if (_.get(this.query_order, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].query_order) {
|
|
1162
|
-
|
|
1154
|
+
console.log('%s %s query_order expired: %j', exchange, pair, res)
|
|
1163
1155
|
this.query_order[exchange] = _.omit(this.query_order[exchange], [pair])
|
|
1164
1156
|
} else if (res.error !== 'opening') {
|
|
1165
|
-
|
|
1157
|
+
console.error('%s %s query_order: %j', exchange, pair, res)
|
|
1166
1158
|
}
|
|
1167
1159
|
}
|
|
1168
1160
|
let get_query_order_all = (Exchanges, exchange_pairs, interval_dict) => {
|
|
@@ -1318,7 +1310,7 @@ module.exports = class Orders {
|
|
|
1318
1310
|
*/
|
|
1319
1311
|
throttled_process_rates()
|
|
1320
1312
|
} else if (_.get(this.rates, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].rates) {
|
|
1321
|
-
|
|
1313
|
+
console.log('%s %s rates expired: %j', exchange, pair, res)
|
|
1322
1314
|
this.rates[exchange] = _.omit(this.rates[exchange], [pair])
|
|
1323
1315
|
this.rates_observable.notify()
|
|
1324
1316
|
}
|
|
@@ -1398,7 +1390,7 @@ module.exports = class Orders {
|
|
|
1398
1390
|
if (res.success) {
|
|
1399
1391
|
_.set(this.market_history, [exchange, pair], { data: res.body, update_time: new Date() })
|
|
1400
1392
|
} else if (_.get(this.market_history, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].market_history) {
|
|
1401
|
-
|
|
1393
|
+
console.log('%s %s market_history expired: %j', exchange, pair, res)
|
|
1402
1394
|
this.market_history[exchange] = _.omit(this.market_history[exchange], [pair])
|
|
1403
1395
|
}
|
|
1404
1396
|
}
|
|
@@ -1439,7 +1431,7 @@ module.exports = class Orders {
|
|
|
1439
1431
|
_.set(this.ohlcv, [exchange, pair], { data: res.body, update_time: new Date() })
|
|
1440
1432
|
}
|
|
1441
1433
|
} else if (_.get(this.ohlcv, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].ohlcv) {
|
|
1442
|
-
|
|
1434
|
+
console.log('%s %s ohlcv expired: %j', exchange, pair, res)
|
|
1443
1435
|
this.ohlcv[exchange] = _.omit(this.ohlcv[exchange], [pair])
|
|
1444
1436
|
}
|
|
1445
1437
|
}
|