Elffuss BraidHub

kikocis/freqtrade public

Merge pull request #4734 from freqtrade/get_tickers_ccache

/balance should use cached tickers when possible
Changeset4ff30cb89dd9c674a357cdb31faffeb7b6bf0af22ab4d6aaaad090f2a9d8346e
AuthorMatthias <xmatthias@outlook.com> · 2021-04-14 06:30 +02:00
CommitterGitHub <noreply@github.com> · 2021-04-14 06:30 +02:00
Parentsa355c1751cba, 7b2f62efd41a

Browse the files at this changeset

4 files changed

modified freqtrade/exchange/exchange.py +16 −2
@@ -14,6 +14,7 @@
1414 import arrow
1515 import ccxt
1616 import ccxt.async_support as ccxt_async
17+from cachetools import TTLCache
1718 from ccxt.base.decimal_to_precision import (ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE,
1819 decimal_to_precision)
1920 from pandas import DataFrame
@@ -84,6 +85,9 @@
8485 # Timestamp of last markets refresh
8586 self._last_markets_refresh: int = 0
8687
88+ # Cache for 10 minutes ...
89+ self._fetch_tickers_cache: TTLCache = TTLCache(maxsize=1, ttl=60 * 10)
90+
8791 # Holds candles
8892 self._klines: Dict[Tuple[str, str], DataFrame] = {}
8993
@@ -693,9 +697,19 @@
693697 raise OperationalException(e) from e
694698
695699 @retrier
696- def get_tickers(self) -> Dict:
700+ def get_tickers(self, cached: bool = False) -> Dict:
701+ """
702+ :param cached: Allow cached result
703+ :return: fetch_tickers result
704+ """
705+ if cached:
706+ tickers = self._fetch_tickers_cache.get('fetch_tickers')
707+ if tickers:
708+ return tickers
697709 try:
698- return self._api.fetch_tickers()
710+ tickers = self._api.fetch_tickers()
711+ self._fetch_tickers_cache['fetch_tickers'] = tickers
712+ return tickers
699713 except ccxt.NotSupported as e:
700714 raise OperationalException(
701715 f'Exchange {self._api.name} does not support fetching tickers in batch. '
modified freqtrade/rpc/rpc.py +1 −1
@@ -442,7 +442,7 @@
442442 output = []
443443 total = 0.0
444444 try:
445- tickers = self._freqtrade.exchange.get_tickers()
445+ tickers = self._freqtrade.exchange.get_tickers(cached=True)
446446 except (ExchangeError):
447447 raise RPCException('Error getting current tickers.')
448448
modified tests/exchange/test_exchange.py +10 −0
@@ -1319,6 +1319,16 @@
13191319 assert tickers['ETH/BTC']['ask'] == 1
13201320 assert tickers['BCH/BTC']['bid'] == 0.6
13211321 assert tickers['BCH/BTC']['ask'] == 0.5
1322+ assert api_mock.fetch_tickers.call_count == 1
1323+
1324+ api_mock.fetch_tickers.reset_mock()
1325+
1326+ # Cached ticker should not call api again
1327+ tickers2 = exchange.get_tickers(cached=True)
1328+ assert tickers2 == tickers
1329+ assert api_mock.fetch_tickers.call_count == 0
1330+ tickers2 = exchange.get_tickers(cached=False)
1331+ assert api_mock.fetch_tickers.call_count == 1
13221332
13231333 ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
13241334 "get_tickers", "fetch_tickers")
modified tests/rpc/test_rpc.py +2 −0
@@ -569,6 +569,8 @@
569569 result = rpc._rpc_balance(default_conf['stake_currency'], default_conf['fiat_display_currency'])
570570 assert prec_satoshi(result['total'], 12.309096315)
571571 assert prec_satoshi(result['value'], 184636.44472997)
572+ assert tickers.call_count == 1
573+ assert tickers.call_args_list[0][1]['cached'] is True
572574 assert 'USD' == result['symbol']
573575 assert result['currencies'] == [
574576 {'currency': 'BTC',