diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 7bb5b71f9..63174024e 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -220,53 +220,48 @@ class Telegram(RPCHandler): msg['stake_amount_fiat'] = 0 content = [] - content.append( - f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}" - f" (#{msg['trade_id']})\n" - ) - if msg.get('buy_tag', None): - content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n") - content.append(f"*Amount:* `{msg['amount']:.8f}`\n") - content.append(f"*Open Rate:* `{msg['limit']:.8f}`\n") - content.append(f"*Current Rate:* `{msg['current_rate']:.8f}`\n") - content.append( - f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}" - ) - if msg.get('fiat_currency', None): + # Buy Order Fill + if msg['type'] == RPCMessageType.BUY_FILL: content.append( - f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" + f"\N{CHECK MARK} *{msg['exchange']}:* Bought {msg['pair']}" + f" (#{msg['trade_id']})\n" ) + if msg.get('buy_tag', None): + content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n") + content.append(f"*Amount:* `{msg['amount']:.8f}`\n") + content.append(f"*Open Rate:* `{msg['open_rate']:.8f}`\n") + content.append( + f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}" + ) + if msg.get('fiat_currency', None): + content.append( + f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" + ) - message = ''.join(content) - message += ")`" - return message - - def _format_buy_fill_msg(self, msg: Dict[str, Any]) -> str: - if self._rpc._fiat_converter: - msg['stake_amount_fiat'] = self._rpc._fiat_converter.convert_amount( - msg['stake_amount'], msg['stake_currency'], msg['fiat_currency']) + message = ''.join(content) + message += ")`" + # Buy Order Ask else: - msg['stake_amount_fiat'] = 0 - - content = [] - content.append( - f"\N{CHECK MARK} *{msg['exchange']}:* Bought {msg['pair']}" - f" (#{msg['trade_id']})\n" - ) - if msg.get('buy_tag', None): - content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n") - content.append(f"*Amount:* `{msg['amount']:.8f}`\n") - content.append(f"*Open Rate:* `{msg['open_rate']:.8f}`\n") - content.append( - f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}" - ) - if msg.get('fiat_currency', None): content.append( - f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" + f"\N{LARGE BLUE CIRCLE} *{msg['exchange']}:* Buying {msg['pair']}" + f" (#{msg['trade_id']})\n" ) + if msg.get('buy_tag', None): + content.append(f"*Buy Tag:* `{msg['buy_tag']}`\n") + content.append(f"*Amount:* `{msg['amount']:.8f}`\n") + content.append(f"*Open Rate:* `{msg['limit']:.8f}`\n") + content.append(f"*Current Rate:* `{msg['current_rate']:.8f}`\n") + content.append( + f"*Total:* `({round_coin_value(msg['stake_amount'], msg['stake_currency'])}" + ) + if msg.get('fiat_currency', None): + content.append( + f", {round_coin_value(msg['stake_amount_fiat'], msg['fiat_currency'])}" + ) + + message = ''.join(content) + message += ")`" - message = ''.join(content) - message += ")`" return message def _format_sell_msg(self, msg: Dict[str, Any]) -> str: @@ -290,71 +285,42 @@ class Telegram(RPCHandler): else: msg['profit_extra'] = '' - message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n" - "*Unrealized Profit:* `{profit_ratio:.2%}{profit_extra}`\n" - "*Buy Tag:* `{buy_tag}`\n" - "*Sell Reason:* `{sell_reason}`\n" - "*Duration:* `{duration} ({duration_min:.1f} min)`\n" - "*Amount:* `{amount:.8f}`\n" - "*Open Rate:* `{open_rate:.8f}`\n" - "*Current Rate:* `{current_rate:.8f}`\n" - "*Close Rate:* `{limit:.8f}`").format(**msg) + # Sell Order Fill + if msg['type'] == RPCMessageType.SELL_FILL: + message = ("{emoji} *{exchange}:* Sold {pair} (#{trade_id})\n" + "*Profit:* `{profit_ratio:.2%}{profit_extra}`\n" + "*Buy Tag:* `{buy_tag}`\n" + "*Sell Reason:* `{sell_reason}`\n" + "*Duration:* `{duration} ({duration_min:.1f} min)`\n" + "*Amount:* `{amount:.8f}`\n" + "*Close Rate:* `{close_rate:.8f}`").format(**msg) - return message - - def _format_sell_fill_msg(self, msg: Dict[str, Any]) -> str: - import pprint - pprint.pprint(msg) - msg['amount'] = round(msg['amount'], 8) - msg['profit_percent'] = round(msg['profit_ratio'] * 100, 2) - msg['duration'] = msg['close_date'].replace( - microsecond=0) - msg['open_date'].replace(microsecond=0) - msg['duration_min'] = msg['duration'].total_seconds() / 60 - - msg['buy_tag'] = msg['buy_tag'] if "buy_tag" in msg.keys() else None - msg['emoji'] = self._get_sell_emoji(msg) - - # Check if all sell properties are available. - # This might not be the case if the message origin is triggered by /forcesell - if (all(prop in msg for prop in ['gain', 'fiat_currency', 'stake_currency']) - and self._rpc._fiat_converter): - msg['profit_fiat'] = self._rpc._fiat_converter.convert_amount( - msg['profit_amount'], msg['stake_currency'], msg['fiat_currency']) - msg['profit_extra'] = (' ({gain}: {profit_amount:.8f} {stake_currency}' - ' / {profit_fiat:.3f} {fiat_currency})').format(**msg) + # Sell Order Ask else: - msg['profit_extra'] = '' - - message = ("{emoji} *{exchange}:* Sold {pair} (#{trade_id})\n" - "*Profit:* `{profit_ratio:.2%}{profit_extra}`\n" - "*Buy Tag:* `{buy_tag}`\n" - "*Sell Reason:* `{sell_reason}`\n" - "*Duration:* `{duration} ({duration_min:.1f} min)`\n" - "*Amount:* `{amount:.8f}`\n" - "*Close Rate:* `{close_rate:.8f}`").format(**msg) - + message = ("{emoji} *{exchange}:* Selling {pair} (#{trade_id})\n" + "*Unrealized Profit:* `{profit_ratio:.2%}{profit_extra}`\n" + "*Buy Tag:* `{buy_tag}`\n" + "*Sell Reason:* `{sell_reason}`\n" + "*Duration:* `{duration} ({duration_min:.1f} min)`\n" + "*Amount:* `{amount:.8f}`\n" + "*Open Rate:* `{open_rate:.8f}`\n" + "*Current Rate:* `{current_rate:.8f}`\n" + "*Close Rate:* `{limit:.8f}`").format(**msg) return message def compose_message(self, msg: Dict[str, Any], msg_type: RPCMessageType) -> str: - - if msg_type == RPCMessageType.BUY: + if msg_type in [RPCMessageType.BUY, RPCMessageType.BUY_FILL]: message = self._format_buy_msg(msg) + elif msg_type in [RPCMessageType.SELL, RPCMessageType.SELL_FILL]: + message = self._format_sell_msg(msg) + 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: - message = self._format_buy_fill_msg(msg) - - elif msg_type == RPCMessageType.SELL_FILL: - message = self._format_sell_fill_msg(msg) - - elif msg_type == RPCMessageType.SELL: - message = self._format_sell_msg(msg) - elif msg_type == RPCMessageType.PROTECTION_TRIGGER: message = ( "*Protection* triggered due to {reason}. "