-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
422 lines (291 loc) · 9.46 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Importing Modules
*/
var poloniex = require('plnx');
var nodemailer = require('nodemailer');
/**
* Importing file dependencies
*/
var config = require('./config.js');
var params = require('./params.js');
/** Trade values */
var last, lowestAsk, highestBid, percentChange, baseVolume, quoteVolume, isFrozen, high24hr, low24hr;
var Bot = function() {
this.order = null;
this.basePrice = null;
this.priceToBuy = null;
this.priceToSell = null;
this.securityMargin = null;
this.latestPrices = [];
this.waitingGrow = false;
this.lastBasePriceUpdate = null;
this.canBuy = false;
this.waitingOrderCallback = false;
this.hasOpenedOrders = false;
this.balance = 0;
this.altcoinBalance = 0;
this.loadBalance();
};
Bot.prototype.loadBalance = function() {
poloniex.returnCompleteBalances({key: config.KEY, secret: config.SECRET}, function(err, data) {
if (err) {
bot.log('=== ERROR ===', 'There was an error to retrieve your balance. Verify your API Key and try again.');
} else {
var availableBalance = Number(data.BTC.available).toFixed(8);
var onOrderBalance = Number(data.BTC.onOrders).toFixed(8);
if (availableBalance || onOrderBalance) {
if (onOrderBalance > 0) {
bot.log('=== ERROR ===', 'You already have orders, please remove these orders to start trading.');
} else {
bot.balance = availableBalance;
bot.canBuy = true;
bot.startBot();
}
} else {
bot.log('=== ERROR ===', 'You don\'t have enough BTC balance.');
}
}
});
};
Bot.prototype.startBot = function() {
bot.log('=== BOT STARTED ===', 'current balance: ' + this.balance + ' BTC');
this.setCurrencyPair();
this.initTasks();
this.subscribeToTicker();
};
Bot.prototype.setCurrencyPair = function() {
if (process.argv[2]) {
config.CURRENCY = process.argv[2];
}
};
Bot.prototype.initTasks = function() {
this.dailyTasks();
// set interval for daily tasks
setInterval(function() {
bot.dailyTasks();
}, 86400 * 1000);
};
Bot.prototype.dailyTasks = function() {
this.analyzeBestCurrenciesToTrade();
};
Bot.prototype.analyzeBestCurrenciesToTrade = function() {
poloniex.returnTicker(function(err, data) {
var recommendedCurrencies = [];
if (data) {
for (var currencyPair in data) {
// only BTC market now
if (currencyPair.substr(0, 4) == 'BTC_') {
var currencyData = data[currencyPair];
var volume = currencyData.baseVolume;
if (volume > params.MINIMUM_VOLUME_GOOD_CURRENCY) {
var variation24h = (100 - (currencyData.high24hr * 100 / currencyData.low24hr)) * -1;
if (variation24h >= params.MINIMUM_VARIATION_SUGGEST_TRADE) {
var distanceLow = currencyData.last - currencyData.low24hr;
var distanceHigh = currencyData.high24hr - currencyData.last;
if (distanceHigh < distanceLow) {
currencyData.variation24h = variation24h;
recommendedCurrencies[currencyPair] = currencyData;
}
}
}
}
}
var message = '';
if (recommendedCurrencies) {
for (var currencyPair in recommendedCurrencies) {
var currency = recommendedCurrencies[currencyPair];
message += currencyPair + ' VOL ' + currency.baseVolume + ' VAR 24h ' + currency.variation24h.toFixed(2) + '%\n';
}
} else {
message = 'BOT DIDN\'T DETECT A GOOD CURRENCY FOR TRADING';
}
bot.log('=== RECOMMENDED CURRENCIES TO TRADE TODAY ===', message, true);
}
});
};
Bot.prototype.subscribeToTicker = function() {
poloniex.push(function(session) {
session.subscribe('ticker', function(data) {
if (data[0] == config.CURRENCY) {
last = data[1];
lowestAsk = data[2];
highestBid = data[3];
percentChange = data[4] * 100;
baseVolume = data[5];
quoteVolume = data[6];
isFrozen = data[7];
high24hr = data[8];
low24hr = data[9];
bot.updateLatestPrices();
bot.tickerUpdate();
}
});
});
};
Bot.prototype.updateLatestPrices = function() {
this.latestPrices.unshift(last);
if (this.latestPrices.length > params.MAX_LATEST_PRICES) {
this.latestPrices.pop();
}
};
Bot.prototype.tickerUpdate = function() {
if (this.waitingGrow) {
if (this.identifyLatestPricesDirection() > 0) {
this.waitingGrow = false;
}
} else {
if (this.hasOpenedOrders) {
this.verifyOpenOrders();
} else {
if (!this.basePrice || (this.lastBasePriceUpdate && this.getCurrentTime() - this.lastBasePriceUpdate > params.TIME_DELAY_PRICE)) {
this.basePrice = last;
this.priceToBuy = (this.basePrice * ((100 - params.MARGIN_TO_BUY) / 100)).toFixed(8);
this.securityMargin = (this.basePrice * ((100 - params.SECURITY_MARGIN) / 100)).toFixed(8);
this.lastBasePriceUpdate = this.getCurrentTime();
var message = 'base price: ' + this.basePrice + '\nprice target to buy: ' + this.priceToBuy;
bot.log('=== PRICE INFORMATION UPDATE ===', message);
} else if (!this.order && last <= this.priceToBuy && last > this.securityMargin && this.canBuy) {
this.buy();
} else if (this.order && last >= this.priceToSell) {
this.sell();
} else if (this.order && last <= this.securityMargin) {
bot.log('=== SECURITY MARGIN REACHED ===', 'Currency decreased to the security level you have defined. Bot will sell all your coins and wait until currency recover its value');
this.waitingGrow = true;
this.sell();
}
}
}
};
Bot.prototype.verifyOpenOrders = function() {
if (this.hasOpenedOrders) {
poloniex.returnOpenOrders({
currencyPair: config.CURRENCY,
key: config.KEY,
secret: config.SECRET
}, function(err, data) {
if (!err && data) {
if (data.length == 0) {
var currencyPair = config.CURRENCY.split('_');
var currencyB = currencyPair[1];
poloniex.returnCompleteBalances({key: config.KEY, secret: config.SECRET}, function(err, data) {
if (!err && data) {
bot.altcoinBalance = data[currencyB].available;
bot.balance = data.BTC.available;
bot.hasOpenedOrders = false;
bot.canBuy = true;
} else {
//bot.log('*** POLONIEX CALLBACK | returnCompleteBalances ***', err);
}
});
}
} else {
//bot.log('*** POLONIEX CALLBACK | returnOpenOrders ***', err);
}
});
}
};
Bot.prototype.buy = function() {
if (this.canBuy && !this.waitingOrderCallback) {
this.waitingOrderCallback = true;
var rate = last;
var amount = this.balance / last;
poloniex.buy({
currencyPair: config.CURRENCY,
rate: rate,
amount: amount,
key: config.KEY,
secret: config.SECRET
}, function(err, data) {
bot.waitingOrderCallback = false;
if (!err && data) {
bot.log('*** POLONIEX CALLBACK | buy ***', data);
bot.canBuy = false;
bot.order = {
rate: rate,
amount: amount,
orderNumber: data.orderNumber
};
bot.hasOpenedOrders = true;
bot.priceToSell = (bot.order.rate * ((100 + params.MARGIN_TO_SELL) / 100)).toFixed(8);
bot.securityMargin = (bot.basePrice * ((100 - params.SECURITY_MARGIN) / 100)).toFixed(8);
var message = 'type: BUY\n' + 'price: ' + bot.order.rate + '\ntarget to sell: ' + bot.priceToSell + '\nsecurity margin: ' + bot.securityMargin;
bot.log('=== ORDER INFORMATION ===', message, true);
} else {
bot.log('*** POLONIEX CALLBACK | buy ***', err);
}
});
}
};
Bot.prototype.sell = function() {
if (!this.waitingOrderCallback) {
this.waitingOrderCallback = true;
poloniex.sell({
currencyPair: config.CURRENCY,
rate: last,
amount: this.altcoinBalance,
key: config.KEY,
secret: config.SECRET
}, function(err, data) {
bot.waitingOrderCallback = false;
if (!err && data) {
bot.log('*** POLONIEX CALLBACK | sell ***', data);
var message = 'type: SELL\n' + 'price: ' + last + '\nprofit: ' + (last * 100 / bot.order.rate - 100).toFixed(2) + '%'
bot.log('=== ORDER INFORMATION ===', message, true);
bot.hasOpenedOrders = true;
bot.order = null;
bot.basePrice = null;
bot.priceToBuy = null;
bot.priceToSell = null;
} else {
bot.log('*** POLONIEX CALLBACK | sell ***', err);
}
});
}
};
Bot.prototype.differenceFirstLastPrices = function() {
if (this.latestPrices[0] > this.latestPrices[this.latestPrices - 1]) {
return 1;
} else if (this.latestPrices[0] < this.latestPrices[this.latestPrices - 1]) {
return -1;
} else {
return 0;
}
};
Bot.prototype.identifyLatestPricesDirection = function() {
// return positive or negative (> 0 = growing, < 0 = falling)
var direction = 0;
for (var i = 0; i < this.latestPrices.length - 1; i++) {
var price = this.latestPrices[i];
var previousPrice = this.latestPrices[i+1];
if (price > previousPrice) {
direction++;
} else if (price < previousPrice) {
direction--;
}
}
return direction;
};
Bot.prototype.getCurrentTime = function() {
return new Date().getTime() / 1000;
};
Bot.prototype.log = function(subject, message, sendEmail) {
console.log(subject);
console.log(message);
console.log('');
if (sendEmail && config.SMTP) {
var transporter = nodemailer.createTransport(config.SMTP_PROTOCOL + '://' + config.SMTP_EMAIL + ':' + config.SMTP_PASSWORD + '@' + config.SMTP_HOST);
// setup e-mail data with unicode symbols
var mailOptions = {
from: config.SMTP_EMAIL, // sender address
to: config.SMTP_EMAIL, // list of receivers
subject: subject, // Subject line
text: message // plaintext body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error) return console.log(error);
});
}
};
/** Start trading (!!!) */
var bot = new Bot();