forked from amavis442/GDAX-Trading-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
405 lines (304 loc) · 11.3 KB
/
index.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
#!/usr/bin/env node
/*
============================================================================
Name : GDAX Trading Bot
Author : Kenshiro
Version : 7.08
Copyright : GNU General Public License (GPLv3)
Description : Trading bot for the Coinbase Pro exchange
============================================================================
*/
const APP_VERSION = "v7.08";
const GdaxModule = require('coinbase-pro');
const PASSPHRASE = process.env.TRADING_BOT_PASSPHRASE || '';
const KEY = process.env.TRADING_BOT_KEY || '';
const SECRET = process.env.TRADING_BOT_SECRET || '';
const GDAX_URI = 'https://api.pro.coinbase.com';
const MKR_BTC_CURRENCY_PAIR = 'MKR-BTC';
const ETH_BTC_CURRENCY_PAIR = 'ETH-BTC';
const BITCOIN_TICKER = 'BTC';
const MAKER_TICKER = 'MKR';
const ETHEREUM_TICKER = 'ETH';
const SLEEP_TIME = 30000;
// The seed is the amount of coins that the program will trade continuously
const SEED_MKR_AMOUNT = 0.01;
const SEED_ETH_AMOUNT = 0.01;
// Profit percentage trading a seed
const PROFIT_PERCENTAGE = 2.0;
const MINIMUM_BUY_PRICE_MULTIPLIER = 101.0 / 100.0;
const SELL_PRICE_MULTIPLIER = (100.0 + PROFIT_PERCENTAGE) / 100.0;
let askPriceMAKER = null;
let averagePriceMAKER = null;
let lastBuyOrderIdMAKER = null;
let lastBuyOrderPriceMAKER = null;
let askPriceETH = null;
let averagePriceETH = null;
let lastBuyOrderIdETH = null;
let lastBuyOrderPriceETH = null;
let btcAvailable = 0;
let btcBalance = 0;
let mkrAvailable = 0;
let mkrBalance = 0;
let ethAvailable = 0;
let ethBalance = 0;
let numberOfCyclesCompleted = 0;
let estimatedProfit = 0;
let authenticatedClient = null;
let publicClient = null;
// Callbacks
const buyOrderCallbackMAKER = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.status==='pending'))
lastBuyOrderIdMAKER = data.id;
return console.log(data);
}
const buyOrderCallbackETH = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.status==='pending'))
lastBuyOrderIdETH = data.id;
return console.log(data);
}
const sellOrderCallbackMAKER = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.status==='pending'))
{
estimatedProfit = estimatedProfit + SEED_MKR_AMOUNT * (parseFloat(data.price) - lastBuyOrderPriceMAKER);
averagePriceMAKER = lastBuyOrderPriceMAKER;
lastBuyOrderPriceMAKER = null;
lastBuyOrderIdMAKER = null;
numberOfCyclesCompleted++;
}
return console.log(data);
}
const sellOrderCallbackETH = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.status==='pending'))
{
estimatedProfit = estimatedProfit + SEED_ETH_AMOUNT * (parseFloat(data.price) - lastBuyOrderPriceETH);
averagePriceETH = lastBuyOrderPriceETH;
lastBuyOrderPriceETH = null;
lastBuyOrderIdETH = null;
numberOfCyclesCompleted++;
}
return console.log(data);
}
const getProductTickerCallbackMAKER = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.ask!=null) && (data.time!=null))
{
askPriceMAKER = parseFloat(data.ask);
if (averagePriceMAKER===null)
console.log("[MAKER TICKER] Now: " + askPriceMAKER.toFixed(5) + " BTC, time: " + data.time);
else
console.log("[MAKER TICKER] Now: " + askPriceMAKER.toFixed(5) + " BTC, average: " + averagePriceMAKER.toFixed(5) + " BTC, time: " + data.time);
const buyPrice = askPriceMAKER * SEED_MKR_AMOUNT;
if ((btcAvailable>=buyPrice) && (averagePriceMAKER!=null) && (lastBuyOrderIdMAKER===null))
placeBuyOrderMAKER();
else if ((mkrAvailable>=SEED_MKR_AMOUNT) && (lastBuyOrderIdMAKER!=null))
placeSellOrderMAKER();
if (averagePriceMAKER===null)
averagePriceMAKER = askPriceMAKER;
else
averagePriceMAKER = (averagePriceMAKER * 10000 + askPriceMAKER) / 10001;
setTimeout(()=>publicClient.getProductTicker(ETH_BTC_CURRENCY_PAIR, getProductTickerCallbackETH), 10000);
}
}
const getProductTickerCallbackETH= (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.ask!=null) && (data.time!=null))
{
askPriceETH = parseFloat(data.ask);
if (averagePriceETH==null)
console.log("\n[ETHER TICKER] Now: " + askPriceETH.toFixed(5) + " BTC, time: " + data.time);
else
console.log("\n[ETHER TICKER] Now: " + askPriceETH.toFixed(5) + " BTC, average: " + averagePriceETH.toFixed(5) + " BTC, time: " + data.time);
const buyPrice = askPriceETH * SEED_ETH_AMOUNT;
if ((btcAvailable>=buyPrice) && (averagePriceETH!=null) && (lastBuyOrderIdETH===null))
placeBuyOrderETH();
else if ((ethAvailable>=SEED_ETH_AMOUNT) && (lastBuyOrderIdETH!=null))
placeSellOrderETH();
if (averagePriceETH===null)
averagePriceETH = askPriceETH;
else
averagePriceETH = (averagePriceETH * 10000 + askPriceETH) / 10001;
}
}
const getAccountsCallback = (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (Symbol.iterator in Object(data)))
{
for(var item of data)
{
if (item.currency===BITCOIN_TICKER)
{
btcAvailable = parseFloat(item.available);
btcBalance = parseFloat(item.balance);
}
else if (item.currency===MAKER_TICKER)
{
mkrAvailable = parseFloat(item.available);
mkrBalance = parseFloat(item.balance);
}
else if (item.currency===ETHEREUM_TICKER)
{
ethAvailable = parseFloat(item.available);
ethBalance = parseFloat(item.balance);
}
}
console.log("[BITCOIN WALLET] Available: " + btcAvailable.toFixed(8) + " BTC, Balance: " + btcBalance.toFixed(8) + " BTC");
console.log("[MAKER WALLET] Available: " + mkrAvailable.toFixed(8) + " MKR, Balance: " + mkrBalance.toFixed(8) + " MKR");
console.log("[ETHER WALLET] Available: " + ethAvailable.toFixed(8) + " ETH, Balance: " + ethBalance.toFixed(8) + " ETH\n");
console.log("[INFO] Number of cycles completed: " + numberOfCyclesCompleted + ", estimated profit: " + estimatedProfit.toFixed(8) + " BTC\n");
publicClient.getProductTicker(MKR_BTC_CURRENCY_PAIR, getProductTickerCallbackMAKER);
}
}
const getFilledPriceCallbackMAKER = (error, response, data) =>
{
if (error)
return console.log(error);
if ((Array.isArray(data)) && (data.length >= 1))
{
lastBuyOrderPriceMAKER = parseFloat(data[0].price);
let highestPrice;
if (askPriceMAKER>lastBuyOrderPriceMAKER)
highestPrice = askPriceMAKER;
else
highestPrice = lastBuyOrderPriceMAKER;
const sellPrice = highestPrice * SELL_PRICE_MULTIPLIER;
const sellSize = mkrAvailable - 0.0000001;
const sellParams =
{
'price': sellPrice.toFixed(5),
'size': sellSize.toFixed(6),
'product_id': MKR_BTC_CURRENCY_PAIR,
'post_only': true,
};
console.log("");
console.log("\x1b[41m%s\x1b[0m", "[SELL ORDER] Price: " + sellPrice.toFixed(5) + " BTC, size: " + sellSize.toFixed(2) + " MKR");
setTimeout(()=>authenticatedClient.sell(sellParams, sellOrderCallbackMAKER), 3000);
}
return console.log(data);
}
const getFilledPriceCallbackETH = (error, response, data) =>
{
if (error)
return console.log(error);
if ((Array.isArray(data)) && (data.length >= 1))
{
lastBuyOrderPriceETH = parseFloat(data[0].price);
let highestPrice;
if (askPriceETH>lastBuyOrderPriceETH)
highestPrice = askPriceETH;
else
highestPrice = lastBuyOrderPriceETH;
const sellPrice = highestPrice * SELL_PRICE_MULTIPLIER;
const sellSize = ethAvailable - 0.000000001;
const sellParams =
{
'price': sellPrice.toFixed(5),
'size': sellSize.toFixed(8),
'product_id': ETH_BTC_CURRENCY_PAIR,
'post_only': true,
};
console.log("");
console.log("\x1b[41m%s\x1b[0m", "[SELL ORDER] Price: " + sellPrice.toFixed(5) + " BTC, size: " + sellSize.toFixed(2) + " ETH");
setTimeout(()=>authenticatedClient.sell(sellParams, sellOrderCallbackETH), 3000);
}
return console.log(data);
}
// Functions
function placeBuyOrderMAKER()
{
const minimumBuyPrice = averagePriceMAKER * MINIMUM_BUY_PRICE_MULTIPLIER;
if (askPriceMAKER>=minimumBuyPrice)
{
const buySize = SEED_MKR_AMOUNT;
const buyParams =
{
'size': buySize.toFixed(2),
'product_id': MKR_BTC_CURRENCY_PAIR,
'type': 'market'
};
console.log("");
console.log("\x1b[42m%s\x1b[0m", "[BUY ORDER] Size: " + buySize.toFixed(2) + " MKR");
authenticatedClient.buy(buyParams, buyOrderCallbackMAKER);
}
}
function placeBuyOrderETH()
{
const minimumBuyPrice = averagePriceETH * MINIMUM_BUY_PRICE_MULTIPLIER;
if (askPriceETH>=minimumBuyPrice)
{
const buySize = SEED_ETH_AMOUNT;
const buyParams =
{
'size': buySize.toFixed(2),
'product_id': ETH_BTC_CURRENCY_PAIR,
'type': 'market'
};
console.log("");
console.log("\x1b[42m%s\x1b[0m", "[BUY ORDER] Size: " + buySize.toFixed(2) + " ETH");
authenticatedClient.buy(buyParams, buyOrderCallbackETH);
}
}
function placeSellOrderMAKER()
{
const params =
{
order_id: lastBuyOrderIdMAKER
};
authenticatedClient.getFills(params, getFilledPriceCallbackMAKER);
}
function placeSellOrderETH()
{
const params =
{
order_id: lastBuyOrderIdETH
};
authenticatedClient.getFills(params, getFilledPriceCallbackETH);
}
// Main logic
console.log("\n");
console.log(" __________ ___ _ __ ______ ___");
console.log(" / ____/ __ \\/ | | |/ / /_ __/________ _____/ (_)___ ____ _");
console.log(" / / __/ / / / /| | | / / / / ___/ __ `/ __ / / __ \\/ __ `/");
console.log(" / /_/ / /_/ / ___ |/ | / / / / / /_/ / /_/ / / / / / /_/ / ");
console.log(" \\____/_____/_/ |_/_/|_| /_/ /_/ \\__,_/\\__,_/_/_/ /_/\\__, /");
console.log(" /____/");
console.log(" ____ __");
console.log(" / __ )____ / /_");
console.log(" / __ / __ \\/ __/");
console.log(" / /_/ / /_/ / /_ ");
console.log(" /_____/\\____/\\__/ " + APP_VERSION);
console.log("\n\n\n\n \"The Revolution Will Be Decentralized\"");
console.log("\n\n\n\nConnecting to Coinbase Pro in " + parseInt(SLEEP_TIME/1000) + " seconds ...");
setInterval(() =>
{
console.log('\n\n');
askPriceMAKER = null;
askPriceETH = null;
btcAvailable = 0;
btcBalance = 0;
mkrAvailable = 0;
mkrBalance = 0;
ethAvailable = 0;
ethBalance = 0;
publicClient = new GdaxModule.PublicClient(GDAX_URI);
authenticatedClient = new GdaxModule.AuthenticatedClient(KEY, SECRET, PASSPHRASE, GDAX_URI);
// Get the balance of the wallets and execute the trading strategy
authenticatedClient.getAccounts(getAccountsCallback);
}, SLEEP_TIME);