-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrader.js
528 lines (475 loc) · 18.4 KB
/
trader.js
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
const Gdax = require('gdax');
const licenses = require('./licenses.js');
const authedClient = new Gdax.AuthenticatedClient(
licenses.key,
licenses.secret,
licenses.passphrase,
licenses.URL
);
//***********************************
//Define supported exchanges object and the arbitrager object
supportedExchanges = new SupportedExchanges(['BTC-USD', 'BCH-BTC', 'BCH-USD', 'ETH-BTC', 'ETH-USD', 'LTC-BTC', 'LTC-USD']);
arbitrager = new GDAXTriangularArbitrager(authedClient);
//****************************************************************************************************
// Creating this object is asynchronous, since it pulls account data
// Shouldn't be a problem, though, because enough time passes when another call to
// getProducts() happens.
function GDAXTriangularArbitrager(gdaxAuthedClient) {
this.gdaxAuthedClient = gdaxAuthedClient;
this.pathResults = [];
// All of these objects are declare asynchronously below. Be aware that they exist.
// this.accountCoin;
// this.accountValue;
// this.accountValueInTermsOfEndCoin;
// this.pathFinder;
//This happens last, after pulling all data from GDAX
//It calculates the difference in price if the money is exchanged through the path
this.calculatePriceExchanges = function () {
var runningValue = this.accountValue;
var runningCurrency = this.accountCoin;
for (var i = 0; i < this.pathFinder.completedPaths.length; i++) {
for (var j = 0; j < this.pathFinder.completedPaths[i].length; j++) {
var xName = this.pathFinder.completedPaths[i][j].name;
// I need to do this because I copy the exchange objects out of the
// "supportedExchanges" internal array, and so my exchange objects don't
// have the bid/ask data.
// Very poor design. Not sure if it's worth solving at this point.
var xWithBidAsk = supportedExchanges.getExchangeFromName(xName);
var returnArray = xWithBidAsk.exchangeCurrency(runningCurrency, runningValue);
runningCurrency = returnArray[0];
runningValue = returnArray[1];
// [runningCurrency, runningValue]
}
this.pathResults.push(runningValue);
runningValue = this.accountValue;
runningCurrency = this.accountCoin;
}
//I've calculated the paths in terms of endCoin.
//Now convert what I HAVE already into terms of endCoin.
this.accountValueInTermsOfEndCoin = this.accountValue;
if(this.accountCoin != this.pathFinder.endCoin)
this.accountValueInTermsOfEndCoin = supportedExchanges.exchangeCoinForCoin(this.accountValue, this.accountCoin, this.pathFinder.endCoin);
// console.log(this.accountValue);
// console.log(this.accountCoin);
// console.log(this.pathFinder.endCoin);
// console.log(this.accountValueInTermsOfEndCoin);
// PRINT YOUR RESULTS
for (var i = 0; i < this.pathResults.length; i++) {
console.log("**********");
var path = this.pathFinder.completedPaths[i];
var pathArray = [];
for (var j = 0; j < path.length; j++) {
pathArray.push(path[j].name);
}
console.log("path -> ", pathArray);
console.log("path result ", i, " is ", this.pathResults[i]);
console.log("difference is ", this.pathResults[i] - this.accountValueInTermsOfEndCoin);
}
}
//Pull data from GDAX about the exchange rates of all coins
this.pullCoinDataFromGDAX = function () {
this.gdaxAuthedClient.getProducts(
(error, response, book) => {
for (i = 0; i < supportedExchanges.exchanges.length; i++) {
this.gdaxAuthedClient.getProductOrderBook(
supportedExchanges.exchanges[i].name,
(error, response, book) => {
if(error)
console.log("Got an error: ", error);
var exchange = getExchangeFromResponse(response, supportedExchanges.exchanges);
supportedExchanges.insertBidAndAsk(exchange, book.bids[0][0], book.asks[0][0]);
if(supportedExchanges.checkIfComplete())
{
this.calculatePriceExchanges();
}
}
);
}
}
);
}
//This is executed first, and pulls down the data from my user account.
//After that, it triggers pullCoinDataFromGDAX(), which calls calculatePriceExchanges();
this.gdaxAuthedClient.getAccounts(
(error, response, book) => {
var maxVal = -1, maxCoin;
for (var i = 0; i < book.length; i++) {
if(maxVal < book[i].available)
{
maxVal = book[i].available;
maxCoin = book[i].currency;
}
}
this.accountCoin = maxCoin;
this.accountValue = maxVal;
// this.accountCoin = "LTC";
// this.accountValue = 0.002;
this.endCoin = "USD";
// this.accountValueInTermsOfEndCoin can't be set yet. Wait until supportedExchanges is complete
this.pathFinder = new PathFinder(this.accountCoin, this.endCoin);
// this.pathFinder.printFullPaths();
this.pullCoinDataFromGDAX();
}
);
}
// ******************************************************************************** //
// ******************************************************************************** //
// Websocket stuff
if(false)
{
const websocket = new Gdax.WebsocketClient(
['BTC-USD'],
licenses.sandbox_ws_feed,
{
key: licenses.sandbox_key,
secret: licenses.sandbox_secret,
passphrase: licenses.sandbox_passphrase,
},
{ channels: ['full'] }
);
// websocket.unsubscribe({ channels: ['heartbeat'] });
// const websocket = new Gdax.WebsocketClient(['BTC-USD', 'ETH-USD']);
websocket.on('message', data => {
if(data.type != "heartbeat")
console.log("message received ", data);
});
websocket.on('error', err => {
console.log("error received");
});
websocket.on('close', () => {
console.log("close received");
});
userInput = false;
query = function(callback) {
process.stdin.resume();
process.stdout.write("Please enter some input: ");
process.stdin.once("data", function(data) {
callback(data.toString().trim());
});
};
query(function(response) {
console.log("you wrote ", response);
process.stdin.pause();
});
intervalFunc = function() {
console.log("blah blah blah");
}
setInterval(intervalFunc, 1000);
}
// ******************************************************************************** //
// ******************************************************************************** //
// Classes for the Arbitrager
function PathFinder(startCoin, endCoin) {
this.startCoin = startCoin;
this.endCoin = endCoin;
this.pathLevels = [];
this.completedPaths = [];
this.doIt = function () {
this.initializePathLevels();
this.constructPathLevels();
this.calculateFullPaths();
}
this.initializePathLevels = function () {
this.pathLevels = [];
this.pathLevels.push(supportedExchanges.getExchangeObjectsWithCoin(startCoin));
}
this.constructPathLevels = function () {
//Get the most recent level (an array of exchanges at that step in the path)
var lastIndex = this.pathLevels.length - 1;
var currentLevel = this.pathLevels[lastIndex];
//Iterate through all of the exchanges, set parent and child exchanges
var allNextLevelXs = [];
for (var i = 0; i < currentLevel.length; i++) {
var x = currentLevel[i];
var currentCoin = x.tracePathForCurrentCoin(this.startCoin);
//I have the current coin. Get the "paired" coin on the exchange
var pairedCoin = x.getPairedCoin(currentCoin, x);
if(pairedCoin != endCoin)
{
//Find all potential child exchanges
var potentialNextLevelXs = supportedExchanges.getExchangeObjectsWithCoin(pairedCoin);
var validatedNextLevelXs = [];
//Loop deletes any Xs that immediately backtrack
for (var j = 0; j < potentialNextLevelXs.length; j++) {
var thisChild = potentialNextLevelXs[j];
if(x.name != thisChild.name)
{
thisChild.setParent(x); //This is how I track the paths among the nested arrays
validatedNextLevelXs.push(thisChild);
}
}
allNextLevelXs = allNextLevelXs.concat(validatedNextLevelXs);
}
}
if(allNextLevelXs.length > 0)
{
this.pathLevels.push(allNextLevelXs);
this.constructPathLevels();
}
}
this.printPathLevels = function () {
for (var h = 0; h < this.pathLevels.length; h++) {
console.log("********** Level ", h, " ************");
for (var i = 0; i < this.pathLevels[h].length; i++) {
var x = this.pathLevels[h][i];
var parent = x.link.parent.name;
var children = [];
for (var j = 0; j < x.link.children.length; j++) {
children.push(x.link.children[j].name);
}
console.log(parent, "<==== ", this.pathLevels[h][i].name, " =====> ", children);
}
}
}
this.printFullPaths = function () {
for (var f = 0; f < this.completedPaths.length; f++) {
var tempPath = [];
for (var i = 0; i < this.completedPaths[f].length; i++) {
tempPath.push(this.completedPaths[f][i].name);
}
console.log(tempPath);
}
}
this.calculateFullPaths = function () {
// work backwards, finding all exchanges with no children
var noChildrenXs = [];
for (var h = this.pathLevels.length - 1; h >= 0; h--) {
for (var j = 0; j < this.pathLevels[h].length; j++) {
if(this.pathLevels[h][j].link.children.length == 0)
noChildrenXs.push(this.pathLevels[h][j]);
}
}
for (var i = 0; i < noChildrenXs.length; i++) {
var x = noChildrenXs[i];
var runningPath = [];
while(x)
{
runningPath.unshift(x);
x = x.link.parent;
}
this.completedPaths.push(runningPath);
}
}
this.doIt();
}
function PathLinks(parentObject) {
this.parent = parentObject;
this.children = [];
}
function Exchange(name, parentLink = 0, bid = 0, ask = 0) {
this.name = name;
this.baseCoin = getFirstCoinOfExchange(name);
this.quoteCoin = getSecondCoinOfExchange(name);
this.bid = bid;
this.ask = ask;
switch(this.quoteCoin)
{
case "BTC":
this.quoteMinIncrement = 0.00001;
break;
case "USD":
default:
this.quoteMinIncrement = 0.01;
break;
}
switch(this.baseCoin)
{
case "BTC":
case "BCH":
this.takerFee = 0.0025;
break;
case "ETH":
case "LTC":
default:
this.takerFee = 0.0030;
break;
}
// Link data to facilitate create paths later
this.link = new PathLinks(parentLink);
this.getPairedCoin = function (currentCoin)
{
if(this.baseCoin == currentCoin)
return this.quoteCoin;
else if(this.quoteCoin == currentCoin)
return this.baseCoin;
else
return false;
}
this.tracePathForCurrentCoin = function (startCoin)
{
// Go all the way to the original parent, caching the path
var path = [];
path.push(this);
var tempMe = this;
var tempParent = tempMe.link.parent;
while (tempParent)
{
tempMe = tempParent;
tempParent = tempMe.link.parent;
path.unshift(tempMe);
}
var runningCurrentCoin = startCoin;
for (var i = 0; i < path.length - 1; i++) {
runningCurrentCoin = path[i].getPairedCoin(runningCurrentCoin);
}
return runningCurrentCoin;
}
this.setParent = function (parent)
{
this.link.parent = parent;
parent.setChildAfterCheckingForDupes(this);
}
this.setChildAfterCheckingForDupes = function(child)
{
var duplicates = false;
for (var i = 0; i < this.link.children.length; i++)
{
if(this.link.children[i].name == child.name)
{
duplicates = true;
break;
}
}
if(!duplicates)
this.link.children.push(child);
}
this.exchangeCurrency = function(currency, value)
{
var exchangeName = this.name;
if(currency == this.quoteCoin)
{
//quote coin == currency
var newValue = floorPrecision(value, this.quoteMinIncrement);
var rate = this.bid;
// console.log(exchangeName, ": buying ", newValue / rate, " ", this.baseCoin, " with ", newValue, currency, " at a rate of " , rate, ". (Ask rate is ", this.ask, ").");
// console.log("... lost dust = ", value - newValue);
return [this.baseCoin, (newValue / rate)];// * (1 - this.takerFee) ];
} else //currency == this.baseCoin
{
var rate = this.ask;
var otherValue = value * rate;
var truncatedOtherValue = floorPrecision(otherValue, this.quoteMinIncrement);
//Adjust value now
value = truncatedOtherValue / rate;
// console.log(exchangeName, ": selling ", value, currency, " for ", truncatedOtherValue, " ", this.quoteCoin, " at a rate of ", rate, ". (buy rate is ", this.bid, ").");
// console.log("... lost dust = ", otherValue - truncatedOtherValue);
return [this.quoteCoin, truncatedOtherValue];// * (1 - this.takerFee)];
}
};
}
function SupportedExchanges(supportedExchangesArray) {
this.exchanges = [];
this.exchangesUpdated = [];
for (var i = 0; i < supportedExchangesArray.length; i++) {
var ex = new Exchange(supportedExchangesArray[i]);
this.exchanges.push(ex);
};
this.insertBidAndAsk = function (exchange, bid, ask) {
for (var i = 0; i < this.exchanges.length; i++) {
if (this.exchanges[i].name == exchange.name)
{
this.exchanges[i].bid = bid;
this.exchanges[i].ask = ask;
}
}
this.exchangesUpdated.push(i);
},
this.checkIfComplete = function () {
if(this.exchangesUpdated.length == this.exchanges.length)
return true;
return false;
},
this.getExchangeObjectsWithCoin = function(coin) {
var returnArray = [];
for (var i = 0; i < this.exchanges.length; i++) {
if(this.exchanges[i].name.includes(coin))
{
var thisX = this.exchanges[i];
var tempX = new Exchange(thisX.name, thisX.link.parent, thisX.bid, thisX.ask);
returnArray.push(tempX);
}
}
return returnArray;
}
this.getExchangeObjectWithTwoCoins = function (coin1, coin2) {
for (var i = 0; i < this.exchanges.length; i++) {
if(this.exchanges[i].name.includes(coin1) && this.exchanges[i].name.includes(coin2))
{
var thisX = this.exchanges[i];
var tempX = new Exchange(thisX.name, thisX.link.parent, thisX.bid, thisX.ask);
return tempX;
}
}
return false;
}
this.getExchangeFromName = function(name) {
for (var i = 0; i < this.exchanges.length; i++) {
if(this.exchanges[i].name == name)
return this.exchanges[i];
}
}
this.exchangeCoinForCoin = function (value, startCoin, goalCoin)
{
var x = this.getExchangeObjectWithTwoCoins(startCoin, goalCoin);
// console.log(x, startCoin, goalCoin);
return x.exchangeCurrency(startCoin, value)[1];
}
}
//Helper Functions
function floorPrecision(value, precision)
{
return Math.floor(value / precision) * precision;
}
function roundPrecision(value, precision)
{
return Math.round(value / precision) * precision;
}
function getFirstCoinOfExchange(exchange)
{
return exchange.substr(0,3);
}
function getSecondCoinOfExchange(exchange)
{
return exchange.substr(4,3);
}
function getExchangeFromResponse(response, exchanges)
{
// console.log(response.socket._httpMessage.path);
var pathOfGdaxExchange = response.socket._httpMessage.path;
for (var i = 0; i < exchanges.length; i++) {
if(pathOfGdaxExchange.includes(exchanges[i].name))
return exchanges[i];
}
return "ERROR - couldn't grab exchange from GDAX api response";
}
//****************************************************************************************************
// authedClient.getProductOrderBook(
// 'BTC-USD',
// (error, response, book) => {
// console.log("GET PRODUCT ORDER BOOK for btc usd");
// // console.log(error);
// console.log(response.socket._httpMessage.path);
// console.log(getExchangeFromResponse(response, supportedExchanges));
// // console.log(obj);
// // console.log(book);
// }
// );
// const myCallback = (err, response, data) => {
// console.log(data);
// };
// const result = authedClient.getProducts(myCallback);
// const publicClient = new Gdax.PublicClient('https://api.gdax.com');
// publicClient.getProductOrderBook(
// 'ETH-USD',
// (error, response, book) => {
// console.log(book);
// }
// );
// const myCallback = (err, response, data) => {
// console.log(data);
// };
// const result = publicClient.getProducts(myCallback);
// const myCallback = (err, response, data) => {
// console.log(data);
// };
// const result = publicClient.getCurrencies(myCallback);