@icgio/icg-exchanges-wrapper 1.9.85 → 1.9.87

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.
@@ -13,20 +13,36 @@ forex_rate.get_forex_rate('USDTHB', (res) => {
13
13
  USDTHB = res
14
14
  })
15
15
 
16
+ const CMC_RATES_EXPIRY_TIMEOUT = 4 * 60 * 60 * 1000
17
+ const DISTRIBUTION_MIN_BENCHMARK = 0.0001
18
+
16
19
  module.exports = class Account {
17
- constructor(initial_balances) {
18
- this.initial_exchange_assets_benchmark = {}
19
- this.exchange_assets_benchmark = {}
20
- this.initial_assets_benchmark = {}
21
- this.assets_benchmark = {}
20
+ constructor(initial_balances, raw_benchmarks) {
21
+ // balances
22
+ this.initial_exchange_assets_balances = {}
23
+ this.initial_assets_balances = {}
22
24
  for (let exchange in initial_balances) {
23
25
  for (let cur in initial_balances[exchange].total) {
24
- _.set(this.initial_exchange_assets_benchmark, [exchange, cur], initial_balances[exchange].total[cur])
25
- _.set(this.initial_assets_benchmark, cur, _.get(this.initial_assets_benchmark, cur, 0) + initial_balances[exchange].total[cur])
26
+ if (initial_balances[exchange].total[cur] > 0) {
27
+ _.set(this.initial_exchange_assets_balances, [exchange, cur], initial_balances[exchange].total[cur])
28
+ _.set(this.initial_assets_balances, cur, _.get(this.initial_assets_balances, cur, 0) + initial_balances[exchange].total[cur])
29
+ }
30
+ }
31
+ }
32
+ this.non_initial_exchange_assets_balances = _.cloneDeep(this.initial_exchange_assets_balances)
33
+ this.non_initial_assets_balances = _.cloneDeep(this.initial_assets_balances)
34
+
35
+ // benchmarks
36
+ this.raw_exchange_assets_benchmarks = {}
37
+ this.raw_assets_benchmarks = {}
38
+ for (let exchange in raw_benchmarks) {
39
+ for (let cur in raw_benchmarks[exchange]) {
40
+ _.set(this.raw_exchange_assets_benchmarks, [exchange, cur], raw_benchmarks[exchange])
41
+ _.set(this.raw_assets_benchmarks, cur, _.get(this.raw_assets_benchmarks, cur, 0) + raw_benchmarks[exchange])
26
42
  }
27
43
  }
28
- this.exchange_assets_benchmark = _.cloneDeep(this.initial_exchange_assets_benchmark)
29
- this.assets_benchmark = _.cloneDeep(this.initial_assets_benchmark)
44
+ this.exchange_assets_benchmarks = _.cloneDeep(this.raw_exchange_assets_benchmarks)
45
+ this.assets_benchmarks = _.cloneDeep(this.raw_assets_benchmarks)
30
46
  }
31
47
  get_depth(rates, cmc_rates, percentages) {
32
48
  let depth = {}
@@ -47,30 +63,105 @@ module.exports = class Account {
47
63
  }
48
64
  return depth
49
65
  }
50
- set_exchange_assets_from_balances(balances) {
66
+ update_non_initial_from_balances(balances) {
51
67
  for (let exchange in balances) {
52
68
  for (let cur in balances[exchange].total) {
53
- _.set(this.exchange_assets_benchmark, [exchange, cur], balances[exchange].total[cur])
69
+ _.set(this.non_initial_exchange_assets_balances, [exchange, cur], balances[exchange].total[cur])
70
+ }
71
+ }
72
+ }
73
+ // balances
74
+ get_exchange_position__balances(balances, initial = true) {
75
+ let exchange_assets_balances = initial ? this.initial_exchange_assets_balances : this.non_initial_exchange_assets_balances
76
+ let exchange_position = {}
77
+ for (let exchange in exchange_assets_balances) {
78
+ for (let cur in exchange_assets_balances[exchange]) {
79
+ if (!_.get(balances, [exchange, 'total'])) {
80
+ console.error('get_exchange_position__balances error: ', exchange, balances[exchange])
81
+ }
82
+ if (_.get(balances, [exchange, 'total', cur]) > 0) {
83
+ let diff = balances[exchange].total[cur] - exchange_assets_balances[exchange][cur]
84
+ _.set(exchange_position, [exchange, cur, 'amount'], diff)
85
+ _.set(exchange_position, [exchange, cur, 'percentage'], ((diff / exchange_assets_balances[exchange][cur]) * 100).toPrecision(3) + '%')
86
+ }
87
+ }
88
+ }
89
+ return exchange_position
90
+ }
91
+ get_position__balances(balances, initial = true) {
92
+ let exchange_position = this.get_exchange_position__balances(balances, initial)
93
+ let position = {}
94
+ for (let exchange in exchange_position) {
95
+ for (let cur in exchange_position[exchange]) {
96
+ if (!position[cur]) {
97
+ _.set(position, [cur, 'amount'], 0)
98
+ _.set(position, [cur, 'percentage'], 0)
99
+ }
100
+ position[cur].amount += exchange_position[exchange][cur].amount
101
+ position[cur].percentage = ((position[cur].amount / this.non_initial_assets_balances[cur]) * 100).toPrecision(3) + '%'
54
102
  }
55
103
  }
104
+ return position
105
+ }
106
+ get_exchange_profit__balances(balances, cmc_rates, initial = true) {
107
+ let exchange_position = this.get_exchange_position__balances(balances, initial)
108
+ let exchange_profit = {}
109
+ for (let exchange in exchange_position) {
110
+ let profit = {
111
+ USD: 0,
112
+ }
113
+ for (let cur in exchange_position[exchange]) {
114
+ let position = exchange_position[exchange][cur].amount
115
+ if (_.includes(constants.stablecoins, cur)) {
116
+ profit.USD += position
117
+ } else if (_.includes(['KRW'], cur)) {
118
+ profit.USD += position / USDKRW
119
+ } else if (_.includes(['THB'], cur)) {
120
+ profit.USD += position / USDTHB
121
+ } else if (_.includes(['CNYT'], cur)) {
122
+ profit.USD += position / USDCNY
123
+ } else {
124
+ if (!cmc_rates[cur]) {
125
+ console.log('get_exchange_profit__balances rates_cmc %s does not exist', cur)
126
+ } else if (cmc_rates[cur].update_time < new Date().getTime() - CMC_RATES_EXPIRY_TIMEOUT) {
127
+ // some currencies like USDT are updating slow
128
+ console.log('get_exchange_profit__balances rates_cmc %s expired', cur)
129
+ }
130
+ profit.USD += position * _.get(cmc_rates, [cur, 'USD'], 0)
131
+ }
132
+ }
133
+ _.set(exchange_profit, [exchange], profit)
134
+ }
135
+ return exchange_profit
136
+ }
137
+ get_profit__balances(balances, cmc_rates, initial = true) {
138
+ let exchange_profit = this.get_exchange_profit__balances(balances, cmc_rates, initial)
139
+ let profit = {
140
+ USD: 0,
141
+ }
142
+ for (let exchange in exchange_profit) {
143
+ profit.USD += exchange_profit[exchange].USD
144
+ }
145
+ return profit
56
146
  }
57
- get_exchange_position(balances, initial = true) {
58
- let exchange_assets_benchmark = initial ? this.initial_exchange_assets_benchmark : this.exchange_assets_benchmark
147
+ // benchmarks
148
+ get_exchange_position__benchmarks(benchmarks) {
149
+ let exchange_assets_benchmarks = this.exchange_assets_benchmarks
59
150
  let exchange_position = {}
60
- for (let exchange in exchange_assets_benchmark) {
61
- for (let cur in exchange_assets_benchmark[exchange]) {
62
- if (!_.get(balances, [exchange, 'total'])) console.error('get_exchange_position error: ', exchange, balances[exchange])
63
- if (_.get(balances, [exchange, 'total', cur])) {
64
- let diff = balances[exchange].total[cur] - exchange_assets_benchmark[exchange][cur]
151
+ for (let exchange in exchange_assets_benchmarks) {
152
+ for (let cur in exchange_assets_benchmarks[exchange]) {
153
+ if (!_.get(benchmarks, [exchange])) console.error('get_exchange_position__benchmarks error: ', exchange, benchmarks[exchange])
154
+ if (_.get(benchmarks, [exchange, cur])) {
155
+ let diff = benchmarks[exchange][cur] - exchange_assets_benchmarks[exchange][cur]
65
156
  _.set(exchange_position, [exchange, cur, 'amount'], diff)
66
- _.set(exchange_position, [exchange, cur, 'percentage'], ((diff / exchange_assets_benchmark[exchange][cur]) * 100).toPrecision(3) + '%')
157
+ _.set(exchange_position, [exchange, cur, 'percentage'], ((diff / exchange_assets_benchmarks[exchange][cur]) * 100).toPrecision(3) + '%')
67
158
  }
68
159
  }
69
160
  }
70
161
  return exchange_position
71
162
  }
72
- get_position(balances, initial = true) {
73
- let exchange_position = this.get_exchange_position(balances, initial)
163
+ get_position__benchmarks(benchmarks) {
164
+ let exchange_position = this.get_exchange_position__benchmarks(benchmarks)
74
165
  let position = {}
75
166
  for (let exchange in exchange_position) {
76
167
  for (let cur in exchange_position[exchange]) {
@@ -79,13 +170,13 @@ module.exports = class Account {
79
170
  _.set(position, [cur, 'percentage'], 0)
80
171
  }
81
172
  position[cur].amount += exchange_position[exchange][cur].amount
82
- position[cur].percentage = ((position[cur].amount / this.assets_benchmark[cur]) * 100).toPrecision(3) + '%'
173
+ position[cur].percentage = ((position[cur].amount / this.assets_benchmarks[cur]) * 100).toPrecision(3) + '%'
83
174
  }
84
175
  }
85
176
  return position
86
177
  }
87
- get_exchange_profit(balances, cmc_rates, initial = true) {
88
- let exchange_position = this.get_exchange_position(balances, initial)
178
+ get_exchange_profit__benchmarks(benchmarks, cmc_rates) {
179
+ let exchange_position = this.get_exchange_position__benchmarks(benchmarks)
89
180
  let exchange_profit = {}
90
181
  for (let exchange in exchange_position) {
91
182
  let profit = {
@@ -103,10 +194,10 @@ module.exports = class Account {
103
194
  profit.USD += position / USDCNY
104
195
  } else {
105
196
  if (!cmc_rates[cur]) {
106
- console.log('get_exchange_profit rates_cmc %s does not exist', cur)
107
- } else if (cmc_rates[cur].update_time < new Date().getTime() - 4 * 60 * 60 * 1000) {
197
+ console.log('get_exchange_profit__benchmarks rates_cmc %s does not exist', cur)
198
+ } else if (cmc_rates[cur].update_time < new Date().getTime() - CMC_RATES_EXPIRY_TIMEOUT) {
108
199
  // some currencies like USDT are updating slow
109
- console.log('get_exchange_profit rates_cmc %s expired', cur)
200
+ console.log('get_exchange_profit__benchmarks rates_cmc %s expired', cur)
110
201
  }
111
202
  profit.USD += position * _.get(cmc_rates, [cur, 'USD'], 0)
112
203
  }
@@ -115,8 +206,8 @@ module.exports = class Account {
115
206
  }
116
207
  return exchange_profit
117
208
  }
118
- get_profit(balances, cmc_rates, initial = true) {
119
- let exchange_profit = this.get_exchange_profit(balances, cmc_rates, initial)
209
+ get_profit__benchmarks(benchmarks, cmc_rates) {
210
+ let exchange_profit = this.get_exchange_profit__benchmarks(benchmarks, cmc_rates)
120
211
  let profit = {
121
212
  USD: 0,
122
213
  }
@@ -143,7 +234,7 @@ module.exports = class Account {
143
234
  } else {
144
235
  percentage = (balances[exchange].total[cur] * _.get(cmc_rates, [cur, 'USD'], 0)) / total_in_USD
145
236
  }
146
- if (percentage > 0.0001) {
237
+ if (percentage > DISTRIBUTION_MIN_BENCHMARK) {
147
238
  _.set(exchange_distribution, [exchange, cur], (percentage * 100).toPrecision(3) + '%')
148
239
  }
149
240
  }
@@ -170,7 +261,7 @@ module.exports = class Account {
170
261
  } else {
171
262
  percentage = (balances[exchange].total[cur] * _.get(cmc_rates, [cur, 'USD'], 0)) / total_in_USD
172
263
  }
173
- if (percentage > 0.0001) {
264
+ if (percentage > DISTRIBUTION_MIN_BENCHMARK) {
174
265
  _.set(total_distribution, [cur, exchange], (percentage * 100).toPrecision(3) + '%')
175
266
  }
176
267
  }
@@ -69,9 +69,9 @@ module.exports = class Orders {
69
69
  this.f_filled_threshold = settings.f_filled_threshold || 0.99
70
70
  this.previous_orders_canceled = false
71
71
  this.previous_orders_canceled_exchange_pair = {}
72
- this.init_account_from_first_balances = settings.init_account_from_first_balances || true
73
72
  this.account_init = false
74
73
  this.cmc_curs = settings.cmc_curs || []
74
+ this.balance_benchmarks = settings.maker_2_balance_benchmark || {}
75
75
  for (let exchange in exchange_pairs) {
76
76
  let api_key_temp = cd[exchange]['api_keys'] || cd[exchange]['api_key'],
77
77
  secret_key_temp = cd[exchange]['secret_keys'] || cd[exchange]['secret_key'],
@@ -159,11 +159,11 @@ module.exports = class Orders {
159
159
  }
160
160
  this.ws_market_first_time = {}
161
161
  }
162
- init_account(initial_balances) {
163
- this.account = new Account(initial_balances)
162
+ init_account(balances, benchmarks) {
163
+ this.account = new Account(balances, benchmarks)
164
164
  }
165
- set_exchange_assets_from_balances(balances) {
166
- this.account.set_exchange_assets_from_balances(balances)
165
+ update_non_initial_from_balances(balances) {
166
+ this.account.update_non_initial_from_balances(balances)
167
167
  }
168
168
  limit_trade(exchange, pair, method, price, amount, id = uuid(), order_type = '', limiter_on = true) {
169
169
  price = round_price(_.upperFirst(exchange), pair, price)
@@ -638,7 +638,7 @@ module.exports = class Orders {
638
638
  }
639
639
  if (!this.account_init && _.keys(this.balances).length === _.keys(this.exchange_pairs).length) {
640
640
  this.account_init = true
641
- this.init_account(this.balances)
641
+ this.init_account(this.balances, this.balance_benchmarks)
642
642
  }
643
643
  } else if (_.get(this.balances, [exchange, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].balances) {
644
644
  this.info_log('%s balances expired: %j', exchange, res)
@@ -685,7 +685,7 @@ module.exports = class Orders {
685
685
  this.buy_sell_limits = _.omit(this.buy_sell_limits, [exchange])
686
686
  }
687
687
  }
688
- function get_positions(Exchanges, interval_dict) {
688
+ function get_position__balancess(Exchanges, interval_dict) {
689
689
  for (let exchange in Exchanges) {
690
690
  if (Exchanges[exchange].position_all_ws) {
691
691
  if (Exchanges[exchange].ws_status().trading === 'n-opened') Exchanges[exchange].ws_account()
@@ -720,7 +720,7 @@ module.exports = class Orders {
720
720
  }
721
721
  }
722
722
  }
723
- get_positions(this.Exchanges, this.interval_dict)
723
+ get_position__balancess(this.Exchanges, this.interval_dict)
724
724
  }
725
725
  check_open_orders() {
726
726
  const open_orders_timeframe = 12 * 60 * 60 * 1000
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.9.85",
3
+ "version": "1.9.87",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@
18
18
  "homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
19
19
  "dependencies": {
20
20
  "@icgio/icg-exchanges": "^1.27.70",
21
- "@icgio/icg-exchanges-data": "^1.6.37",
21
+ "@icgio/icg-exchanges-data": "^1.6.38",
22
22
  "@icgio/icg-utils": "^1.7.13",
23
23
  "influx": "^5.7.0",
24
24
  "lodash": "^4.17.20",