diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index dce3136df..2eecb5802 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.notify_sell(trade) result = True elif self.config.get('trailing_stop', False): # if trailing stoploss is enabled we check if stoploss value has changed @@ -846,10 +848,17 @@ class FreqtradeBot(object): trade.open_order_id = order_id trade.close_rate_requested = limit trade.sell_reason = sell_reason.value + Trade.session.flush() + self.notify_sell(trade) - profit_trade = trade.calc_profit(rate=limit) + def notify_sell(self, trade: Trade): + """ + Sends rpc notification when a sell occured. + """ + profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested + profit_trade = trade.calc_profit(rate=profit_rate) current_rate = self.exchange.get_ticker(trade.pair)['bid'] - profit_percent = trade.calc_profit_percent(limit) + profit_percent = trade.calc_profit_percent(profit_rate) gain = "profit" if profit_percent > 0 else "loss" msg = { @@ -857,13 +866,13 @@ class FreqtradeBot(object): 'exchange': trade.exchange.capitalize(), 'pair': trade.pair, 'gain': gain, - 'limit': limit, + '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': sell_reason.value + 'sell_reason': trade.sell_reason } # For regular case, when the configuration exists @@ -877,4 +886,3 @@ class FreqtradeBot(object): # Send the message self.rpc.send_msg(msg) - Trade.session.flush() diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index f603b139f..10aff72ec 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -266,6 +266,7 @@ class Trade(_DECL_BASE): logger.info('%s_SELL has been fulfilled for %s.', order_type.upper(), self) elif order_type == 'stop_loss_limit': self.stoploss_order_id = None + self.close_rate_requested = self.stop_loss logger.info('STOP_LOSS_LIMIT is hit for %s.', self) self.close(order['average']) else: diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index bc69c97ad..7f0d0a5d4 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -2,7 +2,7 @@ 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.rpc import RPC, RPCMessageType diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 2f66a5153..bd40a063f 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -2105,7 +2105,7 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, assert trade.is_open is False print(trade.sell_reason) assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value - assert rpc_mock.call_count == 1 + assert rpc_mock.call_count == 2 def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee,