chore: update usage of TTLCache to FtTTLCache

This commit is contained in:
Matthias
2025-11-24 07:07:13 +01:00
parent f0a5b95ec0
commit 8274a6c3a9
13 changed files with 34 additions and 37 deletions

View File

@@ -5,7 +5,6 @@ from datetime import UTC, datetime
from pathlib import Path from pathlib import Path
import ccxt import ccxt
from cachetools import TTLCache
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import DEFAULT_DATAFRAME_COLUMNS from freqtrade.constants import DEFAULT_DATAFRAME_COLUMNS
@@ -21,6 +20,7 @@ from freqtrade.exchange.common import retrier
from freqtrade.exchange.exchange_types import FtHas, Tickers from freqtrade.exchange.exchange_types import FtHas, Tickers
from freqtrade.exchange.exchange_utils_timeframe import timeframe_to_msecs from freqtrade.exchange.exchange_utils_timeframe import timeframe_to_msecs
from freqtrade.misc import deep_merge_dicts, json_load from freqtrade.misc import deep_merge_dicts, json_load
from freqtrade.util import FtTTLCache
from freqtrade.util.datetime_helpers import dt_from_ts, dt_ts from freqtrade.util.datetime_helpers import dt_from_ts, dt_ts
@@ -76,7 +76,7 @@ class Binance(Exchange):
def __init__(self, *args, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._spot_delist_schedule_cache: TTLCache = TTLCache(maxsize=100, ttl=300) self._spot_delist_schedule_cache: FtTTLCache = FtTTLCache(maxsize=100, ttl=300)
def get_proxy_coin(self) -> str: def get_proxy_coin(self) -> str:
""" """

View File

@@ -16,7 +16,6 @@ from typing import Any, Literal, TypeGuard, TypeVar
import ccxt import ccxt
import ccxt.pro as ccxt_pro import ccxt.pro as ccxt_pro
from cachetools import TTLCache
from ccxt import TICK_SIZE from ccxt import TICK_SIZE
from dateutil import parser from dateutil import parser
from pandas import DataFrame, concat from pandas import DataFrame, concat
@@ -107,9 +106,8 @@ from freqtrade.misc import (
file_load_json, file_load_json,
safe_value_fallback2, safe_value_fallback2,
) )
from freqtrade.util import dt_from_ts, dt_now from freqtrade.util import FtTTLCache, PeriodicCache, dt_from_ts, dt_now
from freqtrade.util.datetime_helpers import dt_humanize_delta, dt_ts, format_ms_time from freqtrade.util.datetime_helpers import dt_humanize_delta, dt_ts, format_ms_time
from freqtrade.util.periodic_cache import PeriodicCache
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -230,13 +228,13 @@ class Exchange:
self._cache_lock = Lock() self._cache_lock = Lock()
# Cache for 10 minutes ... # Cache for 10 minutes ...
self._fetch_tickers_cache: TTLCache = TTLCache(maxsize=4, ttl=60 * 10) self._fetch_tickers_cache: FtTTLCache = FtTTLCache(maxsize=4, ttl=60 * 10)
# Cache values for 300 to avoid frequent polling of the exchange for prices # Cache values for 300 to avoid frequent polling of the exchange for prices
# Caching only applies to RPC methods, so prices for open trades are still # Caching only applies to RPC methods, so prices for open trades are still
# refreshed once every iteration. # refreshed once every iteration.
# Shouldn't be too high either, as it'll freeze UI updates in case of open orders. # Shouldn't be too high either, as it'll freeze UI updates in case of open orders.
self._exit_rate_cache: TTLCache = TTLCache(maxsize=100, ttl=300) self._exit_rate_cache: FtTTLCache = FtTTLCache(maxsize=100, ttl=300)
self._entry_rate_cache: TTLCache = TTLCache(maxsize=100, ttl=300) self._entry_rate_cache: FtTTLCache = FtTTLCache(maxsize=100, ttl=300)
# Holds candles # Holds candles
self._klines: dict[PairWithTimeframe, DataFrame] = {} self._klines: dict[PairWithTimeframe, DataFrame] = {}
@@ -2164,7 +2162,9 @@ class Exchange:
name = side.capitalize() name = side.capitalize()
strat_name = "entry_pricing" if side == "entry" else "exit_pricing" strat_name = "entry_pricing" if side == "entry" else "exit_pricing"
cache_rate: TTLCache = self._entry_rate_cache if side == "entry" else self._exit_rate_cache cache_rate: FtTTLCache = (
self._entry_rate_cache if side == "entry" else self._exit_rate_cache
)
if not refresh: if not refresh:
with self._cache_lock: with self._cache_lock:
rate = cache_rate.get(pair) rate = cache_rate.get(pair)

View File

@@ -1,6 +1,8 @@
from collections.abc import Callable from collections.abc import Callable
from cachetools import TTLCache, cached from cachetools import cached
from freqtrade.util import FtTTLCache
class LoggingMixin: class LoggingMixin:
@@ -18,7 +20,7 @@ class LoggingMixin:
""" """
self.logger = logger self.logger = logger
self.refresh_period = refresh_period self.refresh_period = refresh_period
self._log_cache: TTLCache = TTLCache(maxsize=1024, ttl=self.refresh_period) self._log_cache: FtTTLCache = FtTTLCache(maxsize=1024, ttl=self.refresh_period)
def log_once(self, message: str, logmethod: Callable, force_show: bool = False) -> None: def log_once(self, message: str, logmethod: Callable, force_show: bool = False) -> None:
""" """

View File

@@ -7,11 +7,10 @@ Provides dynamic pair list based on Market Cap
import logging import logging
import math import math
from cachetools import TTLCache
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange.exchange_types import Tickers from freqtrade.exchange.exchange_types import Tickers
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.util import FtTTLCache
from freqtrade.util.coin_gecko import FtCoinGeckoApi from freqtrade.util.coin_gecko import FtCoinGeckoApi
@@ -38,7 +37,7 @@ class MarketCapPairList(IPairList):
self._max_rank = self._pairlistconfig.get("max_rank", 30) self._max_rank = self._pairlistconfig.get("max_rank", 30)
self._refresh_period = self._pairlistconfig.get("refresh_period", 86400) self._refresh_period = self._pairlistconfig.get("refresh_period", 86400)
self._categories = self._pairlistconfig.get("categories", []) self._categories = self._pairlistconfig.get("categories", [])
self._marketcap_cache: TTLCache = TTLCache(maxsize=1, ttl=self._refresh_period) self._marketcap_cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=self._refresh_period)
_coingecko_config = self._config.get("coingecko", {}) _coingecko_config = self._config.get("coingecko", {})

View File

@@ -10,7 +10,6 @@ import logging
from datetime import timedelta from datetime import timedelta
from typing import TypedDict from typing import TypedDict
from cachetools import TTLCache
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes, PairWithTimeframe from freqtrade.constants import ListPairsWithTimeframes, PairWithTimeframe
@@ -18,7 +17,7 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date
from freqtrade.exchange.exchange_types import Ticker, Tickers from freqtrade.exchange.exchange_types import Ticker, Tickers
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.util import dt_now, format_ms_time from freqtrade.util import FtTTLCache, dt_now, format_ms_time
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -47,7 +46,7 @@ class PercentChangePairList(IPairList):
self._min_value = self._pairlistconfig.get("min_value", None) self._min_value = self._pairlistconfig.get("min_value", None)
self._max_value = self._pairlistconfig.get("max_value", None) self._max_value = self._pairlistconfig.get("max_value", None)
self._refresh_period = self._pairlistconfig.get("refresh_period", 1800) self._refresh_period = self._pairlistconfig.get("refresh_period", 1800)
self._pair_cache: TTLCache = TTLCache(maxsize=1, ttl=self._refresh_period) self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=self._refresh_period)
self._lookback_days = self._pairlistconfig.get("lookback_days", 0) self._lookback_days = self._pairlistconfig.get("lookback_days", 0)
self._lookback_timeframe = self._pairlistconfig.get("lookback_timeframe", "1d") self._lookback_timeframe = self._pairlistconfig.get("lookback_timeframe", "1d")
self._lookback_period = self._pairlistconfig.get("lookback_period", 0) self._lookback_period = self._pairlistconfig.get("lookback_period", 0)

View File

@@ -10,7 +10,6 @@ from typing import Any
import rapidjson import rapidjson
import requests import requests
from cachetools import TTLCache
from freqtrade import __version__ from freqtrade import __version__
from freqtrade.configuration.load_config import CONFIG_PARSE_MODE from freqtrade.configuration.load_config import CONFIG_PARSE_MODE
@@ -18,6 +17,7 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange.exchange_types import Tickers from freqtrade.exchange.exchange_types import Tickers
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
from freqtrade.util import FtTTLCache
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -48,7 +48,7 @@ class RemotePairList(IPairList):
self._number_pairs = self._pairlistconfig["number_assets"] self._number_pairs = self._pairlistconfig["number_assets"]
self._refresh_period: int = self._pairlistconfig.get("refresh_period", 1800) self._refresh_period: int = self._pairlistconfig.get("refresh_period", 1800)
self._keep_pairlist_on_failure = self._pairlistconfig.get("keep_pairlist_on_failure", True) self._keep_pairlist_on_failure = self._pairlistconfig.get("keep_pairlist_on_failure", True)
self._pair_cache: TTLCache = TTLCache(maxsize=1, ttl=self._refresh_period) self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=self._refresh_period)
self._pairlist_url = self._pairlistconfig.get("pairlist_url", "") self._pairlist_url = self._pairlistconfig.get("pairlist_url", "")
self._read_timeout = self._pairlistconfig.get("read_timeout", 60) self._read_timeout = self._pairlistconfig.get("read_timeout", 60)
self._bearer_token = self._pairlistconfig.get("bearer_token", "") self._bearer_token = self._pairlistconfig.get("bearer_token", "")
@@ -159,7 +159,7 @@ class RemotePairList(IPairList):
) )
self._refresh_period = remote_refresh_period self._refresh_period = remote_refresh_period
self._pair_cache = TTLCache(maxsize=1, ttl=remote_refresh_period) self._pair_cache = FtTTLCache(maxsize=1, ttl=remote_refresh_period)
self._init_done = True self._init_done = True

View File

@@ -7,7 +7,6 @@ import sys
from datetime import timedelta from datetime import timedelta
import numpy as np import numpy as np
from cachetools import TTLCache
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes from freqtrade.constants import ListPairsWithTimeframes
@@ -15,7 +14,7 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange.exchange_types import Tickers from freqtrade.exchange.exchange_types import Tickers
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.util import dt_floor_day, dt_now, dt_ts from freqtrade.util import FtTTLCache, dt_floor_day, dt_now, dt_ts
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -38,7 +37,7 @@ class VolatilityFilter(IPairList):
self._def_candletype = self._config["candle_type_def"] self._def_candletype = self._config["candle_type_def"]
self._sort_direction: str | None = self._pairlistconfig.get("sort_direction", None) self._sort_direction: str | None = self._pairlistconfig.get("sort_direction", None)
self._pair_cache: TTLCache = TTLCache(maxsize=1000, ttl=self._refresh_period) self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1000, ttl=self._refresh_period)
candle_limit = self._exchange.ohlcv_candle_limit("1d", self._def_candletype) candle_limit = self._exchange.ohlcv_candle_limit("1d", self._def_candletype)
if self._days < 1: if self._days < 1:

View File

@@ -8,14 +8,12 @@ import logging
from datetime import timedelta from datetime import timedelta
from typing import Any, Literal from typing import Any, Literal
from cachetools import TTLCache
from freqtrade.constants import ListPairsWithTimeframes from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date
from freqtrade.exchange.exchange_types import Tickers from freqtrade.exchange.exchange_types import Tickers
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.util import dt_now, format_ms_time from freqtrade.util import FtTTLCache, dt_now, format_ms_time
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -43,7 +41,7 @@ class VolumePairList(IPairList):
self._min_value = self._pairlistconfig.get("min_value", 0) self._min_value = self._pairlistconfig.get("min_value", 0)
self._max_value = self._pairlistconfig.get("max_value", None) self._max_value = self._pairlistconfig.get("max_value", None)
self._refresh_period = self._pairlistconfig.get("refresh_period", 1800) self._refresh_period = self._pairlistconfig.get("refresh_period", 1800)
self._pair_cache: TTLCache = TTLCache(maxsize=1, ttl=self._refresh_period) self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=self._refresh_period)
self._lookback_days = self._pairlistconfig.get("lookback_days", 0) self._lookback_days = self._pairlistconfig.get("lookback_days", 0)
self._lookback_timeframe = self._pairlistconfig.get("lookback_timeframe", "1d") self._lookback_timeframe = self._pairlistconfig.get("lookback_timeframe", "1d")
self._lookback_period = self._pairlistconfig.get("lookback_period", 0) self._lookback_period = self._pairlistconfig.get("lookback_period", 0)

View File

@@ -5,7 +5,6 @@ Rate of change pairlist filter
import logging import logging
from datetime import timedelta from datetime import timedelta
from cachetools import TTLCache
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes from freqtrade.constants import ListPairsWithTimeframes
@@ -13,7 +12,7 @@ from freqtrade.exceptions import OperationalException
from freqtrade.exchange.exchange_types import Tickers from freqtrade.exchange.exchange_types import Tickers
from freqtrade.misc import plural from freqtrade.misc import plural
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
from freqtrade.util import dt_floor_day, dt_now, dt_ts from freqtrade.util import FtTTLCache, dt_floor_day, dt_now, dt_ts
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -32,7 +31,7 @@ class RangeStabilityFilter(IPairList):
self._def_candletype = self._config["candle_type_def"] self._def_candletype = self._config["candle_type_def"]
self._sort_direction: str | None = self._pairlistconfig.get("sort_direction", None) self._sort_direction: str | None = self._pairlistconfig.get("sort_direction", None)
self._pair_cache: TTLCache = TTLCache(maxsize=1000, ttl=self._refresh_period) self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1000, ttl=self._refresh_period)
candle_limit = self._exchange.ohlcv_candle_limit("1d", self._def_candletype) candle_limit = self._exchange.ohlcv_candle_limit("1d", self._def_candletype)
if self._days < 1: if self._days < 1:

View File

@@ -5,7 +5,7 @@ PairList manager class
import logging import logging
from functools import partial from functools import partial
from cachetools import LRUCache, TTLCache, cached from cachetools import LRUCache, cached
from freqtrade.constants import Config, ListPairsWithTimeframes from freqtrade.constants import Config, ListPairsWithTimeframes
from freqtrade.data.dataprovider import DataProvider from freqtrade.data.dataprovider import DataProvider
@@ -17,6 +17,7 @@ from freqtrade.mixins import LoggingMixin
from freqtrade.plugins.pairlist.IPairList import IPairList, SupportsBacktesting from freqtrade.plugins.pairlist.IPairList import IPairList, SupportsBacktesting
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
from freqtrade.resolvers import PairListResolver from freqtrade.resolvers import PairListResolver
from freqtrade.util import FtTTLCache
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -129,7 +130,7 @@ class PairListManager(LoggingMixin):
"""List of short_desc for each Pairlist Handler""" """List of short_desc for each Pairlist Handler"""
return [{p.name: p.short_desc()} for p in self._pairlist_handlers] return [{p.name: p.short_desc()} for p in self._pairlist_handlers]
@cached(TTLCache(maxsize=1, ttl=1800)) @cached(FtTTLCache(maxsize=1, ttl=1800))
def _get_cached_tickers(self) -> Tickers: def _get_cached_tickers(self) -> Tickers:
return self._exchange.get_tickers() return self._exchange.get_tickers()

View File

@@ -37,7 +37,7 @@ class ApiBG:
# Generic background jobs # Generic background jobs
# TODO: Change this to TTLCache # TODO: Change this to FtTTLCache
jobs: dict[str, JobsContainer] = {} jobs: dict[str, JobsContainer] = {}
# Pairlist evaluate things # Pairlist evaluate things
pairlist_running: bool = False pairlist_running: bool = False

View File

@@ -7,11 +7,11 @@ import logging
from datetime import datetime from datetime import datetime
from typing import Any from typing import Any
from cachetools import TTLCache
from requests.exceptions import RequestException from requests.exceptions import RequestException
from freqtrade.constants import SUPPORTED_FIAT, Config from freqtrade.constants import SUPPORTED_FIAT, Config
from freqtrade.mixins.logging_mixin import LoggingMixin from freqtrade.mixins.logging_mixin import LoggingMixin
from freqtrade.util import FtTTLCache
from freqtrade.util.coin_gecko import FtCoinGeckoApi from freqtrade.util.coin_gecko import FtCoinGeckoApi
@@ -54,7 +54,7 @@ class CryptoToFiatConverter(LoggingMixin):
def __init__(self, config: Config) -> None: def __init__(self, config: Config) -> None:
# Timeout: 6h # Timeout: 6h
self._pair_price: TTLCache = TTLCache(maxsize=500, ttl=6 * 60 * 60) self._pair_price: FtTTLCache = FtTTLCache(maxsize=500, ttl=6 * 60 * 60)
_coingecko_config = config.get("coingecko", {}) _coingecko_config = config.get("coingecko", {})
self._coingecko = FtCoinGeckoApi( self._coingecko = FtCoinGeckoApi(

View File

@@ -2,7 +2,7 @@ import logging
import time import time
from collections.abc import Callable from collections.abc import Callable
from cachetools import TTLCache from freqtrade.util import FtTTLCache
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -27,7 +27,7 @@ class MeasureTime:
""" """
self._callback = callback self._callback = callback
self._time_limit = time_limit self._time_limit = time_limit
self.__cache: TTLCache = TTLCache(maxsize=1, ttl=ttl) self.__cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=ttl)
def __enter__(self): def __enter__(self):
self._start = time.time() self._start = time.time()