2018-11-30 05:34:56 +00:00
|
|
|
"""
|
2019-03-02 16:24:28 +00:00
|
|
|
Volume PairList provider
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
Provides dynamic pair list based on trade volumes
|
|
|
|
"""
|
2018-11-30 05:34:56 +00:00
|
|
|
import logging
|
2019-11-09 12:40:36 +00:00
|
|
|
from datetime import datetime
|
2020-02-02 04:00:40 +00:00
|
|
|
from typing import Any, Dict, List
|
2019-10-29 09:39:27 +00:00
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-10-29 09:39:27 +00:00
|
|
|
from freqtrade.pairlist.IPairList import IPairList
|
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
|
2018-11-30 05:34:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
|
2018-12-04 06:12:56 +00:00
|
|
|
SORT_VALUES = ['askVolume', 'bidVolume', 'quoteVolume']
|
|
|
|
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2018-12-05 19:44:56 +00:00
|
|
|
class VolumePairList(IPairList):
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2020-05-20 10:27:07 +00:00
|
|
|
def __init__(self, exchange, pairlistmanager,
|
|
|
|
config: Dict[str, Any], pairlistconfig: Dict[str, Any],
|
2019-11-09 14:04:04 +00:00
|
|
|
pairlist_pos: int) -> None:
|
|
|
|
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
|
2019-11-09 05:55:16 +00:00
|
|
|
|
|
|
|
if 'number_assets' not in self._pairlistconfig:
|
2018-12-06 18:36:33 +00:00
|
|
|
raise OperationalException(
|
2020-05-18 09:40:25 +00:00
|
|
|
'`number_assets` not specified. Please check your configuration '
|
2018-12-06 18:36:33 +00:00
|
|
|
'for "pairlist.config.number_assets"')
|
2020-05-15 00:00:55 +00:00
|
|
|
|
2020-05-15 00:59:13 +00:00
|
|
|
self._stake_currency = config['stake_currency']
|
2019-11-09 05:55:16 +00:00
|
|
|
self._number_pairs = self._pairlistconfig['number_assets']
|
|
|
|
self._sort_key = self._pairlistconfig.get('sort_key', 'quoteVolume')
|
2020-02-03 06:44:17 +00:00
|
|
|
self._min_value = self._pairlistconfig.get('min_value', 0)
|
2019-11-19 05:48:56 +00:00
|
|
|
self.refresh_period = self._pairlistconfig.get('refresh_period', 1800)
|
2018-12-04 06:12:56 +00:00
|
|
|
|
2019-11-09 05:55:16 +00:00
|
|
|
if not self._exchange.exchange_has('fetchTickers'):
|
2018-11-30 19:08:50 +00:00
|
|
|
raise OperationalException(
|
|
|
|
'Exchange does not support dynamic whitelist.'
|
|
|
|
'Please edit your config and restart the bot'
|
|
|
|
)
|
2020-05-15 00:00:55 +00:00
|
|
|
|
2018-12-05 18:48:50 +00:00
|
|
|
if not self._validate_keys(self._sort_key):
|
2018-12-04 06:12:56 +00:00
|
|
|
raise OperationalException(
|
|
|
|
f'key {self._sort_key} not in {SORT_VALUES}')
|
2020-05-15 00:00:55 +00:00
|
|
|
|
2020-04-25 14:29:17 +00:00
|
|
|
if self._sort_key != 'quoteVolume':
|
|
|
|
logger.warning(
|
|
|
|
"DEPRECATED: using any key other than quoteVolume for VolumePairList is deprecated."
|
|
|
|
)
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2019-11-09 06:05:17 +00:00
|
|
|
@property
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
2018-12-05 18:48:50 +00:00
|
|
|
def _validate_keys(self, key):
|
2018-12-04 06:12:56 +00:00
|
|
|
return key in SORT_VALUES
|
|
|
|
|
2018-12-03 19:31:25 +00:00
|
|
|
def short_desc(self) -> str:
|
|
|
|
"""
|
|
|
|
Short whitelist method description - used for startup-messages
|
|
|
|
"""
|
2019-11-09 06:07:33 +00:00
|
|
|
return f"{self.name} - top {self._pairlistconfig['number_assets']} volume pairs."
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2019-11-09 12:40:36 +00:00
|
|
|
def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]:
|
2018-11-30 05:34: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
|
|
|
|
:param pairlist: pairlist to filter or sort
|
|
|
|
:param tickers: Tickers (from exchange.get_tickers()). May be cached.
|
|
|
|
:return: new whitelist
|
2018-11-30 05:34:56 +00:00
|
|
|
"""
|
2018-11-30 19:08:50 +00:00
|
|
|
# Generate dynamic whitelist
|
2020-04-14 18:21:30 +00:00
|
|
|
# Must always run if this pairlist is not the first in the list.
|
|
|
|
if (self._pairlist_pos != 0 or
|
|
|
|
(self._last_refresh + self.refresh_period < datetime.now().timestamp())):
|
|
|
|
|
2019-11-09 13:00:32 +00:00
|
|
|
self._last_refresh = int(datetime.now().timestamp())
|
2020-05-15 00:59:13 +00:00
|
|
|
pairs = self._gen_pair_whitelist(pairlist, tickers)
|
2019-11-09 12:40:36 +00:00
|
|
|
else:
|
2020-04-14 18:21:30 +00:00
|
|
|
pairs = pairlist
|
2020-05-15 00:00:55 +00:00
|
|
|
|
2020-04-14 18:21:30 +00:00
|
|
|
self.log_on_refresh(logger.info, f"Searching {self._number_pairs} pairs: {pairs}")
|
2020-05-15 00:00:55 +00:00
|
|
|
|
2020-04-14 18:21:30 +00:00
|
|
|
return pairs
|
2019-11-09 12:40:36 +00:00
|
|
|
|
2020-05-15 00:59:13 +00:00
|
|
|
def _gen_pair_whitelist(self, pairlist: List[str], tickers: Dict) -> List[str]:
|
2018-11-30 19:08:50 +00:00
|
|
|
"""
|
|
|
|
Updates the whitelist with with a dynamically generated list
|
2020-05-15 00:59:13 +00:00
|
|
|
:param pairlist: pairlist to filter or sort
|
2019-11-09 05:55:16 +00:00
|
|
|
:param tickers: Tickers (from exchange.get_tickers()).
|
2018-11-30 19:08:50 +00:00
|
|
|
:return: List of pairs
|
|
|
|
"""
|
2019-11-09 14:04:04 +00:00
|
|
|
if self._pairlist_pos == 0:
|
|
|
|
# If VolumePairList is the first in the list, use fresh pairlist
|
2020-02-26 06:00:08 +00:00
|
|
|
# Check if pair quote currency equals to the stake currency.
|
2020-05-15 00:59:13 +00:00
|
|
|
filtered_tickers = [
|
|
|
|
v for k, v in tickers.items()
|
|
|
|
if (self._exchange.get_pair_quote_currency(k) == self._stake_currency
|
2020-05-16 06:21:36 +00:00
|
|
|
and v[self._sort_key] is not None)]
|
2019-11-09 14:04:04 +00:00
|
|
|
else:
|
2020-05-15 00:00:55 +00:00
|
|
|
# If other pairlist is in front, use the incoming pairlist.
|
2019-11-09 14:04:04 +00:00
|
|
|
filtered_tickers = [v for k, v in tickers.items() if k in pairlist]
|
|
|
|
|
2020-05-15 00:59:13 +00:00
|
|
|
if self._min_value > 0:
|
|
|
|
filtered_tickers = [
|
|
|
|
v for v in filtered_tickers if v[self._sort_key] > self._min_value]
|
2020-02-03 06:44:17 +00:00
|
|
|
|
2020-05-15 00:59:13 +00:00
|
|
|
sorted_tickers = sorted(filtered_tickers, reverse=True, key=lambda t: t[self._sort_key])
|
2019-11-09 14:04:04 +00:00
|
|
|
|
2019-03-02 16:24:28 +00:00
|
|
|
# Validate whitelist to only have active market pairs
|
2019-11-09 05:55:16 +00:00
|
|
|
pairs = self._whitelist_for_active_markets([s['symbol'] for s in sorted_tickers])
|
2020-05-19 20:51:39 +00:00
|
|
|
pairs = self.verify_blacklist(pairs, logger.info)
|
2020-05-17 11:26:21 +00:00
|
|
|
# Limit pairlist to the requested number of pairs
|
2019-11-09 06:19:46 +00:00
|
|
|
pairs = pairs[:self._number_pairs]
|
2019-08-18 16:06:36 +00:00
|
|
|
|
2018-11-30 19:08:50 +00:00
|
|
|
return pairs
|