@icgio/icg-exchanges-wrapper 1.9.101 → 1.9.102
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 +118 -64
- package/package.json +1 -1
package/middleware/orders.js
CHANGED
|
@@ -41,12 +41,19 @@ const MARKET_HISTORY_DEFAULT_TIME = 10 * 60 * 1000
|
|
|
41
41
|
const TRADES_DEFAULT_TIME = 12 * 60 * 60 * 1000
|
|
42
42
|
|
|
43
43
|
module.exports = class Orders {
|
|
44
|
-
constructor(
|
|
44
|
+
constructor(exchange_basecur_curs_dict, cd, settings) {
|
|
45
|
+
let { exchange_basecur_curs, exchange_basecur_curs_rates_only } = exchange_basecur_curs_dict
|
|
45
46
|
this.exchange_basecur_curs = exchange_basecur_curs
|
|
46
|
-
let { exchange_pairs,
|
|
47
|
+
let { exchange_pairs, pair_exchanges } = utils.exchange_basecur_curs_converter(exchange_basecur_curs)
|
|
48
|
+
this.exchange_basecur_curs_rates_only = exchange_basecur_curs_rates_only
|
|
49
|
+
let { exchange_pairs: exchange_pairs_rates_only, pair_exchanges: pair_exchanges_rates_only } = utils.exchange_basecur_curs_converter(exchange_basecur_curs_rates_only)
|
|
47
50
|
this.exchange_pairs = exchange_pairs
|
|
48
51
|
this.pair_exchanges = pair_exchanges
|
|
52
|
+
this.exchange_pairs_rates_only = exchange_pairs_rates_only
|
|
53
|
+
this.pair_exchanges_rates_only = pair_exchanges_rates_only
|
|
54
|
+
this.rates_only_exchanges = pair_exchanges_rates_only
|
|
49
55
|
this.Exchanges = {}
|
|
56
|
+
this.ExchangesRatesOnly = {}
|
|
50
57
|
this.settings = settings || {}
|
|
51
58
|
this.ws_enabled = settings.ws_enabled || false
|
|
52
59
|
this.interval_dict = settings.interval_dict || {}
|
|
@@ -103,6 +110,9 @@ module.exports = class Orders {
|
|
|
103
110
|
this.interval_dict[exchange].market_history = this.interval_dict[exchange].market_history || 2 * 1000
|
|
104
111
|
}
|
|
105
112
|
}
|
|
113
|
+
for (let exchange in this.exchange_basecur_curs_rates_only) {
|
|
114
|
+
this.ExchangesRatesOnly[exchange] = new icg_exchanges[_.upperFirst(exchange)]('', '', STANDARD_LIMITS)
|
|
115
|
+
}
|
|
106
116
|
;(this.balances = {}), (this.open_orders = {}), (this.trades = {}), (this.rates = {}), (this.market_history = {}), (this.cmc_rates = {})
|
|
107
117
|
;(this.rates_combined_all = {}),
|
|
108
118
|
(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance = {}),
|
|
@@ -611,14 +621,18 @@ module.exports = class Orders {
|
|
|
611
621
|
let pair = cur + base_cur
|
|
612
622
|
let max_cur_amount = cur_balance - _.get(this.min_reserve_balance, [exchange, cur], 0)
|
|
613
623
|
let max_base_cur_amount = base_cur_balance - _.get(this.min_reserve_balance, [exchange, base_cur], 0)
|
|
614
|
-
if (max_cur_amount > 0
|
|
624
|
+
if (max_cur_amount > 0) {
|
|
615
625
|
} else {
|
|
616
|
-
|
|
626
|
+
if (cur_balance > 0) {
|
|
627
|
+
console.error('balance low', exchange, cur, cur_balance, this.min_reserve_balance[exchange][cur])
|
|
628
|
+
}
|
|
617
629
|
max_cur_amount = 0
|
|
618
630
|
}
|
|
619
|
-
if (max_base_cur_amount > 0
|
|
631
|
+
if (max_base_cur_amount > 0) {
|
|
620
632
|
} else {
|
|
621
|
-
|
|
633
|
+
if (base_cur_balance > 0) {
|
|
634
|
+
console.error('balance low', exchange, cur, base_cur_balance, this.min_reserve_balance[exchange][base_cur])
|
|
635
|
+
}
|
|
622
636
|
max_base_cur_amount = 0
|
|
623
637
|
}
|
|
624
638
|
_.set(this.buy_sell_limits_against_reserve_balance, [exchange, pair], {
|
|
@@ -971,44 +985,49 @@ module.exports = class Orders {
|
|
|
971
985
|
get_trades_all(this.Exchanges, this.exchange_pairs, this.interval_dict)
|
|
972
986
|
}
|
|
973
987
|
check_rates() {
|
|
974
|
-
let
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
988
|
+
let process_rates = () => {
|
|
989
|
+
for (let pair in this.pair_exchanges) {
|
|
990
|
+
let orderbook_all = {},
|
|
991
|
+
orderbook_all_with_buy_sell_limits_against_reserve_balance = {},
|
|
992
|
+
orderbook_major = {},
|
|
993
|
+
orderbook_major_temp = {},
|
|
994
|
+
orderbook_good_with_buy_sell_limits_against_reserve_balance = {},
|
|
995
|
+
orderbook_good_temp = {}
|
|
996
|
+
let exchanges_list = _.concat(this.pair_exchanges[pair], this.pair_exchanges_rates_only[pair])
|
|
997
|
+
for (let exchange of exchanges_list) {
|
|
998
|
+
if (_.get(this.rates, [exchange, pair, 'asks'], []).length > 0 && _.get(this.rates, [exchange, pair, 'bids'], []).length > 0) {
|
|
999
|
+
orderbook_all[exchange] = this.rates[exchange][pair]
|
|
1000
|
+
} else {
|
|
1001
|
+
orderbook_all = _.omit(orderbook_all, [exchange])
|
|
1002
|
+
}
|
|
1003
|
+
if (_.get(this.rates, [exchange, pair, 'asks'], []).length > 0 && _.get(this.rates, [exchange, pair, 'bids'], []).length > 0) {
|
|
1004
|
+
let rates = JSON.parse(JSON.stringify(this.rates[exchange][pair]))
|
|
1005
|
+
|
|
1006
|
+
let major_rates_only_exchanges_length = this.major_exchanges[pair].length + this.rates_only_exchanges[pair].length
|
|
1007
|
+
if (this.major_exchanges[pair] && _.includes(this.major_exchanges[pair], exchange)) {
|
|
1008
|
+
orderbook_major_temp[exchange] = rates
|
|
1009
|
+
if (_.keys(orderbook_major_temp).length === major_rates_only_exchanges_length) {
|
|
1010
|
+
orderbook_major = orderbook_major_temp
|
|
1011
|
+
}
|
|
1012
|
+
} else if (this.rates_only_exchanges[pair] && _.includes(this.rates_only_exchanges[pair], exchange)) {
|
|
1013
|
+
orderbook_major_temp[exchange] = rates
|
|
1014
|
+
if (_.keys(orderbook_major_temp).length === major_rates_only_exchanges_length) {
|
|
1015
|
+
orderbook_major = orderbook_major_temp
|
|
1016
|
+
}
|
|
991
1017
|
}
|
|
992
1018
|
|
|
993
|
-
if (
|
|
994
|
-
|
|
995
|
-
_.get(this.rates, [exchange, pair, 'bids'], []).length > 0 &&
|
|
1019
|
+
if (_.includes(this.pair_exchanges_rates_only[pair], exchange)) {
|
|
1020
|
+
} else if (
|
|
996
1021
|
_.get(this.buy_sell_limits_against_reserve_balance, [exchange, pair, 'buy']) &&
|
|
997
1022
|
_.get(this.buy_sell_limits_against_reserve_balance, [exchange, pair, 'sell'])
|
|
998
1023
|
) {
|
|
999
|
-
let rates = JSON.parse(JSON.stringify(this.rates[exchange][pair]))
|
|
1000
1024
|
let rates_with_buy_sell_limits_against_reserve_balance = {
|
|
1001
1025
|
asks: utils.sum_position_orderbook(rates.asks, this.buy_sell_limits_against_reserve_balance[exchange][pair].buy),
|
|
1002
1026
|
bids: utils.vol_position_orderbook(rates.bids, this.buy_sell_limits_against_reserve_balance[exchange][pair].sell),
|
|
1003
1027
|
}
|
|
1004
1028
|
orderbook_all_with_buy_sell_limits_against_reserve_balance[exchange] = rates_with_buy_sell_limits_against_reserve_balance
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
if (_.keys(orderbook_major_temp).length === this.major_exchanges[pair].length) {
|
|
1008
|
-
orderbook_major = orderbook_major_temp
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
if (!_.includes(this.no_arb_exchanges, exchange)) {
|
|
1029
|
+
|
|
1030
|
+
if (!_.includes(this.no_arb_exchanges, exchange) || !this.exchange_pairs_rates_only[exchange]) {
|
|
1012
1031
|
orderbook_good_temp[exchange] = rates_with_buy_sell_limits_against_reserve_balance
|
|
1013
1032
|
if (_.keys(orderbook_good_temp).length === this.pair_exchanges[pair].length - this.no_arb_exchanges.length) {
|
|
1014
1033
|
orderbook_good_with_buy_sell_limits_against_reserve_balance = orderbook_good_temp
|
|
@@ -1021,46 +1040,57 @@ module.exports = class Orders {
|
|
|
1021
1040
|
orderbook_good_with_buy_sell_limits_against_reserve_balance = _.omit(orderbook_good_with_buy_sell_limits_against_reserve_balance, [exchange])
|
|
1022
1041
|
orderbook_good_temp = _.omit(orderbook_good_temp, [exchange])
|
|
1023
1042
|
}
|
|
1043
|
+
} else {
|
|
1044
|
+
orderbook_major = _.omit(orderbook_major, [exchange])
|
|
1045
|
+
orderbook_major_temp = _.omit(orderbook_major_temp, [exchange])
|
|
1024
1046
|
}
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1047
|
+
}
|
|
1048
|
+
if (_.keys(orderbook_all).length > 0) {
|
|
1049
|
+
let o = utils.combine_orderbooks(orderbook_all, 100)
|
|
1050
|
+
if (o.asks.length > 0 && o.bids.length > 0) {
|
|
1051
|
+
_.set(this.rates_combined_all, [pair], o)
|
|
1052
|
+
} else {
|
|
1053
|
+
this.rates_combined_all = _.omit(this.rates_combined_all, [pair])
|
|
1032
1054
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1055
|
+
}
|
|
1056
|
+
if (_.keys(orderbook_all_with_buy_sell_limits_against_reserve_balance).length > 0) {
|
|
1057
|
+
let o = utils.combine_orderbooks(orderbook_all_with_buy_sell_limits_against_reserve_balance, 100)
|
|
1058
|
+
if (o.asks.length > 0 && o.bids.length > 0) {
|
|
1059
|
+
_.set(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair], o)
|
|
1060
|
+
} else {
|
|
1061
|
+
this.rates_combined_all_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair])
|
|
1040
1062
|
}
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1063
|
+
}
|
|
1064
|
+
if (_.keys(orderbook_major).length > 0) {
|
|
1065
|
+
let o = utils.combine_orderbooks(orderbook_major, 100)
|
|
1066
|
+
if (o.asks.length > 0 && o.bids.length > 0) {
|
|
1067
|
+
_.set(this.rates_combined_major, [pair], o)
|
|
1068
|
+
} else {
|
|
1069
|
+
this.rates_combined_major = _.omit(this.rates_combined_major, [pair])
|
|
1048
1070
|
}
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1071
|
+
}
|
|
1072
|
+
if (_.keys(orderbook_good_with_buy_sell_limits_against_reserve_balance).length > 0) {
|
|
1073
|
+
let o = utils.combine_orderbooks(orderbook_good_with_buy_sell_limits_against_reserve_balance, 100)
|
|
1074
|
+
if (o.asks.length > 0 && o.bids.length > 0) {
|
|
1075
|
+
_.set(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair], o)
|
|
1076
|
+
} else {
|
|
1077
|
+
this.rates_combined_good_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair])
|
|
1056
1078
|
}
|
|
1057
1079
|
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
let rates_handler = (exchange, pair, res) => {
|
|
1083
|
+
if (res.success) {
|
|
1084
|
+
res.body.asks = res.body.asks.slice(0, RATES_LENGTH_MAX)
|
|
1085
|
+
res.body.bids = res.body.bids.slice(0, RATES_LENGTH_MAX)
|
|
1086
|
+
_.set(this.rates, [exchange, pair], Object.assign(res.body, { update_time: new Date() }))
|
|
1087
|
+
process_rates()
|
|
1058
1088
|
} else if (_.get(this.rates, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].rates) {
|
|
1059
1089
|
this.info_log('%s %s rates expired: %j', exchange, pair, res)
|
|
1060
1090
|
this.rates[exchange] = _.omit(this.rates[exchange], [pair])
|
|
1061
1091
|
}
|
|
1062
1092
|
}
|
|
1063
|
-
let get_rates = (Exchanges, exchange_pairs, interval_dict) => {
|
|
1093
|
+
let get_rates = (Exchanges, ExchangesRatesOnly, exchange_pairs, exchange_pairs_rates_only, interval_dict) => {
|
|
1064
1094
|
for (let exchange in Exchanges) {
|
|
1065
1095
|
if (Exchanges[exchange].ws_market) {
|
|
1066
1096
|
setInterval(() => {
|
|
@@ -1085,8 +1115,32 @@ module.exports = class Orders {
|
|
|
1085
1115
|
}
|
|
1086
1116
|
}
|
|
1087
1117
|
}
|
|
1118
|
+
for (let exchange in ExchangesRatesOnly) {
|
|
1119
|
+
if (ExchangesRatesOnly[exchange].ws_market) {
|
|
1120
|
+
setInterval(() => {
|
|
1121
|
+
if (!this.ws_market_first_time[exchange]) {
|
|
1122
|
+
ExchangesRatesOnly[exchange].ws_market(exchange_pairs[exchange])
|
|
1123
|
+
this.ws_market_first_time[exchange] = true
|
|
1124
|
+
} else {
|
|
1125
|
+
for (let pair of exchange_pairs[exchange]) {
|
|
1126
|
+
ExchangesRatesOnly[exchange].rate_ws(pair, (res) => {
|
|
1127
|
+
rates_handler(exchange, pair, res)
|
|
1128
|
+
})
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}, interval_dict[exchange].rates)
|
|
1132
|
+
} else {
|
|
1133
|
+
for (let pair of exchange_pairs_rates_only[exchange]) {
|
|
1134
|
+
setInterval(() => {
|
|
1135
|
+
ExchangesRatesOnly[exchange].rate(pair, RATES_LENGTH_DEFAULT, (res) => {
|
|
1136
|
+
rates_handler(exchange, pair, res)
|
|
1137
|
+
})
|
|
1138
|
+
}, interval_dict[exchange].rates)
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1088
1142
|
}
|
|
1089
|
-
get_rates(this.Exchanges, this.exchange_pairs, this.interval_dict)
|
|
1143
|
+
get_rates(this.Exchanges, this.ExchangesRatesOnly, this.exchange_pairs, this.exchange_pairs_rates_only, this.interval_dict)
|
|
1090
1144
|
}
|
|
1091
1145
|
check_market_history() {
|
|
1092
1146
|
let market_history_handler = (exchange, pair, res) => {
|