2018-11-30 05:34:56 +00:00
|
|
|
"""
|
|
|
|
Static List provider
|
|
|
|
|
|
|
|
Provides lists as configured in config.json
|
|
|
|
|
|
|
|
"""
|
|
|
|
import logging
|
2018-11-30 19:08:50 +00:00
|
|
|
from typing import List
|
|
|
|
from cachetools import TTLCache, cached
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2018-11-30 19:08:50 +00:00
|
|
|
from freqtrade.pairlist.StaticPairList import StaticPairList
|
|
|
|
from freqtrade import OperationalException
|
2018-11-30 05:34:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-12-04 06:12:56 +00:00
|
|
|
SORT_VALUES = ['askVolume', 'bidVolume', 'quoteVolume']
|
|
|
|
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2018-11-30 19:08:50 +00:00
|
|
|
class VolumePairList(StaticPairList):
|
2018-11-30 05:34:56 +00:00
|
|
|
|
|
|
|
def __init__(self, freqtrade, config: dict) -> None:
|
|
|
|
self._freqtrade = freqtrade
|
|
|
|
self._config = config
|
2018-12-04 19:23:03 +00:00
|
|
|
self._whitelistconf = self._config.get('pairlist', {}).get('config')
|
2018-11-30 05:34:56 +00:00
|
|
|
self._whitelist = self._config['exchange']['pair_whitelist']
|
|
|
|
self._blacklist = self._config['exchange'].get('pair_blacklist', [])
|
2018-12-03 19:31:25 +00:00
|
|
|
self._number_pairs = self._whitelistconf['number_assets']
|
2018-12-04 06:12:56 +00:00
|
|
|
self._sort_key = self._whitelistconf.get('sort_key', 'quoteVolume')
|
|
|
|
|
2018-11-30 19:08:50 +00:00
|
|
|
if not self._freqtrade.exchange.exchange_has('fetchTickers'):
|
|
|
|
raise OperationalException(
|
|
|
|
'Exchange does not support dynamic whitelist.'
|
|
|
|
'Please edit your config and restart the bot'
|
|
|
|
)
|
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}')
|
2018-11-30 05:54:20 +00:00
|
|
|
# self.refresh_whitelist()
|
2018-11-30 05:34:56 +00:00
|
|
|
|
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
|
|
|
|
-> Please overwrite in subclasses
|
|
|
|
"""
|
|
|
|
return f"{self.name} - top {self._whitelistconf['number_assets']} volume pairs."
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2018-11-30 06:02:08 +00:00
|
|
|
def refresh_whitelist(self) -> None:
|
2018-11-30 05:34:56 +00:00
|
|
|
"""
|
2018-11-30 06:06:02 +00:00
|
|
|
Refreshes whitelist and assigns it to self._whitelist
|
2018-11-30 05:34:56 +00:00
|
|
|
"""
|
2018-11-30 19:08:50 +00:00
|
|
|
# Generate dynamic whitelist
|
2018-12-04 06:12:56 +00:00
|
|
|
pairs = self._gen_pair_whitelist(self._config['stake_currency'], self._sort_key)
|
2018-11-30 19:08:50 +00:00
|
|
|
# Validate whitelist to only have active market pairs
|
|
|
|
self._whitelist = self._validate_whitelist(pairs)[:self._number_pairs]
|
2018-11-30 05:34:56 +00:00
|
|
|
|
2018-11-30 19:08:50 +00:00
|
|
|
@cached(TTLCache(maxsize=1, ttl=1800))
|
2018-12-04 06:16:34 +00:00
|
|
|
def _gen_pair_whitelist(self, base_currency: str, key: str) -> List[str]:
|
2018-11-30 19:08:50 +00:00
|
|
|
"""
|
|
|
|
Updates the whitelist with with a dynamically generated list
|
|
|
|
:param base_currency: base currency as str
|
|
|
|
:param key: sort key (defaults to 'quoteVolume')
|
|
|
|
:return: List of pairs
|
|
|
|
"""
|
|
|
|
|
|
|
|
tickers = self._freqtrade.exchange.get_tickers()
|
|
|
|
# check length so that we make sure that '/' is actually in the string
|
|
|
|
tickers = [v for k, v in tickers.items()
|
|
|
|
if len(k.split('/')) == 2 and k.split('/')[1] == base_currency]
|
|
|
|
|
|
|
|
sorted_tickers = sorted(tickers, reverse=True, key=lambda t: t[key])
|
|
|
|
pairs = [s['symbol'] for s in sorted_tickers]
|
|
|
|
return pairs
|
|
|
|
|