diff --git a/freqtrade/optimize/hyperopt_tools.py b/freqtrade/optimize/hyperopt_tools.py index e2133a956..1e7befdf6 100644 --- a/freqtrade/optimize/hyperopt_tools.py +++ b/freqtrade/optimize/hyperopt_tools.py @@ -23,6 +23,8 @@ logger = logging.getLogger(__name__) NON_OPT_PARAM_APPENDIX = " # value loaded from strategy" +HYPER_PARAMS_FILE_FORMAT = rapidjson.NM_NATIVE | rapidjson.NM_NAN + def hyperopt_serializer(x): if isinstance(x, np.integer): @@ -76,9 +78,18 @@ class HyperoptTools(): with filename.open('w') as f: rapidjson.dump(final_params, f, indent=2, default=hyperopt_serializer, - number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN + number_mode=HYPER_PARAMS_FILE_FORMAT ) + @staticmethod + def load_params(filename: Path) -> Dict: + """ + Load parameters from file + """ + with filename.open('r') as f: + params = rapidjson.load(f, number_mode=HYPER_PARAMS_FILE_FORMAT) + return params + @staticmethod def try_export_params(config: Config, strategy_name: str, params: Dict): if params.get(FTHYPT_FILEVERSION, 1) >= 2 and not config.get('disableparamexport', False): @@ -189,7 +200,7 @@ class HyperoptTools(): for s in ['buy', 'sell', 'protection', 'roi', 'stoploss', 'trailing', 'max_open_trades']: HyperoptTools._params_update_for_json(result_dict, params, non_optimized, s) - print(rapidjson.dumps(result_dict, default=str, number_mode=rapidjson.NM_NATIVE)) + print(rapidjson.dumps(result_dict, default=str, number_mode=HYPER_PARAMS_FILE_FORMAT)) else: HyperoptTools._params_pretty_print(params, 'buy', "Buy hyperspace params:", diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 52ba22951..d38110a2a 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -8,7 +8,7 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union from freqtrade.constants import Config from freqtrade.exceptions import OperationalException -from freqtrade.misc import deep_merge_dicts, json_load +from freqtrade.misc import deep_merge_dicts from freqtrade.optimize.hyperopt_tools import HyperoptTools from freqtrade.strategy.parameters import BaseParameter @@ -124,8 +124,7 @@ class HyperStrategyMixin: if filename.is_file(): logger.info(f"Loading parameters from file {filename}") try: - with filename.open('r') as f: - params = json_load(f) + params = HyperoptTools.load_params(filename) if params.get('strategy_name') != self.__class__.__name__: raise OperationalException('Invalid parameter file provided.') return params diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 7b1399507..713ffa5cb 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -986,7 +986,8 @@ def test_auto_hyperopt_interface_loadparams(default_conf, mocker, caplog): } } } - mocker.patch('freqtrade.strategy.hyper.json_load', return_value=expected_result) + mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params', + return_value=expected_result) PairLocks.timeframe = default_conf['timeframe'] strategy = StrategyResolver.load_strategy(default_conf) assert strategy.stoploss == -0.05 @@ -1005,11 +1006,13 @@ def test_auto_hyperopt_interface_loadparams(default_conf, mocker, caplog): } } - mocker.patch('freqtrade.strategy.hyper.json_load', return_value=expected_result) + mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params', + return_value=expected_result) with pytest.raises(OperationalException, match="Invalid parameter file provided."): StrategyResolver.load_strategy(default_conf) - mocker.patch('freqtrade.strategy.hyper.json_load', MagicMock(side_effect=ValueError())) + mocker.patch('freqtrade.strategy.hyper.HyperoptTools.load_params', + MagicMock(side_effect=ValueError())) StrategyResolver.load_strategy(default_conf) assert log_has("Invalid parameter file format.", caplog)