@icgio/icg-utils 1.9.21 → 1.9.22
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/lib/utils.js +107 -51
- package/package.json +1 -1
package/lib/utils.js
CHANGED
|
@@ -120,7 +120,7 @@ function binary_find(search_element, is_ask, keys) {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
function ordered_dict(is_ask) {
|
|
124
124
|
let keys = [],
|
|
125
125
|
vals = {}
|
|
126
126
|
return {
|
|
@@ -221,11 +221,11 @@ module.exports.ordered_dict = function (is_ask) {
|
|
|
221
221
|
// [6, 2],
|
|
222
222
|
// [7, 1],
|
|
223
223
|
// ]
|
|
224
|
-
// let asks_dict =
|
|
224
|
+
// let asks_dict = ordered_dict(true)
|
|
225
225
|
// asks_dict.init(asks)
|
|
226
226
|
// // asks_dict.del_before(3.333)
|
|
227
227
|
// // asks_dict.del_after(5)
|
|
228
|
-
// // asks_dict =
|
|
228
|
+
// // asks_dict = ordered_dict(true)
|
|
229
229
|
|
|
230
230
|
// console.log(asks_dict.entries())
|
|
231
231
|
|
|
@@ -238,33 +238,33 @@ module.exports.ordered_dict = function (is_ask) {
|
|
|
238
238
|
// [2, 2],
|
|
239
239
|
// [1, 1],
|
|
240
240
|
// ]
|
|
241
|
-
// let bids_dict =
|
|
241
|
+
// let bids_dict = ordered_dict(false)
|
|
242
242
|
// bids_dict.init(bids)
|
|
243
243
|
// bids_dict.del_before(8)
|
|
244
244
|
// // bids_dict.del_after(7)
|
|
245
245
|
// console.log(bids_dict.entries())
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
function random_id() {
|
|
248
248
|
return _.random(100000000, 999999999)
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
-
|
|
251
|
+
function get_hhmmss() {
|
|
252
252
|
return new Date().toTimeString().split(' ')[0]
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
function round(number, precision) {
|
|
256
256
|
if (typeof number === 'string') number = parseFloat(number)
|
|
257
257
|
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
|
|
260
|
+
function cut_orderbook(input_array, level_target) {
|
|
261
261
|
return input_array.slice(level_target)
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
|
|
264
|
+
function level_position(input_array, level_target) {
|
|
265
265
|
if (input_array.length <= level_target) {
|
|
266
266
|
level_target--
|
|
267
|
-
return
|
|
267
|
+
return level_position(input_array, level_target)
|
|
268
268
|
}
|
|
269
269
|
let vol = 0,
|
|
270
270
|
price = 0,
|
|
@@ -279,7 +279,7 @@ module.exports.level_position = function (input_array, level_target) {
|
|
|
279
279
|
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, pos }
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
-
|
|
282
|
+
function price_position(input_array, price) {
|
|
283
283
|
if (input_array.length === 1) {
|
|
284
284
|
return {
|
|
285
285
|
price: input_array[0][0],
|
|
@@ -311,7 +311,7 @@ module.exports.price_position = function (input_array, price) {
|
|
|
311
311
|
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, pos }
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
-
|
|
314
|
+
function sum_position(input_array, sum_target) {
|
|
315
315
|
if (input_array.length === 1) {
|
|
316
316
|
return {
|
|
317
317
|
price: input_array[0][0],
|
|
@@ -341,7 +341,7 @@ module.exports.sum_position = function (input_array, sum_target) {
|
|
|
341
341
|
return { price, avg_price: sum / vol ? sum / vol : price, vol, sum, sum_original, pos }
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
-
|
|
344
|
+
function vol_position(input_array, vol_target) {
|
|
345
345
|
if (input_array.length === 1) {
|
|
346
346
|
return {
|
|
347
347
|
price: input_array[0][0],
|
|
@@ -373,28 +373,28 @@ module.exports.vol_position = function (input_array, vol_target) {
|
|
|
373
373
|
return { price, avg_price: sum / vol ? sum / vol : price, vol, vol_original, sum, pos }
|
|
374
374
|
}
|
|
375
375
|
|
|
376
|
-
|
|
376
|
+
function sum_position_orderbook(input_array, sum_target) {
|
|
377
377
|
if (sum_target === 0) {
|
|
378
378
|
return []
|
|
379
379
|
}
|
|
380
|
-
let { sum_original, pos } =
|
|
380
|
+
let { sum_original, pos } = sum_position(input_array, sum_target)
|
|
381
381
|
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
382
382
|
result[pos][1] += (sum_target - sum_original) / result[pos][0]
|
|
383
383
|
return result
|
|
384
384
|
}
|
|
385
385
|
|
|
386
|
-
|
|
386
|
+
function vol_position_orderbook(input_array, vol_target) {
|
|
387
387
|
if (vol_target === 0) {
|
|
388
388
|
return []
|
|
389
389
|
}
|
|
390
|
-
let { vol_original, pos } =
|
|
390
|
+
let { vol_original, pos } = vol_position(input_array, vol_target)
|
|
391
391
|
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
392
392
|
result[pos][1] += vol_target - vol_original
|
|
393
393
|
return result
|
|
394
394
|
}
|
|
395
395
|
|
|
396
396
|
// console.log(
|
|
397
|
-
//
|
|
397
|
+
// sum_position_orderbook(
|
|
398
398
|
// [
|
|
399
399
|
// [2, 1],
|
|
400
400
|
// [6, 6],
|
|
@@ -404,7 +404,7 @@ module.exports.vol_position_orderbook = (input_array, vol_target) => {
|
|
|
404
404
|
// )
|
|
405
405
|
// )
|
|
406
406
|
// console.log(
|
|
407
|
-
//
|
|
407
|
+
// vol_position_orderbook(
|
|
408
408
|
// [
|
|
409
409
|
// [0.00004637, 14.12],
|
|
410
410
|
// [0.00004636, 19.37],
|
|
@@ -451,7 +451,7 @@ module.exports.vol_position_orderbook = (input_array, vol_target) => {
|
|
|
451
451
|
// )
|
|
452
452
|
// )
|
|
453
453
|
|
|
454
|
-
|
|
454
|
+
function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
455
455
|
function merge_sorted(type) {
|
|
456
456
|
let count = {}
|
|
457
457
|
for (let exchange in orderbooks_dict) {
|
|
@@ -500,7 +500,7 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
|
|
|
500
500
|
}
|
|
501
501
|
|
|
502
502
|
// console.log(
|
|
503
|
-
//
|
|
503
|
+
// combine_orderbooks(
|
|
504
504
|
// {
|
|
505
505
|
// binance: {
|
|
506
506
|
// asks: [
|
|
@@ -542,9 +542,9 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
|
|
|
542
542
|
// 100
|
|
543
543
|
// )
|
|
544
544
|
// )
|
|
545
|
-
// console.log(
|
|
545
|
+
// console.log(combine_orderbooks({}, 100))
|
|
546
546
|
|
|
547
|
-
|
|
547
|
+
function arb_opportunities_from_combined_orderbook(orderbook_combined, profit_percentage = 0, amount_cap = 0, quote_amount_cap = 0) {
|
|
548
548
|
let i = 0,
|
|
549
549
|
j = 0,
|
|
550
550
|
ask_amount = 0,
|
|
@@ -644,7 +644,7 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
644
644
|
return opportunities
|
|
645
645
|
}
|
|
646
646
|
|
|
647
|
-
// console.log(
|
|
647
|
+
// console.log(arb_opportunities_from_combined_orderbook({
|
|
648
648
|
// asks: [ [ 0.3049, 150, 'bitget' ],
|
|
649
649
|
// [ 0.30791, 50.5556, 'bitget' ],
|
|
650
650
|
// [ 0.30792, 175.49623901, 'bitfinex' ],
|
|
@@ -657,7 +657,7 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
657
657
|
// [ 0.3027, 867.8638, 'huobi' ] ]
|
|
658
658
|
// }, 0.0004, amount_cap=1))
|
|
659
659
|
|
|
660
|
-
|
|
660
|
+
function vol_position_combined(input_array, vol_target) {
|
|
661
661
|
if (input_array.length === 1) {
|
|
662
662
|
return {
|
|
663
663
|
price: input_array[0][0],
|
|
@@ -700,7 +700,7 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
700
700
|
return { price, price_exchange, avg_price, vol, vol_exchange, sum, sum_exchange, pos, pos_exchange }
|
|
701
701
|
}
|
|
702
702
|
|
|
703
|
-
// console.log(
|
|
703
|
+
// console.log(vol_position_combined([ [ 1, 1, 'binance' ],
|
|
704
704
|
// [ 1, 1, 'poloniex' ],
|
|
705
705
|
// [ 2, 2, 'binance' ],
|
|
706
706
|
// [ 3, 3, 'binance' ],
|
|
@@ -710,7 +710,7 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
710
710
|
// [ 7, 7, 'poloniex' ],
|
|
711
711
|
// [ 8, 8, 'bittrex' ] ], 5))
|
|
712
712
|
|
|
713
|
-
|
|
713
|
+
function sort_dict(dict, mode) {
|
|
714
714
|
var items = Object.keys(dict).map(function (key) {
|
|
715
715
|
return [key, dict[key]]
|
|
716
716
|
})
|
|
@@ -724,7 +724,7 @@ module.exports.sort_dict = function (dict, mode) {
|
|
|
724
724
|
return items
|
|
725
725
|
}
|
|
726
726
|
|
|
727
|
-
|
|
727
|
+
function merge_trades_with_same_time(array) {
|
|
728
728
|
if (array.length === 1) {
|
|
729
729
|
return array
|
|
730
730
|
} else {
|
|
@@ -754,7 +754,7 @@ module.exports.merge_trades_with_same_time = function (array) {
|
|
|
754
754
|
}
|
|
755
755
|
}
|
|
756
756
|
|
|
757
|
-
|
|
757
|
+
function merge_trades_with_same_id(array) {
|
|
758
758
|
if (array.length === 1) {
|
|
759
759
|
return array
|
|
760
760
|
} else {
|
|
@@ -784,14 +784,14 @@ module.exports.merge_trades_with_same_id = function (array) {
|
|
|
784
784
|
}
|
|
785
785
|
}
|
|
786
786
|
|
|
787
|
-
|
|
787
|
+
function similar(a, b, scale) {
|
|
788
788
|
if (Math.abs(a - b) / a < scale) {
|
|
789
789
|
return true
|
|
790
790
|
}
|
|
791
791
|
return false
|
|
792
792
|
}
|
|
793
793
|
|
|
794
|
-
|
|
794
|
+
function parse_pair(pair) {
|
|
795
795
|
pair = pair.toUpperCase()
|
|
796
796
|
let time
|
|
797
797
|
;[pair, time] = _.split(pair, '_')
|
|
@@ -808,25 +808,25 @@ module.exports.parse_pair = function (pair) {
|
|
|
808
808
|
return result
|
|
809
809
|
}
|
|
810
810
|
|
|
811
|
-
|
|
812
|
-
let [cur, quote_cur] =
|
|
811
|
+
function reverse_pair(pair) {
|
|
812
|
+
let [cur, quote_cur] = parse_pair(pair)
|
|
813
813
|
return quote_cur + cur
|
|
814
814
|
}
|
|
815
815
|
|
|
816
|
-
|
|
816
|
+
function cur_dict_to_pair_list(cur_dict) {
|
|
817
817
|
return [].concat.apply(
|
|
818
818
|
[],
|
|
819
819
|
Object.entries(cur_dict).map((e) => e[1].map((j) => j + e[0]))
|
|
820
820
|
)
|
|
821
821
|
}
|
|
822
822
|
|
|
823
|
-
|
|
823
|
+
function number_with_commas(x) {
|
|
824
824
|
let parts = x.toString().split('.')
|
|
825
825
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
826
826
|
return parts.join('.')
|
|
827
827
|
}
|
|
828
828
|
|
|
829
|
-
|
|
829
|
+
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
|
|
830
830
|
let exchange_pairs = {},
|
|
831
831
|
exchange_curs = {}
|
|
832
832
|
for (let exchange in exchange_cur_quote_cur) {
|
|
@@ -846,39 +846,95 @@ module.exports.exchange_cur_quote_cur_converter = (exchange_cur_quote_cur) => {
|
|
|
846
846
|
pair_exchanges[pair].push(exchange)
|
|
847
847
|
})
|
|
848
848
|
}
|
|
849
|
-
// Add
|
|
850
|
-
let
|
|
849
|
+
// Add exchange_pairs_without_affiliates
|
|
850
|
+
let exchange_pairs_without_affiliates = {}
|
|
851
851
|
for (let exchange in exchange_pairs) {
|
|
852
852
|
if (!exchange.includes('_')) {
|
|
853
|
-
|
|
853
|
+
exchange_pairs_without_affiliates[exchange] = exchange_pairs[exchange]
|
|
854
854
|
}
|
|
855
855
|
}
|
|
856
|
-
// Add
|
|
857
|
-
let
|
|
856
|
+
// Add exchange_curs_without_affiliates
|
|
857
|
+
let exchange_curs_without_affiliates = {}
|
|
858
858
|
for (let exchange in exchange_curs) {
|
|
859
859
|
if (!exchange.includes('_')) {
|
|
860
|
-
|
|
860
|
+
exchange_curs_without_affiliates[exchange] = exchange_curs[exchange]
|
|
861
861
|
}
|
|
862
862
|
}
|
|
863
|
-
// Add
|
|
864
|
-
let
|
|
863
|
+
// Add pair_exchanges_without_affiliates
|
|
864
|
+
let pair_exchanges_without_affiliates = {}
|
|
865
865
|
for (let pair in pair_exchanges) {
|
|
866
|
-
|
|
867
|
-
if (
|
|
868
|
-
delete
|
|
866
|
+
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) => !exchange.includes('_'))
|
|
867
|
+
if (pair_exchanges_without_affiliates[pair].length === 0) {
|
|
868
|
+
delete pair_exchanges_without_affiliates[pair]
|
|
869
869
|
}
|
|
870
870
|
}
|
|
871
|
+
|
|
872
|
+
let exchange_affiliates = {}
|
|
873
|
+
for (let exchange in exchange_pairs) {
|
|
874
|
+
if (!exchange.includes('_')) {
|
|
875
|
+
exchange_affiliates[exchange] = []
|
|
876
|
+
} else {
|
|
877
|
+
exchange_affiliates[exchange.split('_')[0]].push(exchange)
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// Add exchange_pairs_without_2
|
|
871
882
|
return {
|
|
872
883
|
exchange_cur_quote_cur,
|
|
873
884
|
exchange_pairs,
|
|
874
|
-
|
|
885
|
+
exchange_pairs_without_affiliates,
|
|
875
886
|
exchange_curs,
|
|
876
|
-
|
|
887
|
+
exchange_curs_without_affiliates,
|
|
877
888
|
pair_exchanges,
|
|
878
|
-
|
|
889
|
+
pair_exchanges_without_affiliates,
|
|
890
|
+
exchange_affiliates,
|
|
879
891
|
}
|
|
880
892
|
}
|
|
881
893
|
|
|
882
|
-
|
|
894
|
+
// console.log(
|
|
895
|
+
// exchange_cur_quote_cur_converter({
|
|
896
|
+
// bitget: {
|
|
897
|
+
// USDT: ['MAK'],
|
|
898
|
+
// },
|
|
899
|
+
// bybit: {
|
|
900
|
+
// USDT: ['MAK'],
|
|
901
|
+
// },
|
|
902
|
+
// bybit_2: {
|
|
903
|
+
// USDT: ['MAK'],
|
|
904
|
+
// },
|
|
905
|
+
// bybit_3: {
|
|
906
|
+
// USDT: ['MAK'],
|
|
907
|
+
// },
|
|
908
|
+
// })
|
|
909
|
+
// )
|
|
910
|
+
|
|
911
|
+
function sort_keys(dict) {
|
|
883
912
|
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
884
913
|
}
|
|
914
|
+
|
|
915
|
+
module.exports = {
|
|
916
|
+
ordered_dict,
|
|
917
|
+
random_id,
|
|
918
|
+
get_hhmmss,
|
|
919
|
+
round,
|
|
920
|
+
cut_orderbook,
|
|
921
|
+
level_position,
|
|
922
|
+
price_position,
|
|
923
|
+
sum_position,
|
|
924
|
+
vol_position,
|
|
925
|
+
sum_position_orderbook,
|
|
926
|
+
vol_position_orderbook,
|
|
927
|
+
combine_orderbooks,
|
|
928
|
+
arb_opportunities_from_combined_orderbook,
|
|
929
|
+
vol_position_combined,
|
|
930
|
+
sort_dict,
|
|
931
|
+
merge_trades_with_same_time,
|
|
932
|
+
merge_trades_with_same_id,
|
|
933
|
+
similar,
|
|
934
|
+
parse_pair,
|
|
935
|
+
reverse_pair,
|
|
936
|
+
cur_dict_to_pair_list,
|
|
937
|
+
number_with_commas,
|
|
938
|
+
exchange_cur_quote_cur_converter,
|
|
939
|
+
sort_keys,
|
|
940
|
+
}
|