2020-05-17 11:26:21 +00:00
|
|
|
"""
|
|
|
|
Precision 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
|
|
|
|
2022-09-18 17:36:11 +00:00
|
|
|
from freqtrade.constants import Config
|
2020-06-09 12:33:57 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2020-12-23 15:54:35 +00:00
|
|
|
from freqtrade.plugins.pairlist.IPairList import IPairList
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-11-09 05:55:16 +00:00
|
|
|
class PrecisionFilter(IPairList):
|
2019-10-30 14:59:52 +00:00
|
|
|
|
2020-05-15 01:17:23 +00:00
|
|
|
def __init__(self, exchange, pairlistmanager,
|
2022-09-18 11:31:52 +00:00
|
|
|
config: Config, pairlistconfig: Dict[str, Any],
|
2020-05-15 01:17:23 +00:00
|
|
|
pairlist_pos: int) -> None:
|
|
|
|
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
|
|
|
|
|
2020-06-09 12:33:57 +00:00
|
|
|
if 'stoploss' not in self._config:
|
|
|
|
raise OperationalException(
|
|
|
|
'PrecisionFilter can only work with stoploss defined. Please add the '
|
|
|
|
'stoploss key to your configuration (overwrites eventual strategy settings).')
|
2020-05-20 10:41:00 +00:00
|
|
|
self._stoploss = self._config['stoploss']
|
|
|
|
self._enabled = self._stoploss != 0
|
|
|
|
|
2020-05-17 08:11:49 +00:00
|
|
|
# Precalculate sanitized stoploss value to avoid recalculation for every pair
|
2020-05-20 10:41:00 +00:00
|
|
|
self._stoploss = 1 - abs(self._stoploss)
|
2020-05-15 01:17:23 +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
|
|
|
|
"""
|
|
|
|
return f"{self.name} - Filtering untradable pairs."
|
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
|
|
|
"""
|
|
|
|
Check if pair has enough room to add a stoploss to avoid "unsellable" buys of very
|
|
|
|
low value pairs.
|
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()
|
2020-12-15 07:36:42 +00:00
|
|
|
:return: True if the pair can stay, false if it should be removed
|
2019-10-30 14:59:52 +00:00
|
|
|
"""
|
2022-08-18 18:07:41 +00:00
|
|
|
if ticker.get('last', None) is None:
|
2022-09-02 18:08:38 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, because "
|
2022-08-18 18:07:41 +00:00
|
|
|
"ticker['last'] is empty (Usually no trade in the last 24h).",
|
|
|
|
logger.info)
|
|
|
|
return False
|
2022-03-13 14:23:01 +00:00
|
|
|
stop_price = ticker['last'] * self._stoploss
|
2020-05-15 01:24:18 +00:00
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
# Adjust stop-prices to precision
|
2020-12-15 07:36:42 +00:00
|
|
|
sp = self._exchange.price_to_precision(pair, stop_price)
|
2020-05-15 01:24:18 +00:00
|
|
|
|
2020-12-15 07:36:42 +00:00
|
|
|
stop_gap_price = self._exchange.price_to_precision(pair, stop_price * 0.99)
|
2022-09-02 18:08:38 +00:00
|
|
|
logger.debug(f"{pair} - {sp} : {stop_gap_price}")
|
2020-05-15 01:24:18 +00:00
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
if sp <= stop_gap_price:
|
2022-09-02 18:08:38 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, because "
|
2020-11-22 10:49:41 +00:00
|
|
|
f"stop price {sp} would be <= stop limit {stop_gap_price}", logger.info)
|
2019-10-30 14:59:52 +00:00
|
|
|
return False
|
2020-05-15 01:24:18 +00:00
|
|
|
|
2019-10-30 14:59:52 +00:00
|
|
|
return True
|