diff --git a/freqtrade/plugins/pairlist/VolumePairList.py b/freqtrade/plugins/pairlist/VolumePairList.py index 5820bc667..40e6afa07 100644 --- a/freqtrade/plugins/pairlist/VolumePairList.py +++ b/freqtrade/plugins/pairlist/VolumePairList.py @@ -4,15 +4,16 @@ Volume PairList provider Provides dynamic pair list based on trade volumes """ import logging -import arrow from typing import Any, Dict, List +import arrow from cachetools.ttl import TTLCache from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_minutes -from freqtrade.plugins.pairlist.IPairList import IPairList from freqtrade.misc import format_ms_time +from freqtrade.plugins.pairlist.IPairList import IPairList + logger = logging.getLogger(__name__) @@ -44,8 +45,9 @@ class VolumePairList(IPairList): if (self._lookback_days > 0) & (self._lookback_period > 0): raise OperationalException( - f'Ambigous configuration: lookback_days and lookback_period both set in pairlist config. ' - f'Please set lookback_days only or lookback_period and lookback_timeframe and restart the bot.' + 'Ambigous configuration: lookback_days and lookback_period both set in pairlist ' + 'config. Please set lookback_days only or lookback_period and lookback_timeframe ' + 'and restart the bot.' ) # overwrite lookback timeframe and days when lookback_days is set @@ -53,17 +55,18 @@ class VolumePairList(IPairList): self._lookback_timeframe = '1d' self._lookback_period = self._lookback_days - # get timeframe in minutes and seconds + # get timeframe in minutes and seconds self._tf_in_min = timeframe_to_minutes(self._lookback_timeframe) self._tf_in_sec = self._tf_in_min * 60 - + # wether to use range lookback or not self._use_range = (self._tf_in_min > 0) & (self._lookback_period > 0) if self._use_range & (self._refresh_period < self._tf_in_sec): raise OperationalException( - f'Refresh period of {self._refresh_period} seconds is smaller than one timeframe of {self._lookback_timeframe}. ' - f'Please adjust refresh_period to at least {self._tf_in_sec} and restart the bot.' + f'Refresh period of {self._refresh_period} seconds is smaller than one ' + f'timeframe of {self._lookback_timeframe}. Please adjust refresh_period ' + f'to at least {self._tf_in_sec} and restart the bot.' ) if not self._exchange.exchange_has('fetchTickers'): @@ -76,7 +79,6 @@ class VolumePairList(IPairList): raise OperationalException( f'key {self._sort_key} not in {SORT_VALUES}') - if self._lookback_period < 0: raise OperationalException("VolumeFilter requires lookback_period to be >= 0") if self._lookback_period > exchange.ohlcv_candle_limit(self._lookback_timeframe): @@ -136,15 +138,16 @@ class VolumePairList(IPairList): :param tickers: Tickers (from exchange.get_tickers()). May be cached. :return: new whitelist """ - # Use the incoming pairlist. + # Use the incoming pairlist. filtered_tickers = [v for k, v in tickers.items() if k in pairlist] # get lookback period in ms, for exchange ohlcv fetch - if self._use_range == True: + if self._use_range: since_ms = int(arrow.utcnow() - .floor('minute') - .shift(minutes=-(self._lookback_period * self._tf_in_min) - self._tf_in_min) - .float_timestamp) * 1000 + .floor('minute') + .shift(minutes=-(self._lookback_period * self._tf_in_min) + - self._tf_in_min) + .float_timestamp) * 1000 to_ms = int(arrow.utcnow() .floor('minute') @@ -152,20 +155,35 @@ class VolumePairList(IPairList): .float_timestamp) * 1000 # todo: utc date output for starting date - self.log_once(f"Using volume range of {self._lookback_period} candles, timeframe: {self._lookback_timeframe}, starting from {format_ms_time(since_ms)} till {format_ms_time(to_ms)}", logger.info) - needed_pairs = [(p, self._lookback_timeframe) for p in [s['symbol'] for s in filtered_tickers] if p not in self._pair_cache] + self.log_once(f"Using volume range of {self._lookback_period} candles, timeframe: " + f"{self._lookback_timeframe}, starting from {format_ms_time(since_ms)} " + f"till {format_ms_time(to_ms)}", logger.info) + needed_pairs = [ + (p, self._lookback_timeframe) for p in + [ + s['symbol'] for s in filtered_tickers + ] if p not in self._pair_cache + ] # Get all candles candles = {} if needed_pairs: - candles = self._exchange.refresh_latest_ohlcv(needed_pairs, since_ms=since_ms, cache=False) + candles = self._exchange.refresh_latest_ohlcv( + needed_pairs, since_ms=since_ms, cache=False + ) - for i,p in enumerate(filtered_tickers): - pair_candles = candles[(p['symbol'], self._lookback_timeframe)] if (p['symbol'], self._lookback_timeframe) in candles else None + for i, p in enumerate(filtered_tickers): + pair_candles = candles[ + (p['symbol'], self._lookback_timeframe) + ] if (p['symbol'], self._lookback_timeframe) in candles else None # in case of candle data calculate typical price and quoteVolume for candle if not pair_candles.empty: - pair_candles['typical_price'] = (pair_candles['high'] + pair_candles['low'] + pair_candles['close']) / 3 - pair_candles['quoteVolume'] = pair_candles['volume'] * pair_candles['typical_price'] + pair_candles['typical_price'] = (pair_candles['high'] + pair_candles['low'] + + pair_candles['close']) / 3 + pair_candles['quoteVolume'] = ( + pair_candles['volume'] * pair_candles['typical_price'] + ) + # replace quoteVolume with range quoteVolume sum calculated above filtered_tickers[i]['quoteVolume'] = pair_candles['quoteVolume'].sum() else: @@ -179,7 +197,7 @@ class VolumePairList(IPairList): # Validate whitelist to only have active market pairs pairs = self._whitelist_for_active_markets([s['symbol'] for s in sorted_tickers]) - pairs = self.verify_blacklist(pairs, logger.info) + pairs = self.verify_blacklist(pairs, logger.info) # Limit pairlist to the requested number of pairs pairs = pairs[:self._number_pairs]