stable/freqtrade/plugins/pairlist/SpreadFilter.py

60 lines
2.1 KiB
Python
Raw Normal View History

2020-05-17 11:26:21 +00:00
"""
Spread pair list filter
"""
import logging
from typing import Any, Dict, Optional
2022-09-18 11:20:36 +00:00
from freqtrade.constants import Config
from freqtrade.exchange.types import Ticker
from freqtrade.plugins.pairlist.IPairList import IPairList
2020-05-17 11:26:21 +00:00
logger = logging.getLogger(__name__)
class SpreadFilter(IPairList):
2020-05-20 10:27:07 +00:00
def __init__(self, exchange, pairlistmanager,
2022-09-18 11:20:36 +00:00
config: Config, pairlistconfig: Dict[str, Any],
pairlist_pos: int) -> None:
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
self._max_spread_ratio = pairlistconfig.get('max_spread_ratio', 0.005)
self._enabled = self._max_spread_ratio != 0
@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
def short_desc(self) -> str:
"""
Short whitelist method description - used for startup-messages
"""
return (f"{self.name} - Filtering pairs with ask/bid diff above "
2021-11-11 14:58:30 +00:00
f"{self._max_spread_ratio:.2%}.")
def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool:
2020-05-15 02:14:06 +00:00
"""
Validate spread for the ticker
: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
2020-05-15 02:14:06 +00:00
"""
if ticker and 'bid' in ticker and 'ask' in ticker and ticker['ask'] and ticker['bid']:
2020-05-15 02:14:06 +00:00
spread = 1 - ticker['bid'] / ticker['ask']
if spread > self._max_spread_ratio:
self.log_once(f"Removed {pair} from whitelist, because spread "
f"{spread:.3%} > {self._max_spread_ratio:.3%}",
2020-11-22 10:49:41 +00:00
logger.info)
2020-05-15 02:14:06 +00:00
return False
else:
return True
self.log_once(f"Removed {pair} from whitelist due to invalid ticker data: {ticker}",
logger.info)
2020-05-15 02:14:06 +00:00
return False