Extract hyperopt_defaults_serializer to hyperopt_tools

This commit is contained in:
Matthias 2021-06-29 20:39:07 +02:00
parent 55f032b18e
commit 84703080b8
2 changed files with 14 additions and 7 deletions

View File

@ -29,7 +29,7 @@ from freqtrade.optimize.backtesting import Backtesting
from freqtrade.optimize.hyperopt_auto import HyperOptAuto from freqtrade.optimize.hyperopt_auto import HyperOptAuto
from freqtrade.optimize.hyperopt_interface import IHyperOpt # noqa: F401 from freqtrade.optimize.hyperopt_interface import IHyperOpt # noqa: F401
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss # noqa: F401 from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss # noqa: F401
from freqtrade.optimize.hyperopt_tools import HyperoptTools from freqtrade.optimize.hyperopt_tools import HyperoptTools, hyperopt_parser
from freqtrade.optimize.optimize_reports import generate_strategy_stats from freqtrade.optimize.optimize_reports import generate_strategy_stats
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver
@ -163,13 +163,9 @@ class Hyperopt:
While not a valid json object - this allows appending easily. While not a valid json object - this allows appending easily.
:param epoch: result dictionary for this epoch. :param epoch: result dictionary for this epoch.
""" """
def default_parser(x):
if isinstance(x, np.integer):
return int(x)
return str(x)
epoch[FTHYPT_FILEVERSION] = 2 epoch[FTHYPT_FILEVERSION] = 2
with self.results_file.open('a') as f: with self.results_file.open('a') as f:
rapidjson.dump(epoch, f, default=default_parser, rapidjson.dump(epoch, f, default=hyperopt_parser,
number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN) number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN)
f.write("\n") f.write("\n")

View File

@ -2,9 +2,11 @@
import io import io
import logging import logging
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
import numpy as np
import rapidjson import rapidjson
import tabulate import tabulate
from colorama import Fore, Style from colorama import Fore, Style
@ -20,6 +22,12 @@ logger = logging.getLogger(__name__)
NON_OPT_PARAM_APPENDIX = " # value loaded from strategy" NON_OPT_PARAM_APPENDIX = " # value loaded from strategy"
def hyperopt_parser(x):
if isinstance(x, np.integer):
return int(x)
return str(x)
class HyperoptTools(): class HyperoptTools():
@staticmethod @staticmethod
@ -48,9 +56,12 @@ class HyperoptTools():
'strategy_name': strategy_name, 'strategy_name': strategy_name,
'params': final_params, 'params': final_params,
'ft_stratparam_v': 1, 'ft_stratparam_v': 1,
'export_time': datetime.now(timezone.utc),
} }
logger.info(f"Dumping parameters to {filename}") logger.info(f"Dumping parameters to {filename}")
rapidjson.dump(final_params, filename.open('w'), indent=2) rapidjson.dump(final_params, filename.open('w'), indent=2,
default=hyperopt_parser, number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN
)
@staticmethod @staticmethod
def try_export_params(config: Dict[str, Any], strategy_name: str, val: Dict): def try_export_params(config: Dict[str, Any], strategy_name: str, val: Dict):