2019-02-17 03:01:43 +00:00
|
|
|
"""
|
|
|
|
This module loads custom exchanges
|
|
|
|
"""
|
2024-05-12 14:21:12 +00:00
|
|
|
|
2019-02-17 03:01:43 +00:00
|
|
|
import logging
|
2023-06-03 06:28:44 +00:00
|
|
|
from inspect import isclass
|
|
|
|
from typing import Any, Dict, List, Optional
|
2019-02-17 03:01:43 +00:00
|
|
|
|
2019-02-21 06:07:45 +00:00
|
|
|
import freqtrade.exchange as exchanges
|
2023-05-15 05:27:08 +00:00
|
|
|
from freqtrade.constants import Config, ExchangeConfig
|
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
|
|
|
|
"""
|
2024-05-12 14:21:12 +00:00
|
|
|
|
2019-12-24 12:34:37 +00:00
|
|
|
object_type = Exchange
|
2019-02-17 03:01:43 +00:00
|
|
|
|
2019-12-23 09:03:18 +00:00
|
|
|
@staticmethod
|
2024-05-12 14:21:12 +00:00
|
|
|
def load_exchange(
|
|
|
|
config: Config,
|
|
|
|
*,
|
|
|
|
exchange_config: Optional[ExchangeConfig] = None,
|
|
|
|
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
|
2019-02-21 06:07:45 +00:00
|
|
|
:param config: configuration dictionary
|
2019-02-17 03:01:43 +00:00
|
|
|
"""
|
2024-05-12 14:21:12 +00:00
|
|
|
exchange_name: str = config["exchange"]["name"]
|
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-12-23 09:03:18 +00:00
|
|
|
exchange = None
|
2019-02-20 19:13:23 +00:00
|
|
|
try:
|
2022-07-23 17:56:38 +00:00
|
|
|
exchange = ExchangeResolver._load_exchange(
|
|
|
|
exchange_name,
|
|
|
|
kwargs={
|
2024-05-12 14:21:12 +00:00
|
|
|
"config": config,
|
|
|
|
"validate": validate,
|
|
|
|
"exchange_config": exchange_config,
|
|
|
|
"load_leverage_tiers": load_leverage_tiers,
|
|
|
|
},
|
2022-07-23 17:56:38 +00:00
|
|
|
)
|
2019-02-20 19:13:23 +00:00
|
|
|
except ImportError:
|
|
|
|
logger.info(
|
2024-05-12 14:21:12 +00:00
|
|
|
f"No {exchange_name} specific subclass found. Using the generic class instead."
|
|
|
|
)
|
2019-12-23 09:03:18 +00:00
|
|
|
if not exchange:
|
2024-05-12 14:21:12 +00:00
|
|
|
exchange = Exchange(
|
|
|
|
config,
|
|
|
|
validate=validate,
|
|
|
|
exchange_config=exchange_config,
|
|
|
|
)
|
2019-12-23 09:03:18 +00:00
|
|
|
return exchange
|
2019-02-17 03:01:43 +00:00
|
|
|
|
2019-12-23 09:03:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def _load_exchange(exchange_name: str, kwargs: dict) -> Exchange:
|
2019-02-17 03:01:43 +00:00
|
|
|
"""
|
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)
|
|
|
|
|
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
|
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
|
|
|
)
|
2023-06-03 06:28:44 +00:00
|
|
|
|
|
|
|
@classmethod
|
2024-05-12 14:21:12 +00:00
|
|
|
def search_all_objects(
|
|
|
|
cls, config: Config, enum_failed: bool, recursive: bool = False
|
|
|
|
) -> List[Dict[str, Any]]:
|
2023-06-03 06:28:44 +00:00
|
|
|
"""
|
|
|
|
Searches for valid objects
|
|
|
|
:param config: Config object
|
|
|
|
:param enum_failed: If True, will return None for modules which fail.
|
|
|
|
Otherwise, failing modules are skipped.
|
|
|
|
:param recursive: Recursively walk directory tree searching for strategies
|
|
|
|
:return: List of dicts containing 'name', 'class' and 'location' entries
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for exchange_name in dir(exchanges):
|
|
|
|
exchange = getattr(exchanges, exchange_name)
|
|
|
|
if isclass(exchange) and issubclass(exchange, Exchange):
|
2024-05-12 14:21:12 +00:00
|
|
|
result.append(
|
|
|
|
{
|
|
|
|
"name": exchange_name,
|
|
|
|
"class": exchange,
|
|
|
|
"location": exchange.__module__,
|
|
|
|
"location_rel: ": exchange.__module__.replace("freqtrade.", ""),
|
|
|
|
}
|
|
|
|
)
|
2023-06-03 06:28:44 +00:00
|
|
|
return result
|