forked from amavis442/GDAX-Trading-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
310 lines (246 loc) · 10.6 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
#!/usr/bin/env node
/*
============================================================================
Name : GDAX Trading Bot
Author : Kenshiro, ahaenggli
Version : 8.01
Copyright : GNU General Public License (GPLv3)
Description : Trading bot for the Coinbase Pro exchange
============================================================================
*/
'use strict';
const APP_VERSION = "v8.01";
const GdaxModule = require('coinbase-pro');
// .env Files are awesome
const dotenv = require('dotenv');
dotenv.config();
// load environment vars or empty
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 SLEEP_TIME = 30000;
// Profit percentage trading a seed
const PROFIT_PERCENTAGE = 2.00;
const MINIMUM_BUY_PRICE_MULTIPLIER = 100.5 / 100.0;
const SELL_PRICE_MULTIPLIER = (100.0 + PROFIT_PERCENTAGE) / 100.0;
const CURRENCY_PAIRS =
{
LTC_BTC : {Name:'LTC-BTC',Rounding:6,SEED_AMOUNT:1.00},
ETH_BTC : {Name:'ETH-BTC',Rounding:5,SEED_AMOUNT:1.00},
XLM_BTC : {Name:'XLM-BTC',Rounding:8,SEED_AMOUNT:1.00},
EOS_BTC : {Name:'EOS-BTC',Rounding:6,SEED_AMOUNT:1.00},
ZRX_BTC : {Name:'ZRX-BTC',Rounding:8,SEED_AMOUNT:1.00},
ETC_BTC : {Name:'ETC-BTC',Rounding:6,SEED_AMOUNT:1.00},
XTZ_BTC : {Name:'XTZ-BTC',Rounding:8,SEED_AMOUNT:1.00}
};
const TICKERS = {};
let numberOfCyclesCompleted = 0;
let estimatedProfit = 0;
let authenticatedClient = null;
let publicClient = null;
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
const sellOrderCallbackGeneric = async(error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.status==='pending'))
{
var myProduct = data.product_id;
var myKey = myProduct.replace('-','_');
var cur = myProduct.split('-')[0];
var btc = myProduct.split('-')[1];
estimatedProfit = estimatedProfit + CURRENCY_PAIRS[myKey]['SEED_AMOUNT'] * (parseFloat(data.price) - TICKERS[cur]['lastBuyOrderPrice']);
TICKERS[cur]['averagePrice'] = TICKERS[cur]['lastBuyOrderPrice'];
TICKERS[cur]['lastBuyOrderPrice'] = null;
TICKERS[cur]['lastBuyOrderId'] = null;
numberOfCyclesCompleted++;
}
return console.log(data);
}
const getFilledPriceCallbackGeneric = async(error, response, data) =>
{
if (error)
return console.log(error);
if ((Array.isArray(data)) && (data.length >= 1))
{
var myProduct = data[0].product_id;
var myKey = myProduct.replace('-','_');
var cur = myProduct.split('-')[0];
var btc = myProduct.split('-')[1];
TICKERS[cur]['lastBuyOrderPrice'] = null;
TICKERS[cur]['lastBuyOrderPrice'] = parseFloat(data[0].price);
let highestPrice;
if (TICKERS[cur]['askPrice'] > TICKERS[cur]['lastBuyOrderPrice'])
highestPrice = TICKERS[cur]['askPrice'];
else
highestPrice = TICKERS[cur]['lastBuyOrderPrice'];
const sellPrice = highestPrice * SELL_PRICE_MULTIPLIER;
const sellSize = TICKERS[cur]['Available'] - 0.000000001;
const sellParams =
{
'price': sellPrice.toFixed(CURRENCY_PAIRS[myKey]['Rounding']),
'size': sellSize.toFixed(4),
'product_id': myProduct,
'post_only': true,
};
console.log("");
console.log("\x1b[41m%s\x1b[0m", "[SELL ORDER <" + myKey + ">] Price: " + sellPrice.toFixed(CURRENCY_PAIRS[myKey]['Rounding']) + ", size: " + sellSize.toFixed(2) + "");
setTimeout(()=>authenticatedClient.sell(sellParams, sellOrderCallbackGeneric), 3000);
}
return; //console.log(data);
}
async function placeGenericSellOrder(lastId, product_id)
{
if(lastId !== undefined && lastId != null)
await authenticatedClient.getFills({ order_id: lastId }, getFilledPriceCallbackGeneric);
else await authenticatedClient.getFills({ product_id: product_id }, getFilledPriceCallbackGeneric);
}
const buyOrderCallbackGeneric = async(error, response, data) =>
{
if (error)
return console.log(error);
if ((data != null) && (data.status === 'pending')){
var myTicker = data.product_id;
myTickerKey = myTicker.replace('-', '_');
var cur = myTicker.split('-')[0];
var btc = myTicker.split('-')[1];
console.log(data.id);
TICKERS[cur]['lastBuyOrderId'] = data.id;
}
return console.log(data);
}
async function placeGenericBuyOrder(cur_pair, cur_key)
{
var cur = cur_pair.split('-')[0];
var btc = cur_pair.split('-')[1];
const minimumBuyPrice = TICKERS[cur]['averagePrice'] * MINIMUM_BUY_PRICE_MULTIPLIER;
if (TICKERS[cur]['askPrice'] >= minimumBuyPrice)
{
const buySize = CURRENCY_PAIRS[cur_key]['SEED_AMOUNT'];
const buyParams =
{
'size': buySize.toFixed(CURRENCY_PAIRS[cur_key]['ROUNDING']),
'product_id': cur_pair,
'type': 'market'
};
console.log("");
console.log("\x1b[42m%s\x1b[0m", "[BUY ORDER] Size: " + buySize.toFixed(CURRENCY_PAIRS[cur_key]['ROUNDING']) + " "+cur);
console.log(buyParams);
await authenticatedClient.buy(buyParams, buyOrderCallbackGeneric);
}
}
const getGenericProductTickerCallback = async (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (data.ask!=null) && (data.time!=null))
{
var myTicker = (response['request']['href']);
myTicker = myTicker.replace('https://api.pro.coinbase.com/products/', '');
myTicker = myTicker.replace('/ticker', '');
var myTickerKey = myTicker.replace('-', '_');
var cur = myTicker.split('-')[0];
var btc = myTicker.split('-')[1];
// no trading possible...
if(!TICKERS[cur]['trading_enabled']) return;
// disabled trading with amount === 0
if(CURRENCY_PAIRS[myTickerKey]['SEED_AMOUNT'] === 0) return;
TICKERS[cur]['askPrice'] = parseFloat(data.ask);
if (TICKERS[cur]['averagePrice']===null)
console.log("["+cur+" TICKER] Now: " + TICKERS[cur]['askPrice'].toFixed(8) + ", time: " + data.time);
else
console.log("["+cur+" TICKER] Now: " + TICKERS[cur]['askPrice'].toFixed(8) + ", average: " + TICKERS[cur]['averagePrice'].toFixed(8) + ", time: " + data.time);
const buyPrice = TICKERS[cur]['askPrice'] * CURRENCY_PAIRS[myTickerKey]['SEED_AMOUNT'];
if ((TICKERS[btc]['Available'] >= buyPrice) && (TICKERS[cur]['averagePrice'] != null) && TICKERS[cur]['Available'] <= 0)
placeGenericBuyOrder(myTicker,myTickerKey);
else if ((TICKERS[cur]['Available'] >= CURRENCY_PAIRS[myTickerKey]['SEED_AMOUNT']))
placeGenericSellOrder(TICKERS[cur]['lastBuyOrderId'], myTicker);
if (TICKERS[cur]['averagePrice'] === null)
TICKERS[cur]['averagePrice'] = TICKERS[cur]['askPrice'];
else
TICKERS[cur]['averagePrice'] = (TICKERS[cur]['averagePrice'] * 1000 + TICKERS[cur]['askPrice']) / 1001;
}
}
const getAccountsCallback = async (error, response, data) =>
{
if (error)
return console.log(error);
if ((data!=null) && (Symbol.iterator in Object(data)))
{
for(const item of data)
{
if(TICKERS[item.currency] !== undefined){
TICKERS[item.currency]['Available'] = parseFloat(item.available);
TICKERS[item.currency]['Balance'] = parseFloat(item.balance);
TICKERS[item.currency]['trading_enabled'] = item.trading_enabled;
if(TICKERS[item.currency]['Pair'] !== undefined // is it pair i wanna trade?
&& TICKERS[item.currency]['trading_enabled'] // is trading enabled?
&& CURRENCY_PAIRS[TICKERS[item.currency]['Pair']]['SEED_AMOUNT'] > 0) // did i configure bigger 0?
{
await publicClient.getProductTicker(CURRENCY_PAIRS[TICKERS[item.currency]['Pair']]['Name'], getGenericProductTickerCallback);
// wait for 1 second (because of API limitations...)
await sleep(1000);
}
//console.log(item);
}
}
console.table(TICKERS);
console.log("[INFO] Number of cycles completed: " + numberOfCyclesCompleted + ", estimated profit: " + estimatedProfit.toFixed(8) + " BTC\n");
}
}
// 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 ...");
/* just once upon a time */
TICKERS['BTC'] = {
Pair: undefined,
Available : 0,
Balance : 0,
trading_enabled : false
};
for(var x in CURRENCY_PAIRS){
var c1 = x.split('_')[0];
if(CURRENCY_PAIRS[x].SEED_AMOUNT > 0)
TICKERS[c1] = {
Pair: x,
askPrice : null,
Available : 0,
Balance : 0,
trading_enabled : false,
lastBuyOrderId: null,
averagePrice: null
};
}
/* every X miliseconds */
setInterval(async () =>
{
console.log('\n\n');
//set to default each time
for(var x in CURRENCY_PAIRS){
if(CURRENCY_PAIRS[x].SEED_AMOUNT > 0){
var c1 = x.split('_')[0];
TICKERS[c1]['askPrice'] = null;
TICKERS[c1]['Available'] = 0;
TICKERS[c1]['Balance'] = 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
await authenticatedClient.getAccounts(getAccountsCallback);
}, SLEEP_TIME);