|
| 1 | +"""Cross Market pair list filter""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from freqtrade.constants import PairPrefixes |
| 6 | +from freqtrade.exchange.exchange_types import Tickers |
| 7 | +from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting |
| 8 | +from freqtrade.util import FtTTLCache |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class CrossMarketPairList(IPairList): |
| 15 | + is_pairlist_generator = True |
| 16 | + supports_backtesting = SupportsBacktesting.BIASED |
| 17 | + |
| 18 | + def __init__(self, *args, **kwargs) -> None: |
| 19 | + super().__init__(*args, **kwargs) |
| 20 | + |
| 21 | + self._pairs_exist_on: str = self._pairlistconfig.get("pairs_exist_on", "both_markets") |
| 22 | + self._stake_currency: str = self._config["stake_currency"] |
| 23 | + self._target_mode = "spot" if self._config["trading_mode"] == "futures" else "futures" |
| 24 | + self._refresh_period = self._pairlistconfig.get("refresh_period", 1800) |
| 25 | + self._pair_cache: FtTTLCache = FtTTLCache(maxsize=1, ttl=self._refresh_period) |
| 26 | + |
| 27 | + @property |
| 28 | + def needstickers(self) -> bool: |
| 29 | + """ |
| 30 | + Boolean property defining if tickers are necessary. |
| 31 | + If no Pairlist requires tickers, an empty Dict is passed |
| 32 | + as tickers argument to filter_pairlist |
| 33 | + """ |
| 34 | + return False |
| 35 | + |
| 36 | + def short_desc(self) -> str: |
| 37 | + """ |
| 38 | + Short whitelist method description - used for startup-messages |
| 39 | + """ |
| 40 | + pairs_exist_on = self._pairs_exist_on |
| 41 | + msg = f"{self.name} - Pairs that exists on {pairs_exist_on.capitalize()}." |
| 42 | + return msg |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def description() -> str: |
| 46 | + return "Filter pairs if they exist or not on another market." |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def available_parameters() -> dict[str, PairlistParameter]: |
| 50 | + return { |
| 51 | + "pairs_exist_on": { |
| 52 | + "type": "option", |
| 53 | + "default": "both_markets", |
| 54 | + "options": ["current_market_only", "both_markets"], |
| 55 | + "description": "Mode of operation", |
| 56 | + "help": "Mode of operation (current_market_only/both_markets)", |
| 57 | + }, |
| 58 | + **IPairList.refresh_period_parameter(), |
| 59 | + } |
| 60 | + |
| 61 | + def get_base_list(self) -> list[str]: |
| 62 | + target_mode = self._target_mode |
| 63 | + spot_only = True if target_mode == "spot" else False |
| 64 | + futures_only = True if target_mode == "futures" else False |
| 65 | + bases = [ |
| 66 | + v.get("base", "") |
| 67 | + for _, v in self._exchange.get_markets( |
| 68 | + quote_currencies=[self._stake_currency], |
| 69 | + tradable_only=False, |
| 70 | + active_only=True, |
| 71 | + spot_only=spot_only, |
| 72 | + futures_only=futures_only, |
| 73 | + ).items() |
| 74 | + ] |
| 75 | + return bases |
| 76 | + |
| 77 | + def gen_pairlist(self, tickers: Tickers) -> list[str]: |
| 78 | + """ |
| 79 | + Generate the pairlist |
| 80 | + :param tickers: Tickers (from exchange.get_tickers). May be cached. |
| 81 | + :return: List of pairs |
| 82 | + """ |
| 83 | + # Generate dynamic whitelist |
| 84 | + # Must always run if this pairlist is the first in the list. |
| 85 | + pairlist = self._pair_cache.get("pairlist") |
| 86 | + if pairlist: |
| 87 | + # Item found - no refresh necessary |
| 88 | + return pairlist.copy() |
| 89 | + else: |
| 90 | + # Use fresh pairlist |
| 91 | + # Check if pair quote currency equals to the stake currency. |
| 92 | + _pairlist = [ |
| 93 | + k |
| 94 | + for k in self._exchange.get_markets( |
| 95 | + quote_currencies=[self._stake_currency], tradable_only=True, active_only=True |
| 96 | + ).keys() |
| 97 | + ] |
| 98 | + |
| 99 | + _pairlist = self.verify_blacklist(_pairlist, logger.info) |
| 100 | + |
| 101 | + pairlist = self.filter_pairlist(_pairlist, tickers) |
| 102 | + self._pair_cache["pairlist"] = pairlist.copy() |
| 103 | + |
| 104 | + return pairlist |
| 105 | + |
| 106 | + def filter_pairlist(self, pairlist: list[str], tickers: Tickers) -> list[str]: |
| 107 | + bases = self.get_base_list() |
| 108 | + pairs_exist_on = self._pairs_exist_on |
| 109 | + is_whitelist_mode = pairs_exist_on == "both_markets" |
| 110 | + whitelisted_pairlist: list[str] = [] |
| 111 | + filtered_pairlist = pairlist.copy() |
| 112 | + |
| 113 | + for pair in pairlist: |
| 114 | + base = self._exchange.get_pair_base_currency(pair) |
| 115 | + if not base: |
| 116 | + self.log_once( |
| 117 | + f"Unable to get base currency for pair {pair}, skipping it.", logger.warning |
| 118 | + ) |
| 119 | + filtered_pairlist.remove(pair) |
| 120 | + continue |
| 121 | + found_in_bases = base in bases |
| 122 | + if not found_in_bases: |
| 123 | + for prefix in PairPrefixes: |
| 124 | + # Check in case of PEPE needs to be changed into 1000PEPE for example |
| 125 | + test_prefix = f"{prefix}{base}" |
| 126 | + found_in_bases = test_prefix in bases |
| 127 | + if found_in_bases: |
| 128 | + break |
| 129 | + |
| 130 | + # Avoid false positive since there are KAVA and AVA pairs, which aren't related |
| 131 | + if prefix != "K": |
| 132 | + # Check in case of 1000PEPE needs to be changed into PEPE for example |
| 133 | + if base.startswith(prefix): |
| 134 | + temp_base = base.removeprefix(prefix) |
| 135 | + found_in_bases = temp_base in bases |
| 136 | + if found_in_bases: |
| 137 | + break |
| 138 | + if found_in_bases: |
| 139 | + whitelisted_pairlist.append(pair) |
| 140 | + filtered_pairlist.remove(pair) |
| 141 | + |
| 142 | + return whitelisted_pairlist if is_whitelist_mode else filtered_pairlist |
0 commit comments