@icgio/icg-exchanges-wrapper 1.8.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/README.md +1 -0
- package/functions/get-exchanges-balance.js +32 -0
- package/functions/get-exchanges-open-orders.js +0 -0
- package/functions/get-exchanges-rates-market-history.js +113 -0
- package/functions/get-exchanges-trades.js +0 -0
- package/index.js +16 -0
- package/middleware/account.js +102 -0
- package/middleware/orders.js +1024 -0
- package/middleware/websocket.js +69 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# icg-exchanges-wrapper
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
|
|
3
|
+
let first_time = {}, balances = {}
|
|
4
|
+
module.exports = function (Exchanges, cb) {
|
|
5
|
+
if (!Array.isArray(Exchanges)) {
|
|
6
|
+
console.error('Exchanges should be an array')
|
|
7
|
+
return
|
|
8
|
+
} else {
|
|
9
|
+
for (let Exchange of Exchanges) {
|
|
10
|
+
if (Exchange.balance_ws) {
|
|
11
|
+
if (Exchange.ws_status().trading === 'n-opened')
|
|
12
|
+
Exchange.ws_account()
|
|
13
|
+
Exchange.balance_ws(function (res) {
|
|
14
|
+
if (res.success) {
|
|
15
|
+
_.set(balances, Exchange.exchange_id, res.body)
|
|
16
|
+
} else {
|
|
17
|
+
delete balances[Exchange.exchange_id]
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
} else {
|
|
21
|
+
Exchange.balance(function (res) {
|
|
22
|
+
if (res.success) {
|
|
23
|
+
_.set(balances, Exchange.exchange_id, res.body)
|
|
24
|
+
} else {
|
|
25
|
+
delete balances[Exchange.exchange_id]
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
cb(balances)
|
|
32
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
|
|
3
|
+
let first_time = {}, checked_rates = {}, exchange_pair_rates = {}, pair_exchange_rates = {}
|
|
4
|
+
module.exports.get_exchanges_rates = function (Exchanges, exchange_pair_dict, cb, ws_enabled=true) {
|
|
5
|
+
checked_rates = {}
|
|
6
|
+
if (!Array.isArray(Exchanges)) {
|
|
7
|
+
console.error('Exchanges should be an array')
|
|
8
|
+
return
|
|
9
|
+
} else {
|
|
10
|
+
for (let Exchange of Exchanges) {
|
|
11
|
+
let Exchange_name = _.upperFirst(Exchange.name)
|
|
12
|
+
if (checked_rates[Exchange_name])
|
|
13
|
+
continue
|
|
14
|
+
checked_rates[Exchange_name] = true
|
|
15
|
+
if (!exchange_pair_dict[Exchange_name]) continue
|
|
16
|
+
if (Exchange.ws_market && ws_enabled) {
|
|
17
|
+
if (!first_time[Exchange_name]) {
|
|
18
|
+
Exchange.ws_market(exchange_pair_dict[Exchange_name])
|
|
19
|
+
first_time[Exchange_name] = true
|
|
20
|
+
} else {
|
|
21
|
+
for (let pair of exchange_pair_dict[Exchange_name]) {
|
|
22
|
+
Exchange.rate_ws(pair, function (res) {
|
|
23
|
+
if (res.success) {
|
|
24
|
+
_.set(exchange_pair_rates, [Exchange_name, pair], res.body)
|
|
25
|
+
_.set(pair_exchange_rates, [pair, Exchange_name], res.body)
|
|
26
|
+
} else {
|
|
27
|
+
if (exchange_pair_rates[Exchange_name]) {
|
|
28
|
+
delete exchange_pair_rates[Exchange_name][pair]
|
|
29
|
+
}
|
|
30
|
+
if (pair_exchange_rates[pair]) {
|
|
31
|
+
delete pair_exchange_rates[pair][Exchange_name]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
for (let pair of exchange_pair_dict[Exchange_name]) {
|
|
39
|
+
Exchange.rate(pair, 50, function (res) {
|
|
40
|
+
if (res.success) {
|
|
41
|
+
_.set(exchange_pair_rates, [Exchange_name, pair], res.body)
|
|
42
|
+
_.set(pair_exchange_rates, [pair, Exchange_name], res.body)
|
|
43
|
+
} else {
|
|
44
|
+
if (exchange_pair_rates[Exchange_name]) {
|
|
45
|
+
delete exchange_pair_rates[Exchange_name][pair]
|
|
46
|
+
}
|
|
47
|
+
if (pair_exchange_rates[pair]) {
|
|
48
|
+
delete pair_exchange_rates[pair][Exchange_name]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
cb({ exchange_pair_rates, pair_exchange_rates })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let checked_market_history = {}, exchange_pair_market_history = {}, pair_exchange_market_history = {}
|
|
60
|
+
module.exports.get_exchanges_market_history = function (Exchanges, exchange_pair_dict, timeframe_in_ms, cb) {
|
|
61
|
+
checked_market_history = {}
|
|
62
|
+
if (!Array.isArray(Exchanges)) {
|
|
63
|
+
console.error('Exchanges should be an array')
|
|
64
|
+
return
|
|
65
|
+
} else {
|
|
66
|
+
for (let Exchange of Exchanges) {
|
|
67
|
+
let Exchange_name = _.upperFirst(Exchange.name)
|
|
68
|
+
if (checked_market_history[Exchange_name])
|
|
69
|
+
continue
|
|
70
|
+
checked_market_history[Exchange_name] = true
|
|
71
|
+
if (!exchange_pair_dict[Exchange_name]) continue
|
|
72
|
+
if (Exchange.ws_market) {
|
|
73
|
+
if (!first_time[Exchange_name]) {
|
|
74
|
+
Exchange.ws_market(exchange_pair_dict[Exchange_name])
|
|
75
|
+
first_time[Exchange_name] = true
|
|
76
|
+
} else {
|
|
77
|
+
for (let pair of exchange_pair_dict[Exchange_name]) {
|
|
78
|
+
Exchange.market_history_ws(pair, timeframe_in_ms, function (res) {
|
|
79
|
+
if (res.success) {
|
|
80
|
+
_.set(exchange_pair_market_history, [Exchange_name, pair], res.body)
|
|
81
|
+
_.set(pair_exchange_market_history, [pair, Exchange_name], res.body)
|
|
82
|
+
} else {
|
|
83
|
+
if (exchange_pair_market_history[Exchange_name]) {
|
|
84
|
+
delete exchange_pair_market_history[Exchange_name][pair]
|
|
85
|
+
}
|
|
86
|
+
if (pair_exchange_market_history[pair]) {
|
|
87
|
+
delete pair_exchange_market_history[pair][Exchange_name]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} else if (Exchange.market_history) {
|
|
94
|
+
for (let pair of exchange_pair_dict[Exchange_name]) {
|
|
95
|
+
Exchange.market_history(pair, timeframe_in_ms, function (res) {
|
|
96
|
+
if (res.success) {
|
|
97
|
+
_.set(exchange_pair_market_history, [Exchange_name, pair], res.body)
|
|
98
|
+
_.set(pair_exchange_market_history, [pair, Exchange_name], res.body)
|
|
99
|
+
} else {
|
|
100
|
+
if (exchange_pair_market_history[Exchange_name]) {
|
|
101
|
+
delete exchange_pair_market_history[Exchange_name][pair]
|
|
102
|
+
}
|
|
103
|
+
if (pair_exchange_market_history[pair]) {
|
|
104
|
+
delete pair_exchange_market_history[pair][Exchange_name]
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
cb({ exchange_pair_market_history, pair_exchange_market_history })
|
|
113
|
+
}
|
|
File without changes
|
package/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const get_exchanges_balance = require('./functions/get-exchanges-balance.js')
|
|
2
|
+
const get_exchanges_open_orders = require('./functions/get-exchanges-open-orders.js')
|
|
3
|
+
const get_exchanges_rates = require('./functions/get-exchanges-rates-market-history.js').get_exchanges_rates
|
|
4
|
+
const get_exchanges_market_history = require('./functions/get-exchanges-rates-market-history.js').get_exchanges_market_history
|
|
5
|
+
const get_exchanges_trades = require('./functions/get-exchanges-trades.js')
|
|
6
|
+
|
|
7
|
+
module.exports.functions = {
|
|
8
|
+
get_exchanges_balance,
|
|
9
|
+
get_exchanges_open_orders,
|
|
10
|
+
get_exchanges_rates,
|
|
11
|
+
get_exchanges_market_history,
|
|
12
|
+
get_exchanges_trades
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports.orders = require('./middleware/orders.js')
|
|
16
|
+
module.exports.websocket = require('./middleware/websocket.js')
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
|
|
3
|
+
const USDCNY = 7.1, USDKRW = 1178, USDTHB = 30.3
|
|
4
|
+
|
|
5
|
+
module.exports = class Account {
|
|
6
|
+
constructor (initial_balances) {
|
|
7
|
+
this.initial_exchange_assets_benchmark = {}
|
|
8
|
+
this.exchange_assets_benchmark = {}
|
|
9
|
+
this.initial_assets_benchmark = {}
|
|
10
|
+
this.assets_benchmark = {}
|
|
11
|
+
for (let exchange in initial_balances) {
|
|
12
|
+
for (let cur in initial_balances[exchange].total) {
|
|
13
|
+
_.set(this.initial_exchange_assets_benchmark, [exchange, cur], initial_balances[exchange].total[cur])
|
|
14
|
+
_.set(this.initial_assets_benchmark, cur, _.get(this.initial_assets_benchmark, cur, 0) + initial_balances[exchange].total[cur])
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
this.exchange_assets_benchmark = _.cloneDeep(this.initial_exchange_assets_benchmark)
|
|
18
|
+
this.assets_benchmark = _.cloneDeep(this.initial_assets_benchmark)
|
|
19
|
+
}
|
|
20
|
+
set_exchange_assets_from_balances (balances) {
|
|
21
|
+
for (let exchange in balances) {
|
|
22
|
+
for (let cur in balances[exchange].total) {
|
|
23
|
+
_.set(this.exchange_assets_benchmark, [exchange, cur], balances[exchange].total[cur])
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get_exchange_position (balances, initial=true) {
|
|
28
|
+
let exchange_assets_benchmark = initial ? this.initial_exchange_assets_benchmark : this.exchange_assets_benchmark
|
|
29
|
+
let exchange_position = {}
|
|
30
|
+
for (let exchange in exchange_assets_benchmark) {
|
|
31
|
+
for (let cur in exchange_assets_benchmark[exchange]) {
|
|
32
|
+
if (!_.get(balances, [exchange, 'total']))
|
|
33
|
+
console.error('get_exchange_position error: ', exchange, balances[exchange])
|
|
34
|
+
if (_.get(balances, [exchange, 'total', cur])) {
|
|
35
|
+
let diff = balances[exchange].total[cur] - exchange_assets_benchmark[exchange][cur]
|
|
36
|
+
_.set(exchange_position, [exchange, cur, 'amount'], diff)
|
|
37
|
+
_.set(exchange_position, [exchange, cur, 'percentage'], diff / exchange_assets_benchmark[exchange][cur])
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return exchange_position
|
|
42
|
+
}
|
|
43
|
+
get_position (balances, initial=true) {
|
|
44
|
+
let exchange_position = this.get_exchange_position(balances, initial)
|
|
45
|
+
let position = {}
|
|
46
|
+
for (let exchange in exchange_position) {
|
|
47
|
+
for (let cur in exchange_position[exchange]) {
|
|
48
|
+
if (!position[cur]) {
|
|
49
|
+
_.set(position, [cur, 'amount'], 0)
|
|
50
|
+
_.set(position, [cur, 'percentage'], 0)
|
|
51
|
+
}
|
|
52
|
+
position[cur].amount += exchange_position[exchange][cur].amount
|
|
53
|
+
position[cur].percentage = position[cur].amount / this.assets_benchmark[cur]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return position
|
|
57
|
+
}
|
|
58
|
+
get_exchange_profit (balances, cmc_rates, initial=true) {
|
|
59
|
+
let exchange_position = this.get_exchange_position(balances, initial)
|
|
60
|
+
let exchange_profit = {}
|
|
61
|
+
for (let exchange in exchange_position) {
|
|
62
|
+
let profit = {
|
|
63
|
+
BTC: 0
|
|
64
|
+
}
|
|
65
|
+
for (let cur in exchange_position[exchange]) {
|
|
66
|
+
if (!cmc_rates[cur]) {
|
|
67
|
+
// console.error('get_exchange_profit rates_cmc %s does not exist', cur)
|
|
68
|
+
continue
|
|
69
|
+
} else if (cmc_rates[cur].update_time < new Date() - 60 * 60 * 1000) {
|
|
70
|
+
console.error('get_exchange_profit rates_cmc %s expired', cur)
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
let position = exchange_position[exchange][cur].amount
|
|
74
|
+
if (cur === 'BTC') {
|
|
75
|
+
profit.BTC += position
|
|
76
|
+
} else if (_.includes(['USD', 'USDT', 'USDC', 'BUSD', 'PAX', 'GUSD', 'HUSD'], cur)) {
|
|
77
|
+
profit.BTC += position / cmc_rates.BTC.USD
|
|
78
|
+
} else if (_.includes(['KRW'], cur)) {
|
|
79
|
+
profit.BTC += position / (cmc_rates.BTC.USD * USDKRW)
|
|
80
|
+
} else if (_.includes(['THB'], cur)) {
|
|
81
|
+
profit.BTC += position / (cmc_rates.BTC.USD * USDTHB)
|
|
82
|
+
} else if (_.includes(['CNYT'], cur)) {
|
|
83
|
+
profit.BTC += position / (cmc_rates.BTC.USD * USDCNY)
|
|
84
|
+
} else {
|
|
85
|
+
profit.BTC += position * cmc_rates[cur].BTC
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
_.set(exchange_profit, [exchange], profit)
|
|
89
|
+
}
|
|
90
|
+
return exchange_profit
|
|
91
|
+
}
|
|
92
|
+
get_profit (balances, cmc_rates, initial=true) {
|
|
93
|
+
let exchange_profit = this.get_exchange_profit(balances, cmc_rates, initial)
|
|
94
|
+
let profit = {
|
|
95
|
+
BTC: 0,
|
|
96
|
+
}
|
|
97
|
+
for (let exchange in exchange_profit) {
|
|
98
|
+
profit.BTC += exchange_profit[exchange].BTC
|
|
99
|
+
}
|
|
100
|
+
return profit
|
|
101
|
+
}
|
|
102
|
+
}
|