From c5c323ca8895d45567d4d0e5a7379a6c73a51606 Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 27 May 2021 16:35:27 +0700 Subject: [PATCH] Settings notify sell in telegram base on sell reason (#5028) * BREAK: notification sell by sell reason * Update constants.py * Update telegram.py * Update telegram-usage.md * Update telegram.py * Update telegram.py * Fix test fail * Update config_full.json.example * Update telegram-usage.md * Update telegram.py * Update telegram.py * Update telegram-usage.md * validate value of sell object * Fix linter * Update constants.py * Make telegram sample slightly more positive Co-authored-by: Matthias --- config_full.json.example | 11 ++++++++++- docs/telegram-usage.md | 41 ++++++++++++++++++++++++--------------- freqtrade/constants.py | 8 +++++++- freqtrade/rpc/telegram.py | 40 +++++++++++++++++++++++++------------- 4 files changed, 69 insertions(+), 31 deletions(-) diff --git a/config_full.json.example b/config_full.json.example index 24d364fdf..6aeb756f3 100644 --- a/config_full.json.example +++ b/config_full.json.example @@ -165,7 +165,16 @@ "startup": "on", "buy": "on", "buy_fill": "on", - "sell": "on", + "sell": { + "roi": "off", + "emergency_sell": "off", + "force_sell": "off", + "sell_signal": "off", + "trailing_stop_loss": "off", + "stop_loss": "off", + "stoploss_on_exchange": "off", + "custom_sell": "off" + }, "sell_fill": "on", "buy_cancel": "on", "sell_cancel": "on" diff --git a/docs/telegram-usage.md b/docs/telegram-usage.md index 6174bf0fe..991b5f1fb 100644 --- a/docs/telegram-usage.md +++ b/docs/telegram-usage.md @@ -72,22 +72,31 @@ Example configuration showing the different settings: ``` json "telegram": { - "enabled": true, - "token": "your_telegram_token", - "chat_id": "your_telegram_chat_id", - "notification_settings": { - "status": "silent", - "warning": "on", - "startup": "off", - "buy": "silent", - "sell": "on", - "buy_cancel": "silent", - "sell_cancel": "on", - "buy_fill": "off", - "sell_fill": "off" - }, - "balance_dust_level": 0.01 - }, + "enabled": true, + "token": "your_telegram_token", + "chat_id": "your_telegram_chat_id", + "notification_settings": { + "status": "silent", + "warning": "on", + "startup": "off", + "buy": "silent", + "sell": { + "roi": "silent", + "emergency_sell": "on", + "force_sell": "on", + "sell_signal": "silent", + "trailing_stop_loss": "on", + "stop_loss": "on", + "stoploss_on_exchange": "on", + "custom_sell": "silent" + }, + "buy_cancel": "silent", + "sell_cancel": "on", + "buy_fill": "off", + "sell_fill": "off" + }, + "balance_dust_level": 0.01 +}, ``` `buy` notifications are sent when the order is placed, while `buy_fill` notifications are sent when the order is filled on the exchange. diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 5ec60eb59..2e0efc8e7 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -260,7 +260,13 @@ CONF_SCHEMA = { 'enum': TELEGRAM_SETTING_OPTIONS, 'default': 'off' }, - 'sell': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, + 'sell': { + 'type': 'object', + 'additionalProperties': { + 'type': 'string', + 'enum': TELEGRAM_SETTING_OPTIONS + } + }, 'sell_cancel': {'type': 'string', 'enum': TELEGRAM_SETTING_OPTIONS}, 'sell_fill': { 'type': 'string', diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index b9e90dc8d..cca87ad91 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -233,44 +233,58 @@ class Telegram(RPCHandler): def send_msg(self, msg: Dict[str, Any]) -> None: """ Send a message to telegram channel """ - noti = self._config['telegram'].get('notification_settings', {} - ).get(str(msg['type']), 'on') + default_noti = 'on' + + msg_type = msg['type'] + noti = '' + if msg_type == RPCMessageType.SELL: + sell_noti = self._config['telegram'] \ + .get('notification_settings', {}).get(str(msg_type), {}) + # For backward compatibility sell still be string + if isinstance(noti, str): + noti = sell_noti + else: + noti = sell_noti.get(str(msg['sell_reason']), default_noti) + else: + noti = self._config['telegram'] \ + .get('notification_settings', {}).get(str(msg_type), default_noti) + if noti == 'off': - logger.info(f"Notification '{msg['type']}' not sent.") + logger.info(f"Notification '{msg_type}' not sent.") # Notification disabled return - if msg['type'] == RPCMessageType.BUY: + if msg_type == RPCMessageType.BUY: message = self._format_buy_msg(msg) - elif msg['type'] in (RPCMessageType.BUY_CANCEL, RPCMessageType.SELL_CANCEL): - msg['message_side'] = 'buy' if msg['type'] == RPCMessageType.BUY_CANCEL else 'sell' + elif msg_type in (RPCMessageType.BUY_CANCEL, RPCMessageType.SELL_CANCEL): + msg['message_side'] = 'buy' if msg_type == RPCMessageType.BUY_CANCEL else 'sell' message = ("\N{WARNING SIGN} *{exchange}:* " "Cancelling open {message_side} Order for {pair} (#{trade_id}). " "Reason: {reason}.".format(**msg)) - elif msg['type'] == RPCMessageType.BUY_FILL: + elif msg_type == RPCMessageType.BUY_FILL: message = ("\N{LARGE CIRCLE} *{exchange}:* " "Buy order for {pair} (#{trade_id}) filled " "for {open_rate}.".format(**msg)) - elif msg['type'] == RPCMessageType.SELL_FILL: + elif msg_type == RPCMessageType.SELL_FILL: message = ("\N{LARGE CIRCLE} *{exchange}:* " "Sell order for {pair} (#{trade_id}) filled " "for {close_rate}.".format(**msg)) - elif msg['type'] == RPCMessageType.SELL: + elif msg_type == RPCMessageType.SELL: message = self._format_sell_msg(msg) - elif msg['type'] == RPCMessageType.STATUS: + elif msg_type == RPCMessageType.STATUS: message = '*Status:* `{status}`'.format(**msg) - elif msg['type'] == RPCMessageType.WARNING: + elif msg_type == RPCMessageType.WARNING: message = '\N{WARNING SIGN} *Warning:* `{status}`'.format(**msg) - elif msg['type'] == RPCMessageType.STARTUP: + elif msg_type == RPCMessageType.STARTUP: message = '{status}'.format(**msg) else: - raise NotImplementedError('Unknown message type: {}'.format(msg['type'])) + raise NotImplementedError('Unknown message type: {}'.format(msg_type)) self._send_msg(message, disable_notification=(noti == 'silent'))