Remove fetch_ticker caching
This commit is contained in:
parent
43add0b159
commit
f5b4a6d3d7
@ -66,8 +66,6 @@ class Exchange:
|
|||||||
|
|
||||||
self._config.update(config)
|
self._config.update(config)
|
||||||
|
|
||||||
self._cached_ticker: Dict[str, Any] = {}
|
|
||||||
|
|
||||||
# Holds last candle refreshed time of each pair
|
# Holds last candle refreshed time of each pair
|
||||||
self._pairs_last_refresh_time: Dict[Tuple[str, str], int] = {}
|
self._pairs_last_refresh_time: Dict[Tuple[str, str], int] = {}
|
||||||
# Timestamp of last markets refresh
|
# Timestamp of last markets refresh
|
||||||
@ -591,28 +589,17 @@ class Exchange:
|
|||||||
raise OperationalException(e) from e
|
raise OperationalException(e) from e
|
||||||
|
|
||||||
@retrier
|
@retrier
|
||||||
def fetch_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
|
def fetch_ticker(self, pair: str) -> dict:
|
||||||
if refresh or pair not in self._cached_ticker.keys():
|
try:
|
||||||
try:
|
if pair not in self._api.markets or not self._api.markets[pair].get('active'):
|
||||||
if pair not in self._api.markets or not self._api.markets[pair].get('active'):
|
raise DependencyException(f"Pair {pair} not available")
|
||||||
raise DependencyException(f"Pair {pair} not available")
|
data = self._api.fetch_ticker(pair)
|
||||||
data = self._api.fetch_ticker(pair)
|
return data
|
||||||
try:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
self._cached_ticker[pair] = {
|
raise TemporaryError(
|
||||||
'bid': float(data['bid']),
|
f'Could not load ticker due to {e.__class__.__name__}. Message: {e}') from e
|
||||||
'ask': float(data['ask']),
|
except ccxt.BaseError as e:
|
||||||
}
|
raise OperationalException(e) from e
|
||||||
except KeyError:
|
|
||||||
logger.debug("Could not cache ticker data for %s", pair)
|
|
||||||
return data
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
||||||
raise TemporaryError(
|
|
||||||
f'Could not load ticker due to {e.__class__.__name__}. Message: {e}') from e
|
|
||||||
except ccxt.BaseError as e:
|
|
||||||
raise OperationalException(e) from e
|
|
||||||
else:
|
|
||||||
logger.info("returning cached ticker-data for %s", pair)
|
|
||||||
return self._cached_ticker[pair]
|
|
||||||
|
|
||||||
def get_historic_ohlcv(self, pair: str, timeframe: str,
|
def get_historic_ohlcv(self, pair: str, timeframe: str,
|
||||||
since_ms: int) -> List:
|
since_ms: int) -> List:
|
||||||
|
@ -253,7 +253,7 @@ class FreqtradeBot:
|
|||||||
else:
|
else:
|
||||||
if not tick:
|
if not tick:
|
||||||
logger.info('Using Last Ask / Last Price')
|
logger.info('Using Last Ask / Last Price')
|
||||||
ticker = self.exchange.fetch_ticker(pair, refresh)
|
ticker = self.exchange.fetch_ticker(pair)
|
||||||
else:
|
else:
|
||||||
ticker = tick
|
ticker = tick
|
||||||
if ticker['ask'] < ticker['last']:
|
if ticker['ask'] < ticker['last']:
|
||||||
@ -631,7 +631,7 @@ class FreqtradeBot:
|
|||||||
rate = order_book['bids'][0][0]
|
rate = order_book['bids'][0][0]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
rate = self.exchange.fetch_ticker(pair, refresh)['bid']
|
rate = self.exchange.fetch_ticker(pair)['bid']
|
||||||
return rate
|
return rate
|
||||||
|
|
||||||
def handle_trade(self, trade: Trade) -> bool:
|
def handle_trade(self, trade: Trade) -> bool:
|
||||||
|
@ -1121,25 +1121,16 @@ def test_fetch_ticker(default_conf, mocker, exchange_name):
|
|||||||
assert ticker['bid'] == 0.5
|
assert ticker['bid'] == 0.5
|
||||||
assert ticker['ask'] == 1
|
assert ticker['ask'] == 1
|
||||||
|
|
||||||
assert 'ETH/BTC' in exchange._cached_ticker
|
|
||||||
assert exchange._cached_ticker['ETH/BTC']['bid'] == 0.5
|
|
||||||
assert exchange._cached_ticker['ETH/BTC']['ask'] == 1
|
|
||||||
|
|
||||||
# Test caching
|
|
||||||
api_mock.fetch_ticker = MagicMock()
|
|
||||||
exchange.fetch_ticker(pair='ETH/BTC', refresh=False)
|
|
||||||
assert api_mock.fetch_ticker.call_count == 0
|
|
||||||
|
|
||||||
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
||||||
"fetch_ticker", "fetch_ticker",
|
"fetch_ticker", "fetch_ticker",
|
||||||
pair='ETH/BTC', refresh=True)
|
pair='ETH/BTC')
|
||||||
|
|
||||||
api_mock.fetch_ticker = MagicMock(return_value={})
|
api_mock.fetch_ticker = MagicMock(return_value={})
|
||||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
||||||
exchange.fetch_ticker(pair='ETH/BTC', refresh=True)
|
exchange.fetch_ticker(pair='ETH/BTC')
|
||||||
|
|
||||||
with pytest.raises(DependencyException, match=r'Pair XRP/ETH not available'):
|
with pytest.raises(DependencyException, match=r'Pair XRP/ETH not available'):
|
||||||
exchange.fetch_ticker(pair='XRP/ETH', refresh=True)
|
exchange.fetch_ticker(pair='XRP/ETH')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
||||||
|
@ -67,8 +67,6 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
|
|||||||
|
|
||||||
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
||||||
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
||||||
# invalidate ticker cache
|
|
||||||
rpc._freqtrade.exchange._cached_ticker = {}
|
|
||||||
results = rpc._rpc_trade_status()
|
results = rpc._rpc_trade_status()
|
||||||
assert isnan(results[0]['current_profit'])
|
assert isnan(results[0]['current_profit'])
|
||||||
assert isnan(results[0]['current_rate'])
|
assert isnan(results[0]['current_rate'])
|
||||||
@ -136,8 +134,6 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
|
|||||||
|
|
||||||
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
||||||
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
||||||
# invalidate ticker cache
|
|
||||||
rpc._freqtrade.exchange._cached_ticker = {}
|
|
||||||
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
|
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
|
||||||
assert 'instantly' == result[0][2]
|
assert 'instantly' == result[0][2]
|
||||||
assert 'ETH/BTC' in result[0][1]
|
assert 'ETH/BTC' in result[0][1]
|
||||||
@ -262,8 +258,6 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
|
|||||||
# Test non-available pair
|
# Test non-available pair
|
||||||
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
||||||
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
|
||||||
# invalidate ticker cache
|
|
||||||
rpc._freqtrade.exchange._cached_ticker = {}
|
|
||||||
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
|
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||||
assert stats['trade_count'] == 2
|
assert stats['trade_count'] == 2
|
||||||
assert stats['first_trade_date'] == 'just now'
|
assert stats['first_trade_date'] == 'just now'
|
||||||
|
Loading…
Reference in New Issue
Block a user