2020-05-17 11:26:21 +00:00
|
|
|
"""
|
|
|
|
Price pair list filter
|
|
|
|
"""
|
2019-10-30 14:59:52 +00:00
|
|
|
import logging
|
2020-05-20 10:27:07 +00:00
|
|
|
from typing import Any, Dict
|
2019-10-30 14:59:52 +00:00
|
|
|
|
2020-07-22 19:21:30 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2020-12-23 15:54:35 +00:00
|
|
|
from freqtrade.plugins.pairlist.IPairList import IPairList
|
2019-10-30 14:59:52 +00:00
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-11-19 05:41:05 +00:00
|
|
|
class PriceFilter(IPairList):
|
2019-10-30 14:59:52 +00:00
|
|
|
|
2020-02-02 04:00:40 +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-10-30 14:59:52 +00:00
|
|
|
|
2019-11-19 05:34:54 +00:00
|
|
|
self._low_price_ratio = pairlistconfig.get('low_price_ratio', 0)
|
2020-07-22 19:21:30 +00:00
|
|
|
if self._low_price_ratio < 0:
|
2020-08-15 07:08:50 +00:00
|
|
|
raise OperationalException("PriceFilter requires low_price_ratio to be >= 0")
|
2020-07-08 21:02:04 +00:00
|
|
|
self._min_price = pairlistconfig.get('min_price', 0)
|
2020-07-22 19:21:30 +00:00
|
|
|
if self._min_price < 0:
|
2020-08-15 07:08:50 +00:00
|
|
|
raise OperationalException("PriceFilter requires min_price to be >= 0")
|
2020-07-08 21:02:04 +00:00
|
|
|
self._max_price = pairlistconfig.get('max_price', 0)
|
2020-07-22 19:21:30 +00:00
|
|
|
if self._max_price < 0:
|
2020-08-15 07:08:50 +00:00
|
|
|
raise OperationalException("PriceFilter requires max_price to be >= 0")
|
2021-05-17 04:47:49 +00:00
|
|
|
self._max_value = pairlistconfig.get('max_value', 0)
|
|
|
|
if self._max_value < 0:
|
|
|
|
raise OperationalException("PriceFilter requires max_value to be >= 0")
|
2020-07-22 19:21:30 +00:00
|
|
|
self._enabled = ((self._low_price_ratio > 0) or
|
|
|
|
(self._min_price > 0) or
|
2021-05-17 04:47:49 +00:00
|
|
|
(self._max_price > 0) or
|
2021-05-17 04:47:58 +00:00
|
|
|
(self._max_value > 0))
|
2019-11-09 05:55:16 +00:00
|
|
|
|
2019-11-09 06:05:17 +00:00
|
|
|
@property
|
|
|
|
def needstickers(self) -> bool:
|
|
|
|
"""
|
|
|
|
Boolean property defining if tickers are necessary.
|
2020-11-24 19:24:51 +00:00
|
|
|
If no Pairlist requires tickers, an empty Dict is passed
|
2019-11-09 06:05:17 +00:00
|
|
|
as tickers argument to filter_pairlist
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
2019-11-09 05:55:16 +00:00
|
|
|
def short_desc(self) -> str:
|
|
|
|
"""
|
|
|
|
Short whitelist method description - used for startup-messages
|
|
|
|
"""
|
2020-07-08 21:02:04 +00:00
|
|
|
active_price_filters = []
|
|
|
|
if self._low_price_ratio != 0:
|
2021-11-11 14:58:30 +00:00
|
|
|
active_price_filters.append(f"below {self._low_price_ratio:.1%}")
|
2020-07-08 21:02:04 +00:00
|
|
|
if self._min_price != 0:
|
|
|
|
active_price_filters.append(f"below {self._min_price:.8f}")
|
|
|
|
if self._max_price != 0:
|
|
|
|
active_price_filters.append(f"above {self._max_price:.8f}")
|
2021-05-17 04:47:49 +00:00
|
|
|
if self._max_value != 0:
|
|
|
|
active_price_filters.append(f"Value above {self._max_value:.8f}")
|
2020-07-08 21:02:04 +00:00
|
|
|
|
|
|
|
if len(active_price_filters):
|
|
|
|
return f"{self.name} - Filtering pairs priced {' or '.join(active_price_filters)}."
|
|
|
|
|
|
|
|
return f"{self.name} - No price filters configured."
|
2019-10-30 14:59:52 +00:00
|
|
|
|
2020-12-15 07:36:42 +00:00
|
|
|
def _validate_pair(self, pair: str, ticker: Dict[str, Any]) -> bool:
|
2019-10-30 14:59:52 +00:00
|
|
|
"""
|
2019-11-19 05:41:05 +00:00
|
|
|
Check if if one price-step (pip) is > than a certain barrier.
|
2020-12-15 07:36:42 +00:00
|
|
|
:param pair: Pair that's currently validated
|
2021-05-16 18:34:02 +00:00
|
|
|
:param ticker: ticker dict as returned from ccxt.fetch_tickers()
|
2019-10-30 14:59:52 +00:00
|
|
|
:return: True if the pair can stay, false if it should be removed
|
|
|
|
"""
|
2022-05-17 22:11:10 +00:00
|
|
|
if ticker.get('last') is None or ticker.get('last') == 0:
|
2020-12-15 07:36:42 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, because "
|
2020-11-22 10:49:41 +00:00
|
|
|
"ticker['last'] is empty (Usually no trade in the last 24h).",
|
|
|
|
logger.info)
|
2020-04-23 18:04:36 +00:00
|
|
|
return False
|
2020-07-08 21:02:04 +00:00
|
|
|
|
|
|
|
# Perform low_price_ratio check.
|
|
|
|
if self._low_price_ratio != 0:
|
2020-12-15 07:36:42 +00:00
|
|
|
compare = self._exchange.price_get_one_pip(pair, ticker['last'])
|
2020-07-08 21:02:04 +00:00
|
|
|
changeperc = compare / ticker['last']
|
|
|
|
if changeperc > self._low_price_ratio:
|
2020-12-15 07:36:42 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, "
|
2021-11-11 12:55:55 +00:00
|
|
|
f"because 1 unit is {changeperc:.3%}", logger.info)
|
2020-07-08 21:02:04 +00:00
|
|
|
return False
|
|
|
|
|
2021-05-17 04:47:49 +00:00
|
|
|
# Perform low_amount check
|
|
|
|
if self._max_value != 0:
|
|
|
|
price = ticker['last']
|
|
|
|
market = self._exchange.markets[pair]
|
|
|
|
limits = market['limits']
|
2022-01-31 08:40:10 +00:00
|
|
|
if (limits['amount']['min'] is not None):
|
2021-05-17 04:47:49 +00:00
|
|
|
min_amount = limits['amount']['min']
|
|
|
|
min_precision = market['precision']['amount']
|
|
|
|
|
|
|
|
min_value = min_amount * price
|
|
|
|
if self._exchange.precisionMode == 4:
|
|
|
|
# tick size
|
|
|
|
next_value = (min_amount + min_precision) * price
|
|
|
|
else:
|
|
|
|
# Decimal places
|
|
|
|
min_precision = pow(0.1, min_precision)
|
|
|
|
next_value = (min_amount + min_precision) * price
|
|
|
|
diff = next_value - min_value
|
|
|
|
|
|
|
|
if diff > self._max_value:
|
|
|
|
self.log_once(f"Removed {pair} from whitelist, "
|
2021-05-21 11:24:13 +00:00
|
|
|
f"because min value change of {diff} > {self._max_value}.",
|
2021-05-17 04:47:49 +00:00
|
|
|
logger.info)
|
|
|
|
return False
|
|
|
|
|
2020-07-08 21:02:04 +00:00
|
|
|
# Perform min_price check.
|
|
|
|
if self._min_price != 0:
|
|
|
|
if ticker['last'] < self._min_price:
|
2020-12-15 07:36:42 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, "
|
2020-11-22 10:49:41 +00:00
|
|
|
f"because last price < {self._min_price:.8f}", logger.info)
|
2020-07-08 21:02:04 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Perform max_price check.
|
|
|
|
if self._max_price != 0:
|
|
|
|
if ticker['last'] > self._max_price:
|
2021-05-17 04:47:49 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, "
|
2020-11-22 10:49:41 +00:00
|
|
|
f"because last price > {self._max_price:.8f}", logger.info)
|
2020-07-08 21:02:04 +00:00
|
|
|
return False
|
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
return True
|