2019-02-17 03:01:43 +00:00
|
|
|
"""
|
|
|
|
This module loads custom exchanges
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2019-10-14 09:36:42 +00:00
|
|
|
from freqtrade.exchange import Exchange, MAP_EXCHANGE_CHILDCLASS
|
2019-02-21 06:07:45 +00:00
|
|
|
import freqtrade.exchange as exchanges
|
2019-02-17 03:01:43 +00:00
|
|
|
from freqtrade.resolvers import IResolver
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeResolver(IResolver):
|
|
|
|
"""
|
|
|
|
This class contains all the logic to load a custom exchange class
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ['exchange']
|
|
|
|
|
2019-02-18 00:03:09 +00:00
|
|
|
def __init__(self, exchange_name: str, config: dict) -> None:
|
2019-02-17 03:01:43 +00:00
|
|
|
"""
|
|
|
|
Load the custom class from config parameter
|
2019-02-21 06:07:45 +00:00
|
|
|
:param config: configuration dictionary
|
2019-02-17 03:01:43 +00:00
|
|
|
"""
|
2019-10-14 09:36:42 +00:00
|
|
|
# Map exchange name to avoid duplicate classes for identical exchanges
|
|
|
|
exchange_name = MAP_EXCHANGE_CHILDCLASS.get(exchange_name, exchange_name)
|
2019-06-22 14:52:14 +00:00
|
|
|
exchange_name = exchange_name.title()
|
2019-02-20 19:13:23 +00:00
|
|
|
try:
|
|
|
|
self.exchange = self._load_exchange(exchange_name, kwargs={'config': config})
|
|
|
|
except ImportError:
|
|
|
|
logger.info(
|
|
|
|
f"No {exchange_name} specific subclass found. Using the generic class instead.")
|
2019-06-30 18:30:31 +00:00
|
|
|
if not hasattr(self, "exchange"):
|
2019-02-20 19:13:23 +00:00
|
|
|
self.exchange = Exchange(config)
|
2019-02-17 03:01:43 +00:00
|
|
|
|
|
|
|
def _load_exchange(
|
|
|
|
self, exchange_name: str, kwargs: dict) -> Exchange:
|
|
|
|
"""
|
2019-02-21 06:07:45 +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:
|
2019-02-21 06:07:45 +00:00
|
|
|
ex_class = getattr(exchanges, exchange_name)
|
|
|
|
|
|
|
|
exchange = ex_class(kwargs['config'])
|
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
|
2019-02-21 06:07:45 +00:00
|
|
|
except AttributeError:
|
2019-07-14 11:30:57 +00:00
|
|
|
# Pass and raise ImportError instead
|
2019-02-21 06:07:45 +00:00
|
|
|
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
|
|
|
)
|