type: 'MARKET', recvWindow: 1000000, }) .then( (order_result) => { order_id = order_result.orderId auto_trade() }) .catch((error) => { //console.error(JSON.stringify(error)) report.fail(error) ask_pair_budget() }) } } auto_trade = () => { step = 1 report.text = "" report.start() // LISTEN TO KEYBOARD PRSEED KEYS process.stdin.resume() process.stdin.setRawMode(true) console.log(chalk.grey(" ⚠️ Press [ CTRL + c ] or q to cancel the trade and sell everything at market price. ⚠️ ")) console.log(" ") constcurr_trade = trade_count constclean_trades = client.ws.trades([pair], trade => { if (curr_trade !== trade_count) clean_trades() report.text = add_status_to_trade_report(trade, "") // CHECK IF INITIAL BUY ORDER IS EXECUTED if ( order_id&& (step === 1) ) { step = 99 checkBuyOrderStatus() } // SWITCH PRICE REACHED SETTING UP SELL FOR PROFIT ORDER if ( (selling_method === "Profit") &&order_id&& (step === 3) && (trade.price>switch_price) ) { step = 99 console.log(chalk.grey(" CANCEL STOP LOSS AND GO FOR PROFIT ")) client.cancelOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then(() => { client.order({ symbol: pair, side: 'SELL', quantity: buy_amount, price: sell_price, recvWindow: 1000000,
}) .then((order) => { step = 5 order_id = order.orderId varlog_report = chalk.grey(" SELL ORDER READY ") console.log(log_report) }) .catch((error) => { varlog_report = chalk.magenta(" ERROR #555 ") console.error(log_report + error) }) }) .catch((error) => { console.log(" ERROR #547 ") console.error(error) }) } // INCREASE THE TRAILING STOP LOSS PRICE if ( (selling_method === "Trailing") &&order_id&& (step === 3) && (trade.price>switch_price) ) { step = 99 tot_cancel = tot_cancel + 1 console.log(chalk.grey(" CANCEL CURRENT STOP LOSS ")) client.cancelOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then(() => { stop_price = (parseFloat(stop_price) + (parseFloat(stop_price) * trailing_percent / 100.00)).toFixed(tickSize) loss_price = (parseFloat(stop_price) - (parseFloat(stop_price) * 0.040)).toFixed(tickSize) set_stop_loss_order() switch_price = (parseFloat(switch_price) + (parseFloat(switch_price) * trailing_percent / 100.00)).toFixed(tickSize) console.log(chalk.grey(" NEW TRAILING STOP LOSS SET @ " + stop_price)) step = 3 }) .catch((error) => { console.log(" ERROR #547 ") console.error(error) }) } // PRICE BELLOW BUY PRICE SETTING UP STOP LOSS ORDER if ( (selling_method==='Profit') &&order_id&& (step === 5) && (trade.price
recvWindow: 1000000, }) .then(() => { set_stop_loss_order() }) .catch((error) => { pnl = 100.00*(buy_price - trade.price)/buy_price varlog_report = chalk.magenta(" LOSS PRICE REACHED THE BOT SHOULD HAVE SOLD EVERYTHING #454 ") report.fail(add_status_to_trade_report(trade, log_report)) reset_trade() setTimeout( () => { ask_pair_budget(), 1000 } ) }) } // CURRENT PRICE REACHED SELL PRICE if ( (selling_method === "Profit") &&order_id&& (step === 5) && (trade.price>= sell_price) ) { step = 99 client.getOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then( (order_result) => { if ( parseFloat(order_result.executedQty) <parseFloat(order_result.origQty) ) { varlog_report = chalk.grey(" PROFIT PRICE REACHED BUT NOT ALL EXECUTED " + order_result.executedQty ) report.text = add_status_to_trade_report(trade, log_report) step = 5 } else { clean_trades() pnl = 100.00*(trade.price - buy_price)/buy_price varlog_report = chalk.greenBright(" ⚠️ !!! WE HAVE A WINNER !!! ⚠️ ") report.text = add_status_to_trade_report(trade, log_report) reset_trade() report.succeed() setTimeout( () => { ask_pair_budget(), 1000 } ) } }) .catch((error) => { console.error(" ERROR 8 " + error) }) } // CURRENT PRICE REACHED STOP PRICE if ( order_id&& (step === 3) && (trade.price<= stop_price) ) { step = 99 client.getOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then( (order_result) => {
if ( parseFloat(order_result.executedQty) <parseFloat(order_result.origQty) ) { varlog_report = chalk.grey(" STOP PRICE REACHED BUT NOT ALL EXECUTED " + order_result.executedQty ) report.text = add_status_to_trade_report(trade, log_report) step = 5 } else { clean_trades() pnl = 100.00*(buy_price - trade.price)/buy_price varlog_report = chalk.magenta(" STOP LOSS ALL EXECUTED") report.text = add_status_to_trade_report(trade, log_report) reset_trade() report.succeed() setTimeout( () => { ask_pair_budget(), 1400 } ) } }) .catch((error) => { console.error(" API ERROR #9 " + error) clean_trades() pnl = 100.00*(buy_price - trade.price)/buy_price varlog_report = chalk.magenta(" TRADE STOPPED ") report.text = add_status_to_trade_report(trade, log_report) reset_trade() report.fail() setTimeout( () => { ask_pair_budget(), 1400 } ) }) } }) } sell_at_market_price = () => { console.log(chalk.keyword('orange')(" SELLING AT MARKET PRICE ")) client.order({ symbol: pair, side: 'SELL', type: 'MARKET', quantity: buy_amount, recvWindow: 1000000, }) .then( order => { reset_trade() report.succeed(chalk.magenta(" THE BOT SOLD AT MARKET PRICE #777 ") ) setTimeout( () => { ask_pair_budget(), 2500 } ) }) .catch( error => { report.fail(" ERROR #7771 " + buy_amount + " :: " + error ) reset_trade() }) } checkBuyOrderStatus = () => { client.getOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then( order => { if (order.status === "FILLED") { init_buy_filled = true
buy_amount = parseFloat(order.executedQty) console.log(chalk.white(" INITAL BUY ORDER FULLY EXECUTED ")) client.myTrades({ symbol: pair, limit: 1, recvWindow: 1000000 }).then( mytrade => { buy_price = parseFloat(mytrade[0].price) console.log(chalk.gray(" FINAL BUY PRICE @ ") + chalk.cyan(buy_price)) if (selling_method==="Trailing") { stop_price = (buy_price - (buy_price * trailing_percent / 100.00)).toFixed(tickSize) loss_price = (stop_price - (stop_price * 0.040)).toFixed(tickSize) set_stop_loss_order() switch_price = (buy_price + (buy_price * trailing_percent / 100.00)).toFixed(tickSize) } else { stop_price = (buy_price - (buy_price * loss_percent / 100.00)).toFixed(tickSize) loss_price = (stop_price - (stop_price * 0.040)).toFixed(tickSize) set_stop_loss_order() switch_price = (buy_price + (buy_price * profit_percent / 200.00)).toFixed(tickSize) sell_price = (buy_price + (buy_price * profit_percent / 100.00)).toFixed(tickSize) } }) } else { console.log(chalk.gray(" BUY ORDER NOT YET FULLY EXECUTED ")) init_buy_filled = false step = 1 } }) } set_stop_loss_order = () => { client.order({ symbol: pair, side: 'SELL', type: 'STOP_LOSS_LIMIT', stopPrice: stop_price, quantity: buy_amount, price: loss_price, recvWindow: 1000000, }) .then((order) => { order_id = order.orderId varlog_report = chalk.grey(" STOP LOSS READY (" + tot_cancel + ") @ ") + chalk.cyan(stop_price) console.log(log_report) step = 3 }) .catch((error) => { console.error(" ERRROR #1233 STOP PRICE (" + stop_price + ") " + error ) if (String(error).includes("MIN_NOTIONAL")) { console.error("⚠️ PLEASE MAKE SURE YOUR BUDGET VALUE IS SUPERIOR THAN 15 USD ⚠️") } sell_at_market_price() }) }
add_status_to_trade_report = (trade, status) => { if (init_buy_filled) { varpnl = 100.00*(parseFloat(trade.price)-parseFloat(buy_price))/parseFloat(buy_price) } else { varpnl = 0.00 } returnchalk.grey(moment().format('h:mm:ss').padStart(8)) + chalk.yellow(trade.symbol.padStart(10)) + (!trade.maker?chalk.green((chalk.grey("qty:")+numeral(trade.quantity).format("0.000") ).padStart(30)):chalk.red((chalk.grey("qty:")+numeral(trade.quantity).format("0.000") ).padStart(30))) + chalk.grey(" @ ") + chalk.cyan(trade.price).padEnd(24) + ((pnl>= 0)?chalk.green((chalk.grey("pnl:")+numeral(pnl).format("0.000")).padStart(20)):chalk. red((chalk.grey("pnl:")+numeral(pnl).format("0.000")).padStart(20))) + chalk.white(status) } reset_trade = () => { step = 0 trade_count = trade_count + 1 order_id = 0 buy_price = 0.00 stop_price = 0.00 loss_price = 0.00 sell_price = 0.00 tot_cancel = 0 init_buy_filled = false } //////////////////////////////////////////////////////////////////// // LISTEN TO KEYBOARD AND CANCEL THE TRADE IF (CRTL + C) OR Q PRESSED process.stdin.setEncoding('utf8' ) process.stdin.on('keypress', ( key ) => { if ( (key === '\u0003') || (key === 'q') ) { if (order_id) { trade_count = trade_count + 1 console.log(" --- STOPPING THE TRADE --- ") client.cancelOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then( (order) => { console.log(" CURRENT ORDER CANCELED ") client.getOrder({ symbol: pair, orderId: order_id, recvWindow: 1000000, }) .then( (order_result) => { if (order_result.status === "FILLED") { console.log("PREV ORDER FILLED")
sell_at_market_price() } elseif (order_result.status === "PARTIALLY_FILLED") { console.log("PREV ORDER PARTIALLY_FILLED") if (order_result.side === "BUY") { buy_amount = parseFloat(order_result.executedQty) sell_at_market_price() } else { buy_amount = parseFloat(order_result.origQty) - parseFloat(order_result.executedQty) sell_at_market_price() } } elseif (order_result.status === "CANCELED") { if (order_result.side === "SELL") { sell_at_market_price() } else { sell_at_market_price() reset_trade() report.succeed(chalk.magenta(" THE BOT STOPPED THE TRADE #3365 ") ) setTimeout( () => { ask_pair_budget(), 2500 } ) } } }) .catch((error) => { console.error(" GET FINAL ORDER ERROR : " + error) sell_at_market_price() }) }) .catch((error) => { console.error(" FINAL CANCEL ERROR : " + error) sell_at_market_price() }) } } }) //////////////////////////////////////////////////////////////////// //You can test all of this out by adding the following to index.js: const run = async () => { // We call the function ask_pair_budget() ask_pair_budget() } // We call run() to kickstart the program run() // Then run the script using node index.js. //After that, install the module globally and you’ll have a working shell command: //npminstall -g