From 5f67400649d40637177a4a5071e364e94cdea0e6 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 9 Apr 2021 21:58:15 +0200 Subject: [PATCH] Add SKDecimal Space --- freqtrade/optimize/decimalspace.py | 33 ++++++++++++++++++++++++++++++ freqtrade/strategy/hyper.py | 16 ++++----------- 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 freqtrade/optimize/decimalspace.py diff --git a/freqtrade/optimize/decimalspace.py b/freqtrade/optimize/decimalspace.py new file mode 100644 index 000000000..fbc6a7af6 --- /dev/null +++ b/freqtrade/optimize/decimalspace.py @@ -0,0 +1,33 @@ +import numpy as np + +from skopt.space import Integer + + +class SKDecimal(Integer): + + def __init__(self, low, high, decimals=3, prior="uniform", base=10, transform=None, + name=None, dtype=np.int64): + self.decimals = decimals + _low = int(low * pow(10, self.decimals)) + _high = int(high * pow(10, self.decimals)) + self.low_orig = low + self.high_orig = high + + super().__init__(_low, _high, prior, base, transform, name, dtype) + + def __repr__(self): + return "Decimal(low={}, high={}, decimals={}, prior='{}', transform='{}')".format( + self.low_orig, self.high_orig, self.decimals, self.prior, self.transform_) + + def __contains__(self, point): + if isinstance(point, list): + point = np.array(point) + return self.low_orig <= point <= self.high_orig + + def transform(self, Xt): + aa = [int(x * pow(10, self.decimals)) for x in Xt] + return super().transform(aa) + + def inverse_transform(self, Xt): + res = super().inverse_transform(Xt) + return [round(x * pow(0.1, self.decimals), self.decimals) for x in res] diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 709179997..9a7fc4ba4 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -10,6 +10,7 @@ from typing import Any, Iterator, Optional, Sequence, Tuple, Union with suppress(ImportError): from skopt.space import Integer, Real, Categorical + from freqtrade.optimize.decimalspace import SKDecimal from freqtrade.exceptions import OperationalException @@ -168,22 +169,13 @@ class DecimalParameter(RealParameter): super().__init__(low=low, high=high, default=default, space=space, optimize=optimize, load=load, **kwargs) - def get_space(self, name: str) -> 'Integer': + def get_space(self, name: str) -> 'SKDecimal': """ Create skopt optimization space. :param name: A name of parameter field. """ - low = int(self.opt_range[0] * pow(10, self._decimals)) - high = int(self.opt_range[1] * pow(10, self._decimals)) - return Integer(low, high, name=name, **self._space_params) - - def _set_value(self, value: int): - """ - Update current value. Used by hyperopt functions for the purpose where optimization and - value spaces differ. - :param value: An integer value. - """ - self.value = round(value * pow(0.1, self._decimals), self._decimals) + return SKDecimal(*self.opt_range, decimals=self._decimals, name=name, + **self._space_params) class CategoricalParameter(BaseParameter):