diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 7fd0e5f43..4af9db6db 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -375,6 +375,8 @@ class Exchange(object): def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict: if refresh or pair not in self._cached_ticker.keys(): try: + if pair not in self._api.markets: + raise DependencyException(f"Pair {pair} not available") data = self._api.fetch_ticker(pair) try: self._cached_ticker[pair] = { diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index d653ea176..900ad1998 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -10,10 +10,10 @@ from typing import Dict, Any, List, Optional import arrow import sqlalchemy as sql -from numpy import mean, nan_to_num +from numpy import mean, nan_to_num, NAN from pandas import DataFrame -from freqtrade import TemporaryError +from freqtrade import TemporaryError, DependencyException from freqtrade.fiat_convert import CryptoToFiatConverter from freqtrade.misc import shorten_date from freqtrade.persistence import Trade @@ -93,7 +93,10 @@ class RPC(object): if trade.open_order_id: order = self._freqtrade.exchange.get_order(trade.open_order_id, trade.pair) # calculate profit and send message to user - current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + try: + current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + except DependencyException: + current_rate = NAN current_profit = trade.calc_profit_percent(current_rate) fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%' if trade.close_profit else None) @@ -122,7 +125,10 @@ class RPC(object): trades_list = [] for trade in trades: # calculate profit and send message to user - current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + try: + current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + except DependencyException: + current_rate = NAN trade_perc = (100 * trade.calc_profit_percent(current_rate)) trades_list.append([ trade.id, @@ -207,7 +213,10 @@ class RPC(object): profit_closed_percent.append(profit_percent) else: # Get current rate - current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + try: + current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid'] + except DependencyException: + current_rate = NAN profit_percent = trade.calc_profit_percent(rate=current_rate) profit_all_coin.append( @@ -275,7 +284,7 @@ class RPC(object): rate = 1.0 / self._freqtrade.exchange.get_ticker('BTC/USDT', False)['bid'] else: rate = self._freqtrade.exchange.get_ticker(coin + '/BTC', False)['bid'] - except TemporaryError: + except (TemporaryError, DependencyException): continue est_btc: float = rate * balance['total'] total = total + est_btc diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index 93cd1e546..788ef4518 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -572,6 +572,7 @@ def test_get_ticker(default_conf, mocker): 'last': 0.0001, } api_mock.fetch_ticker = MagicMock(return_value=tick) + api_mock.markets = {'ETH/BTC': {}} exchange = get_patched_exchange(mocker, default_conf, api_mock) # retrieve original ticker ticker = exchange.get_ticker(pair='ETH/BTC') @@ -614,6 +615,9 @@ def test_get_ticker(default_conf, mocker): exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange.get_ticker(pair='ETH/BTC', refresh=True) + with pytest.raises(DependencyException, match=r'Pair XRP/ETH not available'): + exchange.get_ticker(pair='XRP/ETH', refresh=True) + def test_get_history(default_conf, mocker, caplog): exchange = get_patched_exchange(mocker, default_conf)