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

freqtrade / freqtrade / 1557291825

pending completion
1557291825

push

github-actions

GitHub
Merge pull request #4685 from freqtrade/ft_has

34 of 34 new or added lines in 4 files covered. (100.0%)

8853 of 9006 relevant lines covered (98.3%)

0.98 hits per line

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

90.32
/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 Any, Callable, Dict, List
1✔
9

10
from skopt.space import Categorical, Dimension, Integer, Real
1✔
11

12
from freqtrade.exceptions import OperationalException
1✔
13
from freqtrade.exchange import timeframe_to_minutes
1✔
14
from freqtrade.misc import round_dict
1✔
15
from freqtrade.strategy import IStrategy
1✔
16

17

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

20

21
def _format_exception_message(method: str, space: str) -> str:
1✔
22
    return (f"The '{space}' space is included into the hyperoptimization "
1✔
23
            f"but {method}() method is not found in your "
24
            f"custom Hyperopt class. You should either implement this "
25
            f"method or remove the '{space}' space from hyperoptimization.")
26

27

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

33
    Class attributes you can use:
34
        timeframe -> int: value of the timeframe to use for the strategy
35
    """
36
    ticker_interval: str  # DEPRECATED
1✔
37
    timeframe: str
1✔
38
    strategy: IStrategy
1✔
39

40
    def __init__(self, config: dict) -> None:
1✔
41
        self.config = config
1✔
42

43
        # Assign ticker_interval to be used in hyperopt
44
        IHyperOpt.ticker_interval = str(config['timeframe'])  # DEPRECATED
1✔
45
        IHyperOpt.timeframe = str(config['timeframe'])
1✔
46

47
    def buy_strategy_generator(self, params: Dict[str, Any]) -> Callable:
1✔
48
        """
49
        Create a buy strategy generator.
50
        """
51
        raise OperationalException(_format_exception_message('buy_strategy_generator', 'buy'))
1✔
52

53
    def sell_strategy_generator(self, params: Dict[str, Any]) -> Callable:
1✔
54
        """
55
        Create a sell strategy generator.
56
        """
57
        raise OperationalException(_format_exception_message('sell_strategy_generator', 'sell'))
1✔
58

59
    def indicator_space(self) -> List[Dimension]:
1✔
60
        """
61
        Create an indicator space.
62
        """
63
        raise OperationalException(_format_exception_message('indicator_space', 'buy'))
1✔
64

65
    def sell_indicator_space(self) -> List[Dimension]:
1✔
66
        """
67
        Create a sell indicator space.
68
        """
69
        raise OperationalException(_format_exception_message('sell_indicator_space', 'sell'))
1✔
70

71
    def generate_roi_table(self, params: Dict) -> Dict[int, float]:
1✔
72
        """
73
        Create a ROI table.
74

75
        Generates the ROI table that will be used by Hyperopt.
76
        You may override it in your custom Hyperopt class.
77
        """
78
        roi_table = {}
1✔
79
        roi_table[0] = params['roi_p1'] + params['roi_p2'] + params['roi_p3']
1✔
80
        roi_table[params['roi_t3']] = params['roi_p1'] + params['roi_p2']
1✔
81
        roi_table[params['roi_t3'] + params['roi_t2']] = params['roi_p1']
1✔
82
        roi_table[params['roi_t3'] + params['roi_t2'] + params['roi_t1']] = 0
1✔
83

84
        return roi_table
1✔
85

86
    def roi_space(self) -> List[Dimension]:
1✔
87
        """
88
        Create a ROI space.
89

90
        Defines values to search for each ROI steps.
91

92
        This method implements adaptive roi hyperspace with varied
93
        ranges for parameters which automatically adapts to the
94
        timeframe used.
95

96
        It's used by Freqtrade by default, if no custom roi_space method is defined.
97
        """
98

99
        # Default scaling coefficients for the roi hyperspace. Can be changed
100
        # to adjust resulting ranges of the ROI tables.
101
        # Increase if you need wider ranges in the roi hyperspace, decrease if shorter
102
        # ranges are needed.
103
        roi_t_alpha = 1.0
1✔
104
        roi_p_alpha = 1.0
1✔
105

106
        timeframe_min = timeframe_to_minutes(self.timeframe)
1✔
107

108
        # We define here limits for the ROI space parameters automagically adapted to the
109
        # timeframe used by the bot:
110
        #
111
        # * 'roi_t' (limits for the time intervals in the ROI tables) components
112
        #   are scaled linearly.
113
        # * 'roi_p' (limits for the ROI value steps) components are scaled logarithmically.
114
        #
115
        # The scaling is designed so that it maps exactly to the legacy Freqtrade roi_space()
116
        # method for the 5m timeframe.
117
        roi_t_scale = timeframe_min / 5
1✔
118
        roi_p_scale = math.log1p(timeframe_min) / math.log1p(5)
1✔
119
        roi_limits = {
1✔
120
            'roi_t1_min': int(10 * roi_t_scale * roi_t_alpha),
121
            'roi_t1_max': int(120 * roi_t_scale * roi_t_alpha),
122
            'roi_t2_min': int(10 * roi_t_scale * roi_t_alpha),
123
            'roi_t2_max': int(60 * roi_t_scale * roi_t_alpha),
124
            'roi_t3_min': int(10 * roi_t_scale * roi_t_alpha),
125
            'roi_t3_max': int(40 * roi_t_scale * roi_t_alpha),
126
            'roi_p1_min': 0.01 * roi_p_scale * roi_p_alpha,
127
            'roi_p1_max': 0.04 * roi_p_scale * roi_p_alpha,
128
            'roi_p2_min': 0.01 * roi_p_scale * roi_p_alpha,
129
            'roi_p2_max': 0.07 * roi_p_scale * roi_p_alpha,
130
            'roi_p3_min': 0.01 * roi_p_scale * roi_p_alpha,
131
            'roi_p3_max': 0.20 * roi_p_scale * roi_p_alpha,
132
        }
133
        logger.debug(f"Using roi space limits: {roi_limits}")
1✔
134
        p = {
1✔
135
            'roi_t1': roi_limits['roi_t1_min'],
136
            'roi_t2': roi_limits['roi_t2_min'],
137
            'roi_t3': roi_limits['roi_t3_min'],
138
            'roi_p1': roi_limits['roi_p1_min'],
139
            'roi_p2': roi_limits['roi_p2_min'],
140
            'roi_p3': roi_limits['roi_p3_min'],
141
        }
142
        logger.info(f"Min roi table: {round_dict(self.generate_roi_table(p), 5)}")
1✔
143
        p = {
1✔
144
            'roi_t1': roi_limits['roi_t1_max'],
145
            'roi_t2': roi_limits['roi_t2_max'],
146
            'roi_t3': roi_limits['roi_t3_max'],
147
            'roi_p1': roi_limits['roi_p1_max'],
148
            'roi_p2': roi_limits['roi_p2_max'],
149
            'roi_p3': roi_limits['roi_p3_max'],
150
        }
151
        logger.info(f"Max roi table: {round_dict(self.generate_roi_table(p), 5)}")
1✔
152

153
        return [
1✔
154
            Integer(roi_limits['roi_t1_min'], roi_limits['roi_t1_max'], name='roi_t1'),
155
            Integer(roi_limits['roi_t2_min'], roi_limits['roi_t2_max'], name='roi_t2'),
156
            Integer(roi_limits['roi_t3_min'], roi_limits['roi_t3_max'], name='roi_t3'),
157
            Real(roi_limits['roi_p1_min'], roi_limits['roi_p1_max'], name='roi_p1'),
158
            Real(roi_limits['roi_p2_min'], roi_limits['roi_p2_max'], name='roi_p2'),
159
            Real(roi_limits['roi_p3_min'], roi_limits['roi_p3_max'], name='roi_p3'),
160
        ]
161

162
    def stoploss_space(self) -> List[Dimension]:
1✔
163
        """
164
        Create a stoploss space.
165

166
        Defines range of stoploss values to search.
167
        You may override it in your custom Hyperopt class.
168
        """
169
        return [
1✔
170
            Real(-0.35, -0.02, name='stoploss'),
171
        ]
172

173
    def generate_trailing_params(self, params: Dict) -> Dict:
1✔
174
        """
175
        Create dict with trailing stop parameters.
176
        """
177
        return {
1✔
178
            'trailing_stop': params['trailing_stop'],
179
            'trailing_stop_positive': params['trailing_stop_positive'],
180
            'trailing_stop_positive_offset': (params['trailing_stop_positive'] +
181
                                              params['trailing_stop_positive_offset_p1']),
182
            'trailing_only_offset_is_reached': params['trailing_only_offset_is_reached'],
183
        }
184

185
    def trailing_space(self) -> List[Dimension]:
1✔
186
        """
187
        Create a trailing stoploss space.
188

189
        You may override it in your custom Hyperopt class.
190
        """
191
        return [
1✔
192
            # It was decided to always set trailing_stop is to True if the 'trailing' hyperspace
193
            # is used. Otherwise hyperopt will vary other parameters that won't have effect if
194
            # trailing_stop is set False.
195
            # This parameter is included into the hyperspace dimensions rather than assigning
196
            # it explicitly in the code in order to have it printed in the results along with
197
            # other 'trailing' hyperspace parameters.
198
            Categorical([True], name='trailing_stop'),
199

200
            Real(0.01, 0.35, name='trailing_stop_positive'),
201

202
            # 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive',
203
            # so this intermediate parameter is used as the value of the difference between
204
            # them. The value of the 'trailing_stop_positive_offset' is constructed in the
205
            # generate_trailing_params() method.
206
            # This is similar to the hyperspace dimensions used for constructing the ROI tables.
207
            Real(0.001, 0.1, name='trailing_stop_positive_offset_p1'),
208

209
            Categorical([True, False], name='trailing_only_offset_is_reached'),
210
        ]
211

212
    # This is needed for proper unpickling the class attribute ticker_interval
213
    # which is set to the actual value by the resolver.
214
    # Why do I still need such shamanic mantras in modern python?
215
    def __getstate__(self):
1✔
216
        state = self.__dict__.copy()
×
217
        state['timeframe'] = self.timeframe
×
218
        return state
×
219

220
    def __setstate__(self, state):
1✔
221
        self.__dict__.update(state)
×
222
        IHyperOpt.ticker_interval = state['timeframe']
×
223
        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

© 2024 Coveralls, Inc