mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 08:33:07 +00:00
Voliatilityfilter - sorting
This commit is contained in:
@@ -37,6 +37,7 @@ class VolatilityFilter(IPairList):
|
|||||||
self._max_volatility = pairlistconfig.get('max_volatility', sys.maxsize)
|
self._max_volatility = pairlistconfig.get('max_volatility', sys.maxsize)
|
||||||
self._refresh_period = pairlistconfig.get('refresh_period', 1440)
|
self._refresh_period = pairlistconfig.get('refresh_period', 1440)
|
||||||
self._def_candletype = self._config['candle_type_def']
|
self._def_candletype = self._config['candle_type_def']
|
||||||
|
self._sort_direction: Optional[str] = pairlistconfig.get('sort_direction', None)
|
||||||
|
|
||||||
self._pair_cache: TTLCache = TTLCache(maxsize=1000, ttl=self._refresh_period)
|
self._pair_cache: TTLCache = TTLCache(maxsize=1000, ttl=self._refresh_period)
|
||||||
|
|
||||||
@@ -89,6 +90,13 @@ class VolatilityFilter(IPairList):
|
|||||||
"description": "Maximum Volatility",
|
"description": "Maximum Volatility",
|
||||||
"help": "Maximum volatility a pair must have to be considered.",
|
"help": "Maximum volatility a pair must have to be considered.",
|
||||||
},
|
},
|
||||||
|
"sort_direction": {
|
||||||
|
"type": "option",
|
||||||
|
"default": None,
|
||||||
|
"options": [None, "asc", "desc"],
|
||||||
|
"description": "Sort pairlist",
|
||||||
|
"help": "Sort Pairlist",
|
||||||
|
},
|
||||||
**IPairList.refresh_period_parameter()
|
**IPairList.refresh_period_parameter()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,14 +114,34 @@ class VolatilityFilter(IPairList):
|
|||||||
candles = self._exchange.refresh_ohlcv_with_cache(needed_pairs, since_ms=since_ms)
|
candles = self._exchange.refresh_ohlcv_with_cache(needed_pairs, since_ms=since_ms)
|
||||||
|
|
||||||
resulting_pairlist: List[str] = []
|
resulting_pairlist: List[str] = []
|
||||||
|
volatilitys: Dict[str, float] = {}
|
||||||
for p in pairlist:
|
for p in pairlist:
|
||||||
daily_candles = candles[(p, '1d', self._def_candletype)] if (
|
daily_candles = candles[(p, '1d', self._def_candletype)] if (
|
||||||
p, '1d', self._def_candletype) in candles else None
|
p, '1d', self._def_candletype) in candles else None
|
||||||
if self._validate_pair_loc(p, daily_candles):
|
|
||||||
resulting_pairlist.append(p)
|
if daily_candles is not None and not daily_candles.empty:
|
||||||
|
volatility_avg = self._calculate_volatility(deepcopy(daily_candles))
|
||||||
|
|
||||||
|
if self._validate_pair_loc(p, volatility_avg):
|
||||||
|
resulting_pairlist.append(p)
|
||||||
|
if self._sort_direction:
|
||||||
|
volatilitys[p] = volatility_avg if not np.isnan(volatility_avg) else 0
|
||||||
|
|
||||||
|
if self._sort_direction:
|
||||||
|
resulting_pairlist = sorted(resulting_pairlist,
|
||||||
|
key=lambda p: volatilitys[p],
|
||||||
|
reverse=self._sort_direction == 'desc')
|
||||||
return resulting_pairlist
|
return resulting_pairlist
|
||||||
|
|
||||||
def _validate_pair_loc(self, pair: str, daily_candles: Optional[DataFrame]) -> bool:
|
def _calculate_volatility(self, daily_candles: DataFrame) -> float:
|
||||||
|
returns = (np.log(daily_candles["close"].shift(1) / daily_candles["close"]))
|
||||||
|
returns.fillna(0, inplace=True)
|
||||||
|
|
||||||
|
volatility_series = returns.rolling(window=self._days).std() * np.sqrt(self._days)
|
||||||
|
volatility_avg = volatility_series.mean()
|
||||||
|
return volatility_avg
|
||||||
|
|
||||||
|
def _validate_pair_loc(self, pair: str, volatility_avg: float) -> bool:
|
||||||
"""
|
"""
|
||||||
Validate trading range
|
Validate trading range
|
||||||
:param pair: Pair that's currently validated
|
:param pair: Pair that's currently validated
|
||||||
@@ -125,23 +153,17 @@ class VolatilityFilter(IPairList):
|
|||||||
return cached_res
|
return cached_res
|
||||||
|
|
||||||
result = False
|
result = False
|
||||||
if daily_candles is not None and not daily_candles.empty:
|
|
||||||
returns = (np.log(daily_candles["close"].shift(1) / daily_candles["close"]))
|
|
||||||
returns.fillna(0, inplace=True)
|
|
||||||
|
|
||||||
volatility_series = returns.rolling(window=self._days).std() * np.sqrt(self._days)
|
if self._min_volatility <= volatility_avg <= self._max_volatility:
|
||||||
volatility_avg = volatility_series.mean()
|
result = True
|
||||||
|
else:
|
||||||
if self._min_volatility <= volatility_avg <= self._max_volatility:
|
self.log_once(f"Removed {pair} from whitelist, because volatility "
|
||||||
result = True
|
f"over {self._days} {plural(self._days, 'day')} "
|
||||||
else:
|
f"is: {volatility_avg:.3f} "
|
||||||
self.log_once(f"Removed {pair} from whitelist, because volatility "
|
f"which is not in the configured range of "
|
||||||
f"over {self._days} {plural(self._days, 'day')} "
|
f"{self._min_volatility}-{self._max_volatility}.",
|
||||||
f"is: {volatility_avg:.3f} "
|
logger.info)
|
||||||
f"which is not in the configured range of "
|
result = False
|
||||||
f"{self._min_volatility}-{self._max_volatility}.",
|
self._pair_cache[pair] = result
|
||||||
logger.info)
|
|
||||||
result = False
|
|
||||||
self._pair_cache[pair] = result
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user