From f9d3775d4c36f1c45a137f5919ebef12f6703c1e Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 11 Jul 2022 14:09:39 +0200 Subject: [PATCH] Move "candle" logic for message to telegram this avoids calling this method unless necessary --- freqtrade/freqtradebot.py | 16 ---------------- freqtrade/rpc/telegram.py | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index bde9ea8aa..469bfda7e 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -861,23 +861,9 @@ class FreqtradeBot(LoggingMixin): 'current_rate': current_rate, } - self._analyzed_candle_to_msg(trade.pair, msg) - # Send the message self.rpc.send_msg(msg) - def _analyzed_candle_to_msg(self, pair: str, msg: Dict): - """Msg dict will be enhanced with analyzed_candle if possible.""" - # display the candle analyzed in telegram - analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(pair, - self.strategy.timeframe) - analyzed_candle = analyzed_df.iloc[-1] if len(analyzed_df) > 0 else None - if analyzed_candle is not None: - candle_columns = analyzed_candle[['date', 'open', 'high', 'low', 'close']] - msg.update({ - 'analyzed_candle': candle_columns.to_dict() - }) - def _notify_enter_cancel(self, trade: Trade, order_type: str, reason: str) -> None: """ Sends rpc notification when a entry order cancel occurred. @@ -1564,8 +1550,6 @@ class FreqtradeBot(LoggingMixin): 'fiat_currency': self.config['fiat_display_currency'], }) - self._analyzed_candle_to_msg(trade.pair, msg) - # Send the message self.rpc.send_msg(msg) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 9ade8cb5f..2aff1d210 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -243,16 +243,19 @@ class Telegram(RPCHandler): """ return f"{msg['exchange']}{' (dry)' if self._config['dry_run'] else ''}" - def _add_analyzed_candle(self, msg) -> str: + def _add_analyzed_candle(self, pair: str) -> str: candle_val = self._config['telegram'].get( 'notification_settings', {}).get('show_candle', 'off') - if candle_val != 'off' and msg.get('analyzed_candle'): + if candle_val != 'off': if candle_val == 'ohlc': - candle_json = msg['analyzed_candle'] - return ( - f"*Candle OHLC*: `{candle_json['open']}, {candle_json['high']}, " - f"{candle_json['low']}, {candle_json['close']}`\n" - ) + analyzed_df, _ = self._rpc._freqtrade.dataprovider.get_analyzed_dataframe( + pair, self._config['timeframe']) + candle = analyzed_df.iloc[-1].squeeze() if len(analyzed_df) > 0 else None + if candle is not None: + return ( + f"*Candle OHLC*: `{candle['open']}, {candle['high']}, " + f"{candle['low']}, {candle['close']}`\n" + ) return '' @@ -272,7 +275,7 @@ class Telegram(RPCHandler): f" {entry_side['entered'] if is_fill else entry_side['enter']} {msg['pair']}" f" (#{msg['trade_id']})\n" ) - message += self._add_analyzed_candle(msg) + message += self._add_analyzed_candle(msg['pair']) message += f"*Enter Tag:* `{msg['enter_tag']}`\n" if msg.get('enter_tag') else "" message += f"*Amount:* `{msg['amount']:.8f}`\n" if msg.get('leverage') and msg.get('leverage', 1.0) != 1.0: @@ -320,7 +323,7 @@ class Telegram(RPCHandler): message = ( f"{msg['emoji']} *{self._exchange_from_msg(msg)}:* " f"{'Exited' if is_fill else 'Exiting'} {msg['pair']} (#{msg['trade_id']})\n" - f"{self._add_analyzed_candle(msg)}" + f"{self._add_analyzed_candle(msg['pair'])}" f"*{'Profit' if is_fill else 'Unrealized Profit'}:* " f"`{msg['profit_ratio']:.2%}{msg['profit_extra']}`\n" f"*Enter Tag:* `{msg['enter_tag']}`\n"