stable/freqtrade/pairlist/VolumePairList.py

109 lines
4.3 KiB
Python
Raw Normal View History

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
Provides lists as configured in config.json
"""
import logging
2019-11-09 12:40:36 +00:00
from datetime import datetime
2019-11-09 06:07:33 +00:00
from typing import Dict, List
2019-10-29 09:39:27 +00:00
from freqtrade import OperationalException
2019-10-29 09:39:27 +00:00
from freqtrade.pairlist.IPairList import IPairList
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-12-05 19:44:56 +00:00
class VolumePairList(IPairList):
2018-11-30 05:34:56 +00:00
def __init__(self, exchange, pairlistmanager, config, pairlistconfig: dict,
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:
raise OperationalException(
f'`number_assets` not specified. Please check your configuration '
'for "pairlist.config.number_assets"')
2019-11-09 05:55:16 +00:00
self._number_pairs = self._pairlistconfig['number_assets']
self._sort_key = self._pairlistconfig.get('sort_key', 'quoteVolume')
2019-11-09 12:40:36 +00:00
self._ttl = self._pairlistconfig.get('ttl', 1800)
2018-12-04 06:12:56 +00:00
2019-11-09 05:55:16 +00:00
if not self._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}')
2019-11-09 12:40:36 +00:00
self._last_refresh = 0
2018-11-30 05:34:56 +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
"""
# Generate dynamic whitelist
2019-11-09 12:40:36 +00:00
if self._last_refresh + self._ttl < datetime.now().timestamp():
2019-11-09 13:00:32 +00:00
self._last_refresh = int(datetime.now().timestamp())
2019-11-09 12:40:36 +00:00
return self._gen_pair_whitelist(pairlist,
tickers,
self._config['stake_currency'],
self._sort_key,
)
else:
return pairlist
def _gen_pair_whitelist(self, pairlist, tickers, base_currency: str, key: str) -> List[str]:
"""
Updates the whitelist with with a dynamically generated list
:param base_currency: base currency as str
:param key: sort key (defaults to 'quoteVolume')
2019-11-09 05:55:16 +00:00
:param tickers: Tickers (from exchange.get_tickers()).
:return: List of pairs
"""
if self._pairlist_pos == 0:
# If VolumePairList is the first in the list, use fresh pairlist
# check length so that we make sure that '/' is actually in the string
filtered_tickers = [v for k, v in tickers.items()
if (len(k.split('/')) == 2 and k.split('/')[1] == base_currency
and v[key] is not None)]
else:
# If other pairlist is in front, use the incomming pairlist.
filtered_tickers = [v for k, v in tickers.items() if k in pairlist]
sorted_tickers = sorted(filtered_tickers, reverse=True, key=lambda t: t[key])
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])
pairs = self._verify_blacklist(pairs)
# Limit to X number of pairs
pairs = pairs[:self._number_pairs]
logger.info(f"Searching {self._number_pairs} pairs: {pairs}")
return pairs