2020-05-17 11:26:21 +00:00
|
|
|
"""
|
|
|
|
Spread pair list filter
|
|
|
|
"""
|
2020-02-03 06:44:17 +00:00
|
|
|
import logging
|
2022-10-11 19:33:02 +00:00
|
|
|
from typing import Any, Dict, Optional
|
2020-02-03 06:44:17 +00:00
|
|
|
|
2022-09-18 11:20:36 +00:00
|
|
|
from freqtrade.constants import Config
|
2022-03-17 19:21:10 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2022-10-11 19:33:02 +00:00
|
|
|
from freqtrade.exchange.types import Ticker
|
2020-12-23 15:54:35 +00:00
|
|
|
from freqtrade.plugins.pairlist.IPairList import IPairList
|
2020-02-03 06:44:17 +00:00
|
|
|
|
2020-05-17 11:26:21 +00:00
|
|
|
|
2020-02-03 06:44:17 +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],
|
2020-02-03 06:44:17 +00:00
|
|
|
pairlist_pos: int) -> None:
|
|
|
|
super().__init__(exchange, pairlistmanager, config, pairlistconfig, pairlist_pos)
|
|
|
|
|
|
|
|
self._max_spread_ratio = pairlistconfig.get('max_spread_ratio', 0.005)
|
2020-05-20 10:41:00 +00:00
|
|
|
self._enabled = self._max_spread_ratio != 0
|
2020-02-03 06:44:17 +00:00
|
|
|
|
2022-03-17 19:21:10 +00:00
|
|
|
if not self._exchange.exchange_has('fetchTickers'):
|
|
|
|
raise OperationalException(
|
|
|
|
'Exchange does not support fetchTickers, therefore SpreadFilter cannot be used.'
|
|
|
|
'Please edit your config and restart the bot.'
|
|
|
|
)
|
|
|
|
|
2020-02-03 06:44: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
|
2020-02-03 06:44:17 +00:00
|
|
|
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%}.")
|
2020-02-03 06:44:17 +00:00
|
|
|
|
2022-10-11 19:33:02 +00:00
|
|
|
def _validate_pair(self, pair: str, ticker: Optional[Ticker]) -> bool:
|
2020-05-15 02:14:06 +00:00
|
|
|
"""
|
|
|
|
Validate spread for the ticker
|
2020-12-15 07:36:42 +00:00
|
|
|
:param pair: Pair that's currently validated
|
2022-10-10 11:54:13 +00:00
|
|
|
:param ticker: ticker dict as returned from ccxt.fetch_ticker
|
2020-12-15 07:36:42 +00:00
|
|
|
:return: True if the pair can stay, false if it should be removed
|
2020-05-15 02:14:06 +00:00
|
|
|
"""
|
2022-10-11 19:33:02 +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:
|
2020-12-15 07:36:42 +00:00
|
|
|
self.log_once(f"Removed {pair} from whitelist, because spread "
|
2022-01-18 08:35:03 +00:00
|
|
|
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
|
2021-01-26 16:16:57 +00:00
|
|
|
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
|