Skip to content

Commit 0dfc4ed

Browse files
committed
refactor: extract websocket builder logic to it's own function
1 parent 950a0df commit 0dfc4ed

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

freqtrade/exchange/exchange.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,6 +2414,34 @@ async def _async_get_historic_ohlcv(
24142414
data = sorted(data, key=lambda x: x[0])
24152415
return pair, timeframe, candle_type, data, self._ohlcv_partial_candle
24162416

2417+
def _try_build_from_websocket(
2418+
self, pair: str, timeframe: str, candle_type: CandleType
2419+
) -> Coroutine[Any, Any, OHLCVResponse] | None:
2420+
"""
2421+
Try to build a coroutine to get data from websocket.
2422+
"""
2423+
if self._exchange_ws:
2424+
candle_ts = dt_ts(timeframe_to_prev_date(timeframe))
2425+
prev_candle_ts = dt_ts(date_minus_candles(timeframe, 1))
2426+
candles = self._exchange_ws.ohlcvs(pair, timeframe)
2427+
half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5)
2428+
last_refresh_time = int(
2429+
self._exchange_ws.klines_last_refresh.get((pair, timeframe, candle_type), 0)
2430+
)
2431+
2432+
if candles and candles[-1][0] >= prev_candle_ts and last_refresh_time >= half_candle:
2433+
# Usable result, candle contains the previous candle.
2434+
# Also, we check if the last refresh time is no more than half the candle ago.
2435+
logger.debug(f"reuse watch result for {pair}, {timeframe}, {last_refresh_time}")
2436+
2437+
return self._exchange_ws.get_ohlcv(pair, timeframe, candle_type, candle_ts)
2438+
logger.info(
2439+
f"Couldn't reuse watch for {pair}, {timeframe}, falling back to REST api. "
2440+
f"{candle_ts < last_refresh_time}, {candle_ts}, {last_refresh_time}, "
2441+
f"{format_ms_time(candle_ts)}, {format_ms_time(last_refresh_time)} "
2442+
)
2443+
return None
2444+
24172445
def _build_coroutine(
24182446
self,
24192447
pair: str,
@@ -2432,30 +2460,9 @@ def _build_coroutine(
24322460
candle_limit = self.ohlcv_candle_limit(timeframe, candle_type)
24332461
min_ts = dt_ts(date_minus_candles(timeframe, candle_limit - 5))
24342462

2435-
if self._exchange_ws:
2436-
candle_ts = dt_ts(timeframe_to_prev_date(timeframe))
2437-
prev_candle_ts = dt_ts(date_minus_candles(timeframe, 1))
2438-
candles = self._exchange_ws.ohlcvs(pair, timeframe)
2439-
half_candle = int(candle_ts - (candle_ts - prev_candle_ts) * 0.5)
2440-
last_refresh_time = int(
2441-
self._exchange_ws.klines_last_refresh.get((pair, timeframe, candle_type), 0)
2442-
)
2443-
2444-
if (
2445-
candles
2446-
and candles[-1][0] >= prev_candle_ts
2447-
and last_refresh_time >= half_candle
2448-
):
2449-
# Usable result, candle contains the previous candle.
2450-
# Also, we check if the last refresh time is no more than half the candle ago.
2451-
logger.debug(f"reuse watch result for {pair}, {timeframe}, {last_refresh_time}")
2452-
2453-
return self._exchange_ws.get_ohlcv(pair, timeframe, candle_type, candle_ts)
2454-
logger.info(
2455-
f"Couldn't reuse watch for {pair}, {timeframe}, falling back to REST api. "
2456-
f"{candle_ts < last_refresh_time}, {candle_ts}, {last_refresh_time}, "
2457-
f"{format_ms_time(candle_ts)}, {format_ms_time(last_refresh_time)} "
2458-
)
2463+
if ws_resp := self._try_build_from_websocket(pair, timeframe, candle_type):
2464+
# We have a usable websocket response
2465+
return ws_resp
24592466

24602467
# Check if 1 call can get us updated candles without hole in the data.
24612468
if min_ts < self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0):

0 commit comments

Comments
 (0)