Convert ExchangeResolver to static loader class

This commit is contained in:
Matthias 2019-12-23 10:03:18 +01:00
parent 5fefa9e97c
commit 560acb7cea
8 changed files with 24 additions and 24 deletions

View File

@ -60,7 +60,7 @@ class FreqtradeBot:
# Check config consistency here since strategies can set certain options # Check config consistency here since strategies can set certain options
validate_config_consistency(config) validate_config_consistency(config)
self.exchange = ExchangeResolver(self.config['exchange']['name'], self.config).exchange self.exchange = ExchangeResolver.load_exchange(self.config['exchange']['name'], self.config)
persistence.init(self.config.get('db_url', None), persistence.init(self.config.get('db_url', None),
clean_open_orders=self.config.get('dry_run', False)) clean_open_orders=self.config.get('dry_run', False))

View File

@ -60,7 +60,7 @@ class Backtesting:
# Reset keys for backtesting # Reset keys for backtesting
remove_credentials(self.config) remove_credentials(self.config)
self.strategylist: List[IStrategy] = [] self.strategylist: List[IStrategy] = []
self.exchange = ExchangeResolver(self.config['exchange']['name'], self.config).exchange self.exchange = ExchangeResolver.load_exchange(self.config['exchange']['name'], self.config)
if config.get('fee'): if config.get('fee'):
self.fee = config['fee'] self.fee = config['fee']

View File

@ -15,9 +15,8 @@ class ExchangeResolver(IResolver):
This class contains all the logic to load a custom exchange class This class contains all the logic to load a custom exchange class
""" """
__slots__ = ['exchange'] @staticmethod
def load_exchange(exchange_name: str, config: dict, validate: bool = True) -> Exchange:
def __init__(self, exchange_name: str, config: dict, validate: bool = True) -> None:
""" """
Load the custom class from config parameter Load the custom class from config parameter
:param config: configuration dictionary :param config: configuration dictionary
@ -25,17 +24,20 @@ class ExchangeResolver(IResolver):
# Map exchange name to avoid duplicate classes for identical exchanges # Map exchange name to avoid duplicate classes for identical exchanges
exchange_name = MAP_EXCHANGE_CHILDCLASS.get(exchange_name, exchange_name) exchange_name = MAP_EXCHANGE_CHILDCLASS.get(exchange_name, exchange_name)
exchange_name = exchange_name.title() exchange_name = exchange_name.title()
exchange = None
try: try:
self.exchange = self._load_exchange(exchange_name, kwargs={'config': config, exchange = ExchangeResolver._load_exchange(exchange_name,
'validate': validate}) kwargs={'config': config,
'validate': validate})
except ImportError: except ImportError:
logger.info( logger.info(
f"No {exchange_name} specific subclass found. Using the generic class instead.") f"No {exchange_name} specific subclass found. Using the generic class instead.")
if not hasattr(self, "exchange"): if not exchange:
self.exchange = Exchange(config, validate=validate) exchange = Exchange(config, validate=validate)
return exchange
def _load_exchange( @staticmethod
self, exchange_name: str, kwargs: dict) -> Exchange: def _load_exchange(exchange_name: str, kwargs: dict) -> Exchange:
""" """
Loads the specified exchange. Loads the specified exchange.
Only checks for exchanges exported in freqtrade.exchanges Only checks for exchanges exported in freqtrade.exchanges

View File

@ -18,8 +18,6 @@ class PairListResolver(IResolver):
This class contains all the logic to load custom PairList class This class contains all the logic to load custom PairList class
""" """
__slots__ = []
@staticmethod @staticmethod
def load_pairlist(pairlist_name: str, exchange, pairlistmanager, def load_pairlist(pairlist_name: str, exchange, pairlistmanager,
config: dict, pairlistconfig: dict, pairlist_pos: int) -> IPairList: config: dict, pairlistconfig: dict, pairlist_pos: int) -> IPairList:
@ -41,7 +39,6 @@ class PairListResolver(IResolver):
'pairlistconfig': pairlistconfig, 'pairlistconfig': pairlistconfig,
'pairlist_pos': pairlist_pos}) 'pairlist_pos': pairlist_pos})
@staticmethod @staticmethod
def _load_pairlist(pairlist_name: str, config: dict, kwargs: dict) -> IPairList: def _load_pairlist(pairlist_name: str, config: dict, kwargs: dict) -> IPairList:
""" """

View File

@ -198,7 +198,7 @@ def start_download_data(args: Dict[str, Any]) -> None:
pairs_not_available: List[str] = [] pairs_not_available: List[str] = []
# Init exchange # Init exchange
exchange = ExchangeResolver(config['exchange']['name'], config).exchange exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config)
try: try:
if config.get('download_trades'): if config.get('download_trades'):
@ -233,7 +233,7 @@ def start_list_timeframes(args: Dict[str, Any]) -> None:
config['ticker_interval'] = None config['ticker_interval'] = None
# Init exchange # Init exchange
exchange = ExchangeResolver(config['exchange']['name'], config, validate=False).exchange exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
if args['print_one_column']: if args['print_one_column']:
print('\n'.join(exchange.timeframes)) print('\n'.join(exchange.timeframes))
@ -252,7 +252,7 @@ def start_list_markets(args: Dict[str, Any], pairs_only: bool = False) -> None:
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE) config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
# Init exchange # Init exchange
exchange = ExchangeResolver(config['exchange']['name'], config, validate=False).exchange exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
# By default only active pairs/markets are to be shown # By default only active pairs/markets are to be shown
active_only = not args.get('list_pairs_all', False) active_only = not args.get('list_pairs_all', False)
@ -333,7 +333,7 @@ def start_test_pairlist(args: Dict[str, Any]) -> None:
from freqtrade.pairlist.pairlistmanager import PairListManager from freqtrade.pairlist.pairlistmanager import PairListManager
config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE) config = setup_utils_configuration(args, RunMode.UTIL_EXCHANGE)
exchange = ExchangeResolver(config['exchange']['name'], config, validate=False).exchange exchange = ExchangeResolver.load_exchange(config['exchange']['name'], config, validate=False)
quote_currencies = args.get('quote_currencies') quote_currencies = args.get('quote_currencies')
if not quote_currencies: if not quote_currencies:

View File

@ -77,7 +77,7 @@ def get_patched_exchange(mocker, config, api_mock=None, id='bittrex',
patch_exchange(mocker, api_mock, id, mock_markets) patch_exchange(mocker, api_mock, id, mock_markets)
config["exchange"]["name"] = id config["exchange"]["name"] = id
try: try:
exchange = ExchangeResolver(id, config).exchange exchange = ExchangeResolver.load_exchange(id, config)
except ImportError: except ImportError:
exchange = Exchange(config) exchange = Exchange(config)
return exchange return exchange

View File

@ -124,19 +124,19 @@ def test_exchange_resolver(default_conf, mocker, caplog):
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock()) mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock()) mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock()) mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
exchange = ExchangeResolver('Bittrex', default_conf).exchange exchange = ExchangeResolver.load_exchange('Bittrex', default_conf)
assert isinstance(exchange, Exchange) assert isinstance(exchange, Exchange)
assert log_has_re(r"No .* specific subclass found. Using the generic class instead.", caplog) assert log_has_re(r"No .* specific subclass found. Using the generic class instead.", caplog)
caplog.clear() caplog.clear()
exchange = ExchangeResolver('kraken', default_conf).exchange exchange = ExchangeResolver.load_exchange('kraken', default_conf)
assert isinstance(exchange, Exchange) assert isinstance(exchange, Exchange)
assert isinstance(exchange, Kraken) assert isinstance(exchange, Kraken)
assert not isinstance(exchange, Binance) assert not isinstance(exchange, Binance)
assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.", assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.",
caplog) caplog)
exchange = ExchangeResolver('binance', default_conf).exchange exchange = ExchangeResolver.load_exchange('binance', default_conf)
assert isinstance(exchange, Exchange) assert isinstance(exchange, Exchange)
assert isinstance(exchange, Binance) assert isinstance(exchange, Binance)
assert not isinstance(exchange, Kraken) assert not isinstance(exchange, Kraken)
@ -145,7 +145,7 @@ def test_exchange_resolver(default_conf, mocker, caplog):
caplog) caplog)
# Test mapping # Test mapping
exchange = ExchangeResolver('binanceus', default_conf).exchange exchange = ExchangeResolver.load_exchange('binanceus', default_conf)
assert isinstance(exchange, Exchange) assert isinstance(exchange, Exchange)
assert isinstance(exchange, Binance) assert isinstance(exchange, Binance)
assert not isinstance(exchange, Kraken) assert not isinstance(exchange, Kraken)

View File

@ -53,7 +53,8 @@ def test_load_pairlist_noexist(mocker, markets, default_conf):
with pytest.raises(OperationalException, with pytest.raises(OperationalException,
match=r"Impossible to load Pairlist 'NonexistingPairList'. " match=r"Impossible to load Pairlist 'NonexistingPairList'. "
r"This class does not exist or contains Python code errors."): r"This class does not exist or contains Python code errors."):
PairListResolver.load_pairlist('NonexistingPairList', bot.exchange, plm, default_conf, {}, 1) PairListResolver.load_pairlist('NonexistingPairList', bot.exchange, plm,
default_conf, {}, 1)
def test_refresh_market_pair_not_in_whitelist(mocker, markets, static_pl_conf): def test_refresh_market_pair_not_in_whitelist(mocker, markets, static_pl_conf):