From dcf53ac3ff645de6166c43a84e68b9ce5910a5db Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 30 Jun 2021 06:33:40 +0200 Subject: [PATCH] Add test for try_eport_params --- freqtrade/optimize/hyperopt_tools.py | 6 ++-- tests/optimize/test_hyperopttools.py | 47 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/freqtrade/optimize/hyperopt_tools.py b/freqtrade/optimize/hyperopt_tools.py index 7558232f1..7a0b00d01 100755 --- a/freqtrade/optimize/hyperopt_tools.py +++ b/freqtrade/optimize/hyperopt_tools.py @@ -64,12 +64,12 @@ class HyperoptTools(): ) @staticmethod - def try_export_params(config: Dict[str, Any], strategy_name: str, val: Dict): - if val.get(FTHYPT_FILEVERSION, 1) >= 2 and not config.get('disableparamexport', False): + def try_export_params(config: Dict[str, Any], strategy_name: str, params: Dict): + if params.get(FTHYPT_FILEVERSION, 1) >= 2 and not config.get('disableparamexport', False): # Export parameters ... fn = HyperoptTools.get_strategy_filename(config, strategy_name) if fn: - HyperoptTools.export_params(val, strategy_name, fn.with_suffix('.json')) + HyperoptTools.export_params(params, strategy_name, fn.with_suffix('.json')) else: logger.warn("Strategy not found, not exporting parameter file.") diff --git a/tests/optimize/test_hyperopttools.py b/tests/optimize/test_hyperopttools.py index 54e968143..6beb2788a 100644 --- a/tests/optimize/test_hyperopttools.py +++ b/tests/optimize/test_hyperopttools.py @@ -6,6 +6,7 @@ from typing import Dict, List import pytest import rapidjson +from freqtrade.constants import FTHYPT_FILEVERSION from freqtrade.exceptions import OperationalException from freqtrade.optimize.hyperopt_tools import HyperoptTools from tests.conftest import log_has, log_has_re @@ -201,6 +202,52 @@ def test_export_params(tmpdir): assert "trailing" in content["params"] +def test_try_export_params(default_conf, tmpdir, caplog, mocker): + default_conf['disableparamexport'] = False + export_mock = mocker.patch("freqtrade.optimize.hyperopt_tools.HyperoptTools.export_params") + + filename = Path(tmpdir) / "DefaultStrategy.json" + assert not filename.is_file() + params = { + "params_details": { + "buy": { + "buy_rsi": 30 + }, + "sell": { + "sell_rsi": 70 + }, + "roi": { + "0": 0.528, + "346": 0.08499, + "507": 0.049, + "1595": 0 + } + }, + "params_not_optimized": { + "stoploss": -0.05, + "trailing": { + "trailing_stop": False, + "trailing_stop_positive": 0.05, + "trailing_stop_positive_offset": 0.1, + "trailing_only_offset_is_reached": True + }, + }, + FTHYPT_FILEVERSION: 2, + + } + HyperoptTools.try_export_params(default_conf, "DefaultStrategy22", params) + + assert log_has("Strategy not found, not exporting parameter file.", caplog) + assert export_mock.call_count == 0 + caplog.clear() + + HyperoptTools.try_export_params(default_conf, "DefaultStrategy", params) + + assert export_mock.call_count == 1 + assert export_mock.call_args_list[0][0][1] == 'DefaultStrategy' + assert export_mock.call_args_list[0][0][2].name == 'default_strategy.json' + + def test_params_print(capsys): params = {