@icgio/icg-utils 1.9.44 → 1.9.46
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 +24 -28
- package/lib/utils.js +135 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ const limiter = new RateLimiter({
|
|
|
52
52
|
max_concurrent: 5,
|
|
53
53
|
interval_concurrent: 1000,
|
|
54
54
|
abandon_tx: false,
|
|
55
|
-
only_same_args: false
|
|
55
|
+
only_same_args: false,
|
|
56
56
|
})
|
|
57
57
|
|
|
58
58
|
// Process function with rate limiting
|
|
@@ -74,14 +74,10 @@ const position = utils.sum_position(orderbook, target_sum)
|
|
|
74
74
|
const combined = utils.combine_orderbooks(orderbooks_dict, depth)
|
|
75
75
|
|
|
76
76
|
// Find arbitrage opportunities
|
|
77
|
-
const opportunities = utils.arb_opportunities_from_combined_orderbook(
|
|
78
|
-
combined_orderbook,
|
|
79
|
-
profit_percentage,
|
|
80
|
-
amount_cap
|
|
81
|
-
)
|
|
77
|
+
const opportunities = utils.arb_opportunities_from_combined_orderbook(combined_orderbook, profit_percentage, amount_cap)
|
|
82
78
|
|
|
83
79
|
// Parse trading pair
|
|
84
|
-
const [base, quote] = utils.parse_pair('BTCUSDT')
|
|
80
|
+
const [base, quote] = utils.parse_pair('BTCUSDT') // ['BTC', 'USDT']
|
|
85
81
|
```
|
|
86
82
|
|
|
87
83
|
### Observable
|
|
@@ -152,35 +148,35 @@ icg-utils/
|
|
|
152
148
|
|
|
153
149
|
### Orderbook Processing
|
|
154
150
|
|
|
155
|
-
| Function
|
|
156
|
-
|
|
157
|
-
| `vol_position`
|
|
158
|
-
| `sum_position`
|
|
159
|
-
| `price_position`
|
|
160
|
-
| `level_position`
|
|
161
|
-
| `combine_orderbooks`
|
|
162
|
-
| `arb_opportunities_from_combined_orderbook` | Find arbitrage opportunities
|
|
151
|
+
| Function | Description |
|
|
152
|
+
| ------------------------------------------- | ---------------------------------------- |
|
|
153
|
+
| `vol_position` | Find position in orderbook by volume |
|
|
154
|
+
| `sum_position` | Find position in orderbook by quote sum |
|
|
155
|
+
| `price_position` | Find position in orderbook by price |
|
|
156
|
+
| `level_position` | Find position by orderbook level |
|
|
157
|
+
| `combine_orderbooks` | Merge orderbooks from multiple exchanges |
|
|
158
|
+
| `arb_opportunities_from_combined_orderbook` | Find arbitrage opportunities |
|
|
163
159
|
|
|
164
160
|
### General Utilities
|
|
165
161
|
|
|
166
|
-
| Function
|
|
167
|
-
|
|
168
|
-
| `parse_pair`
|
|
169
|
-
| `reverse_pair`
|
|
170
|
-
| `round`
|
|
171
|
-
| `ordered_dict`
|
|
162
|
+
| Function | Description |
|
|
163
|
+
| -------------------- | ------------------------------------ |
|
|
164
|
+
| `parse_pair` | Parse trading pair into base/quote |
|
|
165
|
+
| `reverse_pair` | Reverse a trading pair |
|
|
166
|
+
| `round` | Round number to precision |
|
|
167
|
+
| `ordered_dict` | Sorted dictionary for orderbooks |
|
|
172
168
|
| `number_with_commas` | Format numbers with comma separators |
|
|
173
169
|
|
|
174
170
|
## Dependencies
|
|
175
171
|
|
|
176
|
-
| Package
|
|
177
|
-
|
|
178
|
-
| `@influxdata/influxdb-client`
|
|
172
|
+
| Package | Purpose |
|
|
173
|
+
| ---------------------------------- | ------------------------ |
|
|
174
|
+
| `@influxdata/influxdb-client` | InfluxDB v2 client |
|
|
179
175
|
| `@influxdata/influxdb-client-apis` | InfluxDB management APIs |
|
|
180
|
-
| `lodash`
|
|
181
|
-
| `needle`
|
|
182
|
-
| `winston`
|
|
183
|
-
| `csv-parse`
|
|
176
|
+
| `lodash` | Utility library |
|
|
177
|
+
| `needle` | HTTP client |
|
|
178
|
+
| `winston` | Logging framework |
|
|
179
|
+
| `csv-parse` | CSV parsing |
|
|
184
180
|
|
|
185
181
|
## Code Style
|
|
186
182
|
|
package/lib/utils.js
CHANGED
|
@@ -260,7 +260,7 @@ function cut_orderbook(input_array, level_target) {
|
|
|
260
260
|
}
|
|
261
261
|
|
|
262
262
|
function level_position(input_array, level_target) {
|
|
263
|
-
if (input_array.length
|
|
263
|
+
if (input_array.length < level_target) {
|
|
264
264
|
level_target--
|
|
265
265
|
return level_position(input_array, level_target)
|
|
266
266
|
}
|
|
@@ -826,7 +826,103 @@ function number_with_commas(x) {
|
|
|
826
826
|
return parts.join('.')
|
|
827
827
|
}
|
|
828
828
|
|
|
829
|
-
|
|
829
|
+
// =============================================================================
|
|
830
|
+
// Exchange Key Utilities
|
|
831
|
+
//
|
|
832
|
+
// Canonical exchange *account* keys are: "<base>_<index>", e.g. "mexc_1".
|
|
833
|
+
// We still support reading legacy base-only keys ("mexc") for migration/compat,
|
|
834
|
+
// but new configs should use account keys everywhere.
|
|
835
|
+
// =============================================================================
|
|
836
|
+
|
|
837
|
+
const EXCHANGE_ACCOUNT_KEY_RE = /^([a-z0-9]+)_(\d+)$/
|
|
838
|
+
|
|
839
|
+
function normalize_exchange_key(exchange) {
|
|
840
|
+
return String(exchange || '').trim().toLowerCase()
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function is_exchange_account_key(exchange) {
|
|
844
|
+
return EXCHANGE_ACCOUNT_KEY_RE.test(normalize_exchange_key(exchange))
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function parse_exchange_account_key(exchange_account_key) {
|
|
848
|
+
const key = normalize_exchange_key(exchange_account_key)
|
|
849
|
+
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
850
|
+
if (!m) {
|
|
851
|
+
throw new Error(`Invalid exchange account key "${exchange_account_key}" (expected "base_index", e.g. "mexc_1")`)
|
|
852
|
+
}
|
|
853
|
+
const base = m[1]
|
|
854
|
+
const index = Number(m[2])
|
|
855
|
+
if (!Number.isInteger(index) || index <= 0) {
|
|
856
|
+
throw new Error(`Invalid exchange account index in "${exchange_account_key}"`)
|
|
857
|
+
}
|
|
858
|
+
return { base, index }
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function format_exchange_account_key(base, index) {
|
|
862
|
+
const b = normalize_exchange_key(base)
|
|
863
|
+
if (!b) throw new Error('format_exchange_account_key: base is required')
|
|
864
|
+
if (!Number.isInteger(index) || index <= 0) {
|
|
865
|
+
throw new Error(`format_exchange_account_key: index must be a positive integer (got ${index})`)
|
|
866
|
+
}
|
|
867
|
+
return `${b}_${index}`
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function exchange_base_from_key(exchange_key) {
|
|
871
|
+
const key = normalize_exchange_key(exchange_key)
|
|
872
|
+
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
873
|
+
if (m) return m[1]
|
|
874
|
+
// Legacy base-only key (or other legacy formats): treat whole key as base.
|
|
875
|
+
const underscore = key.indexOf('_')
|
|
876
|
+
return underscore === -1 ? key : key.slice(0, underscore)
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function group_exchange_keys_by_base(exchange_keys) {
|
|
880
|
+
const grouped = {}
|
|
881
|
+
;(exchange_keys || []).forEach((k) => {
|
|
882
|
+
const key = normalize_exchange_key(k)
|
|
883
|
+
if (!key) return
|
|
884
|
+
const base = exchange_base_from_key(key)
|
|
885
|
+
if (!grouped[base]) grouped[base] = []
|
|
886
|
+
grouped[base].push(key)
|
|
887
|
+
})
|
|
888
|
+
// Stable deterministic ordering.
|
|
889
|
+
for (const base in grouped) {
|
|
890
|
+
grouped[base] = _.uniq(grouped[base]).sort()
|
|
891
|
+
}
|
|
892
|
+
return grouped
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function pick_primary_key_for_base(keys_for_base, exchange_roles) {
|
|
896
|
+
const keys = (keys_for_base || []).map(normalize_exchange_key).filter(Boolean)
|
|
897
|
+
if (keys.length === 0) return null
|
|
898
|
+
|
|
899
|
+
if (exchange_roles && typeof exchange_roles === 'object') {
|
|
900
|
+
const primaries = keys.filter((k) => exchange_roles[normalize_exchange_key(k)] === 'primary')
|
|
901
|
+
if (primaries.length === 1) return primaries[0]
|
|
902
|
+
if (primaries.length > 1) return primaries.sort()[0]
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// Legacy heuristic: base-only key (no underscore) is primary if present.
|
|
906
|
+
const base_only = keys.find((k) => k.indexOf('_') === -1)
|
|
907
|
+
if (base_only) return base_only
|
|
908
|
+
|
|
909
|
+
// Otherwise, prefer lowest account index if possible.
|
|
910
|
+
const parsed = []
|
|
911
|
+
for (const k of keys) {
|
|
912
|
+
if (is_exchange_account_key(k)) {
|
|
913
|
+
const { index } = parse_exchange_account_key(k)
|
|
914
|
+
parsed.push({ k, index })
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
if (parsed.length > 0) {
|
|
918
|
+
return _.sortBy(parsed, ['index', 'k'])[0].k
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// Fallback: stable first.
|
|
922
|
+
return keys.sort()[0]
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur, opts = {}) {
|
|
830
926
|
let exchange_pairs = {},
|
|
831
927
|
exchange_curs = {},
|
|
832
928
|
exchange_cur_pairs = {}
|
|
@@ -864,36 +960,47 @@ function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
|
|
|
864
960
|
pair_exchanges[pair].push(exchange)
|
|
865
961
|
})
|
|
866
962
|
}
|
|
867
|
-
//
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
963
|
+
// Primary-only (aka "without affiliates") sets.
|
|
964
|
+
//
|
|
965
|
+
// Legacy behavior: keys without "_" are treated as primary.
|
|
966
|
+
// Canonical behavior: use exchange_roles to pick exactly 1 primary per base.
|
|
967
|
+
const exchange_roles = opts && opts.exchange_roles ? opts.exchange_roles : null
|
|
968
|
+
|
|
969
|
+
const exchange_keys = Object.keys(exchange_pairs)
|
|
970
|
+
const by_base = group_exchange_keys_by_base(exchange_keys)
|
|
971
|
+
|
|
972
|
+
const exchange_primary_by_base = {}
|
|
973
|
+
const primary_exchange_keys = []
|
|
974
|
+
for (const base in by_base) {
|
|
975
|
+
const primary = pick_primary_key_for_base(by_base[base], exchange_roles)
|
|
976
|
+
if (primary) {
|
|
977
|
+
exchange_primary_by_base[base] = primary
|
|
978
|
+
primary_exchange_keys.push(primary)
|
|
872
979
|
}
|
|
873
980
|
}
|
|
874
|
-
|
|
981
|
+
const primary_exchange_key_set = new Set(primary_exchange_keys)
|
|
982
|
+
|
|
983
|
+
let exchange_pairs_without_affiliates = {}
|
|
875
984
|
let exchange_curs_without_affiliates = {}
|
|
876
|
-
for (
|
|
877
|
-
if (
|
|
878
|
-
|
|
879
|
-
}
|
|
985
|
+
for (const exchange of primary_exchange_keys) {
|
|
986
|
+
if (exchange_pairs[exchange]) exchange_pairs_without_affiliates[exchange] = exchange_pairs[exchange]
|
|
987
|
+
if (exchange_curs[exchange]) exchange_curs_without_affiliates[exchange] = exchange_curs[exchange]
|
|
880
988
|
}
|
|
881
|
-
|
|
989
|
+
|
|
882
990
|
let pair_exchanges_without_affiliates = {}
|
|
883
991
|
for (let pair in pair_exchanges) {
|
|
884
|
-
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) =>
|
|
992
|
+
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) => primary_exchange_key_set.has(exchange))
|
|
885
993
|
if (pair_exchanges_without_affiliates[pair].length === 0) {
|
|
886
994
|
delete pair_exchanges_without_affiliates[pair]
|
|
887
995
|
}
|
|
888
996
|
}
|
|
889
997
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
}
|
|
998
|
+
// Map primary exchange key -> affiliate exchange keys (same base, excluding primary).
|
|
999
|
+
const exchange_affiliates = {}
|
|
1000
|
+
for (const base in by_base) {
|
|
1001
|
+
const primary = exchange_primary_by_base[base]
|
|
1002
|
+
if (!primary) continue
|
|
1003
|
+
exchange_affiliates[primary] = by_base[base].filter((k) => k !== primary)
|
|
897
1004
|
}
|
|
898
1005
|
|
|
899
1006
|
// Add exchange_pairs_without_2
|
|
@@ -907,6 +1014,7 @@ function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
|
|
|
907
1014
|
pair_exchanges,
|
|
908
1015
|
pair_exchanges_without_affiliates,
|
|
909
1016
|
exchange_affiliates,
|
|
1017
|
+
exchange_primary_by_base,
|
|
910
1018
|
}
|
|
911
1019
|
}
|
|
912
1020
|
|
|
@@ -973,6 +1081,12 @@ module.exports = {
|
|
|
973
1081
|
reverse_pair,
|
|
974
1082
|
cur_dict_to_pair_list,
|
|
975
1083
|
number_with_commas,
|
|
1084
|
+
normalize_exchange_key,
|
|
1085
|
+
is_exchange_account_key,
|
|
1086
|
+
parse_exchange_account_key,
|
|
1087
|
+
format_exchange_account_key,
|
|
1088
|
+
exchange_base_from_key,
|
|
1089
|
+
group_exchange_keys_by_base,
|
|
976
1090
|
exchange_cur_quote_cur_converter,
|
|
977
1091
|
sort_keys,
|
|
978
1092
|
convert_deposit_withdrawal_chain,
|