-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
222 lines (190 loc) · 6.33 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
var request = require('request');
const crypto = require('crypto');
var nodemailer = require('nodemailer');
const APIKEY = '[YOUR APIKEY]';
const APISECRET = '[YOUR APISECRET]';
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[your mail]@gmail.com',
pass: '[password for email]'
}
});
var mailOptions = {
from: '[your mail]@gmail.com',
to: 'email1, email2',
subject: 'Binance NEW COIN',
text: 'Added new coin'
};
var oldTradingPairs = [];
const priceLimit = 0.0001; //in BTC
const deposit = 0.01; //in BTC
function sendEmail(symbol) {
mailOptions.subject = 'Binance NEW COIN ' + symbol;
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
const signedRequest = function(url, data, callback, method = 'POST') {
let query = Object.keys(data).reduce(function(a,k){
a.push(k+'='+encodeURIComponent(data[k]));
return a
},[]).join('&');
let signature = crypto.createHmac('sha256', APISECRET).update(query).digest('hex'); // set the HMAC hash header
console.log(url+'?'+query+'&signature='+signature);
let opt = {
url: url+'?'+query+'&signature='+signature,
method: method,
headers: {
'Content-type': 'application/x-www-form-urlencoded',
'X-MBX-APIKEY': APIKEY
}
};
request(opt, function(error, response, body) {
callback(error, response, body);
});
};
function getTradingPairs() {
console.log("\nREQUEST exchangeInfo...");
request('https://api.binance.com/api/v1/exchangeInfo', function (error, response, body) {
console.log("REQUEST exchangeInfo OK");
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
var parsedBody = {};
try {
parsedBody = JSON.parse(body);
} catch (err) {
console.log(err);
setTimeout(getTradingPairs, 1000);
return;
}
const symbols = parsedBody.symbols;
console.log("symbols.length="+symbols.length);
var tradingPairs = [];
for (var i = 0; i < symbols.length; i++) {
const symbolObject = symbols[i];
if (symbolObject.status == 'TRADING') {
tradingPairs.push(symbolObject);
}
}
console.log("tradingPairs.length="+tradingPairs.length);
if (oldTradingPairs.length == 0) {
oldTradingPairs = tradingPairs;
console.log("oldTradingPairs.length="+oldTradingPairs.length+" before");
//oldTradingPairs.pop(); //todo: only for test
//oldTradingPairs.pop(); //todo: only for test
console.log("oldTradingPairs.length="+oldTradingPairs.length+" after");
setTimeout(getTradingPairs, 1000);
} else {
var newPairs = [];
for (var k = 0; k < tradingPairs.length; k++) {
const tradingPair = tradingPairs[k];
var found = false;
for (var j = 0; j < oldTradingPairs.length; j++) {
if (oldTradingPairs[j].symbol == tradingPair.symbol) {
found = true;
break;
}
}
if (!found) {
newPairs.push(tradingPair);
}
}
oldTradingPairs = tradingPairs;
if (newPairs.length == 0) {
setTimeout(getTradingPairs, 1000);
} else {
var map = [];
for (var i = 0; i < newPairs.length; i++) {
var newPair = newPairs[i];
var quotes = map[newPair.baseAsset];
if (quotes === undefined) {
quotes = [];
map[newPair.baseAsset] = quotes;
}
quotes.push(newPair.quoteAsset);
}
console.log("newPairs.length="+newPairs.length);
console.log(map);
for (var key in map) {
var quotes = map[key];
if (quotes.indexOf('BTC') >= 0/* && quotes.indexOf('ETH') >= 0*/) {
console.log('detected for ' + key);
getAsks(key + 'BTC');
//getAsks('TRXETH');
//sendEmail(key + 'ETH');
break;
}
}
}
}
});
}
getTradingPairs();
function getAsks(symbol) {
console.log("\nREQUEST depth...");
request('https://api.binance.com/api/v1/depth?symbol='+symbol, function (error, response, body) {
console.log("REQUEST depth OK");
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
const parsedBody = JSON.parse(body);
const asks = parsedBody.asks;
if (asks.length == 0) {
console.log("asks.length = 0");
return;
}
if (asks[0][0] > priceLimit) {
console.log(asks[0][0] + " BTC > " + priceLimit + " BTC");
return;
}
var totalSum = 0;
var totalQuantity = 0;
for (var i = 0; i < asks.length; i++) {
const ask = asks[i];
console.log(ask);
const price = ask[0]; //in BTC
const quantity = ask[1]; //in coins
//console.log("sum="+sum+" deposit="+deposit);
if ((totalSum + price * quantity) >= deposit) {
const currentQuantity = (deposit - totalSum) / price;
buyMarket(symbol, parseInt(totalQuantity + currentQuantity), price);
break;
}
totalSum += price * quantity;
totalQuantity += quantity;
}
});
}
function buyMarket(symbol, quantity, price) {
console.log("\nREQUEST buy market...");
const timestamp = new Date().getTime();
signedRequest('https://api.binance.com/api/v3/order',
{'symbol':symbol, 'side': 'BUY', 'type': 'MARKET', 'quantity': quantity, 'timestamp': timestamp},
function (error, response, body) {
console.log("REQUEST buy market OK");
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log(body);
const parsedBody = JSON.parse(body);
sellLimit(symbol, parsedBody.executedQty, price);
});
}
function sellLimit(symbol, quantity, price) {
console.log("\nREQUEST sell limit...");
const timestamp = new Date().getTime();
const sellPrice = price * 1.5;
signedRequest('https://api.binance.com/api/v3/order',
{'symbol':symbol, 'side': 'SELL', 'type': 'LIMIT', 'quantity': quantity, 'timestamp': timestamp, 'price': sellPrice, 'timeInForce': 'GTC'},
function (error, response, body) {
console.log("REQUEST sell limit OK");
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log(body);
sendEmail(symbol);
}
);
}