Skip to content

Commit c34cd6a

Browse files
committed
refactor: don't use exchange_ws internal variables
1 parent 17678f1 commit c34cd6a

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

freqtrade/exchange/exchange.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,11 +2669,11 @@ def _try_build_from_websocket(
26692669
if self._can_use_websocket(self._exchange_ws, pair, timeframe, candle_type):
26702670
candle_ts = dt_ts(timeframe_to_prev_date(timeframe))
26712671
prev_candle_ts = dt_ts(date_minus_candles(timeframe, 1))
2672-
candles = self._exchange_ws.ohlcvs(pair, timeframe)
2673-
half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5)
2674-
last_refresh_time = int(
2675-
self._exchange_ws.klines_last_refresh.get((pair, timeframe, candle_type), 0)
2672+
candles, last_refresh_time = self._exchange_ws.get_ohlcv_with_refresh(
2673+
pair, timeframe, candle_type
26762674
)
2675+
last_refresh_time = int(last_refresh_time)
2676+
half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5)
26772677

26782678
if (
26792679
candles

freqtrade/exchange/exchange_ws.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ def ohlcvs(self, pair: str, timeframe: str) -> list[list]:
120120
# TemporaryError does not cause backoff - so we're essentially retrying immediately
121121
raise TemporaryError(f"Error deepcopying: {e}") from e
122122

123+
def get_ohlcv_with_refresh(
124+
self, pair: str, timeframe: str, candle_type: CandleType
125+
) -> tuple[list[list], float]:
126+
"""
127+
Get deepcopied klines and update the last refresh time
128+
"""
129+
ohlcvs = self.ohlcvs(pair, timeframe)
130+
with self._state_lock:
131+
last_refresh = self.klines_last_refresh.get((pair, timeframe, candle_type), 0)
132+
return ohlcvs, last_refresh
133+
123134
def cleanup_expired(self) -> None:
124135
"""
125136
Remove pairs from watchlist if they've not been requested within
@@ -254,10 +265,7 @@ async def get_ohlcv(
254265
Returns cached klines from ccxt's "watch" cache.
255266
:param candle_ts: timestamp of the end-time of the candle we expect.
256267
"""
257-
# Deepcopy the response - as it might be modified in the background as new messages arrive
258-
candles = self.ohlcvs(pair, timeframe)
259-
with self._state_lock:
260-
refresh_date = self.klines_last_refresh.get((pair, timeframe, candle_type), 0)
268+
candles, refresh_date = self.get_ohlcv_with_refresh(pair, timeframe, candle_type)
261269
received_ts = candles[-1][0] if candles else 0
262270
drop_hint = received_ts >= candle_ts
263271
if refresh_date and received_ts > refresh_date:

0 commit comments

Comments
 (0)