Changesets · Branches · Tags · Compare
| Changeset | 3efe3ca15543a09fab8cab977bb05f4e32ab427068772456cad6f122d41a53ef |
|---|---|
| Author | Matthias <xmatthias@outlook.com> · 2021-04-12 19:18 +02:00 |
| Committer | GitHub <noreply@github.com> · 2021-04-12 19:18 +02:00 |
| Parents | 462f899044b1, 25373500f6ee |
5 files changed
added freqtrade/optimize/decimalspace.py +32 −0
| @@ -0,0 +1,32 @@ | ||
| 1 | +import numpy as np | |
| 2 | +from skopt.space import Integer | |
| 3 | + | |
| 4 | + | |
| 5 | +class SKDecimal(Integer): | |
| 6 | + | |
| 7 | + def __init__(self, low, high, decimals=3, prior="uniform", base=10, transform=None, | |
| 8 | + name=None, dtype=np.int64): | |
| 9 | + self.decimals = decimals | |
| 10 | + _low = int(low * pow(10, self.decimals)) | |
| 11 | + _high = int(high * pow(10, self.decimals)) | |
| 12 | + self.low_orig = low | |
| 13 | + self.high_orig = high | |
| 14 | + | |
| 15 | + super().__init__(_low, _high, prior, base, transform, name, dtype) | |
| 16 | + | |
| 17 | + def __repr__(self): | |
| 18 | + return "Decimal(low={}, high={}, decimals={}, prior='{}', transform='{}')".format( | |
| 19 | + self.low_orig, self.high_orig, self.decimals, self.prior, self.transform_) | |
| 20 | + | |
| 21 | + def __contains__(self, point): | |
| 22 | + if isinstance(point, list): | |
| 23 | + point = np.array(point) | |
| 24 | + return self.low_orig <= point <= self.high_orig | |
| 25 | + | |
| 26 | + def transform(self, Xt): | |
| 27 | + aa = [int(x * pow(10, self.decimals)) for x in Xt] | |
| 28 | + return super().transform(aa) | |
| 29 | + | |
| 30 | + def inverse_transform(self, Xt): | |
| 31 | + res = super().inverse_transform(Xt) | |
| 32 | + return [round(x * pow(0.1, self.decimals), self.decimals) for x in res] |
modified freqtrade/optimize/hyperopt_auto.py +2 −2
| @@ -27,7 +27,7 @@ | ||
| 27 | 27 | for attr_name, attr in self.strategy.enumerate_parameters('buy'): |
| 28 | 28 | if attr.optimize: |
| 29 | 29 | # noinspection PyProtectedMember |
| 30 | - attr._set_value(params[attr_name]) | |
| 30 | + attr.value = params[attr_name] | |
| 31 | 31 | return self.strategy.populate_buy_trend(dataframe, metadata) |
| 32 | 32 | |
| 33 | 33 | return populate_buy_trend |
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | for attr_name, attr in self.strategy.enumerate_parameters('sell'): |
| 38 | 38 | if attr.optimize: |
| 39 | 39 | # noinspection PyProtectedMember |
| 40 | - attr._set_value(params[attr_name]) | |
| 40 | + attr.value = params[attr_name] | |
| 41 | 41 | return self.strategy.populate_sell_trend(dataframe, metadata) |
| 42 | 42 | |
| 43 | 43 | return populate_sell_trend |
modified freqtrade/strategy/hyper.py +51 −49
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | |
| 11 | 11 | with suppress(ImportError): |
| 12 | 12 | from skopt.space import Integer, Real, Categorical |
| 13 | + from freqtrade.optimize.decimalspace import SKDecimal | |
| 13 | 14 | |
| 14 | 15 | from freqtrade.exceptions import OperationalException |
| 15 | 16 | |
| @@ -24,9 +25,8 @@ | ||
| 24 | 25 | category: Optional[str] |
| 25 | 26 | default: Any |
| 26 | 27 | value: Any |
| 27 | - opt_range: Sequence[Any] | |
| 28 | 28 | |
| 29 | - def __init__(self, *, opt_range: Sequence[Any], default: Any, space: Optional[str] = None, | |
| 29 | + def __init__(self, *, default: Any, space: Optional[str] = None, | |
| 30 | 30 | optimize: bool = True, load: bool = True, **kwargs): |
| 31 | 31 | """ |
| 32 | 32 | Initialize hyperopt-optimizable parameter. |
| @@ -43,7 +43,6 @@ | ||
| 43 | 43 | self.category = space |
| 44 | 44 | self._space_params = kwargs |
| 45 | 45 | self.value = default |
| 46 | - self.opt_range = opt_range | |
| 47 | 46 | self.optimize = optimize |
| 48 | 47 | self.load = load |
| 49 | 48 | |
| @@ -51,24 +50,51 @@ | ||
| 51 | 50 | return f'{self.__class__.__name__}({self.value})' |
| 52 | 51 | |
| 53 | 52 | @abstractmethod |
| 54 | - def get_space(self, name: str) -> Union['Integer', 'Real', 'Categorical']: | |
| 53 | + def get_space(self, name: str) -> Union['Integer', 'Real', 'SKDecimal', 'Categorical']: | |
| 55 | 54 | """ |
| 56 | 55 | Get-space - will be used by Hyperopt to get the hyperopt Space |
| 57 | 56 | """ |
| 58 | 57 | |
| 59 | - def _set_value(self, value: Any): | |
| 58 | + | |
| 59 | +class NumericParameter(BaseParameter): | |
| 60 | + """ Internal parameter used for Numeric purposes """ | |
| 61 | + float_or_int = Union[int, float] | |
| 62 | + default: float_or_int | |
| 63 | + value: float_or_int | |
| 64 | + | |
| 65 | + def __init__(self, low: Union[float_or_int, Sequence[float_or_int]], | |
| 66 | + high: Optional[float_or_int] = None, *, default: float_or_int, | |
| 67 | + space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs): | |
| 60 | 68 | """ |
| 61 | - Update current value. Used by hyperopt functions for the purpose where optimization and | |
| 62 | - value spaces differ. | |
| 63 | - :param value: A numerical value. | |
| 69 | + Initialize hyperopt-optimizable numeric parameter. | |
| 70 | + Cannot be instantiated, but provides the validation for other numeric parameters | |
| 71 | + :param low: Lower end (inclusive) of optimization space or [low, high]. | |
| 72 | + :param high: Upper end (inclusive) of optimization space. | |
| 73 | + Must be none of entire range is passed first parameter. | |
| 74 | + :param default: A default value. | |
| 75 | + :param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if | |
| 76 | + parameter fieldname is prefixed with 'buy_' or 'sell_'. | |
| 77 | + :param optimize: Include parameter in hyperopt optimizations. | |
| 78 | + :param load: Load parameter value from {space}_params. | |
| 79 | + :param kwargs: Extra parameters to skopt.space.*. | |
| 64 | 80 | """ |
| 65 | - self.value = value | |
| 81 | + if high is not None and isinstance(low, Sequence): | |
| 82 | + raise OperationalException(f'{self.__class__.__name__} space invalid.') | |
| 83 | + if high is None or isinstance(low, Sequence): | |
| 84 | + if not isinstance(low, Sequence) or len(low) != 2: | |
| 85 | + raise OperationalException(f'{self.__class__.__name__} space must be [low, high]') | |
| 86 | + self.low, self.high = low | |
| 87 | + else: | |
| 88 | + self.low = low | |
| 89 | + self.high = high | |
| 90 | + | |
| 91 | + super().__init__(default=default, space=space, optimize=optimize, | |
| 92 | + load=load, **kwargs) | |
| 66 | 93 | |
| 67 | 94 | |
| 68 | -class IntParameter(BaseParameter): | |
| 95 | +class IntParameter(NumericParameter): | |
| 69 | 96 | default: int |
| 70 | 97 | value: int |
| 71 | - opt_range: Sequence[int] | |
| 72 | 98 | |
| 73 | 99 | def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int, |
| 74 | 100 | space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs): |
| @@ -84,15 +110,8 @@ | ||
| 84 | 110 | :param load: Load parameter value from {space}_params. |
| 85 | 111 | :param kwargs: Extra parameters to skopt.space.Integer. |
| 86 | 112 | """ |
| 87 | - if high is not None and isinstance(low, Sequence): | |
| 88 | - raise OperationalException('IntParameter space invalid.') | |
| 89 | - if high is None or isinstance(low, Sequence): | |
| 90 | - if not isinstance(low, Sequence) or len(low) != 2: | |
| 91 | - raise OperationalException('IntParameter space must be [low, high]') | |
| 92 | - opt_range = low | |
| 93 | - else: | |
| 94 | - opt_range = [low, high] | |
| 95 | - super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize, | |
| 113 | + | |
| 114 | + super().__init__(low=low, high=high, default=default, space=space, optimize=optimize, | |
| 96 | 115 | load=load, **kwargs) |
| 97 | 116 | |
| 98 | 117 | def get_space(self, name: str) -> 'Integer': |
| @@ -100,13 +119,12 @@ | ||
| 100 | 119 | Create skopt optimization space. |
| 101 | 120 | :param name: A name of parameter field. |
| 102 | 121 | """ |
| 103 | - return Integer(*self.opt_range, name=name, **self._space_params) | |
| 122 | + return Integer(low=self.low, high=self.high, name=name, **self._space_params) | |
| 104 | 123 | |
| 105 | 124 | |
| 106 | -class RealParameter(BaseParameter): | |
| 125 | +class RealParameter(NumericParameter): | |
| 107 | 126 | default: float |
| 108 | 127 | value: float |
| 109 | - opt_range: Sequence[float] | |
| 110 | 128 | |
| 111 | 129 | def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *, |
| 112 | 130 | default: float, space: Optional[str] = None, optimize: bool = True, |
| @@ -123,15 +141,7 @@ | ||
| 123 | 141 | :param load: Load parameter value from {space}_params. |
| 124 | 142 | :param kwargs: Extra parameters to skopt.space.Real. |
| 125 | 143 | """ |
| 126 | - if high is not None and isinstance(low, Sequence): | |
| 127 | - raise OperationalException(f'{self.__class__.__name__} space invalid.') | |
| 128 | - if high is None or isinstance(low, Sequence): | |
| 129 | - if not isinstance(low, Sequence) or len(low) != 2: | |
| 130 | - raise OperationalException(f'{self.__class__.__name__} space must be [low, high]') | |
| 131 | - opt_range = low | |
| 132 | - else: | |
| 133 | - opt_range = [low, high] | |
| 134 | - super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize, | |
| 144 | + super().__init__(low=low, high=high, default=default, space=space, optimize=optimize, | |
| 135 | 145 | load=load, **kwargs) |
| 136 | 146 | |
| 137 | 147 | def get_space(self, name: str) -> 'Real': |
| @@ -139,13 +149,12 @@ | ||
| 139 | 149 | Create skopt optimization space. |
| 140 | 150 | :param name: A name of parameter field. |
| 141 | 151 | """ |
| 142 | - return Real(*self.opt_range, name=name, **self._space_params) | |
| 152 | + return Real(low=self.low, high=self.high, name=name, **self._space_params) | |
| 143 | 153 | |
| 144 | 154 | |
| 145 | -class DecimalParameter(RealParameter): | |
| 155 | +class DecimalParameter(NumericParameter): | |
| 146 | 156 | default: float |
| 147 | 157 | value: float |
| 148 | - opt_range: Sequence[float] | |
| 149 | 158 | |
| 150 | 159 | def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *, |
| 151 | 160 | default: float, decimals: int = 3, space: Optional[str] = None, |
| @@ -161,29 +170,21 @@ | ||
| 161 | 170 | parameter fieldname is prefixed with 'buy_' or 'sell_'. |
| 162 | 171 | :param optimize: Include parameter in hyperopt optimizations. |
| 163 | 172 | :param load: Load parameter value from {space}_params. |
| 164 | - :param kwargs: Extra parameters to skopt.space.Real. | |
| 173 | + :param kwargs: Extra parameters to skopt.space.Integer. | |
| 165 | 174 | """ |
| 166 | 175 | self._decimals = decimals |
| 167 | 176 | default = round(default, self._decimals) |
| 177 | + | |
| 168 | 178 | super().__init__(low=low, high=high, default=default, space=space, optimize=optimize, |
| 169 | 179 | load=load, **kwargs) |
| 170 | 180 | |
| 171 | - def get_space(self, name: str) -> 'Integer': | |
| 181 | + def get_space(self, name: str) -> 'SKDecimal': | |
| 172 | 182 | """ |
| 173 | 183 | Create skopt optimization space. |
| 174 | 184 | :param name: A name of parameter field. |
| 175 | 185 | """ |
| 176 | - low = int(self.opt_range[0] * pow(10, self._decimals)) | |
| 177 | - high = int(self.opt_range[1] * pow(10, self._decimals)) | |
| 178 | - return Integer(low, high, name=name, **self._space_params) | |
| 179 | - | |
| 180 | - def _set_value(self, value: int): | |
| 181 | - """ | |
| 182 | - Update current value. Used by hyperopt functions for the purpose where optimization and | |
| 183 | - value spaces differ. | |
| 184 | - :param value: An integer value. | |
| 185 | - """ | |
| 186 | - self.value = round(value * pow(0.1, self._decimals), self._decimals) | |
| 186 | + return SKDecimal(low=self.low, high=self.high, decimals=self._decimals, name=name, | |
| 187 | + **self._space_params) | |
| 187 | 188 | |
| 188 | 189 | |
| 189 | 190 | class CategoricalParameter(BaseParameter): |
| @@ -208,7 +209,8 @@ | ||
| 208 | 209 | if len(categories) < 2: |
| 209 | 210 | raise OperationalException( |
| 210 | 211 | 'CategoricalParameter space must be [a, b, ...] (at least two parameters)') |
| 211 | - super().__init__(opt_range=categories, default=default, space=space, optimize=optimize, | |
| 212 | + self.opt_range = categories | |
| 213 | + super().__init__(default=default, space=space, optimize=optimize, | |
| 212 | 214 | load=load, **kwargs) |
| 213 | 215 | |
| 214 | 216 | def get_space(self, name: str) -> 'Categorical': |
modified tests/optimize/test_hyperopt.py +18 −0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_hyperopt |
| 16 | 16 | from freqtrade.data.history import load_data |
| 17 | 17 | from freqtrade.exceptions import OperationalException |
| 18 | +from freqtrade.optimize.decimalspace import SKDecimal | |
| 18 | 19 | from freqtrade.optimize.hyperopt import Hyperopt |
| 19 | 20 | from freqtrade.optimize.hyperopt_auto import HyperOptAuto |
| 20 | 21 | from freqtrade.optimize.hyperopt_tools import HyperoptTools |
| @@ -1104,3 +1105,20 @@ | ||
| 1104 | 1105 | assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) |
| 1105 | 1106 | |
| 1106 | 1107 | hyperopt.start() |
| 1108 | + | |
| 1109 | + | |
| 1110 | +def test_SKDecimal(): | |
| 1111 | + space = SKDecimal(1, 2, decimals=2) | |
| 1112 | + assert 1.5 in space | |
| 1113 | + assert 2.5 not in space | |
| 1114 | + assert space.low == 100 | |
| 1115 | + assert space.high == 200 | |
| 1116 | + | |
| 1117 | + assert space.inverse_transform([200]) == [2.0] | |
| 1118 | + assert space.inverse_transform([100]) == [1.0] | |
| 1119 | + assert space.inverse_transform([150, 160]) == [1.5, 1.6] | |
| 1120 | + | |
| 1121 | + assert space.transform([1.5]) == [150] | |
| 1122 | + assert space.transform([2.0]) == [200] | |
| 1123 | + assert space.transform([1.0]) == [100] | |
| 1124 | + assert space.transform([1.5, 1.6]) == [150, 160] |
modified tests/strategy/test_interface.py +0 −2
| @@ -596,8 +596,6 @@ | ||
| 596 | 596 | fltpar = DecimalParameter(low=0.0, high=5.5, default=1.0004, decimals=3, space='buy') |
| 597 | 597 | assert isinstance(fltpar.get_space(''), Integer) |
| 598 | 598 | assert fltpar.value == 1 |
| 599 | - fltpar._set_value(2222) | |
| 600 | - assert fltpar.value == 2.222 | |
| 601 | 599 | |
| 602 | 600 | catpar = CategoricalParameter(['buy_rsi', 'buy_macd', 'buy_none'], |
| 603 | 601 | default='buy_macd', space='buy') |