diff --git a/freqtrade/pairlist/pairlistmanager.py b/freqtrade/pairlist/pairlistmanager.py index 309ada094..0734d7f8f 100644 --- a/freqtrade/pairlist/pairlistmanager.py +++ b/freqtrade/pairlist/pairlistmanager.py @@ -4,6 +4,7 @@ Static List provider Provides lists as configured in config.json """ +from cachetools import TTLCache, cached import logging from typing import Dict, List @@ -68,6 +69,10 @@ class PairListManager(): """ return [{p.name: p.short_desc()} for p in self._pairlists] + @cached(TTLCache(maxsize=1, ttl=1800)) + def _get_cached_tickers(self): + return self._exchange.get_tickers() + def refresh_pairlist(self) -> None: """ Run pairlist through all configured pairlists. @@ -78,7 +83,7 @@ class PairListManager(): # tickers should be cached to avoid calling the exchange on each call. tickers: Dict = {} if self._tickers_needed: - tickers = self._exchange.get_tickers() + tickers = self._get_cached_tickers() # Process all pairlists in chain for pl in self._pairlists: diff --git a/requirements-common.txt b/requirements-common.txt index 52b80d501..c11179fbb 100644 --- a/requirements-common.txt +++ b/requirements-common.txt @@ -4,6 +4,7 @@ ccxt==1.19.14 SQLAlchemy==1.3.10 python-telegram-bot==12.2.0 arrow==0.15.4 +cachetools==3.1.1 requests==2.22.0 urllib3==1.25.6 wrapt==1.11.2 diff --git a/setup.py b/setup.py index 781a5d138..50b8eee9c 100644 --- a/setup.py +++ b/setup.py @@ -66,6 +66,7 @@ setup(name='freqtrade', 'SQLAlchemy', 'python-telegram-bot', 'arrow', + 'cachetools', 'requests', 'urllib3', 'wrapt', diff --git a/tests/pairlist/test_pairlist.py b/tests/pairlist/test_pairlist.py index 0ec6766c2..94b2147f5 100644 --- a/tests/pairlist/test_pairlist.py +++ b/tests/pairlist/test_pairlist.py @@ -268,12 +268,14 @@ def test_volumepairlist_caching(mocker, markets, whitelist_conf, tickers): ) bot = get_patched_freqtradebot(mocker, whitelist_conf) assert bot.pairlists._pairlists[0]._last_refresh == 0 - + assert tickers.call_count == 0 bot.pairlists.refresh_pairlist() + assert tickers.call_count == 1 assert bot.pairlists._pairlists[0]._last_refresh != 0 lrf = bot.pairlists._pairlists[0]._last_refresh bot.pairlists.refresh_pairlist() + assert tickers.call_count == 1 # Time should not be updated. assert bot.pairlists._pairlists[0]._last_refresh == lrf