• 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

96.43
/freqtrade/optimize/hyperopt_interface.py
1
"""
2
IHyperOpt interface
3
This module defines the interface to apply for hyperopt
4
"""
5
import logging
1✔
6
import math
1✔
7
from abc import ABC
1✔
8
from typing import Dict, List, Union
1✔
9

10
from sklearn.base import RegressorMixin
1✔
11
from skopt.space import Categorical, Dimension, Integer
1✔
12

13
from freqtrade.constants import Config
1✔
14
from freqtrade.exchange import timeframe_to_minutes
1✔
15
from freqtrade.misc import round_dict
1✔
16
from freqtrade.optimize.space import SKDecimal
1✔
17
from freqtrade.strategy import IStrategy
1✔
18

19

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

22
EstimatorType = Union[RegressorMixin, str]
1✔
23

24

25
class IHyperOpt(ABC):
1✔
26
    """
27
    Interface for freqtrade hyperopt
28
    Defines the mandatory structure must follow any custom hyperopt
29

30
    Class attributes you can use:
31
        timeframe -> int: value of the timeframe to use for the strategy
32
    """
33
    timeframe: str
1✔
34
    strategy: IStrategy
1✔
35

36
    def __init__(self, config: Config) -> None:
1✔
37
        self.config = config
1✔
38

39
        # Assign timeframe to be used in hyperopt
40
        IHyperOpt.timeframe = str(config['timeframe'])
1✔
41

42
    def generate_estimator(self, dimensions: List[Dimension], **kwargs) -> EstimatorType:
1✔
43
        """
44
        Return base_estimator.
45
        Can be any of "GP", "RF", "ET", "GBRT" or an instance of a class
46
        inheriting from RegressorMixin (from sklearn).
47
        """
48
        return 'ET'
1✔
49

50
    def generate_roi_table(self, params: Dict) -> Dict[int, float]:
1✔
51
        """
52
        Create a ROI table.
53

54
        Generates the ROI table that will be used by Hyperopt.
55
        You may override it in your custom Hyperopt class.
56
        """
57
        roi_table = {}
1✔
58
        roi_table[0] = params['roi_p1'] + params['roi_p2'] + params['roi_p3']
1✔
59
        roi_table[params['roi_t3']] = params['roi_p1'] + params['roi_p2']
1✔
60
        roi_table[params['roi_t3'] + params['roi_t2']] = params['roi_p1']
1✔
61
        roi_table[params['roi_t3'] + params['roi_t2'] + params['roi_t1']] = 0
1✔
62

63
        return roi_table
1✔
64

65
    def roi_space(self) -> List[Dimension]:
1✔
66
        """
67
        Create a ROI space.
68

69
        Defines values to search for each ROI steps.
70

71
        This method implements adaptive roi hyperspace with varied
72
        ranges for parameters which automatically adapts to the
73
        timeframe used.
74

75
        It's used by Freqtrade by default, if no custom roi_space method is defined.
76
        """
77

78
        # Default scaling coefficients for the roi hyperspace. Can be changed
79
        # to adjust resulting ranges of the ROI tables.
80
        # Increase if you need wider ranges in the roi hyperspace, decrease if shorter
81
        # ranges are needed.
82
        roi_t_alpha = 1.0
1✔
83
        roi_p_alpha = 1.0
1✔
84

85
        timeframe_min = timeframe_to_minutes(self.timeframe)
1✔
86

87
        # We define here limits for the ROI space parameters automagically adapted to the
88
        # timeframe used by the bot:
89
        #
90
        # * 'roi_t' (limits for the time intervals in the ROI tables) components
91
        #   are scaled linearly.
92
        # * 'roi_p' (limits for the ROI value steps) components are scaled logarithmically.
93
        #
94
        # The scaling is designed so that it maps exactly to the legacy Freqtrade roi_space()
95
        # method for the 5m timeframe.
96
        roi_t_scale = timeframe_min / 5
1✔
97
        roi_p_scale = math.log1p(timeframe_min) / math.log1p(5)
1✔
98
        roi_limits = {
1✔
99
            'roi_t1_min': int(10 * roi_t_scale * roi_t_alpha),
100
            'roi_t1_max': int(120 * roi_t_scale * roi_t_alpha),
101
            'roi_t2_min': int(10 * roi_t_scale * roi_t_alpha),
102
            'roi_t2_max': int(60 * roi_t_scale * roi_t_alpha),
103
            'roi_t3_min': int(10 * roi_t_scale * roi_t_alpha),
104
            'roi_t3_max': int(40 * roi_t_scale * roi_t_alpha),
105
            'roi_p1_min': 0.01 * roi_p_scale * roi_p_alpha,
106
            'roi_p1_max': 0.04 * roi_p_scale * roi_p_alpha,
107
            'roi_p2_min': 0.01 * roi_p_scale * roi_p_alpha,
108
            'roi_p2_max': 0.07 * roi_p_scale * roi_p_alpha,
109
            'roi_p3_min': 0.01 * roi_p_scale * roi_p_alpha,
110
            'roi_p3_max': 0.20 * roi_p_scale * roi_p_alpha,
111
        }
112
        logger.debug(f"Using roi space limits: {roi_limits}")
1✔
113
        p = {
1✔
114
            'roi_t1': roi_limits['roi_t1_min'],
115
            'roi_t2': roi_limits['roi_t2_min'],
116
            'roi_t3': roi_limits['roi_t3_min'],
117
            'roi_p1': roi_limits['roi_p1_min'],
118
            'roi_p2': roi_limits['roi_p2_min'],
119
            'roi_p3': roi_limits['roi_p3_min'],
120
        }
121
        logger.info(f"Min roi table: {round_dict(self.generate_roi_table(p), 3)}")
1✔
122
        p = {
1✔
123
            'roi_t1': roi_limits['roi_t1_max'],
124
            'roi_t2': roi_limits['roi_t2_max'],
125
            'roi_t3': roi_limits['roi_t3_max'],
126
            'roi_p1': roi_limits['roi_p1_max'],
127
            'roi_p2': roi_limits['roi_p2_max'],
128
            'roi_p3': roi_limits['roi_p3_max'],
129
        }
130
        logger.info(f"Max roi table: {round_dict(self.generate_roi_table(p), 3)}")
1✔
131

132
        return [
1✔
133
            Integer(roi_limits['roi_t1_min'], roi_limits['roi_t1_max'], name='roi_t1'),
134
            Integer(roi_limits['roi_t2_min'], roi_limits['roi_t2_max'], name='roi_t2'),
135
            Integer(roi_limits['roi_t3_min'], roi_limits['roi_t3_max'], name='roi_t3'),
136
            SKDecimal(roi_limits['roi_p1_min'], roi_limits['roi_p1_max'], decimals=3,
137
                      name='roi_p1'),
138
            SKDecimal(roi_limits['roi_p2_min'], roi_limits['roi_p2_max'], decimals=3,
139
                      name='roi_p2'),
140
            SKDecimal(roi_limits['roi_p3_min'], roi_limits['roi_p3_max'], decimals=3,
141
                      name='roi_p3'),
142
        ]
143

144
    def stoploss_space(self) -> List[Dimension]:
1✔
145
        """
146
        Create a stoploss space.
147

148
        Defines range of stoploss values to search.
149
        You may override it in your custom Hyperopt class.
150
        """
151
        return [
1✔
152
            SKDecimal(-0.35, -0.02, decimals=3, name='stoploss'),
153
        ]
154

155
    def generate_trailing_params(self, params: Dict) -> Dict:
1✔
156
        """
157
        Create dict with trailing stop parameters.
158
        """
159
        return {
1✔
160
            'trailing_stop': params['trailing_stop'],
161
            'trailing_stop_positive': params['trailing_stop_positive'],
162
            'trailing_stop_positive_offset': (params['trailing_stop_positive'] +
163
                                              params['trailing_stop_positive_offset_p1']),
164
            'trailing_only_offset_is_reached': params['trailing_only_offset_is_reached'],
165
        }
166

167
    def trailing_space(self) -> List[Dimension]:
1✔
168
        """
169
        Create a trailing stoploss space.
170

171
        You may override it in your custom Hyperopt class.
172
        """
173
        return [
1✔
174
            # It was decided to always set trailing_stop is to True if the 'trailing' hyperspace
175
            # is used. Otherwise hyperopt will vary other parameters that won't have effect if
176
            # trailing_stop is set False.
177
            # This parameter is included into the hyperspace dimensions rather than assigning
178
            # it explicitly in the code in order to have it printed in the results along with
179
            # other 'trailing' hyperspace parameters.
180
            Categorical([True], name='trailing_stop'),
181

182
            SKDecimal(0.01, 0.35, decimals=3, name='trailing_stop_positive'),
183

184
            # 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive',
185
            # so this intermediate parameter is used as the value of the difference between
186
            # them. The value of the 'trailing_stop_positive_offset' is constructed in the
187
            # generate_trailing_params() method.
188
            # This is similar to the hyperspace dimensions used for constructing the ROI tables.
189
            SKDecimal(0.001, 0.1, decimals=3, name='trailing_stop_positive_offset_p1'),
190

191
            Categorical([True, False], name='trailing_only_offset_is_reached'),
192
        ]
193

194
    def max_open_trades_space(self) -> List[Dimension]:
1✔
195
        """
196
        Create a max open trades space.
197

198
        You may override it in your custom Hyperopt class.
199
        """
200
        return [
1✔
201
            Integer(-1, 10, name='max_open_trades'),
202
        ]
203

204
    # This is needed for proper unpickling the class attribute timeframe
205
    # which is set to the actual value by the resolver.
206
    # Why do I still need such shamanic mantras in modern python?
207
    def __getstate__(self):
1✔
208
        state = self.__dict__.copy()
1✔
209
        state['timeframe'] = self.timeframe
1✔
210
        return state
1✔
211

212
    def __setstate__(self, state):
1✔
213
        self.__dict__.update(state)
×
214
        IHyperOpt.timeframe = state['timeframe']
×
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