stable/freqtrade/resolvers/exchange_resolver.py

72 lines
2.3 KiB
Python
Raw Permalink Normal View History

2019-02-17 03:01:43 +00:00
"""
This module loads custom exchanges
"""
import logging
import freqtrade.exchange as exchanges
2022-09-18 17:36:11 +00:00
from freqtrade.constants import Config
2020-09-28 17:39:41 +00:00
from freqtrade.exchange import MAP_EXCHANGE_CHILDCLASS, Exchange
2019-02-17 03:01:43 +00:00
from freqtrade.resolvers import IResolver
2020-09-28 17:39:41 +00:00
2019-02-17 03:01:43 +00:00
logger = logging.getLogger(__name__)
class ExchangeResolver(IResolver):
"""
This class contains all the logic to load a custom exchange class
"""
2019-12-24 12:34:37 +00:00
object_type = Exchange
2019-02-17 03:01:43 +00:00
@staticmethod
2022-09-18 11:31:52 +00:00
def load_exchange(exchange_name: str, config: Config, validate: bool = True,
load_leverage_tiers: bool = False) -> Exchange:
2019-02-17 03:01:43 +00:00
"""
Load the custom class from config parameter
2021-06-25 17:13:31 +00:00
:param exchange_name: name of the Exchange to load
:param config: configuration dictionary
2019-02-17 03:01:43 +00:00
"""
# Map exchange name to avoid duplicate classes for identical exchanges
exchange_name = MAP_EXCHANGE_CHILDCLASS.get(exchange_name, exchange_name)
exchange_name = exchange_name.title()
exchange = None
try:
exchange = ExchangeResolver._load_exchange(
exchange_name,
kwargs={
'config': config,
'validate': validate,
'load_leverage_tiers': load_leverage_tiers}
)
except ImportError:
logger.info(
f"No {exchange_name} specific subclass found. Using the generic class instead.")
if not exchange:
exchange = Exchange(config, validate=validate)
return exchange
2019-02-17 03:01:43 +00:00
@staticmethod
def _load_exchange(exchange_name: str, kwargs: dict) -> Exchange:
2019-02-17 03:01:43 +00:00
"""
Loads the specified exchange.
Only checks for exchanges exported in freqtrade.exchanges
2019-02-17 03:01:43 +00:00
:param exchange_name: name of the module to import
:return: Exchange instance or None
"""
2019-02-19 18:15:22 +00:00
try:
ex_class = getattr(exchanges, exchange_name)
2019-10-13 08:33:22 +00:00
exchange = ex_class(**kwargs)
2019-02-19 18:15:22 +00:00
if exchange:
2019-07-12 20:45:49 +00:00
logger.info(f"Using resolved exchange '{exchange_name}'...")
2019-02-19 18:15:22 +00:00
return exchange
except AttributeError:
# Pass and raise ImportError instead
pass
2019-02-17 03:01:43 +00:00
raise ImportError(
2019-07-12 20:45:49 +00:00
f"Impossible to load Exchange '{exchange_name}'. This class does not exist "
"or contains Python code errors."
2019-02-17 03:01:43 +00:00
)