-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
290 lines (263 loc) · 8.19 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
'use strict'
/*=============================================== USER CONFIGURATION ===============================================*/
const userConfig = {
'binance': {
'apiKey': 'xxx',
'apiSecret': 'xxx',
},
'mail': {
'username': '[email protected]',
'password': 'xxx',
'host': 'imap.gmail.com',
'port': 993,
'mailbox': 'INBOX',
}
}
/*=============================================== ADMIN CONFIGURATION ==============================================*/
/*============================================ DON'T EDIT BELOW THIS LINE===========================================*/
const adminConfig = {
'mail': {
'maxMailAge': 60
},
'retry': {
'retries': 30,
'minTimeout': 1000,
'maxTimeout': 6000,
},
'tradingview': {
'mail': '[email protected]'
}
}
const _ = require('lodash'),
log = require('fancy-log'),
logSymbols = require('log-symbols'),
MailListener = require("mail-listener2-updated"),
promiseRetry = require("promise-retry"),
fetch = require("node-fetch"),
crypto = require("crypto"),
qs = require("qs")
const binance = require('binance-api-node').default
const chalk = require('chalk')
// Binance API initialization //
const binance_client = binance({
apiKey: userConfig.binance.apiKey,
apiSecret: userConfig.binance.apiSecret,
useServerTime: true,
})
log(logSymbols.info, 'Connected to ' + chalk.magenta('Binance'))
let runningMailHandler = false
let trading = {}
let stepSize = {}
let total = {}
let quantity = {}
let balance = {}
let buyPrice = {}
let orderId = {}
let mailListener = new MailListener({
username: userConfig.mail.username,
password: userConfig.mail.password,
host: userConfig.mail.host,
mailbox: userConfig.mail.mailbox,
port: userConfig.mail.port,
tls: true,
tlsOptions: {rejectUnauthorized: false},
markSeen: false
})
mailListener.start()
mailListener.on("server:connected", () => {
log(logSymbols.success, `E-Mail listener connected to ${userConfig.mail.username}`)
log(logSymbols.info, `Listening for new TradingView notifications...`)
})
mailListener.on("server:disconnected", () => {
log(logSymbols.error, `E-Mail listener disconnected. Attempting reconnect...`)
setTimeout(() => {
mailListener.restart()
}, 5* 1000)
})
mailListener.on("error", (err) => {
log(logSymbols.error, "Mail Listener Error:", err)
})
mailListener.on("mail", (mail) => {
runOneAtATime(mail)
})
/**
* Handle new incoming emails
* @param mail
*/
function handleMail(mail) {
var email_text = ""
if (mail.text) {
email_text = mail.text
}
else if (mail.html) {
email_text = mail.html
}
// E-Mail not from TradingView
if (mail.from[0].address.toString() !== adminConfig.tradingview.mail) {
log(logSymbols.info, `Email received from ${mail.from[0].address.toString()}. Ignoring since sender not TradingView.`);
return;
}
// Old email - do nothing
if (new Date(mail.receivedDate) < new Date(Date.now() - adminConfig.mail.maxMailAge * 1000)) {
log(logSymbols.info, `Email received from ${mail.from[0].address.toString()} but email already older than ${adminConfig.mail.maxMailAge}sec. Ignoring email. `);
return;
}
// R-Mail content not readable - do nothing
if (email_text === "") {
log(logSymbols.error, `Email received from ${mail.from[0].address.toString()} but email content not readable. Ignoring email. `);
return;
}
else {
if ( email_text.includes("#BCE_ACTION_START#") ) {
log(chalk.magenta("PROCESSING BINANCE ORDER"))
Binance_trade(email_text)
}
}
}
/**
* Run the Binance Trade
* @param text
*/
function Binance_trade(email_text) {
var action = email_text.substring(
email_text.lastIndexOf("#BCE_ACTION_START#") + 18,
email_text.lastIndexOf("#BCE_ACTION_END#")
).toUpperCase()
log(chalk.grey("BINANCE ACTION: "), action)
var pair = email_text.substring(
email_text.lastIndexOf("#BCE_PAIR_START#") + 16,
email_text.lastIndexOf("#BCE_PAIR_END#")
).toUpperCase()
log(chalk.grey("PAIR: "), pair)
// TD BUY ALERT TEXT FORMAT:
// #BCE_ACTION_START#BUY#BCE_ACTION_END#
// #BCE_PAIR_START#BTCUSDT#BCE_PAIR_END#
// #BCE_TOT_START#15#BCE_TOT_END#
if (action === 'BUY') {
total[pair] = parseFloat(email_text.substring(
email_text.lastIndexOf("#BCE_TOT_START#") + 15,
email_text.lastIndexOf("#BCE_TOT_END#")
))
log(chalk.grey("TOTAL VALUE: "), total[pair])
if (trading[pair]) {
// EXISTING TRADING PAIR //
buy_at_market_price(pair)
}
else {
// NEW TRADING PAIR
// FIND OUT IF PAIR EXISTS:
binance_client.exchangeInfo().then(results => {
// CHECK IF PAIR IS UNKNOWN:
if (_.filter(results.symbols, {symbol: pair}).length > 0) {
// PAIR EXISTS
stepSize[pair] = _.filter(results.symbols, {symbol: pair})[0].filters[1].stepSize
buy_at_market_price(pair)
}
// PAIR UNKNOWN:
else {
log(chalk.yellow(pair + " => This pair is unknown to Binance." ))
return
}
})
}
}
// TD SELL ALERT TEXT FORMAT:
// #BCE_ACTION_START#SELL#BCE_ACTION_END#
// #BCE_PAIR_START#BTCUSDT#BCE_PAIR_END#
else if (action === 'SELL') {
if (trading[pair] && balance[pair]) {
log(chalk.keyword('orange')("SELLING " + balance[pair] + " OF " + pair + " AT MARKET PRICE" ))
binance_client.order({
symbol: pair,
side: 'SELL',
type: 'MARKET',
quantity: balance[pair],
recvWindow: 1000000
})
.then( order => {
orderId[pair] = order.orderId
log(logSymbols.success, chalk.grey("SELL MARKET ORDER SET "))
check_market_order(pair, order.orderId)
balance[pair] = 0
quantity[pair] = 0
})
.catch( error => {
log(logSymbols.error, "MARKET SELL ERROR " + error )
return
})
}
else {
log(chalk.keyword('orange')("THIS PAIR IS NOT YET TRADED: " + pair ))
}
}
}
function buy_at_market_price(pair) {
// GET ORDER BOOK TO FIND OUT OUR BUY PRICE
binance_client.book({ symbol: pair }).then(results => {
// SO WE CAN TRY TO BUY AT THE 1ST BID PRICE + %0.02:
buyPrice[pair] = parseFloat(results.asks[0].price)
log(chalk.grey("CURRENT 1ST ASK PRICE : " + buyPrice[pair]))
var precision = stepSize[pair].toString().split('.')[1].length || 0
quantity[pair] = (( ((total[pair] / buyPrice[pair]) / parseFloat(stepSize[pair])) | 0 ) * parseFloat(stepSize[pair])).toFixed(precision)
log(chalk.grey("BUYING " + quantity[pair] + " OF " + pair + " AT MARKET PRICE" ))
// SETUP MARKET BUY ORDER
binance_client.order({
symbol: pair,
side: 'BUY',
type: 'MARKET',
quantity: quantity[pair],
recvWindow: 1000000
})
.then((order) => {
orderId[pair] = order.orderId
trading[pair] = true
if (balance[pair]) {
var precision = stepSize[pair].toString().split('.')[1].length || 0
balance[pair] = (parseFloat(balance[pair]) + parseFloat(quantity[pair])).toFixed(precision)
}
else {
balance[pair] = quantity[pair]
}
log(logSymbols.success, chalk.grey("BUY MARKET ORDER SET"))
check_market_order(pair, order.orderId)
})
.catch((error) => {
log(logSymbols.error, "BUY MARKET ERROR " + error)
})
})
}
function check_market_order(pair, orderId) {
binance_client.getOrder({
symbol: pair,
orderId: orderId,
recvWindow: 1000000
})
.then( order => {
if (order.status === "FILLED") {
log(logSymbols.success, chalk.gray("MARKET ORDER FILLED "))
return
}
else {
log(logSymbols.warning, chalk.gray("MARKET ORDER NOT YET FILLED "))
check_market_order(pair, orderId)
}
})
.catch( error => {
//log(logSymbols.error, "CHECK MARKET ORDER API ERROR " + error )
return
})
}
/**
* Make sure that only one mail is being handled at a time
*/
function runOneAtATime(mail) {
if (runningMailHandler) {
setTimeout(runOneAtATime, 100, mail);
}
else {
runningMailHandler = true;
handleMail(mail);
log(logSymbols.info, 'Listening for new TradeingView notifications...');
runningMailHandler = false;
}
}