stable/freqtrade/pairlist/IPairList.py

102 lines
3.5 KiB
Python
Raw Normal View History

2018-12-05 19:44:56 +00:00
"""
Static List provider
Provides lists as configured in config.json
"""
import logging
from abc import ABC, abstractmethod, abstractproperty
from copy import deepcopy
2019-10-30 14:59:52 +00:00
from typing import Dict, List
2018-12-05 19:44:56 +00:00
from freqtrade.exchange import market_is_active
2018-12-05 19:44:56 +00:00
logger = logging.getLogger(__name__)
class IPairList(ABC):
def __init__(self, exchange, pairlistmanager, config, pairlistconfig: dict) -> None:
2019-11-09 05:55:16 +00:00
self._exchange = exchange
self._pairlistmanager = pairlistmanager
2018-12-05 19:44:56 +00:00
self._config = config
2019-11-09 05:55:16 +00:00
self._pairlistconfig = pairlistconfig
2018-12-05 19:44:56 +00:00
@property
def name(self) -> str:
"""
Gets name of the class
-> no need to overwrite in subclasses
"""
return self.__class__.__name__
@abstractproperty
def needstickers(self) -> bool:
"""
Boolean property defining if tickers are necessary.
If no Pairlist requries tickers, an empty List is passed
as tickers argument to filter_pairlist
"""
2018-12-05 19:44:56 +00:00
@abstractmethod
def short_desc(self) -> str:
"""
Short whitelist method description - used for startup-messages
-> Please overwrite in subclasses
"""
@abstractmethod
2019-11-09 05:55:16 +00:00
def filter_pairlist(self, pairlist: List[str], tickers: List[Dict]) -> List[str]:
2018-12-05 19:44:56 +00:00
"""
2019-11-09 05:55:16 +00:00
Filters and sorts pairlist and returns the whitelist again.
Called on each bot iteration - please use internal caching if necessary
2018-12-05 19:44:56 +00:00
-> Please overwrite in subclasses
2019-11-09 05:55:16 +00:00
:param pairlist: pairlist to filter or sort
:param tickers: Tickers (from exchange.get_tickers()). May be cached.
:return: new whitelist
2018-12-05 19:44:56 +00:00
"""
@staticmethod
2019-11-09 08:07:46 +00:00
def verify_blacklist(pairlist: List[str], blacklist: List[str]) -> List[str]:
2019-11-09 06:23:34 +00:00
"""
Verify and remove items from pairlist - returning a filtered pairlist.
"""
for pair in deepcopy(pairlist):
2019-11-09 06:23:34 +00:00
if pair in blacklist:
logger.warning(f"Pair {pair} in your blacklist. Removing it from whitelist...")
pairlist.remove(pair)
return pairlist
2019-11-09 06:23:34 +00:00
def _verify_blacklist(self, pairlist: List[str]) -> List[str]:
"""
Proxy method to verify_blacklist for easy access for child classes.
"""
return IPairList.verify_blacklist(pairlist, self._pairlistmanager.blacklist)
2019-10-30 14:59:52 +00:00
def _whitelist_for_active_markets(self, whitelist: List[str]) -> List[str]:
2018-12-05 19:44:56 +00:00
"""
Check available markets and remove pair from whitelist if necessary
2019-03-11 20:10:22 +00:00
:param whitelist: the sorted list of pairs the user might want to trade
:return: the list of pairs the user wants to trade without those unavailable or
2018-12-05 19:44:56 +00:00
black_listed
"""
2019-11-09 05:55:16 +00:00
markets = self._exchange.markets
2018-12-05 19:44:56 +00:00
2019-10-30 14:59:52 +00:00
sanitized_whitelist: List[str] = []
for pair in whitelist:
2019-11-09 05:55:16 +00:00
# pair is not in the generated dynamic market or has the wrong stake currency
if (pair not in markets or not pair.endswith(self._config['stake_currency'])):
2019-03-17 17:18:35 +00:00
logger.warning(f"Pair {pair} is not compatible with exchange "
2019-11-09 05:55:16 +00:00
f"{self._exchange.name}. Removing it from whitelist..")
2018-12-05 19:44:56 +00:00
continue
# Check if market is active
market = markets[pair]
if not market_is_active(market):
2019-03-21 05:14:43 +00:00
logger.info(f"Ignoring {pair} from whitelist. Market is not active.")
continue
2019-10-30 14:59:52 +00:00
if pair not in sanitized_whitelist:
sanitized_whitelist.append(pair)
2018-12-05 19:44:56 +00:00
# We need to remove pairs that are unknown
2019-10-30 14:59:52 +00:00
return sanitized_whitelist