Implement ticker caching

This commit is contained in:
Matthias 2019-11-09 19:45:09 +01:00
parent 4b15873ee1
commit 085aa3084e
4 changed files with 11 additions and 2 deletions

View File

@ -4,6 +4,7 @@ Static List provider
Provides lists as configured in config.json Provides lists as configured in config.json
""" """
from cachetools import TTLCache, cached
import logging import logging
from typing import Dict, List from typing import Dict, List
@ -68,6 +69,10 @@ class PairListManager():
""" """
return [{p.name: p.short_desc()} for p in self._pairlists] 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: def refresh_pairlist(self) -> None:
""" """
Run pairlist through all configured pairlists. 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 should be cached to avoid calling the exchange on each call.
tickers: Dict = {} tickers: Dict = {}
if self._tickers_needed: if self._tickers_needed:
tickers = self._exchange.get_tickers() tickers = self._get_cached_tickers()
# Process all pairlists in chain # Process all pairlists in chain
for pl in self._pairlists: for pl in self._pairlists:

View File

@ -4,6 +4,7 @@ ccxt==1.19.14
SQLAlchemy==1.3.10 SQLAlchemy==1.3.10
python-telegram-bot==12.2.0 python-telegram-bot==12.2.0
arrow==0.15.4 arrow==0.15.4
cachetools==3.1.1
requests==2.22.0 requests==2.22.0
urllib3==1.25.6 urllib3==1.25.6
wrapt==1.11.2 wrapt==1.11.2

View File

@ -66,6 +66,7 @@ setup(name='freqtrade',
'SQLAlchemy', 'SQLAlchemy',
'python-telegram-bot', 'python-telegram-bot',
'arrow', 'arrow',
'cachetools',
'requests', 'requests',
'urllib3', 'urllib3',
'wrapt', 'wrapt',

View File

@ -268,12 +268,14 @@ def test_volumepairlist_caching(mocker, markets, whitelist_conf, tickers):
) )
bot = get_patched_freqtradebot(mocker, whitelist_conf) bot = get_patched_freqtradebot(mocker, whitelist_conf)
assert bot.pairlists._pairlists[0]._last_refresh == 0 assert bot.pairlists._pairlists[0]._last_refresh == 0
assert tickers.call_count == 0
bot.pairlists.refresh_pairlist() bot.pairlists.refresh_pairlist()
assert tickers.call_count == 1
assert bot.pairlists._pairlists[0]._last_refresh != 0 assert bot.pairlists._pairlists[0]._last_refresh != 0
lrf = bot.pairlists._pairlists[0]._last_refresh lrf = bot.pairlists._pairlists[0]._last_refresh
bot.pairlists.refresh_pairlist() bot.pairlists.refresh_pairlist()
assert tickers.call_count == 1
# Time should not be updated. # Time should not be updated.
assert bot.pairlists._pairlists[0]._last_refresh == lrf assert bot.pairlists._pairlists[0]._last_refresh == lrf