freqtrade_origin/freqtrade/plugins/pairlist/ProducerPairList.py

120 lines
3.9 KiB
Python
Raw Normal View History

2022-09-18 13:29:57 +00:00
"""
External Pair List provider
Provides pair list from Leader data
"""
2024-05-12 14:37:11 +00:00
2022-09-18 13:29:57 +00:00
import logging
from typing import Optional
2022-09-18 13:29:57 +00:00
2022-09-25 08:13:00 +00:00
from freqtrade.exceptions import OperationalException
2024-09-04 04:42:51 +00:00
from freqtrade.exchange.exchange_types import Tickers
from freqtrade.plugins.pairlist.IPairList import IPairList, PairlistParameter, SupportsBacktesting
2022-09-18 13:29:57 +00:00
logger = logging.getLogger(__name__)
class ProducerPairList(IPairList):
"""
PairList plugin for use with external_message_consumer.
Will use pairs given from leader data.
Usage:
"pairlists": [
{
"method": "ProducerPairList",
"number_assets": 5,
"producer_name": "default",
}
],
"""
2024-05-12 14:37:11 +00:00
is_pairlist_generator = True
supports_backtesting = SupportsBacktesting.NO
2022-09-18 13:29:57 +00:00
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
2022-09-18 13:29:57 +00:00
2024-05-12 14:37:11 +00:00
self._num_assets: int = self._pairlistconfig.get("number_assets", 0)
self._producer_name = self._pairlistconfig.get("producer_name", "default")
2024-06-09 06:52:40 +00:00
if not self._config.get("external_message_consumer", {}).get("enabled"):
2022-09-25 08:13:00 +00:00
raise OperationalException(
2024-05-12 14:37:11 +00:00
"ProducerPairList requires external_message_consumer to be enabled."
)
2022-09-18 13:29:57 +00:00
@property
def needstickers(self) -> bool:
"""
Boolean property defining if tickers are necessary.
If no Pairlist requires tickers, an empty Dict is passed
as tickers argument to filter_pairlist
"""
return False
def short_desc(self) -> str:
"""
Short whitelist method description - used for startup-messages
-> Please overwrite in subclasses
"""
return f"{self.name} - {self._producer_name}"
@staticmethod
def description() -> str:
return "Get a pairlist from an upstream bot."
2023-04-20 05:20:45 +00:00
@staticmethod
def available_parameters() -> dict[str, PairlistParameter]:
2023-04-20 05:20:45 +00:00
return {
"number_assets": {
"type": "number",
"default": 0,
"description": "Number of assets",
"help": "Number of assets to use from the pairlist",
},
"producer_name": {
"type": "string",
"default": "default",
"description": "Producer name",
2024-05-12 14:37:11 +00:00
"help": (
"Name of the producer to use. Requires additional "
"external_message_consumer configuration."
),
2023-04-20 05:20:45 +00:00
},
}
def _filter_pairlist(self, pairlist: Optional[list[str]]):
2022-09-18 13:29:57 +00:00
upstream_pairlist = self._pairlistmanager._dataprovider.get_producer_pairs(
2024-05-12 14:37:11 +00:00
self._producer_name
)
2022-09-18 13:29:57 +00:00
if pairlist is None:
pairlist = self._pairlistmanager._dataprovider.get_producer_pairs(self._producer_name)
2022-09-25 17:32:39 +00:00
pairs = list(dict.fromkeys(pairlist + upstream_pairlist))
2022-09-25 07:38:20 +00:00
if self._num_assets:
2024-05-12 14:37:11 +00:00
pairs = pairs[: self._num_assets]
2022-09-18 13:29:57 +00:00
return pairs
def gen_pairlist(self, tickers: Tickers) -> list[str]:
2022-09-18 13:29:57 +00:00
"""
Generate the pairlist
:param tickers: Tickers (from exchange.get_tickers). May be cached.
2022-09-18 13:29:57 +00:00
:return: List of pairs
"""
pairs = self._filter_pairlist(None)
self.log_once(f"Received pairs: {pairs}", logger.debug)
pairs = self._whitelist_for_active_markets(self.verify_whitelist(pairs, logger.info))
return pairs
def filter_pairlist(self, pairlist: list[str], tickers: Tickers) -> list[str]:
2022-09-18 13:29:57 +00:00
"""
Filters and sorts pairlist and returns the whitelist again.
Called on each bot iteration - please use internal caching if necessary
:param pairlist: pairlist to filter or sort
:param tickers: Tickers (from exchange.get_tickers). May be cached.
2022-09-18 13:29:57 +00:00
:return: new whitelist
"""
return self._filter_pairlist(pairlist)