Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions freqtrade/exchange/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,9 +2137,10 @@ def get_conversion_rate(self, coin: str, currency: str, *, cached=True) -> float
ticker = tickers_other.get(pair, None)
if ticker:
rate: float | None = safe_value_fallback(ticker, "last", "ask", None)
if rate and pair.startswith(currency) and not pair.endswith(currency):
rate = 1.0 / rate
return rate
if rate:
if pair.startswith(currency) and not pair.endswith(currency):
rate = 1.0 / rate
return rate
except ValueError:
return None
return None
Expand Down
19 changes: 16 additions & 3 deletions tests/exchange/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,7 @@ def test_get_conversion_rate(default_conf_usdt, mocker, exchange_name):
api_mock = MagicMock()
tick = {
"ETH/USDT": {
"last": 42,
"last": None,
},
"BCH/USDT": {
"last": 41,
Expand All @@ -2375,7 +2375,10 @@ def test_get_conversion_rate(default_conf_usdt, mocker, exchange_name):
tick2 = {
"ADA/USDT:USDT": {
"last": 2.5,
}
},
"ETH/USDT:USDT": {
"last": 42,
},
}
mocker.patch(f"{EXMS}.exchange_has", return_value=True)
api_mock.fetch_tickers = MagicMock(side_effect=[tick, tick2])
Expand All @@ -2386,14 +2389,24 @@ def test_get_conversion_rate(default_conf_usdt, mocker, exchange_name):
# retrieve original ticker
assert exchange.get_conversion_rate("USDT", "USDT") == 1
assert api_mock.fetch_tickers.call_count == 0
# ETH must fall back to the "others" market since ETH/USDT is None.
assert exchange.get_conversion_rate("ETH", "USDT") == 42
assert exchange.get_conversion_rate("ETH", "USDC") is None
assert exchange.get_conversion_rate("ETH", "BTC") == 250
assert api_mock.fetch_tickers.call_count == 2
api_mock.fetch_tickers.reset_mock()
api_mock.fetch_tickers.side_effect = [tick, tick2]
# Cached tickers
assert exchange.get_conversion_rate("BTC", "ETH") == 0.004

assert api_mock.fetch_tickers.call_count == 1
assert api_mock.fetch_tickers.call_count == 0
# Uncached tickers
api_mock.fetch_tickers.reset_mock()
assert exchange.get_conversion_rate("BTC", "ETH", cached=False) == 0.004
assert api_mock.fetch_tickers.call_count == 1

api_mock.fetch_tickers.reset_mock()
exchange._fetch_tickers_cache.clear()
assert exchange.get_conversion_rate("ADA", "USDT") == 2.5
# Only the call to the "others" market
assert api_mock.fetch_tickers.call_count == 1
Expand Down
Loading