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

freqtrade / freqtrade / 12921599456

05 Dec 2024 04:27PM UTC coverage: 94.43% (+0.03%) from 94.397%
12921599456

push

github

web-flow
Merge branch 'freqtrade:develop' into develop

8892 of 9389 new or added lines in 271 files covered. (94.71%)

591 existing lines in 99 files now uncovered.

21685 of 22964 relevant lines covered (94.43%)

0.94 hits per line

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

96.43
/freqtrade/optimize/hyperopt/hyperopt_interface.py
1
"""
2
IHyperOpt interface
3
This module defines the interface to apply for hyperopt
4
"""
5

6
import logging
1✔
7
import math
1✔
8
from abc import ABC
1✔
9
from typing import TypeAlias
1✔
10

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

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

20

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

23
EstimatorType: TypeAlias = RegressorMixin | str
1✔
24

25

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

31
    Class attributes you can use:
32
        timeframe -> int: value of the timeframe to use for the strategy
33
    """
34

35
    timeframe: str
1✔
36
    strategy: IStrategy
1✔
37

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

41
        # Assign timeframe to be used in hyperopt
42
        IHyperOpt.timeframe = str(config["timeframe"])
1✔
43

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

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

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

65
        return roi_table
1✔
66

67
    def roi_space(self) -> list[Dimension]:
1✔
68
        """
69
        Create a ROI space.
70

71
        Defines values to search for each ROI steps.
72

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

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

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

87
        timeframe_min = timeframe_to_minutes(self.timeframe)
1✔
88

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

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

149
    def stoploss_space(self) -> list[Dimension]:
1✔
150
        """
151
        Create a stoploss space.
152

153
        Defines range of stoploss values to search.
154
        You may override it in your custom Hyperopt class.
155
        """
156
        return [
1✔
157
            SKDecimal(-0.35, -0.02, decimals=3, name="stoploss"),
158
        ]
159

160
    def generate_trailing_params(self, params: dict) -> dict:
1✔
161
        """
162
        Create dict with trailing stop parameters.
163
        """
164
        return {
1✔
165
            "trailing_stop": params["trailing_stop"],
166
            "trailing_stop_positive": params["trailing_stop_positive"],
167
            "trailing_stop_positive_offset": (
168
                params["trailing_stop_positive"] + params["trailing_stop_positive_offset_p1"]
169
            ),
170
            "trailing_only_offset_is_reached": params["trailing_only_offset_is_reached"],
171
        }
172

173
    def trailing_space(self) -> list[Dimension]:
1✔
174
        """
175
        Create a trailing stoploss space.
176

177
        You may override it in your custom Hyperopt class.
178
        """
179
        return [
1✔
180
            # It was decided to always set trailing_stop is to True if the 'trailing' hyperspace
181
            # is used. Otherwise hyperopt will vary other parameters that won't have effect if
182
            # trailing_stop is set False.
183
            # This parameter is included into the hyperspace dimensions rather than assigning
184
            # it explicitly in the code in order to have it printed in the results along with
185
            # other 'trailing' hyperspace parameters.
186
            Categorical([True], name="trailing_stop"),
187
            SKDecimal(0.01, 0.35, decimals=3, name="trailing_stop_positive"),
188
            # 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive',
189
            # so this intermediate parameter is used as the value of the difference between
190
            # them. The value of the 'trailing_stop_positive_offset' is constructed in the
191
            # generate_trailing_params() method.
192
            # This is similar to the hyperspace dimensions used for constructing the ROI tables.
193
            SKDecimal(0.001, 0.1, decimals=3, name="trailing_stop_positive_offset_p1"),
194
            Categorical([True, False], name="trailing_only_offset_is_reached"),
195
        ]
196

197
    def max_open_trades_space(self) -> list[Dimension]:
1✔
198
        """
199
        Create a max open trades space.
200

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

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

215
    def __setstate__(self, state):
1✔
UNCOV
216
        self.__dict__.update(state)
×
NEW
217
        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

© 2026 Coveralls, Inc