stable/freqtrade/plugins/pairlist/PrecisionFilter.py

73 lines
2.6 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
2020-05-20 10:27:07 +00:00
from typing import Any, Dict
2019-10-30 14:59:52 +00:00
from freqtrade.exceptions import OperationalException
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,
config: Dict[str, Any], pairlistconfig: Dict[str, Any],
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: 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.
: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()
:return: True if the pair can stay, false if it should be removed
2019-10-30 14:59:52 +00:00
"""
if 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)
2020-05-15 01:24:18 +00:00
stop_gap_price = self._exchange.price_to_precision(pair, stop_price * 0.99)
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