-
Notifications
You must be signed in to change notification settings - Fork 37
/
pump.js
663 lines (590 loc) · 16.8 KB
/
pump.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
const isWin = process.platform === 'win32'
/**
* Proxy, uncomment for proxy usage
*/
// if (isWin) {
// process.env.socks_proxy = 'socks5h://127.0.0.1:1084'
// } else {
// process.env.socks_proxy = 'socks5h://127.0.0.1:1086'
// }
// Imports
const chalk = require('chalk')
const readline = require('readline')
const Binance = require('node-binance-api')
const config = require('./config.js')
const pumpConfig = require('./pump-config.js')
const utils = require('./utils.js')
const { API_KEY, API_SECRET, HTTP_INTERVAL } = config
if (!API_KEY || !API_SECRET) {
console.error(chalk.red('PLEASE FILL YOUR API KEY & API SECRET IN config.js'))
process.exit()
}
const {
TRADE_IN,
HARD_TAKE_PROFIT,
HARD_STOP_LOSS,
MAX_DRAWBACK,
MAX_DRAWBACK_START,
BUY_UPON_SYMBOL,
// SOFT_TAKE_PROFIT,
// SOFT_TAKE_PROFIT_PERCENT,
PEAK_TAKE_PROFIT_THRESHOLD,
PEAK_TAKE_PROFIT_TIMEOUT,
} = pumpConfig
// Globals
let TRADE_OUT = ''
let balance = {}
let exchangeInfo = {}
let tradingPairInfo = null
let lotSizeInfo = null
let marketLotSizeInfo = null
// Trade Symbol for the trading pair
let symbol = ''
// Price for TRADE_OUT Coin
let price = ''
// Price Change % for TRADE_OUT Coin
let priceChangePercent = ''
// All prices
let globalMarkets = {}
// Variables
let snapshot_buy_price = ''
// The max profit X we have made
let max_profit_times = 0
// Has init bought (when BUY_UPON_SYMBOL is true)
let initialBought = false
let lastPrice = 0
let timeout = null
let drawbackStarted = false
let softTakeProfitIndex = 0
// Manual control, no take profit or stop loss
let manual = false
const binance = new Binance().options({
APIKEY: API_KEY,
APISECRET: API_SECRET,
useServerTime: true,
recvWindow: 5000, // Set a higher recvWindow to increase response timeout
// verbose: true, // Add extra output when subscribing to WebSockets, etc
log: (log) => {
// console.log(log) // You can create your own logger here, or disable console output
},
/**
* Proxy, uncomment for proxy usage
*/
// Have your shadowsocks ON
// proxy: {
// host: 'localhost',
// port: isWin ? '1084' : '1087',
// },
})
function handlePrice() {
if (symbol) {
if (!price) return
if (price) {
if (BUY_UPON_SYMBOL && !initialBought) {
initialBought = true
market_buy()
}
}
// console.log(price)
process.stdout.clearLine()
process.stdout.cursorTo(0)
let colorFn = chalk.green
if (price < lastPrice) {
colorFn = chalk.red
}
let times = calculateTimesAndTriggerOrders()
process.stdout.write(
`${symbol} ${colorFn(price)} ${colorFn(priceChangePercent + '%')} ${
times ? `${colorFn(times.toFixed(2))}x` : ''
} ${
max_profit_times ? `${chalk.magenta(max_profit_times.toFixed(2))}x` : ''
}`
)
lastPrice = price
}
}
function calculateTimesAndTriggerOrders() {
let times = null
if (snapshot_buy_price && price) {
times = price / snapshot_buy_price
}
if (times) {
if (times > max_profit_times) {
max_profit_times = times
}
// TAKE PROFIT AND STOP LOSS
if (!manual) {
if (HARD_TAKE_PROFIT > 0 && times >= HARD_TAKE_PROFIT) {
console.log('\nTRIGGER HARD TAKE PROFIT')
market_sell()
} else if (times <= HARD_STOP_LOSS) {
console.log('\nTRIGGER HARD STOP LOSS')
market_sell()
}
// if (
// SOFT_TAKE_PROFIT &&
// SOFT_TAKE_PROFIT.length > 0 &&
// SOFT_TAKE_PROFIT[softTakeProfitIndex]
// ) {
// if (times > SOFT_TAKE_PROFIT[softTakeProfitIndex]) {
// console.log(
// '\nTRIGGER SOFT TAKE PROFIT ' +
// SOFT_TAKE_PROFIT[softTakeProfitIndex] +
// 'x'
// )
// market_sell((1 / SOFT_TAKE_PROFIT.length) * SOFT_TAKE_PROFIT_PERCENT)
// softTakeProfitIndex += 1
// }
// }
if (times > PEAK_TAKE_PROFIT_THRESHOLD) {
try {
console.log(
`${
timeout ? 'Refreshing' : 'Triggering'
} PEAK_TAKE_PROFIT countdown `
)
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
console.log('\nTRIGGER PEAK TAKE PROFIT')
market_sell()
}, PEAK_TAKE_PROFIT_TIMEOUT)
} catch (err) {
console.error(err)
}
}
if (drawbackStarted && max_profit_times - times > MAX_DRAWBACK) {
console.log('\nTRIGGER DRAWBACK TAKE PROFIT')
market_sell()
}
if (
!drawbackStarted &&
MAX_DRAWBACK_START > 0 &&
times > MAX_DRAWBACK_START
) {
console.log(
`Reached ${MAX_DRAWBACK_START}, now will take profit when ${MAX_DRAWBACK}x drawback`
)
drawbackStarted = true
}
}
return times
} else {
return ''
}
}
function tickPriceHttp() {
if (symbol) {
binance.prices(symbol, function (error, ticker) {
if (error) {
// console.error('Error fetching price')
return
}
if (price !== ticker[symbol]) {
price = ticker[symbol]
}
handlePrice()
})
binance.prevDay(symbol, (error, prevDay, returnSymbol) => {
if (error) {
// console.error('Error fetching prevDay')
return
}
priceChangePercent = prevDay.priceChangePercent
if (returnSymbol !== symbol) {
console.log(
chalk.redBright(
`WARNING: symbol is ${returnSymbol}, expected${symbol}`
)
)
symbol = returnSymbol
}
handlePrice()
})
}
}
function tickPriceWS() {
if (symbol) {
// binance.websockets.miniTicker(symbol, (error, response) => {
// if (error) {
// console.error(error)
// return
// }
// // console.info('TICKER RESPONSE')
// // console.log(response)
// console.log(typeof response, response)
// price = response.close
// })
binance.websockets.prevDay(symbol, (error, response) => {
if (error) {
try {
console.error(chalk.red(`WS ERROR ${error.split('\n')[0]}`))
} catch (err) {
console.error(err)
}
return
}
price = response.close
priceChangePercent = response.percentChange
handlePrice()
})
}
}
function market_buy(percent) {
if (percent === undefined || percent === null || isNaN(percent)) {
percent = 1
}
if (balance[TRADE_IN]) {
const available = balance[TRADE_IN].available
const fullQuantity = (available / price) * percent
binance.marketBuy(
symbol,
getCorrectQuantity(fullQuantity * 0.6),
(error, response) => {
if (error) {
console.error(error.body ? error.body : error)
console.log(chalk.red('BUY FAILED'))
return
}
console.info(
chalk.bgGreen(`Market Buy ${percent * 100 * 0.6}% SUCCESS`)
)
// Now you can limit sell with a stop loss, etc.
if (price) {
snapshot_buy_price = (' ' + price).slice(1)
}
setTimeout(getBalance, 1500)
}
)
binance.marketBuy(
symbol,
getCorrectQuantity(fullQuantity * 0.11),
(error, response) => {
if (error) {
console.error(error.body ? error.body : error)
console.log(chalk.red('BUY FAILED'))
return
}
console.info(
chalk.bgGreen(`Market Buy ${percent * 100 * 0.11}% SUCCESS`)
)
// Now you can limit sell with a stop loss, etc.
if (price) {
snapshot_buy_price = (' ' + price).slice(1)
}
}
)
binance.marketBuy(
symbol,
getCorrectQuantity(fullQuantity * 0.11),
(error, response) => {
if (error) {
console.error(error.body ? error.body : error)
console.log(chalk.red('BUY FAILED'))
return
}
console.info(
chalk.bgGreen(`Market Buy ${percent * 100 * 0.11}% SUCCESS`)
) // Now you can limit sell with a stop loss, etc.
if (price) {
snapshot_buy_price = (' ' + price).slice(1)
}
}
)
binance.marketBuy(
symbol,
getCorrectQuantity(fullQuantity * 0.11),
(error, response) => {
if (error) {
console.error(error.body ? error.body : error)
console.log(chalk.red('BUY FAILED'))
return
}
console.info(
chalk.bgGreen(`Market Buy ${percent * 100 * 0.11}% SUCCESS`)
) // Now you can limit sell with a stop loss, etc.
if (price) {
snapshot_buy_price = (' ' + price).slice(1)
}
}
)
} else {
console.log(chalk.redBright(`NO ${TRADE_IN} AVAILABLE`))
}
}
function market_sell(percent, retry = true) {
if (percent === undefined || percent === null || isNaN(percent)) {
percent = 1
}
if (balance[TRADE_OUT]) {
const available = balance[TRADE_OUT].available
const quantity = getCorrectQuantity(available * percent)
binance.marketSell(symbol, quantity, (error, response) => {
if (error) {
console.error(error.body ? error.body : error)
console.log(chalk.red('SELL FAILED'))
if (retry) {
getBalance(false, () => {
console.log('\nRETRYING...')
market_sell(percent)
})
}
return
}
console.info(chalk.bgRed(`Market Sell ${percent * 100}% SUCCESS`))
// Now you can limit sell with a stop loss, etc.
setTimeout(getBalance, 1500)
})
} else {
console.log(chalk.redBright(`NO ${TRADE_OUT} AVAILABLE`))
}
}
function resetStatistics() {
console.log(chalk.bgYellow('RESETTING'))
if (snapshot_buy_price) {
snapshot_buy_price = ''
}
if (max_profit_times) {
max_profit_times = 0
}
if (timeout) {
try {
clearTimeout(timeout)
timeout = null
} catch (err) {
console.error(err)
}
}
// drawbackStarted = false
// softTakeProfitIndex = 0
}
function getCorrectQuantity(quantity) {
let minQty
let maxQty
let stepSize
if (lotSizeInfo) {
minQty = lotSizeInfo.minQty
maxQty = lotSizeInfo.maxQty
stepSize = lotSizeInfo.stepSize
} else {
console.error(chalk.red('NO LOT SIZE INFO'))
minQty = '0.01'
maxQty = '99999999999'
stepSize = '0.01'
}
if (marketLotSizeInfo) {
if (parseFloat(maxQty) > parseFloat(marketLotSizeInfo.maxQty)) {
maxQty = marketLotSizeInfo.maxQty
}
if (parseFloat(minQty) < parseFloat(marketLotSizeInfo.minQty)) {
minQty = marketLotSizeInfo.minQty
}
}
let decimals = parseFloat(stepSize).countDecimals()
if (decimals === 0 && parseFloat(stepSize) > 0) {
decimals = 'INT'
}
if (quantity > maxQty) {
console.log(chalk.redBright('quantity is LARGER than max'))
quantity = maxQty
} else if (quantity < parseFloat(minQty)) {
console.log(chalk.redBright('quantity is SMALLER than min'))
quantity = minQty
}
return decimals === 'INT'
? Math.floor(parseFloat(quantity))
: parseFloat(quantity).toFixedDown(decimals)
}
function getBalance(init = false, cb) {
binance.balance((error, balances) => {
if (error) return console.error(error)
let newBalance = balances
// Object.entries(balances)
// .filter((arr) => parseFloat(arr[1].available) > 0)
// .forEach((arr) => {
// newBalance[arr[0]] = arr[1]
// })
if (init) {
if (newBalance[TRADE_IN]) {
console.log(
chalk.yellow(`YOU HAVE ${newBalance[TRADE_IN].available} ${TRADE_IN}`)
)
} else {
console.log(chalk.red(`WARNING: YOU DO NOT HAVE ANY ${TRADE_IN}`))
// process.exit()
}
} else {
if (
balance[TRADE_OUT] &&
newBalance[TRADE_OUT] &&
newBalance[TRADE_OUT].available !== balance[TRADE_OUT].available
) {
console.log(
chalk.yellow(
`NOW YOU HAVE ${newBalance[TRADE_OUT].available} ${TRADE_OUT}`
)
)
try {
let minQty
if (lotSizeInfo) {
minQty = lotSizeInfo.minQty
} else {
minQty = '0.01'
}
if (marketLotSizeInfo) {
if (parseFloat(minQty) < parseFloat(marketLotSizeInfo.minQty)) {
minQty = marketLotSizeInfo.minQty
}
}
if (
parseFloat(newBalance[TRADE_OUT].available) < parseFloat(minQty)
) {
// can no longer make sell orders
resetStatistics()
}
} catch (err) {
console.error(err)
console.error('Reset statistics failed')
}
}
if (
balance[TRADE_IN] &&
newBalance[TRADE_IN] &&
newBalance[TRADE_IN].available !== balance[TRADE_IN].available
) {
console.log(
chalk.yellow(
`NOW YOU HAVE ${newBalance[TRADE_IN].available} ${TRADE_IN}`
)
)
}
}
balance = newBalance
if (cb) {
cb(newBalance)
}
// test
// balance[TRADE_IN] = { available: 100, onOrder: 0 }
// balance[TRADE_OUT] = { available: 100, onOrder: 0 }
})
}
function start() {
//minQty = minimum order quantity
//minNotional = minimum order value (price * quantity)
binance.exchangeInfo(function (error, data) {
if (error) {
console.log(chalk.red(`GET exchangeInfo failed, exiting...`))
// process.exit()
}
exchangeInfo = data.symbols
console.log(chalk.magenta('INPUT FIRST COIN OF TRADE PAIR TO CONTINUE'))
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
})
const ChromeLauncher = require('chrome-launcher')
rl.on('line', function (line) {
if (!TRADE_OUT) {
TRADE_OUT = line.toUpperCase()
symbol = `${TRADE_OUT}${TRADE_IN}`
tradingPairInfo = exchangeInfo.filter(
(item) => item.symbol == symbol
)[0]
if (tradingPairInfo) {
lotSizeInfo = tradingPairInfo.filters.filter(
(item) => item.filterType === 'LOT_SIZE'
)[0]
marketLotSizeInfo = tradingPairInfo.filters.filter(
(item) => item.filterType === 'MARKET_LOT_SIZE'
)[0]
} else {
console.error(chalk.red('\nWARN: NO TRADING PAIR'))
}
console.log(chalk.blue('\nTRADING PAIR SET: ' + symbol))
if (globalMarkets && globalMarkets[symbol]) {
price = globalMarkets[symbol].close
console.log(`GLOBAL ${symbol} is ${globalMarkets[symbol].close}`)
handlePrice()
}
tickPriceHttp()
tickPriceWS()
console.log(
chalk.magenta(
'\nNOW, TYPE\n1 - SELL ALL\n2 - SELL HALF\n3 - SELL QUARTER\n4 - SELL 10%\n5 - BUY ALL\n6 - BUY HALF\n7 - BUY QUARTER\no - Open browser with the Trading Pair\n0 - Toggle Manual(no take profits or stop losses)\n(Enter not needed)'
)
)
rl.close()
var stdin = process.stdin
// without this, we would only get streams once enter is pressed
stdin.setRawMode(true)
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume()
// i don't want binary, do you?
stdin.setEncoding('utf8')
// on any data into stdin
stdin.on('data', function (key) {
if (key === '1') {
market_sell(1, false)
}
if (key === '2') {
market_sell(0.5, false)
}
if (key === '3') {
market_sell(0.25, false)
}
if (key === '4') {
market_sell(0.1, false)
}
if (key === '5') {
market_buy()
}
if (key === '6') {
market_buy(0.5)
}
if (key === '7') {
market_buy(0.25)
}
if (key === '0') {
manual = !manual
if (manual) {
if (timeout) {
clearTimeout(timeout)
}
console.log(chalk.magentaBright('MANUAL ON'))
} else {
console.log(chalk.magentaBright('MANUAL OFF'))
}
}
if (key === 'o') {
ChromeLauncher.launch({
startingUrl: `https://www.binance.com/cn/trade/${TRADE_OUT}_${TRADE_IN}?layout=pro`,
})
}
// ctrl-c EXIT
if (key === '\u0003') {
process.exit()
}
})
}
})
})
setInterval(tickPriceHttp, HTTP_INTERVAL)
setInterval(getBalance, HTTP_INTERVAL * 3)
getBalance(true)
// start getting prices
binance.websockets.miniTicker((markets) => {
try {
if (symbol && markets[symbol]) {
price = markets[symbol].close
} else {
globalMarkets = { ...globalMarkets, ...markets }
}
} catch (err) {
// console.error(err)
}
})
}
start()