Skip to content

Commit

Permalink
Extract hyperopt_defaults_serializer to hyperopt_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Jun 29, 2021
1 parent 55f032b commit 8470308
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 2 additions & 6 deletions freqtrade/optimize/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from freqtrade.optimize.hyperopt_auto import HyperOptAuto
from freqtrade.optimize.hyperopt_interface import IHyperOpt # 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.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver

Expand Down Expand Up @@ -163,13 +163,9 @@ def _save_result(self, epoch: Dict) -> None:
While not a valid json object - this allows appending easily.
: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
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)
f.write("\n")

Expand Down
13 changes: 12 additions & 1 deletion freqtrade/optimize/hyperopt_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import io
import logging
from copy import deepcopy
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional

import numpy as np
import rapidjson
import tabulate
from colorama import Fore, Style
Expand All @@ -20,6 +22,12 @@
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():

@staticmethod
Expand Down Expand Up @@ -48,9 +56,12 @@ def export_params(params, strategy_name: str, filename: Path):
'strategy_name': strategy_name,
'params': final_params,
'ft_stratparam_v': 1,
'export_time': datetime.now(timezone.utc),
}
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
def try_export_params(config: Dict[str, Any], strategy_name: str, val: Dict):
Expand Down

0 comments on commit 8470308

Please sign in to comment.