From 69ae99406ab8cac54e195b3f17b7e0214bb091a1 Mon Sep 17 00:00:00 2001 From: Eoin Date: Wed, 27 Sep 2017 22:32:04 +0100 Subject: [PATCH] add telegram handler for forcesellall --- freqtrade/rpc/telegram.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index bbdece23b..fa91e0f32 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -48,6 +48,7 @@ def init(config: dict) -> None: CommandHandler('start', _start), CommandHandler('stop', _stop), CommandHandler('forcesell', _forcesell), + CommandHandler('forcesellall', _forcesellall), CommandHandler('performance', _performance), CommandHandler('count', _count), CommandHandler('help', _help), @@ -402,6 +403,39 @@ def _count(bot: Bot, update: Update) -> None: logger.debug(message) send_msg(message, parse_mode=ParseMode.HTML) +@authorized_only +def _forcesellall(bot: Bot, update: Update) -> None: + """ + Handler for /forcesellall. + Sells all currently active trades + :param bot: telegram bot + :param update: message update + :return: None + """ + # Fetch open trade + trades = Trade.query.filter(Trade.is_open.is_(True)).all() + if get_state() != State.RUNNING: + send_msg('*Status:* `trader is not running`', bot=bot) + elif not trades: + send_msg('*Status:* `no active order`', bot=bot) + else: + for trade in trades: + # Get current rate + current_rate = exchange.get_ticker(trade.pair)['bid'] + # Get available balance + currency = trade.pair.split('_')[1] + balance = exchange.get_balance(currency) + # Execute sell + profit = trade.exec_sell_order(current_rate, balance) + message = '*{}:* Selling [{}]({}) at rate `{:f} (profit: {}%)`'.format( + trade.exchange.name, + trade.pair.replace('_', '/'), + exchange.get_pair_detail_url(trade.pair), + trade.close_rate, + round(profit, 2) + ) + logger.info(message) + send_msg(message) @authorized_only def _help(bot: Bot, update: Update) -> None: