Add "range" property to IntParameter

This commit is contained in:
Matthias 2021-04-24 07:00:33 +02:00
parent 9dc7f776d9
commit 90476c4287

View File

@ -5,7 +5,7 @@ This module defines a base class for auto-hyperoptable strategies.
import logging import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from contextlib import suppress from contextlib import suppress
from typing import Any, Iterator, Optional, Sequence, Tuple, Union from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, Union
with suppress(ImportError): with suppress(ImportError):
@ -13,6 +13,7 @@ with suppress(ImportError):
from freqtrade.optimize.space import SKDecimal from freqtrade.optimize.space import SKDecimal
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.state import RunMode
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,6 +26,7 @@ class BaseParameter(ABC):
category: Optional[str] category: Optional[str]
default: Any default: Any
value: Any value: Any
hyperopt: bool = False
def __init__(self, *, default: Any, space: Optional[str] = None, def __init__(self, *, default: Any, space: Optional[str] = None,
optimize: bool = True, load: bool = True, **kwargs): optimize: bool = True, load: bool = True, **kwargs):
@ -121,6 +123,20 @@ class IntParameter(NumericParameter):
""" """
return Integer(low=self.low, high=self.high, name=name, **self._space_params) return Integer(low=self.low, high=self.high, name=name, **self._space_params)
@property
def range(self):
"""
Get each value in this space as list.
Returns a List from low to high (inclusive) in Hyperopt mode.
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
calculating 100ds of indicators.
"""
if self.hyperopt:
# Scikit-optimize ranges are "inclusive", while python's "range" is exclusive
return range(self.low, self.high + 1)
else:
return range(self.value, self.value + 1)
class RealParameter(NumericParameter): class RealParameter(NumericParameter):
default: float default: float
@ -227,12 +243,11 @@ class HyperStrategyMixin(object):
strategy logic. strategy logic.
""" """
def __init__(self, *args, **kwargs): def __init__(self, config: Dict[str, Any], *args, **kwargs):
""" """
Initialize hyperoptable strategy mixin. Initialize hyperoptable strategy mixin.
""" """
self._load_params(getattr(self, 'buy_params', None)) self._load_hyper_params(config.get('runmode') == RunMode.HYPEROPT)
self._load_params(getattr(self, 'sell_params', None))
def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]: def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]:
""" """
@ -254,7 +269,14 @@ class HyperStrategyMixin(object):
(attr_name.startswith(category + '_') and attr.category is None)): (attr_name.startswith(category + '_') and attr.category is None)):
yield attr_name, attr yield attr_name, attr
def _load_params(self, params: dict) -> None: def _load_hyper_params(self, hyperopt: bool = False) -> None:
"""
Load Hyperoptable parameters
"""
self._load_params(getattr(self, 'buy_params', None), 'buy', hyperopt)
self._load_params(getattr(self, 'sell_params', None), 'sell', hyperopt)
def _load_params(self, params: dict, space: str, hyperopt: bool = False) -> None:
""" """
Set optimizeable parameter values. Set optimizeable parameter values.
:param params: Dictionary with new parameter values. :param params: Dictionary with new parameter values.
@ -263,6 +285,7 @@ class HyperStrategyMixin(object):
logger.info(f"No params for {space} found, using default values.") logger.info(f"No params for {space} found, using default values.")
for attr_name, attr in self.enumerate_parameters(): for attr_name, attr in self.enumerate_parameters():
attr.hyperopt = hyperopt
if params and attr_name in params: if params and attr_name in params:
if attr.load: if attr.load:
attr.value = params[attr_name] attr.value = params[attr_name]