Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
1084 lines (949 loc) · 58.5 KB
/
Copy pathmain.js
File metadata and controls
1084 lines (949 loc) · 58.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* cripto merde - crime
*/
'use strict'
const ccxt = require ('ccxt');
const ansi = require ('ansicolor').nice
const asTable = require ('as-table')
const fs = require ('fs')
const log = require ('ololog').configure ({ locate: false })
const functions = require ('./functions')
var tradersFee = require ('./trading.fee.json');
var altCoinsAddesses = require('./withdraw.addresses.json');
var therocksqv = require ('./therocksqv.js')
var accounts = require ('./credentials-1.json')
//exchanges_markets load all markets by exchange
var exchanges_markets = []
var markets={}
var exchanges =[]
require('./settings.js')(); //Includes settings file.
var database=require('./db-aca.js'); //sqllite3.x
var engine = require ('./engine-aca.js')
//opportunities tracking
let fileCSV = './data/opportunities.'+sqvAmount+'.'+sqvCoin +'.csv'; //opportunities enalbed
const NULL_CRITERIA="TBD"
const log4js = require('log4js');
// log the cheese logger messages to a file, and the console ones as well.
log4js.configure( {
appenders: {
file: {
type: 'file',
filename: 'logs/crime-arbitrage.log',
maxLogSize: 10 * 1024 * 1024, // = 10Mb
numBackups: 5, // keep five backup files
compress: true, // compress the backups
encoding: 'utf-8',
pattern: 'yyyy-MM-dd-hh',
mode: 0o0640,
flags: 'w+'
},
out: {
type: 'stdout'
}
},
categories: {
default: { appenders: ['file'], level: 'debug' }
}
});
const logger = log4js.getLogger();
//CACHE
const NodeCache = require( "node-cache" );
const cache = new NodeCache( { stdTTL: 86400} );
const enableRateLimit = true
var quoteCoins = ["USD", "USDT","ETH","BTC"];
//var quoteCoins = [ "USD","EUR", "ADA", "BTG","DASH","EOS", "ETC", "ICX", "LSK", "IOTA", "NEO", "OMG", "QTUM", "SC", "TRX", "USDT", "XEM", "XLM", "XMR", "XRB", "XVG", "ZEC", "DOGE", "LTC", "BTC", "ETH", "XRP", "BCH"];
var baseCoins = [ "ADA", "BTG","DASH","EOS", "ETC", "ICX", "LSK", "IOTA", "NEO", "OMG", "QTUM", "SC", "TRX", "USDT", "XEM", "XLM", "XMR", "XRB", "XVG", "ZEC", "DOGE", "LTC", "BTC", "ETH", "XRP", "BCH"];
var bitfinexCoinsWithdraw = ['BTC','LTC','ETH','ETC','OMNI','ZEC','XMR','USD','DASH','XRP','EOS','BCH','NEO','AVT','QTUM','EDO']
var binanceCoinsNoWithdraw = ['IOTA', 'DOGE', 'XRB', 'XEM', 'SC']
var hitbtc2CoinsNoWithdraw = ['ADA', 'ICX', 'IOTA', 'NEO', 'XLM','XMR', 'XRB','TRX'] //not workin on frb 12 2018 the following altcoin: ICX, NEO, XMR, TRX. Please see withdraw.address.json
var poloniexCoinsNoWithdraw = ['ADA', 'BTG', 'EOS', 'ICX', 'IOTA', 'QTUM', 'TRX', 'XRB', 'XVG', 'ETH']
var crypropiaCoinsNoWithdraw = ['BTG', "BCH"] //BTG is BITGEM and BCH is BCash
//var quoteCoins = [ "USD", "DASH", "ZEC", "DOGE", "LTC", "BTC", "ETH", "XRP", "BCH"];
//var baseCoins = [ "DASH","EOS", "ETC", "ICX", "IOTA", "NEO", "XLM", "XMR", "XRB", "XVG", "ZEC", "DOGE", "LTC", "BTC", "ETH", "XRP", "BCH"];
//var baseCoins = [ "ADA", "EOS","ZEC", "NEO", "XEM","XLM", "LTC", "BTC", "ETH", "XRP", "BCH"];
//var quoteCoins= ["USD", "ADA", "EOS","ZEC", "NEO", "XEM","XLM", "LTC", "BTC", "ETH", "XRP", "BCH"];
log.bright.green ('Starting app...');
const request = require('request'), Promise = require("bluebird"); //request for pulling JSON from api. Bluebird for Promises.
const express = require('express'),
app = express(),
helmet = require('helmet'),
http = require('http').Server(app),
io = require('socket.io')(http); // For websocket server functionality
app.use(helmet.hidePoweredBy({setTo: 'PHP/5.4.0'}));
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/docs'));
http.listen(port, function () {
log.bright.magenta('listening on', port);
});
let coinNames = [];
let sqvWallets={}
io.on('connection', function (socket) {
log.magenta("connected")
socket.on('buyAltCoin', async function (coin,exchange_id, amount,price, side, transaction_id) {
await engine.buyAltCoin(coin,exchanges[exchange_id], amount,price, side, transaction_id);
});
socket.on('withdrawal',async function (coin,exchangeA, amount, exchangeB, side, transaction_id) {
log.magenta('hi withdrawal');
await engine.withdrawal(coin,exchanges[exchangeA], amount,exchanges[exchangeB], side, transaction_id);
});
socket.on('sellAltCoin',async function (coin,exchange_id, amount,price, side,transaction_id) {
log.magenta('hi sellAltCoin');
await engine.sellAltCoin(coin,exchanges[exchange_id], amount,price, side,transaction_id)
});
socket.emit('coinsAndMarkets', [marketNames, coinNames]);
socket.emit('results', results);
});
// deprecated
function currencyId (currency) {
if (currency === 'Bitmark')
return 'BTM';
if (currency === 'XLM')
return 'STR';
return currency;
}
function initAnalyticsFile() {
if (COUNT_OPPORTUNITIES) {
try {
fs.unlinkSync(fileCSV,(err) => {
if (err) log.red( err.message);
log.magenta('CSV created for writing the opportunity found during the arbitrage!!!');
});
} catch (error) {
log("error during delete file");
}
fs.appendFile(fileCSV, "date, market1, market2, market1price, market2price, coin, feeTaker,feeMaker,withdrawal,deposit,quoteVolume,quoteBase,spred_wo_fee,coinToBuy,netCoinToBuy,walletAmount,futureWalletAmount,grossPriceSpread,netAmountSpread,grossPriceSpreadPerc,netAmountSpreadPerc,gainAmount,gainAmountPerc",(err) => {
if (err) log.red( err.message);
log.magenta('CSV created for writing the opportunity found during the arbitrage!!!')})
}
}
// coin_prices is an object with data on price differences between markets. = {BTC : {market1 : 2000, market2: 4000, p : 2}, } (P for percentage difference)
// results is a 2D array with coin name and percentage difference, sorted from low to high.
let coin_last_prices= {}, coin_prices = {}, coin_sell = {},coin_prices_quote_volume={}, coin_prices_base_volume={}, numberOfRequests = 0, results = []; // GLOBAL variables to get pushed to browser.
// retrieving ticker details by fetchTicker(symbol)
/**
* @deprecated since version 0.5
* See getMarketTickers(exchId, coin_prices,coin_sell)
*/
function getMarketPrices(exchId, coin_prices,coin_sell) { //GET JSON DATA
return new Promise (async (resolve, reject) => {
try {
const exchange = exchanges[exchId];
var _coionName = "";
for(var j = 0; j < baseCoins.length;j++){
for(var i = 0; i < quoteCoins.length;i++){
let coinName = baseCoins[j] + "/" + quoteCoins[i];
if (cache.get(exchId +"-"+coinName)!=null ) { continue;}
if (baseCoins[j] != quoteCoins[i] && exchanges_markets[exchId].includes(coinName)) {
for (let numRetries = 0; numRetries < maxRetries; numRetries++) {
_coionName = coinName;
try { // try to load exchange markets using current proxy
if (exchId==="poloniex" && coinName.includes("XML")) {
coinName = coinName.replace("XML", "STR");
}
if (exchId==="kraken" && coinName.includes("BTC")) {
coinName = coinName.replace("BTC", "XBT");
}
//exchange.proxy =exchange.has['CORS']?undefined:proxies[currentProxy]
const ticker = await exchange.fetchTicker (coinName)
await ccxt.sleep (exchange.rateLimit)
coinName = _coionName
//console.log ("exchange id ["+exchId+"] hasFetchTrades ["+exchange.hasFetchTrades+"] date time[" + ticker['datetime'] +"] bid [" + ticker['bid'] +"] ask ["+ ticker['ask'] +"]")
coinName = coinName.replace("/", "-");
if (!coin_prices[coinName]) coin_prices[coinName] = {};
if (!coin_prices_quote_volume[coinName]) coin_prices_quote_volume[coinName] = {};
if (!coin_prices_base_volume[coinName]) coin_prices_base_volume[coinName] = {};
if (!coin_sell[coinName]) coin_sell[coinName] = {};
if (!coin_last_prices[coinName]) coin_last_prices[coinName] = {};
coin_prices[coinName][exchId] = ticker['bid'];
coin_prices_quote_volume[coinName][exchId] = ticker['quoteVolume'];
coin_prices_base_volume[coinName][exchId] = ticker['baseVolume'];
coin_sell[coinName][exchId] = ticker['ask'];
coin_last_prices[coinName][exchId]=ticker['last'];
if (DEBUG) {
log.green("coin_buy:", coin_prices);
log.lightBlue("coin_sell:", coin_sell);
//log.cyan("coin_last_prices:", coin_last_prices);
//log.cyan("coin_prices_quote_volume:", coin_prices_quote_volume);
//log.cyan("coin_prices_base_volume:", coin_prices_base_volume);
}
if (numberOfRequests >= 1) computePrices(coin_prices, coin_sell, coin_last_prices);//, coin_sell);
numRetries = maxRetries;
} catch (e) { // rotate proxies in case of connectivity errors, catch all other exceptions
// swallow connectivity exceptions only
if (e instanceof ccxt.DDoSProtection || e.message.includes ('ECONNRESET')) {
log.red ('[DDoS Protection Error] ' + e.message)
} else if (e instanceof ccxt.RequestTimeout) {
log.red ('[Timeout Error] ' + e.message)
} else if (e instanceof ccxt.AuthenticationError) {
log.red ('[Authentication Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeNotAvailable) {
log.red ('[Exchange Not Available Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeError) {
log.red ('[Exchange Error] ' + e.message)
cache.set(exchId +"-"+coinName, "coin ["+coinName+"] not supported from ["+exchId+"]")
} else {
//throw e; // rethrow all other exceptions
//log ("exchange id ["+exchId+"] doesn't support ["+coinName+"]. Please verify it.");
}
// retry next proxy in round-robin fashion in case of error
currentProxy = ++currentProxy % proxies.length
}
}
}
}
}
numberOfRequests++;
resolve([coin_prices, coin_sell, coin_last_prices]);
} catch (error) {
log.bright.red ("Error retrieving tickers ", error); //Throws error
reject(error);
}
})
}
// retrieving ticker details by fetchTickers()
function getMarketTickers(exchId, coin_prices,coin_sell) { //GET JSON DATA
return new Promise (async (resolve, reject) => {
try {
const exchange = exchanges[exchId];
var coinName = "";
for (let numRetries = 0; numRetries < maxRetries; numRetries++) {
try { // try to load exchange markets using current proxy
//exchange.proxy =exchange.has['CORS']?undefined:proxies[currentProxy]
const tickers = await exchange.fetchTickers();
await ccxt.sleep (exchange.rateLimit)
Object.keys (tickers).map (exchangeId => {
const ticker = tickers[exchangeId]
const symbol = ticker['symbol']
var bCoin, qCoin
bCoin = symbol.slice(0,symbol.indexOf("/"))
qCoin = symbol.slice(symbol.indexOf("/")+1)
if (quoteCoins.includes(qCoin) && baseCoins.includes(bCoin) ) {
coinName = symbol.replace("/", "-");
if (!coin_prices[coinName]) coin_prices[coinName] = {};
if (!coin_prices_quote_volume[coinName]) coin_prices_quote_volume[coinName] = {};
if (!coin_prices_base_volume[coinName]) coin_prices_base_volume[coinName] = {};
if (!coin_sell[coinName]) coin_sell[coinName] = {};
if (!coin_last_prices[coinName]) coin_last_prices[coinName] = {};
coin_prices[coinName][exchId] = ticker['bid'];
coin_prices_quote_volume[coinName][exchId] = ticker['quoteVolume'];
coin_prices_base_volume[coinName][exchId] = ticker['baseVolume'];
coin_sell[coinName][exchId] = ticker['ask'];
coin_last_prices[coinName][exchId]=ticker['last'];
if (DEBUG) {
log.green("coin_buy:", coin_prices);
log.lightBlue("coin_sell:", coin_sell);
//log.cyan("coin_last_prices:", coin_last_prices);
//log.cyan("coin_prices_quote_volume:", coin_prices_quote_volume);
//log.cyan("coin_prices_base_volume:", coin_prices_base_volume);
}
if (numberOfRequests >= 1) computePrices(coin_prices, coin_sell, coin_last_prices);//, coin_sell);
numRetries = maxRetries;
}
})
}
catch (e) { // rotate proxies in case of connectivity errors, catch all other exceptions
// swallow connectivity exceptions only
if (e instanceof ccxt.DDoSProtection || e.message.includes ('ECONNRESET')) {
log.red ('[DDoS Protection Error] ' + e.message)
} else if (e instanceof ccxt.RequestTimeout) {
log.red ('[Timeout Error] ' + e.message)
} else if (e instanceof ccxt.AuthenticationError) {
log.red ('[Authentication Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeNotAvailable) {
log.red ('[Exchange Not Available Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeError) {
log.red ('[Exchange Error] ' + e.message)
cache.set(exchId +"-"+coinName, "coin ["+coinName+"] not supported from ["+exchId+"]")
} else {
throw e; // rethrow all other exceptions
//log ("exchange id ["+exchId+"] doesn't support ["+coinName+"]. Please verify it.");
}
// retry next proxy in round-robin fashion in case of error
currentProxy = ++currentProxy % proxies.length
}
}
numberOfRequests++;
resolve([coin_prices, coin_sell, coin_last_prices]);
} catch (error) {
log.bright.red ("Error retrieving tickers ", error); //Throws error
reject(error);
}
})
}
function endsWith(str, suffix) {
console.log("endsWith str", str)
return str.toString().indexOf(suffix, str.length - suffix.length) !== -1;
}
//https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript
function toFixed(x) {
if (Math.abs(x) < 1.0) {
var e = parseInt(x.toString().split('e-')[1]);
if (e) {
x *= Math.pow(10,e-1);
x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
}
} else {
var e = parseInt(x.toString().split('+')[1]);
if (e > 20) {
e -= 20;
x /= Math.pow(10,e);
x += (new Array(e+1)).join('0');
}
}
return x;
}
/**
* Find Bid by Exchange and Market info
*/
function findBidByExchangeAndMarket(exchange, market) {
return coin_prices[market][exchange]
}
/**
* Find Ask by Exchange and Market info
*/
function findAskByExchangeAndMarket(exchange, market) {
return coin_sell[market][exchange]
}
/**
* Find Last by Exchange and Market info
*/
function findLastByExchangeAndMarket(exchange, market) {
return coin_last_prices[market][exchange]
}
//check opportunities where altcoin doens't support withdrawal features (by exchangeIdToBuy)
function checkMarketCoin(exchID,coinBase ) {
//Feb 10 2018: SQV.TEAM EXCEL
if (exchID=="bitfinex" && !bitfinexCoinsWithdraw.includes(coinBase)) {
return false;
}//Feb 10 2018: SQV.TEAM EXCEL
if (exchID=="binance" && binanceCoinsNoWithdraw.includes(coinBase)) {
return false;
}//Feb 14 2018: SQV.TEAM EXCEL
if (exchID=="hitbtc2" && hitbtc2CoinsNoWithdraw.includes(coinBase)) {
return false;
}//Feb 14 2018: SQV.TEAM EXCEL
if (exchID=="poloniex" && poloniexCoinsNoWithdraw.includes(coinBase)) {
return false;
}
if (exchID=="cryptopia" && crypropiaCoinsNoWithdraw.includes(coinBase)) {
return false;
}
crypropiaCoinsNoWithdraw
return true;;
}
//check if a process is already started by exchange id and coinpair
//TO BE TESTED
function isOpportunityOngoing(exchangeid, coinpair){
try {
log("database.sql_select_by_exchangeid_coinpair", exchangeid, coinpair)
database.db.all(database.sql_select_by_exchangeid_coinpair, [exchangeid, coinpair], function(err, rows, fields){
console.log("check process",rows);
res.json({ "data" : rows });
rows.forEach((row) => {
return true;
});
return false;
});
} catch (err) {
console.error(err.message)
return false;
//next(err);
}
}
//calculate Wallet on the other side of opportunity. Used for the roundtrip. See: OPPORTUNITIES_SIDE on settings.js
function calculateFutureWallet(backWallet,coinBase, exchange, exchangeId,depositfee, price ) {
var newWallet =0
try {
const trading_fee = exchange.fees.trading.taker
var withdraw_fee = exchange.fees.funding.withdraw[coinBase];
if (typeof withdraw_fee === 'undefined' || isNaN(withdraw_fee)) {
withdraw_fee = tradersFee.fee[exchangeId][0][coinBase +"_withdrawal"];
}
//log.green("calculateFutureWallet withdraw_fee", withdraw_fee, "trading_fee", trading_fee)
var cost = (withdraw_fee + depositfee)*price
newWallet = (backWallet - (backWallet*trading_fee)) - cost
} catch (error) {
log.red(exchangeId.toString().yellow, coinBase, error.messag)
}
return newWallet
}
//write a ts/sampling for a dummy dataset.
//Used for future analyzing which is the best pair (amount/currency) for the arbitrage
async function opportunitiesDS(params={}) {
if (COUNT_OPPORTUNITIES) {
var textCSV = "\n"+new Date().toISOString() + ","+ params.market1+","+params.market2+","+toFixed(params.market1price)+","+toFixed(params.market2price)+"," + params.coin + ","
textCSV += params.feeTaker+","+params.feeMaker+","+params.withdrawal+","+params.deposit+","
textCSV += params.quoteVolume+","+params.baseVolume+","+params.spred_wo_fee+","+params.coinToBuy+","
textCSV += params.netCoinToBuy+","+params.walletAmount+","+params.futureWalletAmount+","+params.grossPriceSpread+","
textCSV += params.netAmountSpread+","+params.grossPriceSpreadPerc+"%,"+params.netAmountSpreadPerc+"%,"+params.gainAmount+","
textCSV += params.gainAmountPerc +"%"
fs.appendFile(fileCSV,textCSV, (err) => {
if (err) log.red( err.message);
log.magenta('CSV were updated with the opportunity found !!!');
});
}
}
//calculate Volume from coin quote to USD(T)
function calculateUSDTVolume(exchangeId,coinQuote, quoteVolume, data){
var priceCQ;
if (!coinQuote.startsWith("USD")) {
priceCQ = data[coinQuote+"-USDT"][exchangeId]
//log.magenta("calculateUSDTVolume".yellow, exchangeId.green, "USDT priceCQ", priceCQ)
if (priceCQ === undefined) {
priceCQ = data[coinQuote+"-USD"][exchangeId]
//log.magenta("calculateUSDTVolume".yellow, exchangeId.green, "USD priceCQ", priceCQ)
}
} else {
priceCQ=1;//USD
}
const quoteVolumeUSD = quoteVolume * priceCQ
//log.magenta("calculateUSDTVolume".yellow, exchangeId.green, "quoteVolume", quoteVolume, "priceCQ", priceCQ, "quoteVolumeUSD", quoteVolumeUSD)
return quoteVolumeUSD;
}
async function computePrices(data, dataSell, dataLast) {
results = [];
function loopData() {
return new Promise(function (resolve, reject) {
if (numberOfRequests >= 2) {
for (let coin in data) {
if (Object.keys(data[coin]).length > 1) {
if (coinNames.includes(coin) == false) coinNames.push(coin);
let arr = [];
let arrSell = [];
for (let market in data[coin]) {
arr.push([data[coin][market], market]);
}
for (let market in dataSell[coin]) {
if (dataSell[coin][market]!=null) {
arrSell.push([dataSell[coin][market], market]);
}
}
arr.sort(function (a, b) {
return a[0] - b[0];
});
arrSell.sort(function (a, b) {
return a[0] - b[0];
});
for (let i = 0; i < arr.length; i++) {
for (let j = 0 ; j < arrSell.length; j++) {
var boSameMarket = false;
if (arrSell[i]!=null) {
var exchangeIdToBuy = arr[i][1];
var exchangeIdToSell = arrSell[j][1];
if (exchangeIdToBuy===exchangeIdToSell){
//boSameMarket = true;
continue;
}
if (arr[i][0]<arrSell[i][0] && !boSameMarket) {
// retreive base coin currency
let coinBase = coin.slice(0,coin.indexOf("-"));
// retreive quote coin currency -- the currency used for buying the base coin
let coinQuote = coin.slice(coin.indexOf("-")+1);
//exchange A - Exchange for staring the triangular arbitrage
const exchangeToBuy = exchanges[exchangeIdToBuy]
//exchange B - Exchange for closing the triangular arbitrage
const exchangeToSell = exchanges[exchangeIdToSell]
//remove opportunities where altcoin doesn't support withdrawal features by exchangeIdToBuy
if (!checkMarketCoin(exchangeIdToBuy,coinBase)) {
continue;
}
//base and quote Volume for base and quote coin
var baseVolume = coin_prices_base_volume[coin][exchangeIdToBuy]
var quoteVolume = coin_prices_quote_volume[coin][exchangeIdToBuy]
if (typeof baseVolume === 'undefined' ) {
baseVolume =0;
}
if (typeof quoteVolume === 'undefined' ) {
quoteVolume =0;
}
//logger.info("Exchange" , exchangeIdToBuy, "base volume ", baseVolume, "quote volume ", quoteVolume, "for", coin )
//Price to buy the base coin based on quote coin
const priceToBuy = arr[i][0];
//Price to sell the base coin based on quote coin
const priceToSell = arrSell[j][0];
//Trading fee for buying coins
let tradingFeeBuyer = exchangeToBuy.fees.trading.taker
//Trading fee for selling coins
let tradingFeeSeller = exchangeToSell.fees.trading.maker
if (typeof tradingFeeBuyer === 'undefined' ) {
tradingFeeBuyer = tradersFee.fee[exchangeIdToBuy][0]["tradingfee_buy"];
}
if (typeof tradingFeeSeller === 'undefined' ) {
tradingFeeSeller = tradersFee.fee[exchangeIdToSell][0]["tradingfee_sell"];
}
//amount of coins to buy
var walletAmount = fetchBalance(coinQuote, exchangeIdToBuy);
//skip opportunity with wallet balance is undefined
if (typeof walletAmount === 'undefined' || isNaN(walletAmount)) {
continue;
process.exit(1);
}
//skip opportunity where wallet balance is unavailable
var walletAmountSell = fetchBalance(coinQuote, exchangeIdToSell);
if (typeof walletAmountSell === 'undefined' || isNaN(walletAmountSell)) {
continue;
process.exit(1);
}
try {
//skip opportunity where wallet address is unavailable
var exBuyAddress = altCoinsAddesses.addresses[exchangeIdToBuy][0][coinBase +"_address"]
if (typeof exBuyAddress === 'undefined' || exBuyAddress ==='undefined') {
continue;
}
//skip opportunity where wallet address is unavailable
var exSellAddress = altCoinsAddesses.addresses[exchangeIdToSell][0][coinBase +"_address"]
//log.magenta("exSellAddress ",exchangeIdToSell,coinBase, exSellAddress)
if (typeof exSellAddress === 'undefined' || exSellAddress ==='undefined') {
continue;
}
} catch (error) {
log.red(error.message,exchangeIdToBuy,exchangeIdToSell, coinBase )
continue;
}
//looking for the trasferback - rountrip as side (see:OPPORTUNITY_SIDE on settings.js)
var foundTrasfertBack = false;
var deposit_fee_seller = 0;
try {
deposit_fee_seller = exchangeToBuy.fees.funding.deposit[coinBase]; //DEPOSIT CALCULALTED FOR THE ROUND TRIP
} catch (error) {
//log.red("no deposit details found for" ,exchangeIdToBuy, coinBase )
}
if (typeof deposit_fee_seller === 'undefined' || deposit_fee_seller === 'undefined' || deposit_fee_seller ==="") {
deposit_fee_seller=0;
}
if (walletAmount<=0 ) {
//skip opportunity with back wallet (the remote wallet on the roundtrip side) is zero or undefined
if (typeof walletAmountSell === 'undefined' || isNaN(walletAmountSell) || walletAmountSell ===0) {
return
} else {
foundTrasfertBack = true
//walletAmountSell = walletAmountSell - deposit_fee_seller;
walletAmount = calculateFutureWallet(walletAmountSell,coinBase, exchangeToSell, exchangeIdToSell,deposit_fee_seller, priceToBuy )
}
}
//trasform coin from aca format to CCXT format. Maybe it is better to use directly the CCXF format
var ccxtCoin = coin.replace("-","/");
//check amount limit. if wallet availability exeeds min and max amount -> skip opportunity.
//check Quote Volume, if below a threshold -> skip opportunity.
try {
if (LIMIT_FILTER) {
let marketLimits = markets[exchangeIdToBuy][ccxtCoin].limits
if (marketLimits === undefined || marketLimits["price"] === undefined || marketLimits["amount"] === undefined || marketLimits["cost"] === undefined) {
log.red('market.limits.[price|amount|cost] property is not set. I don\'t care',exchangeIdToBuy, ccxtCoin )
} else{
const amount = walletAmount/priceToBuy;
if (marketLimits.amount.min>amount || marketLimits.amount.max < amount) {
continue;
}
}
}
if (VOLUME_FILTER) {
const quoteVolumeUSDBuyer = calculateUSDTVolume(exchangeIdToBuy,coinQuote, coin_prices_quote_volume[coin][exchangeIdToBuy], coin_prices )
const quoteVolumeUSDSeller = calculateUSDTVolume(exchangeIdToSell,coinQuote,coin_prices_quote_volume[coin][exchangeIdToSell],coin_sell )
//if (DEBUG) {
log.magenta(exchangeIdToBuy.green, ccxtCoin, "quoteVolumeUSDBuyer", quoteVolumeUSDBuyer,"VOLUME_THRESHOLD", VOLUME_THRESHOLD, "quoteVolumeUSDBuyer< VOLUME_THRESHOLD", (quoteVolumeUSDBuyer< VOLUME_THRESHOLD))
log.magenta(exchangeIdToSell.green, ccxtCoin, "quoteVolumeUSDSeller", quoteVolumeUSDSeller,"VOLUME_THRESHOLD", VOLUME_THRESHOLD, "quoteVolumeUSDSeller< VOLUME_THRESHOLD", (quoteVolumeUSDSeller< VOLUME_THRESHOLD))
//}
if (quoteVolumeUSDBuyer < VOLUME_THRESHOLD ||
quoteVolumeUSDSeller < VOLUME_THRESHOLD) continue
}
}
catch (error) {
log.red(error.message)
}
//starting withdrawal and deposit fees for trasfer coins to Exchange A to Exchange B
var deposit_fee =0
try {
deposit_fee = exchangeToSell.fees.funding.deposit[coinBase];
} catch (error) {
log.red("no deposit details found for" ,exchangeIdToSell, coinBase )
}
//
if (typeof deposit_fee === 'undefined' || deposit_fee === 'undefined' || deposit_fee ==="") {
deposit_fee=0;
}
var withdrawlBuyer = "";
try {
//try to retieve amount for the withdrawal from CCXT library based on coin base
withdrawlBuyer = exchangeToBuy.fees.funding.withdraw[coinBase];
if (typeof withdrawlBuyer === 'undefined' ) {
//if the statement above is null try to retieve amount for the withdrawal from CCXT library not based on coin base but for the all coins
//POLONIEX & YOBIT CASE. Looking for fees in trading.fee.json file
if (exchangeIdToBuy==='yobit') { //check withdraw issue across exchanges
withdrawlBuyer = exchangeToBuy.fees.funding.withdraw
} else if (exchangeIdToBuy==='poloniex') { //the correct thing to do is to invoke the fetchFees api, but it doesn't work on poloniex.
//withdrawlBuyer = await exchangeToBuy.fetchFees().withdraw[coinBase]
withdrawlBuyer = tradersFee.fee[exchangeIdToBuy][0][coinBase +"_withdrawal"];
} else {
withdrawlBuyer = tradersFee.fee[exchangeIdToBuy][0][coinBase +"_withdrawal"];
}
}
} catch(error) {
withdrawlBuyer = tradersFee.fee[exchangeIdToBuy][0][coinBase +"_withdrawal"];
}
if (typeof withdrawlBuyer === 'undefined' ) {
log.red("withdrawal retrieved. result for the withdrawlBuyer is udenfined --> please add", coinBase.green, "trading.fee.json to exchange", exchangeIdToBuy.magenta);
withdrawlBuyer = 10000//terrificant...like a LSD. best choice is lose the opportunity than follow it.
} else if (withdrawlBuyer.toString().indexOf("%") !== -1 ) { //Check if withdraw is a % or not. see therock for the withdraw on XRP.
withdrawlBuyer = (walletAmount/priceToBuy)*(withdrawlBuyer.toString().slice(0, withdrawlBuyer.toString().indexOf("%"))/100)
}
//log.yellow("withdrawlBuyer", withdrawlBuyer)
var feeBuyerCalculated
const unitsBoughtToBuy = walletAmount/priceToBuy
try {
//just for test
//feeBuyerCalculated= exchangeToBuy.calculateFee(ccxtCoin, "type", "buy", unitsBoughtToBuy, priceToBuy );
//feeBuyerCalculated = functions.calculateFee(exchangeIdToBuy,ccxtCoin, "type", "buy", unitsBoughtToBuy, priceToBuy )
} catch(error) {
log.red("exchanges", exchangeIdToBuy,"calculateFee ", error.message)
}
//log.cyan(exchangeIdToBuy, "feeBuyerCalculated custom", feeBuyerCalculated, "ccxtCoin", ccxtCoin, "unitsBoughtToBuy", unitsBoughtToBuy, "priceToBuy", priceToBuy)
// check if we have enough amoutn to start the opportunity
var grossPriceSpread = priceToSell/priceToBuy;
//calculate trading fee for buying and for selling of coins
//net price for buying a single unit of coin
var netQuantiyCoinsToBuy = (walletAmount/priceToBuy);
var netCostToBuy = ((netQuantiyCoinsToBuy * priceToBuy ) + ((netQuantiyCoinsToBuy * priceToBuy )*tradingFeeBuyer))
var netPriceToSell = ((netQuantiyCoinsToBuy * priceToSell) - ((netQuantiyCoinsToBuy * priceToSell)*tradingFeeSeller))
var netPriceToSellWithdraw = ((netQuantiyCoinsToBuy - withdrawlBuyer) * priceToSell) - (((netQuantiyCoinsToBuy-withdrawlBuyer) * priceToSell)*tradingFeeSeller)
var netSpread = netPriceToSell/netCostToBuy;
var netSpreadWithdraw = netPriceToSellWithdraw/netCostToBuy;
//buy trading fee
var amountToBuy = walletAmount / priceToBuy
//minus trading fee
var netQYToBuy = amountToBuy - (amountToBuy * tradingFeeBuyer)
//minus withdrawal fee
var netQuantityCoinsMinsWithdrawalfee = netQYToBuy - withdrawlBuyer;
//minus deposit fee if needed
netQuantityCoinsMinsWithdrawalfee = netQuantityCoinsMinsWithdrawalfee - deposit_fee;
//sell trading fee
var amountToSell = netQuantityCoinsMinsWithdrawalfee * priceToSell
logger.debug("amountToSell",amountToSell,"netQuantityCoinsMinsWithdrawalfee", netQuantityCoinsMinsWithdrawalfee, "priceToSell", priceToSell);
var newWalletAmount = functions.amountToPrecision (exchangeIdToBuy,markets, ccxtCoin, (amountToSell - (amountToSell * tradingFeeSeller)));
logger.debug("newWalletAmount",newWalletAmount,"amountToSell", amountToSell, "tradingFeeSeller", tradingFeeSeller);
var realAmount = functions.amountToPrecision(exchangeIdToBuy,markets, ccxtCoin,amountToBuy*priceToBuy)
logger.debug("realAmount",realAmount,"amountToBuy", amountToBuy, "priceToBuy", priceToBuy);
if (DEBUG)
if (newWalletAmount>realAmount)
log.magenta("newWalletAmount", newWalletAmount, "walletAmount", walletAmount, "realAmount", realAmount, "diff", (walletAmount-realAmount) );
var netAmountSpread = newWalletAmount/walletAmount
//guadagno
var gainAmount = newWalletAmount - walletAmount;
//guadagno in percentuale
var percGainAmount = (100 * gainAmount) / walletAmount;
//just for logging...
var spreadAmountPerc =((grossPriceSpread - 1) * 100).toFixed(3)
var spreadPricePerc =((netAmountSpread - 1) * 100).toFixed(3)
foundTrasfertBack = (OPPORTUNITIES_SIDE==='oneway')?false:true
// log.yellow("foundTrasfertBack", foundTrasfertBack)
//log.green("(newWalletAmount <= walletAmount)", (newWalletAmount <= walletAmount))
if((newWalletAmount <= realAmount) && (!foundTrasfertBack)) continue;
if (DEBUG) {
log.magenta(".:: BEGIN ::.");
log.magenta("Exchange to buy", exchangeIdToBuy.toString().cyan, "Exchange to sell", exchangeIdToSell.toString().cyan);
log.magenta("Price to buy", priceToBuy.toString().cyan, "Price to sell", priceToSell.toString().cyan, "pair coin", coin.toString().cyan);
log.magenta("Item on Array")
log.magenta("arrSell[j][0]", arrSell[j][0].toString().cyan, "arr[i][0]", arr[i][0].toString().cyan)
log.magenta("market1:", arr[i][1].toString().cyan, "market2:", arrSell[j][1].toString().cyan)
log.magenta("trading fee taker", tradingFeeBuyer.toString().cyan, "trading fee maker", tradingFeeSeller.toString().cyan)
log.magenta("withdrawal", withdrawlBuyer.toString().cyan, "deposit", deposit_fee.toString().cyan)
log.magenta("quote volume", quoteVolume.toString().cyan, "base volume", baseVolume.toString().cyan)
// log.magenta("net price to buy", netCostToBuy)
// log.magenta("net price to sell", netPriceToSell)
log.magenta("spread w/o trading fee and withdrawal", (arrSell[j][0] / arr[i][0]).toString().cyan)
//log.magenta("netSpread w/ trading fee", netSpread)
//log.magenta("netSpread w/ trading fee & Withdraw", netSpreadWithdraw)
log.magenta("wallet amount", walletAmount.toString().cyan)
log.magenta("wallet amount for trading", realWallet.toString().cyan)
log.magenta("quantity of coins to buy", netQuantiyCoinsToBuy.toString().cyan)
log.magenta("coins to buy (trading fee included)", netQYToBuy.toString().cyan);
log.magenta("coins to buy (trading fee & withdrawal included)", netQuantityCoinsMinsWithdrawalfee.toString().cyan);
log.magenta("newWalletAmount", newWalletAmount.toString().cyan);
log.magenta("gross (price) spread ", grossPriceSpread.toString().cyan);
log.magenta("net (amount) spread ", netAmountSpread.toString().cyan);
log.magenta("gross (price) spread(%)", spreadAmountPerc.toString().cyan, "%".cyan);
log.magenta("net (amount) spread(%)", spreadPricePerc.toString().cyan, "%".cyan);
log.magenta("Gain Amount", gainAmount.toString().cyan);
log.magenta("Perc Gain Amount", percGainAmount.toString().cyan);
log.magenta(".:: END ::.");
}
//wite the opportunity found for fute analysis
opportunitiesDS ({
market1:exchangeIdToBuy,
market2:exchangeIdToSell,
market1price:priceToBuy,
market2price:priceToSell,
coin:ccxtCoin,
feeTaker: tradingFeeBuyer,
feeMaker: tradingFeeSeller,
withdrawal:withdrawlBuyer,
deposit:deposit_fee,
quoteVolume:quoteVolume,
baseVolume:baseVolume,
walletAmount:walletAmount,
coinToBuy:netQuantiyCoinsToBuy,
netCoinToBuy:netQuantityCoinsMinsWithdrawalfee,
futureWalletAmount:newWalletAmount,
spred_wo_fee:(arrSell[j][0] / arr[i][0]),
grossPriceSpread:grossPriceSpread,
netAmountSpread:netAmountSpread,
grossPriceSpreadPerc:spreadAmountPerc,
netAmountSpreadPerc:spreadPricePerc,
gainAmount: gainAmount,
gainAmountPerc: percGainAmount
})
logger.info(".:: BEGIN ::.");
logger.info("Exchange to buy", exchangeIdToBuy, "Exchange to sell", exchangeIdToSell);
logger.info("Price to buy", priceToBuy, "Price to sell", priceToSell, "pair coin", coin);
logger.info("Item on Array")
logger.info("arrSell[j][0]", arrSell[j][0], "arr[i][0]", arr[i][0])
logger.info("market1:", arr[i][1], "market1: buy", arr[i][0], "market2:", arrSell[j][1], "market2: sell", arrSell[j][0])
logger.info("trading fee taker", tradingFeeBuyer, "trading fee maker", tradingFeeSeller)
logger.info("withdrawal", withdrawlBuyer, "deposit", deposit_fee)
logger.info("quote volume", quoteVolume, "base volume", baseVolume)
// logger.info("net price to buy", netCostToBuy)
// logger.info("net price to sell", netPriceToSell)
logger.info("spread w/o trading fee and withdrawal", arrSell[j][0] / arr[i][0])
//logger.info("netSpread w/ trading fee", netSpread)
//logger.info("netSpread w/ trading fee & Withdraw", netSpreadWithdraw)
logger.info("wallet amount", walletAmount)
logger.info("quantity of coins to buy", netQuantiyCoinsToBuy)
logger.info("coins to buy (trading fee included)", netQYToBuy);
logger.info("coins to buy (trading fee & withdrawal included)", netQuantityCoinsMinsWithdrawalfee);
logger.info("newWalletAmount", newWalletAmount);
logger.info("gross (price) spread ", grossPriceSpread);
logger.info("net (amount) spread ", netAmountSpread);
logger.info("gross (price) spread(%)", spreadAmountPerc, "%");
logger.info("net (amount) spread(%)", spreadPricePerc, "%");
logger.info("Gain Amount", gainAmount);
logger.info("Perc Gain Amount", percGainAmount);
logger.info(".:: END ::.");
//log.magenta("results.push", coin, netAmountSpread, arrSell[j][1],arrSell[j][0], arr[i][1],arr[i][0]);
results.push(
{
coin: coin,
amount: realAmount,
//spread: arrSell[j][0] / arr[i][0],
spread: netAmountSpread,
journey: OPPORTUNITIES_SIDE,
market2: {
name: arrSell[j][1],
last: arrSell[j][0]
},
market1: {
name: arr[i][1],
last: arr[i][0]
}
}
);
}
}
}
}
}
}
results.sort(function (a, b) {
return a.spread - b.spread;
});
//log.cyan('Finishing function...');
resolve();
}
})
}
await loopData();
//log.green("Emitting Results...")
io.emit('wallets',[coinNames, sqvWallets]);
io.emit('results', results);
}
// Retrieve Wallet by AltCoin and Exchange
function fetchBalance(altcoin, exchId) {
var result = undefined;
try {
result = sqvWallets[exchId][altcoin]
if (typeof result === 'undefined' || result === NULL_CRITERIA) {
result = undefined
}
return result;
} catch (error) {
//empty wallet for the specific exchage and altcoin
//log.red ('empty wallet for the specific exchage and altcoin', exchId,altcoin )
return undefined;
}
}
// Retrieve Wallet from Exchange fro all coins
function fetchBalances(exchId) { //GET BALANCE DATA
return new Promise (async (resolve, reject) => {
try {
const exchange = exchanges[exchId];
//exchange.proxy =exchange.has['CORS']?undefined:'http://localhost:8080/'
//exchange.proxy ='http://localhost:8080/'
//exchange.timeout = 60000
let balance
if (exchId ==='binance') {
let binance = new ccxt.binance (accounts[exchId])
balance = await binance.fetchBalance({'recvWindow': 60000000})
//exchange.verbose = true
} else {
// output the balance from the exchange
balance = await exchange.fetchBalance ()
}
//HITBTC has two kind of wallet: Bank and Exchange wallet --> move to Exchange for trading
if (exchId === "hitbtc2") {
var balanceAccount = await exchange.fetchBalance ({ type: 'account' })
var balanceTotal =balanceAccount.total
for(var i = 0; i < baseCoins.length; i++) {
var amount = balanceTotal[baseCoins[i]];
if (typeof amount !== 'undefined' && amount >0) {
exchange.private_post_account_transfer({'currency': baseCoins[i], 'amount': amount, 'type': 'bankToExchange'})
//log (exchange.name.green, "currency",baseCoins[i], 'balance', "amount", amount)
}
}
}
if (STUB) {
sqvWallets[exchId]=JSON.parse('{"' + sqvCoin + '":"' + sqvAmount +'"}')
} else {
//store the total balance by altcoin
sqvWallets[exchId]=balance.total
}
await ccxt.sleep (exchange.rateLimit)
resolve(balance);
} catch (error) {
console.log("Error retrieving balances ", error); //Throws error
//reject(error);
}
})
}
var capabilities = [
'publicAPI',
'privateAPI',
'CORS',
'fetchTicker',
'fetchTickers',
'fetchOrderBook',
'fetchTrades',
'fetchOHLCV',
'fetchBalance',
'createOrder',
'createMarketOrder',
'createLimitOrder',
'editOrder',
'cancelOrder',
'fetchOrder',
'fetchOrders',
'fetchOpenOrders',
'fetchClosedOrders',
'fetchMyTrades',
'fetchCurrencies',
'fetchDepositAddress',
'createDepositAddress',
'withdraw',
];
function fetchPrice(exchangeId, priceToBuy, priceToSell) {
if (FETCH_TYPE==="ticker") {
return getMarketPrices(exchangeId, priceToBuy, priceToSell)
} else if (FETCH_TYPE==="tickers") {
if ( !exchanges[exchangeId].has["fetchTickers"]) {
return getMarketPrices(exchangeId, priceToBuy, priceToSell)
} else {
return getMarketTickers(exchangeId, priceToBuy, priceToSell)
}
} else {
log.red.error ("PLEASE CHECK FETCH TYPE FOR STARTING ARBITRAGE...")
process.exit(1);
}
}
//helper function for sending message to specific topic/channel via io
async function sendNotification (channel, message) {
log.magenta("sendNotification",channel, message )
io.emit(channel,message);
}
var bootEnv = true;
//MAIN ACA (crime) Engine
(async function main() {
log.bright.green("SQV .::cryptocurrency arbitrage - CRIME::.Version "+version)
log.bright.green("Arbitrage Criteria:", ARBITRAGE_CRITERIA);
log.bright.green("Arbitrage Side:", OPPORTUNITIES_SIDE);
log.bright.green("Arbitrage Engine:", ARBITRAGE_ENGINE);
log.bright.green("Fetch Type:", FETCH_TYPE);
let arrayOfRequests = [];
const ids = ccxt.exchanges.filter (id => id in accounts)
exchanges = ccxt.indexBy (await Promise.all (ids.map (async id => {
let exchange
if (id==="therock") {
// instantiate the exchange
exchange = new therocksqv (ccxt.extend ( { enableRateLimit: ENABLE_RATE_LIMIT }, accounts[id]))
} else if (id==="gdax") {
exchange = new ccxt[id] (ccxt.extend ( { enableRateLimit: ENABLE_RATE_LIMIT }, accounts[id], {
nonce: function () { return (this.milliseconds ()/1000) }