• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 9394559170

26 Apr 2024 06:36AM UTC coverage: 94.656% (-0.02%) from 94.674%
9394559170

push

github

xmatthias
Loader should be passed as kwarg for clarity

20280 of 21425 relevant lines covered (94.66%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.0
/freqtrade/optimize/hyperopt_auto.py
1
"""
2
HyperOptAuto class.
3
This module implements a convenience auto-hyperopt class, which can be used together with strategies
4
 that implement IHyperStrategy interface.
5
"""
6
import logging
1✔
7
from contextlib import suppress
1✔
8
from typing import Callable, Dict, List
1✔
9

10
from freqtrade.exceptions import OperationalException
1✔
11

12

13
with suppress(ImportError):
1✔
14
    from skopt.space import Dimension
1✔
15

16
from freqtrade.optimize.hyperopt_interface import EstimatorType, IHyperOpt
1✔
17

18

19
logger = logging.getLogger(__name__)
1✔
20

21

22
def _format_exception_message(space: str, ignore_missing_space: bool) -> None:
1✔
23
    msg = (f"The '{space}' space is included into the hyperoptimization "
1✔
24
           f"but no parameter for this space was found in your Strategy. "
25
           )
26
    if ignore_missing_space:
1✔
27
        logger.warning(msg + "This space will be ignored.")
1✔
28
    else:
29
        raise OperationalException(
1✔
30
            msg + f"Please make sure to have parameters for this space enabled for optimization "
31
            f"or remove the '{space}' space from hyperoptimization.")
32

33

34
class HyperOptAuto(IHyperOpt):
1✔
35
    """
36
    This class delegates functionality to Strategy(IHyperStrategy) and Strategy.HyperOpt classes.
37
     Most of the time Strategy.HyperOpt class would only implement indicator_space and
38
     sell_indicator_space methods, but other hyperopt methods can be overridden as well.
39
    """
40

41
    def _get_func(self, name) -> Callable:
1✔
42
        """
43
        Return a function defined in Strategy.HyperOpt class, or one defined in super() class.
44
        :param name: function name.
45
        :return: a requested function.
46
        """
47
        hyperopt_cls = getattr(self.strategy, 'HyperOpt', None)
1✔
48
        default_func = getattr(super(), name)
1✔
49
        if hyperopt_cls:
1✔
50
            return getattr(hyperopt_cls, name, default_func)
×
51
        else:
52
            return default_func
1✔
53

54
    def _generate_indicator_space(self, category):
1✔
55
        for attr_name, attr in self.strategy.enumerate_parameters(category):
1✔
56
            if attr.optimize:
1✔
57
                yield attr.get_space(attr_name)
1✔
58

59
    def _get_indicator_space(self, category) -> List:
1✔
60
        # TODO: is this necessary, or can we call "generate_space" directly?
61
        indicator_space = list(self._generate_indicator_space(category))
1✔
62
        if len(indicator_space) > 0:
1✔
63
            return indicator_space
1✔
64
        else:
65
            _format_exception_message(
1✔
66
                category,
67
                self.config.get("hyperopt_ignore_missing_space", False))
68
            return []
1✔
69

70
    def buy_indicator_space(self) -> List['Dimension']:
1✔
71
        return self._get_indicator_space('buy')
1✔
72

73
    def sell_indicator_space(self) -> List['Dimension']:
1✔
74
        return self._get_indicator_space('sell')
1✔
75

76
    def protection_space(self) -> List['Dimension']:
1✔
77
        return self._get_indicator_space('protection')
1✔
78

79
    def generate_roi_table(self, params: Dict) -> Dict[int, float]:
1✔
80
        return self._get_func('generate_roi_table')(params)
1✔
81

82
    def roi_space(self) -> List['Dimension']:
1✔
83
        return self._get_func('roi_space')()
1✔
84

85
    def stoploss_space(self) -> List['Dimension']:
1✔
86
        return self._get_func('stoploss_space')()
1✔
87

88
    def generate_trailing_params(self, params: Dict) -> Dict:
1✔
89
        return self._get_func('generate_trailing_params')(params)
1✔
90

91
    def trailing_space(self) -> List['Dimension']:
1✔
92
        return self._get_func('trailing_space')()
1✔
93

94
    def max_open_trades_space(self) -> List['Dimension']:
1✔
95
        return self._get_func('max_open_trades_space')()
1✔
96

97
    def generate_estimator(self, dimensions: List['Dimension'], **kwargs) -> EstimatorType:
1✔
98
        return self._get_func('generate_estimator')(dimensions=dimensions, **kwargs)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc