Merge branch 'develop' into feat/short
This commit is contained in:
@@ -44,7 +44,6 @@ class HyperOptLossResolver(IResolver):
|
||||
extra_dir=config.get('hyperopt_path'))
|
||||
|
||||
# Assign timeframe to be used in hyperopt
|
||||
hyperoptloss.__class__.ticker_interval = str(config['timeframe'])
|
||||
hyperoptloss.__class__.timeframe = str(config['timeframe'])
|
||||
|
||||
return hyperoptloss
|
||||
|
@@ -6,6 +6,7 @@ This module load custom objects
|
||||
import importlib.util
|
||||
import inspect
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union
|
||||
|
||||
@@ -15,6 +16,22 @@ from freqtrade.exceptions import OperationalException
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PathModifier:
|
||||
def __init__(self, path: Path):
|
||||
self.path = path
|
||||
|
||||
def __enter__(self):
|
||||
"""Inject path to allow importing with relative imports."""
|
||||
sys.path.insert(0, str(self.path))
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Undo insertion of local path."""
|
||||
str_path = str(self.path)
|
||||
if str_path in sys.path:
|
||||
sys.path.remove(str_path)
|
||||
|
||||
|
||||
class IResolver:
|
||||
"""
|
||||
This class contains all the logic to load custom classes
|
||||
@@ -57,27 +74,32 @@ class IResolver:
|
||||
|
||||
# Generate spec based on absolute path
|
||||
# Pass object_name as first argument to have logging print a reasonable name.
|
||||
spec = importlib.util.spec_from_file_location(object_name or "", str(module_path))
|
||||
if not spec:
|
||||
return iter([None])
|
||||
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(module) # type: ignore # importlib does not use typehints
|
||||
except (ModuleNotFoundError, SyntaxError, ImportError, NameError) as err:
|
||||
# Catch errors in case a specific module is not installed
|
||||
logger.warning(f"Could not import {module_path} due to '{err}'")
|
||||
if enum_failed:
|
||||
with PathModifier(module_path.parent):
|
||||
module_name = module_path.stem or ""
|
||||
spec = importlib.util.spec_from_file_location(module_name, str(module_path))
|
||||
if not spec:
|
||||
return iter([None])
|
||||
|
||||
valid_objects_gen = (
|
||||
(obj, inspect.getsource(module)) for
|
||||
name, obj in inspect.getmembers(
|
||||
module, inspect.isclass) if ((object_name is None or object_name == name)
|
||||
and issubclass(obj, cls.object_type)
|
||||
and obj is not cls.object_type)
|
||||
)
|
||||
return valid_objects_gen
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(module) # type: ignore # importlib does not use typehints
|
||||
except (ModuleNotFoundError, SyntaxError, ImportError, NameError) as err:
|
||||
# Catch errors in case a specific module is not installed
|
||||
logger.warning(f"Could not import {module_path} due to '{err}'")
|
||||
if enum_failed:
|
||||
return iter([None])
|
||||
|
||||
valid_objects_gen = (
|
||||
(obj, inspect.getsource(module)) for
|
||||
name, obj in inspect.getmembers(
|
||||
module, inspect.isclass) if ((object_name is None or object_name == name)
|
||||
and issubclass(obj, cls.object_type)
|
||||
and obj is not cls.object_type
|
||||
and obj.__module__ == module_name
|
||||
)
|
||||
)
|
||||
# The __module__ check ensures we only use strategies that are defined in this folder.
|
||||
return valid_objects_gen
|
||||
|
||||
@classmethod
|
||||
def _search_object(cls, directory: Path, *, object_name: str, add_source: bool = False
|
||||
|
@@ -47,14 +47,6 @@ class StrategyResolver(IResolver):
|
||||
strategy_name, config=config,
|
||||
extra_dir=config.get('strategy_path'))
|
||||
|
||||
if hasattr(strategy, 'ticker_interval') and not hasattr(strategy, 'timeframe'):
|
||||
# Assign ticker_interval to timeframe to keep compatibility
|
||||
if 'timeframe' not in config:
|
||||
logger.warning(
|
||||
"DEPRECATED: Please migrate to using 'timeframe' instead of 'ticker_interval'."
|
||||
)
|
||||
strategy.timeframe = strategy.ticker_interval
|
||||
|
||||
if strategy._ft_params_from_file:
|
||||
# Set parameters from Hyperopt results file
|
||||
params = strategy._ft_params_from_file
|
||||
@@ -147,10 +139,6 @@ class StrategyResolver(IResolver):
|
||||
"""
|
||||
Normalize attributes to have the correct type.
|
||||
"""
|
||||
# Assign deprecated variable - to not break users code relying on this.
|
||||
if hasattr(strategy, 'timeframe'):
|
||||
strategy.ticker_interval = strategy.timeframe
|
||||
|
||||
# Sort and apply type conversions
|
||||
if hasattr(strategy, 'minimal_roi'):
|
||||
strategy.minimal_roi = dict(sorted(
|
||||
|
Reference in New Issue
Block a user