From 11cc33a982cbe801c564676b5082e6ec409bdf7d Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 12 Mar 2019 21:49:08 +0100 Subject: [PATCH] Refactor notify_sell to rpc_manager * Call sell_notify also when stoploss_on_exchange is hit fix #1653 --- freqtrade/freqtradebot.py | 34 +++------------------------------- freqtrade/rpc/rpc_manager.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index dce3136df..58888a4f8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -512,6 +512,7 @@ class FreqtradeBot(object): except OperationalException as exception: logger.warning("Could not update trade amount: %s", exception) + # This handles both buy and sell orders! trade.update(order) if self.strategy.order_types.get('stoploss_on_exchange') and trade.is_open: @@ -657,6 +658,7 @@ class FreqtradeBot(object): if order['status'] == 'closed': trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value trade.update(order) + self.rpc.notify_sell(trade, self.config, trade.close_rate) result = True elif self.config.get('trailing_stop', False): # if trailing stoploss is enabled we check if stoploss value has changed @@ -846,35 +848,5 @@ class FreqtradeBot(object): trade.open_order_id = order_id trade.close_rate_requested = limit trade.sell_reason = sell_reason.value - - profit_trade = trade.calc_profit(rate=limit) - current_rate = self.exchange.get_ticker(trade.pair)['bid'] - profit_percent = trade.calc_profit_percent(limit) - gain = "profit" if profit_percent > 0 else "loss" - - msg = { - 'type': RPCMessageType.SELL_NOTIFICATION, - 'exchange': trade.exchange.capitalize(), - 'pair': trade.pair, - 'gain': gain, - 'limit': limit, - 'amount': trade.amount, - 'open_rate': trade.open_rate, - 'current_rate': current_rate, - 'profit_amount': profit_trade, - 'profit_percent': profit_percent, - 'sell_reason': sell_reason.value - } - - # For regular case, when the configuration exists - if 'stake_currency' in self.config and 'fiat_display_currency' in self.config: - stake_currency = self.config['stake_currency'] - fiat_currency = self.config['fiat_display_currency'] - msg.update({ - 'stake_currency': stake_currency, - 'fiat_currency': fiat_currency, - }) - - # Send the message - self.rpc.send_msg(msg) Trade.session.flush() + self.rpc.notify_sell(trade, self.config, self.exchange.get_ticker(trade.pair)['bid']) diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index bc69c97ad..7a3971356 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -2,8 +2,9 @@ This module contains class to manage RPC communications (Telegram, Slack, ...) """ import logging -from typing import List, Dict, Any +from typing import Any, Dict, List +from freqtrade.persistence import Trade from freqtrade.rpc import RPC, RPCMessageType logger = logging.getLogger(__name__) @@ -80,3 +81,35 @@ class RPCManager(object): 'status': f'Searching for {stake_currency} pairs to buy and sell ' f'based on {pairlist.short_desc()}' }) + + def notify_sell(self, trade: Trade, config, current_rate: float): + profit_trade = trade.calc_profit(rate=trade.close_rate_requested) + + profit_percent = trade.calc_profit_percent(trade.close_rate_requested) + gain = "profit" if profit_percent > 0 else "loss" + + msg = { + 'type': RPCMessageType.SELL_NOTIFICATION, + 'exchange': trade.exchange.capitalize(), + 'pair': trade.pair, + 'gain': gain, + 'limit': trade.close_rate_requested, + 'amount': trade.amount, + 'open_rate': trade.open_rate, + 'current_rate': current_rate, + 'profit_amount': profit_trade, + 'profit_percent': profit_percent, + 'sell_reason': trade.sell_reason + } + + # For regular case, when the configuration exists + if 'stake_currency' in config and 'fiat_display_currency' in config: + stake_currency = config['stake_currency'] + fiat_currency = config['fiat_display_currency'] + msg.update({ + 'stake_currency': stake_currency, + 'fiat_currency': fiat_currency, + }) + + # Send the message + self.send_msg(msg)