@icgio/icg-utils 1.7.22 → 1.7.24

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.
Files changed (2) hide show
  1. package/lib/utils.js +124 -61
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -296,6 +296,9 @@ module.exports.vol_position = function (input_array, vol_target) {
296
296
  }
297
297
 
298
298
  module.exports.sum_position_orderbook = (input_array, sum_target) => {
299
+ if (sum_target === 0) {
300
+ return []
301
+ }
299
302
  let { sum_original, pos } = module.exports.sum_position(input_array, sum_target)
300
303
  let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
301
304
  result[pos][1] += (sum_target - sum_original) / result[pos][0]
@@ -303,53 +306,72 @@ module.exports.sum_position_orderbook = (input_array, sum_target) => {
303
306
  }
304
307
 
305
308
  module.exports.vol_position_orderbook = (input_array, vol_target) => {
309
+ if (vol_target === 0) {
310
+ return []
311
+ }
306
312
  let { vol_original, pos } = module.exports.vol_position(input_array, vol_target)
307
313
  let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
308
314
  result[pos][1] += vol_target - vol_original
309
315
  return result
310
316
  }
311
317
 
312
- // console.log(module.exports.sum_position_orderbook([[2, 1], [6, 6], [7, 7]], 0))
313
- // console.log(module.exports.vol_position_orderbook([ [ 0.00004637, 14.12 ],
314
- // [ 0.00004636, 19.37 ],
315
- // [ 0.0000463, 77.76 ],
316
- // [ 0.00004625, 17.93 ],
317
- // [ 0.00004623, 14.89 ],
318
- // [ 0.00004621, 34.25 ],
319
- // [ 0.0000462, 131.77 ],
320
- // [ 0.00004619, 24.79 ],
321
- // [ 0.00004618, 36.71 ],
322
- // [ 0.00004617, 18.36 ],
323
- // [ 0.00004616, 9 ],
324
- // [ 0.00004615, 67.22 ],
325
- // [ 0.00004614, 74.74 ],
326
- // [ 0.00004613, 31.89 ],
327
- // [ 0.00004608, 31.25 ],
328
- // [ 0.00004606, 72 ],
329
- // [ 0.00004605, 11.69 ],
330
- // [ 0.00004604, 36.68 ],
331
- // [ 0.00004601, 29.77 ],
332
- // [ 0.00004598, 11.17 ],
333
- // [ 0.00004596, 25.19 ],
334
- // [ 0.00004593, 9.01 ],
335
- // [ 0.00004592, 15.56 ],
336
- // [ 0.00004589, 15.21 ],
337
- // [ 0.00004588, 21.25 ],
338
- // [ 0.00004583, 25.19 ],
339
- // [ 0.00004582, 81.02 ],
340
- // [ 0.00004581, 24.55 ],
341
- // [ 0.0000458, 0.26 ],
342
- // [ 0.00004577, 71.32 ],
343
- // [ 0.00004576, 43.39 ],
344
- // [ 0.00004574, 41.3 ],
345
- // [ 0.00004573, 0.75 ],
346
- // [ 0.0000457, 10.84 ],
347
- // [ 0.00004567, 0.26 ],
348
- // [ 0.00004549, 239.91 ],
349
- // [ 0.00004548, 115.89 ],
350
- // [ 0.00004542, 192.91 ],
351
- // [ 0.00004512, 0.64 ],
352
- // [ 0.000045, 3.24 ] ], 0))
318
+ // console.log(
319
+ // module.exports.sum_position_orderbook(
320
+ // [
321
+ // [2, 1],
322
+ // [6, 6],
323
+ // [7, 7],
324
+ // ],
325
+ // 0
326
+ // )
327
+ // )
328
+ // console.log(
329
+ // module.exports.vol_position_orderbook(
330
+ // [
331
+ // [0.00004637, 14.12],
332
+ // [0.00004636, 19.37],
333
+ // [0.0000463, 77.76],
334
+ // [0.00004625, 17.93],
335
+ // [0.00004623, 14.89],
336
+ // [0.00004621, 34.25],
337
+ // [0.0000462, 131.77],
338
+ // [0.00004619, 24.79],
339
+ // [0.00004618, 36.71],
340
+ // [0.00004617, 18.36],
341
+ // [0.00004616, 9],
342
+ // [0.00004615, 67.22],
343
+ // [0.00004614, 74.74],
344
+ // [0.00004613, 31.89],
345
+ // [0.00004608, 31.25],
346
+ // [0.00004606, 72],
347
+ // [0.00004605, 11.69],
348
+ // [0.00004604, 36.68],
349
+ // [0.00004601, 29.77],
350
+ // [0.00004598, 11.17],
351
+ // [0.00004596, 25.19],
352
+ // [0.00004593, 9.01],
353
+ // [0.00004592, 15.56],
354
+ // [0.00004589, 15.21],
355
+ // [0.00004588, 21.25],
356
+ // [0.00004583, 25.19],
357
+ // [0.00004582, 81.02],
358
+ // [0.00004581, 24.55],
359
+ // [0.0000458, 0.26],
360
+ // [0.00004577, 71.32],
361
+ // [0.00004576, 43.39],
362
+ // [0.00004574, 41.3],
363
+ // [0.00004573, 0.75],
364
+ // [0.0000457, 10.84],
365
+ // [0.00004567, 0.26],
366
+ // [0.00004549, 239.91],
367
+ // [0.00004548, 115.89],
368
+ // [0.00004542, 192.91],
369
+ // [0.00004512, 0.64],
370
+ // [0.000045, 3.24],
371
+ // ],
372
+ // 1
373
+ // )
374
+ // )
353
375
 
354
376
  module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
355
377
  function merge_sorted(type) {
@@ -358,11 +380,19 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
358
380
  count[exchange] = 0
359
381
  }
360
382
  let result = []
383
+ // * -1 check if all exchanges are done
361
384
  while (_.sum(_.values(count)) !== _.keys(orderbooks_dict).length * -1) {
362
385
  let picked
363
386
  for (let exchange in orderbooks_dict) {
364
- if (count[exchange] === -1) continue
365
- if (_.get(orderbooks_dict, [exchange, type, count[exchange], 'length']) === 2) orderbooks_dict[exchange][type][count[exchange]].push(exchange)
387
+ if (_.get(orderbooks_dict, [exchange, type], []).length === 0) {
388
+ count[exchange] = -1 // -1 means finished
389
+ }
390
+ if (count[exchange] === -1) {
391
+ continue
392
+ }
393
+ if (_.get(orderbooks_dict, [exchange, type, count[exchange], 'length']) === 2) {
394
+ orderbooks_dict[exchange][type][count[exchange]].push(exchange)
395
+ }
366
396
  if (!picked) {
367
397
  picked = orderbooks_dict[exchange][type][count[exchange]]
368
398
  } else if (type === 'asks' && picked[0] > orderbooks_dict[exchange][type][count[exchange]][0]) {
@@ -372,12 +402,15 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
372
402
  }
373
403
  }
374
404
  result.push(picked)
375
- if (count[picked[2]] < orderbooks_dict[picked[2]][type].length - 1) {
376
- count[picked[2]] += 1
405
+ let picked_exchange = picked[2]
406
+ if (count[picked_exchange] < orderbooks_dict[picked_exchange][type].length - 1) {
407
+ count[picked_exchange] += 1
377
408
  } else {
378
- count[picked[2]] = -1
409
+ count[picked_exchange] = -1 // -1 means finished
410
+ }
411
+ if (result.length === length) {
412
+ break
379
413
  }
380
- if (result.length === length) break
381
414
  }
382
415
  return result
383
416
  }
@@ -386,20 +419,50 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
386
419
  return { asks, bids }
387
420
  }
388
421
 
389
- // console.log(module.exports.combine_orderbooks({
390
- // 'binance': {
391
- // asks: [[1, 1], [2, 2], [3, 3]],
392
- // bids: [[0.9, 0.9], [0.5, 0.5], [0.3, 0.2]]
393
- // },
394
- // 'bittrex': {
395
- // asks: [[4, 4], [5, 5], [8, 8]],
396
- // bids: [[0.6, 0.9], [0.5, 0.2], [0.2, 0.2]]
397
- // },
398
- // 'poloniex': {
399
- // asks: [[1, 1], [6, 6], [7, 7]],
400
- // bids: [[0.95, 0.9], [0.7, 0.5], [0.4, 0.2]]
401
- // }
402
- // }, 3))
422
+ // console.log(
423
+ // module.exports.combine_orderbooks(
424
+ // {
425
+ // // binance: {
426
+ // // // asks: [
427
+ // // // [1, 1],
428
+ // // // [2, 2],
429
+ // // // [3, 3],
430
+ // // // ],
431
+ // // // bids: [
432
+ // // // [0.9, 0.9],
433
+ // // // [0.5, 0.5],
434
+ // // // [0.3, 0.2],
435
+ // // // ],
436
+ // // },
437
+ // bittrex: {
438
+ // asks: [
439
+ // [4, 4],
440
+ // [5, 5],
441
+ // [8, 8],
442
+ // ],
443
+ // bids: [
444
+ // // [0.6, 0.9],
445
+ // // [0.5, 0.2],
446
+ // // [0.2, 0.2],
447
+ // ],
448
+ // },
449
+ // poloniex: {
450
+ // asks: [
451
+ // [1, 1],
452
+ // [6, 6],
453
+ // [7, 7],
454
+ // ],
455
+ // bids: [
456
+ // [0.95, 0.9],
457
+ // [0.7, 0.5],
458
+ // [0.4, 0.2],
459
+ // ],
460
+ // },
461
+ // },
462
+ // 100
463
+ // )
464
+ // )
465
+ // console.log(module.exports.combine_orderbooks({}, 100))
403
466
 
404
467
  module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0) => {
405
468
  let i = 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.22",
3
+ "version": "1.7.24",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {