mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-12-14 11:51:19 +00:00
feat: convert get_valid_pair_combination to generator
This commit is contained in:
@@ -7,7 +7,7 @@ import asyncio
|
||||
import inspect
|
||||
import logging
|
||||
import signal
|
||||
from collections.abc import Coroutine
|
||||
from collections.abc import Coroutine, Generator
|
||||
from copy import deepcopy
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from math import floor, isnan
|
||||
@@ -705,14 +705,22 @@ class Exchange:
|
||||
f"Available currencies are: {', '.join(quote_currencies)}"
|
||||
)
|
||||
|
||||
def get_valid_pair_combination(self, curr_1: str, curr_2: str) -> str:
|
||||
def get_valid_pair_combination(self, curr_1: str, curr_2: str) -> Generator[str, None, None]:
|
||||
"""
|
||||
Get valid pair combination of curr_1 and curr_2 by trying both combinations.
|
||||
"""
|
||||
for pair in [f"{curr_1}/{curr_2}", f"{curr_2}/{curr_1}"]:
|
||||
yielded = False
|
||||
for pair in (
|
||||
f"{curr_1}/{curr_2}",
|
||||
f"{curr_2}/{curr_1}",
|
||||
f"{curr_1}/{curr_2}:{curr_2}",
|
||||
f"{curr_2}/{curr_1}:{curr_1}",
|
||||
):
|
||||
if pair in self.markets and self.markets[pair].get("active"):
|
||||
return pair
|
||||
raise ValueError(f"Could not combine {curr_1} and {curr_2} to get a valid pair.")
|
||||
yielded = True
|
||||
yield pair
|
||||
if not yielded:
|
||||
raise ValueError(f"Could not combine {curr_1} and {curr_2} to get a valid pair.")
|
||||
|
||||
def validate_timeframes(self, timeframe: str | None) -> None:
|
||||
"""
|
||||
@@ -1868,26 +1876,25 @@ class Exchange:
|
||||
return 1.0
|
||||
tickers = self.get_tickers(cached=True)
|
||||
try:
|
||||
pair = self.get_valid_pair_combination(coin, currency)
|
||||
for pair in self.get_valid_pair_combination(coin, currency):
|
||||
ticker: Ticker | None = tickers.get(pair, None)
|
||||
if not ticker:
|
||||
tickers_other: Tickers = self.get_tickers(
|
||||
cached=True,
|
||||
market_type=(
|
||||
TradingMode.SPOT
|
||||
if self.trading_mode != TradingMode.SPOT
|
||||
else TradingMode.FUTURES
|
||||
),
|
||||
)
|
||||
ticker = tickers_other.get(pair, None)
|
||||
if ticker:
|
||||
rate: float | None = ticker.get("last", None)
|
||||
if rate and pair.startswith(currency) and not pair.endswith(currency):
|
||||
rate = 1.0 / rate
|
||||
return rate
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
ticker: Ticker | None = tickers.get(pair, None)
|
||||
if not ticker:
|
||||
tickers_other: Tickers = self.get_tickers(
|
||||
cached=True,
|
||||
market_type=(
|
||||
TradingMode.SPOT
|
||||
if self.trading_mode != TradingMode.SPOT
|
||||
else TradingMode.FUTURES
|
||||
),
|
||||
)
|
||||
ticker = tickers_other.get(pair, None)
|
||||
if ticker:
|
||||
rate: float | None = ticker.get("last", None)
|
||||
if rate and pair.startswith(currency) and not pair.endswith(currency):
|
||||
rate = 1.0 / rate
|
||||
return rate
|
||||
return None
|
||||
|
||||
@retrier
|
||||
@@ -2244,10 +2251,13 @@ class Exchange:
|
||||
# If cost is None or 0.0 -> falsy, return None
|
||||
return None
|
||||
try:
|
||||
comb = self.get_valid_pair_combination(fee_curr, self._config["stake_currency"])
|
||||
tick = self.fetch_ticker(comb)
|
||||
|
||||
fee_to_quote_rate = safe_value_fallback2(tick, tick, "last", "ask")
|
||||
for comb in self.get_valid_pair_combination(
|
||||
fee_curr, self._config["stake_currency"]
|
||||
):
|
||||
tick = self.fetch_ticker(comb)
|
||||
fee_to_quote_rate = safe_value_fallback2(tick, tick, "last", "ask")
|
||||
if tick:
|
||||
break
|
||||
except (ValueError, ExchangeError):
|
||||
fee_to_quote_rate = self._config["exchange"].get("unknown_fee_rate", None)
|
||||
if not fee_to_quote_rate:
|
||||
|
||||
Reference in New Issue
Block a user