Skip to content

Commit c39c5f2

Browse files
committed
Extract "can use websocket" method
1 parent 0dfc4ed commit c39c5f2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

freqtrade/exchange/exchange.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ def _try_build_from_websocket(
24202420
"""
24212421
Try to build a coroutine to get data from websocket.
24222422
"""
2423-
if self._exchange_ws:
2423+
if self._can_use_websocket(self._exchange_ws, pair, timeframe, candle_type):
24242424
candle_ts = dt_ts(timeframe_to_prev_date(timeframe))
24252425
prev_candle_ts = dt_ts(date_minus_candles(timeframe, 1))
24262426
candles = self._exchange_ws.ohlcvs(pair, timeframe)
@@ -2442,6 +2442,17 @@ def _try_build_from_websocket(
24422442
)
24432443
return None
24442444

2445+
def _can_use_websocket(
2446+
self, exchange_ws: ExchangeWS | None, pair: str, timeframe: str, candle_type: CandleType
2447+
) -> TypeGuard[ExchangeWS]:
2448+
"""
2449+
Check if we can use websocket for this pair.
2450+
Acts as typeguard for exchangeWs
2451+
"""
2452+
if self._has_watch_ohlcv and exchange_ws:
2453+
return True
2454+
return False
2455+
24452456
def _build_coroutine(
24462457
self,
24472458
pair: str,
@@ -2452,7 +2463,7 @@ def _build_coroutine(
24522463
) -> Coroutine[Any, Any, OHLCVResponse]:
24532464
not_all_data = cache and self.required_candle_call_count > 1
24542465
if cache and candle_type in (CandleType.SPOT, CandleType.FUTURES):
2455-
if self._has_watch_ohlcv and self._exchange_ws:
2466+
if self._can_use_websocket(self._exchange_ws, pair, timeframe, candle_type):
24562467
# Subscribe to websocket
24572468
self._exchange_ws.schedule_ohlcv(pair, timeframe, candle_type)
24582469

0 commit comments

Comments
 (0)