diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 61acdf510..81510d047 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -2669,11 +2669,11 @@ class Exchange: if self._can_use_websocket(self._exchange_ws, pair, timeframe, candle_type): candle_ts = dt_ts(timeframe_to_prev_date(timeframe)) prev_candle_ts = dt_ts(date_minus_candles(timeframe, 1)) - candles = self._exchange_ws.ohlcvs(pair, timeframe) - half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5) - last_refresh_time = int( - self._exchange_ws.klines_last_refresh.get((pair, timeframe, candle_type), 0) + candles, last_refresh_time = self._exchange_ws.get_ohlcv_with_refresh( + pair, timeframe, candle_type ) + last_refresh_time = int(last_refresh_time) + half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5) if ( candles diff --git a/freqtrade/exchange/exchange_ws.py b/freqtrade/exchange/exchange_ws.py index 9da963184..442932ba2 100644 --- a/freqtrade/exchange/exchange_ws.py +++ b/freqtrade/exchange/exchange_ws.py @@ -120,6 +120,17 @@ class ExchangeWS: # TemporaryError does not cause backoff - so we're essentially retrying immediately raise TemporaryError(f"Error deepcopying: {e}") from e + def get_ohlcv_with_refresh( + self, pair: str, timeframe: str, candle_type: CandleType + ) -> tuple[list[list], float]: + """ + Get deepcopied klines and update the last refresh time + """ + ohlcvs = self.ohlcvs(pair, timeframe) + with self._state_lock: + last_refresh = self.klines_last_refresh.get((pair, timeframe, candle_type), 0) + return ohlcvs, last_refresh + def cleanup_expired(self) -> None: """ Remove pairs from watchlist if they've not been requested within @@ -254,10 +265,7 @@ class ExchangeWS: Returns cached klines from ccxt's "watch" cache. :param candle_ts: timestamp of the end-time of the candle we expect. """ - # Deepcopy the response - as it might be modified in the background as new messages arrive - candles = self.ohlcvs(pair, timeframe) - with self._state_lock: - refresh_date = self.klines_last_refresh.get((pair, timeframe, candle_type), 0) + candles, refresh_date = self.get_ohlcv_with_refresh(pair, timeframe, candle_type) received_ts = candles[-1][0] if candles else 0 drop_hint = received_ts >= candle_ts if refresh_date and received_ts > refresh_date: