Repository: freqtrade/freqtrade

Issue:
Telegram RPC to inform about stoploss_on_exchange trade
When in config you have stoploss_on_exchange enabled and telegram enabled, once stoploss is hit, then there will be no message in telegram. 

However, when bots buys or sells with roi or stoploss order, then you will get a message. So it would be nice to close this gap in rpc module.


Discussion:

Resolving patch:
diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py
--- a/freqtrade/freqtradebot.py
+++ b/freqtrade/freqtradebot.py
@@ -512,6 +512,7 @@ def process_maybe_execute_sell(self, trade: Trade) -> bool:
                 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 @@ def handle_stoploss_on_exchange(self, trade: Trade) -> bool:
             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 @@ def execute_sell(self, trade: Trade, limit: float, sell_reason: SellType) -> Non
         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 @@ def execute_sell(self, trade: Trade, limit: float, sell_reason: SellType) -> Non
             '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 @@ def execute_sell(self, trade: Trade, limit: float, sell_reason: SellType) -> Non
 
         # Send the message
         self.rpc.send_msg(msg)
-        Trade.session.flush()
diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py
--- a/freqtrade/persistence.py
+++ b/freqtrade/persistence.py
@@ -266,6 +266,7 @@ def update(self, order: Dict) -> None:
             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
--- 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
 


Test patch:
diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py
--- 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,