stable/freqtrade/plugins/pairlist/PrecisionFilter.py

77 lines
2.9 KiB
Python
Raw Normal View History

2020-05-17 11:26:21 +00:00
"""
Precision pair list filter
"""
2019-10-30 14:59:52 +00:00
import logging
from typing import Any, Dict, Optional
2019-10-30 14:59:52 +00:00
2022-09-18 17:36:11 +00:00
from freqtrade.constants import Config
from freqtrade.exceptions import OperationalException
from freqtrade.exchange import ROUND_UP
from freqtrade.exchange.types import Ticker
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)
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).')
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
self._stoploss = 1 - abs(self._stoploss)
2020-05-15 01:17:23 +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
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
def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> 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.
:param pair: Pair that's currently validated
:param ticker: ticker dict as returned from ccxt.fetch_ticker
:return: True if the pair can stay, false if it should be removed
2019-10-30 14:59:52 +00:00
"""
if not ticker or ticker.get('last', None) is None:
self.log_once(f"Removed {pair} from whitelist, because "
"ticker['last'] is empty (Usually no trade in the last 24h).",
logger.info)
return False
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
sp = self._exchange.price_to_precision(pair, stop_price, rounding_mode=ROUND_UP)
2020-05-15 01:24:18 +00:00
stop_gap_price = self._exchange.price_to_precision(pair, stop_price * 0.99,
rounding_mode=ROUND_UP)
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:
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