freqtrade_origin/tests/optimize/test_hyperopt_tools.py

473 lines
15 KiB
Python
Raw Normal View History

2021-06-14 19:06:15 +00:00
import logging
import re
from pathlib import Path
2021-07-03 13:45:00 +00:00
import numpy as np
2021-06-14 19:06:15 +00:00
import pytest
2021-06-15 04:52:12 +00:00
import rapidjson
2021-06-14 19:06:15 +00:00
2021-06-30 04:33:40 +00:00
from freqtrade.constants import FTHYPT_FILEVERSION
2021-06-14 19:06:15 +00:00
from freqtrade.exceptions import OperationalException
2021-07-03 06:38:55 +00:00
from freqtrade.optimize.hyperopt_tools import HyperoptTools, hyperopt_serializer
2022-01-22 16:25:21 +00:00
from tests.conftest import CURRENT_TEST_STRATEGY, log_has, log_has_re
2021-06-14 19:06:15 +00:00
# Functions for recurrent object patching
def create_results() -> list[dict]:
2024-05-12 14:04:55 +00:00
return [{"loss": 1, "result": "foo", "params": {}, "is_best": True}]
2021-06-14 19:06:15 +00:00
2023-11-05 15:23:22 +00:00
def test_save_results_saves_epochs(hyperopt, tmp_path, caplog) -> None:
2024-05-12 14:04:55 +00:00
hyperopt.results_file = tmp_path / "ut_results.fthypt"
hyperopt_epochs = HyperoptTools.load_filtered_results(hyperopt.results_file, {})
assert log_has_re("Hyperopt file .* not found.", caplog)
assert hyperopt_epochs == ([], 0)
2021-06-14 19:06:15 +00:00
# Test writing to temp dir and reading again
epochs = create_results()
caplog.set_level(logging.DEBUG)
for epoch in epochs:
hyperopt._save_result(epoch)
assert log_has(f"1 epoch saved to '{hyperopt.results_file}'.", caplog)
hyperopt._save_result(epochs[0])
assert log_has(f"2 epochs saved to '{hyperopt.results_file}'.", caplog)
hyperopt_epochs = HyperoptTools.load_filtered_results(hyperopt.results_file, {})
2021-06-14 19:06:15 +00:00
assert len(hyperopt_epochs) == 2
assert hyperopt_epochs[1] == 2
assert len(hyperopt_epochs[0]) == 2
result_gen = HyperoptTools._read_results(hyperopt.results_file, 1)
epoch = next(result_gen)
assert len(epoch) == 1
assert epoch[0] == epochs[0]
epoch = next(result_gen)
assert len(epoch) == 1
epoch = next(result_gen)
assert len(epoch) == 0
with pytest.raises(StopIteration):
next(result_gen)
2021-06-14 19:06:15 +00:00
def test_load_previous_results2(mocker, testdatadir, caplog) -> None:
2024-05-12 14:04:55 +00:00
results_file = testdatadir / "hyperopt_results_SampleStrategy.pickle"
with pytest.raises(
OperationalException, match=r"Legacy hyperopt results are no longer supported.*"
):
HyperoptTools.load_filtered_results(results_file, {})
2021-06-14 19:06:15 +00:00
2024-05-12 14:04:55 +00:00
@pytest.mark.parametrize(
"spaces, expected_results",
[
(
["buy"],
{
"buy": True,
"sell": False,
"roi": False,
"stoploss": False,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["sell"],
{
"buy": False,
"sell": True,
"roi": False,
"stoploss": False,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["roi"],
{
"buy": False,
"sell": False,
"roi": True,
"stoploss": False,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["stoploss"],
{
"buy": False,
"sell": False,
"roi": False,
"stoploss": True,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["trailing"],
{
"buy": False,
"sell": False,
"roi": False,
"stoploss": False,
"trailing": True,
"protection": False,
"trades": False,
},
),
(
["buy", "sell", "roi", "stoploss"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["buy", "sell", "roi", "stoploss", "trailing"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": True,
"protection": False,
"trades": False,
},
),
(
["buy", "roi"],
{
"buy": True,
"sell": False,
"roi": True,
"stoploss": False,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["all"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": True,
"protection": True,
"trades": True,
},
),
(
["default"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["default", "trailing"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": True,
"protection": False,
"trades": False,
},
),
(
["all", "buy"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": True,
"protection": True,
"trades": True,
},
),
(
["default", "buy"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": False,
"protection": False,
"trades": False,
},
),
(
["all"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": True,
"protection": True,
"trades": True,
},
),
(
["protection"],
{
"buy": False,
"sell": False,
"roi": False,
"stoploss": False,
"trailing": False,
"protection": True,
"trades": False,
},
),
(
["trades"],
{
"buy": False,
"sell": False,
"roi": False,
"stoploss": False,
"trailing": False,
"protection": False,
"trades": True,
},
),
(
["default", "trades"],
{
"buy": True,
"sell": True,
"roi": True,
"stoploss": True,
"trailing": False,
"protection": False,
"trades": True,
},
),
],
)
2021-06-14 19:06:15 +00:00
def test_has_space(hyperopt_conf, spaces, expected_results):
2024-05-12 14:04:55 +00:00
for s in ["buy", "sell", "roi", "stoploss", "trailing", "protection", "trades"]:
hyperopt_conf.update({"spaces": spaces})
2021-06-14 19:06:15 +00:00
assert HyperoptTools.has_space(hyperopt_conf, s) == expected_results[s]
def test_show_epoch_details(capsys):
test_result = {
2024-05-12 14:04:55 +00:00
"params_details": {
"trailing": {
"trailing_stop": True,
"trailing_stop_positive": 0.02,
"trailing_stop_positive_offset": 0.04,
"trailing_only_offset_is_reached": True,
2021-06-14 19:06:15 +00:00
},
2024-05-12 14:04:55 +00:00
"roi": {0: 0.18, 90: 0.14, 225: 0.05, 430: 0},
2021-06-14 19:06:15 +00:00
},
2024-05-12 14:04:55 +00:00
"results_explanation": "foo result",
"is_initial_point": False,
"total_profit": 0,
"current_epoch": 2, # This starts from 1 (in a human-friendly manner)
"is_best": True,
2021-06-14 19:06:15 +00:00
}
HyperoptTools.show_epoch_details(test_result, 5, False, no_header=True)
captured = capsys.readouterr()
2024-05-12 14:04:55 +00:00
assert "# Trailing stop:" in captured.out
2021-06-14 19:06:15 +00:00
# re.match(r"Pairs for .*", captured.out)
2024-05-12 14:04:55 +00:00
assert re.search(r"^\s+trailing_stop = True$", captured.out, re.MULTILINE)
assert re.search(r"^\s+trailing_stop_positive = 0.02$", captured.out, re.MULTILINE)
assert re.search(r"^\s+trailing_stop_positive_offset = 0.04$", captured.out, re.MULTILINE)
assert re.search(r"^\s+trailing_only_offset_is_reached = True$", captured.out, re.MULTILINE)
2021-06-14 19:06:15 +00:00
2024-05-12 14:04:55 +00:00
assert "# ROI table:" in captured.out
assert re.search(r"^\s+minimal_roi = \{$", captured.out, re.MULTILINE)
assert re.search(r"^\s+\"90\"\:\s0.14,\s*$", captured.out, re.MULTILINE)
2021-06-14 19:06:15 +00:00
2021-06-15 18:33:35 +00:00
def test__pprint_dict():
2024-05-12 14:04:55 +00:00
params = {"buy_std": 1.2, "buy_rsi": 31, "buy_enable": True, "buy_what": "asdf"}
non_params = {"buy_notoptimied": 55}
2021-06-14 19:06:15 +00:00
2021-06-15 18:33:35 +00:00
x = HyperoptTools._pprint_dict(params, non_params)
2024-05-12 14:04:55 +00:00
assert (
x
== """{
2021-06-14 19:06:15 +00:00
"buy_std": 1.2,
"buy_rsi": 31,
"buy_enable": True,
"buy_what": "asdf",
"buy_notoptimied": 55, # value loaded from strategy
}"""
2024-05-12 14:04:55 +00:00
)
2021-06-15 04:52:12 +00:00
def test_get_strategy_filename(default_conf, tmp_path):
2024-05-12 14:04:55 +00:00
default_conf["user_data_dir"] = tmp_path
x = HyperoptTools.get_strategy_filename(default_conf, "StrategyTestV3")
2021-06-15 04:52:12 +00:00
assert isinstance(x, Path)
2024-05-12 14:04:55 +00:00
assert x == Path(__file__).parents[1] / "strategy/strats/strategy_test_v3.py"
2021-06-15 04:52:12 +00:00
2024-05-12 14:04:55 +00:00
x = HyperoptTools.get_strategy_filename(default_conf, "NonExistingStrategy")
2021-06-15 04:52:12 +00:00
assert x is None
2023-11-05 15:23:22 +00:00
def test_export_params(tmp_path):
filename = tmp_path / f"{CURRENT_TEST_STRATEGY}.json"
2021-06-15 04:52:12 +00:00
assert not filename.is_file()
params = {
"params_details": {
2024-05-12 14:04:55 +00:00
"buy": {"buy_rsi": 30},
"sell": {"sell_rsi": 70},
"roi": {"0": 0.528, "346": 0.08499, "507": 0.049, "1595": 0},
"max_open_trades": {"max_open_trades": 5},
2021-06-15 04:52:12 +00:00
},
"params_not_optimized": {
"stoploss": -0.05,
"trailing": {
"trailing_stop": False,
"trailing_stop_positive": 0.05,
"trailing_stop_positive_offset": 0.1,
2024-05-12 14:04:55 +00:00
"trailing_only_offset_is_reached": True,
2021-06-15 04:52:12 +00:00
},
2024-05-12 14:04:55 +00:00
},
2021-06-15 04:52:12 +00:00
}
HyperoptTools.export_params(params, CURRENT_TEST_STRATEGY, filename)
2021-06-15 04:52:12 +00:00
assert filename.is_file()
2024-05-12 14:04:55 +00:00
with filename.open("r") as f:
content = rapidjson.load(f)
2024-05-12 14:04:55 +00:00
assert content["strategy_name"] == CURRENT_TEST_STRATEGY
assert "params" in content
2021-06-15 04:52:12 +00:00
assert "buy" in content["params"]
assert "sell" in content["params"]
assert "roi" in content["params"]
assert "stoploss" in content["params"]
assert "trailing" in content["params"]
assert "max_open_trades" in content["params"]
2023-11-05 15:23:22 +00:00
def test_try_export_params(default_conf, tmp_path, caplog, mocker):
2024-05-12 14:04:55 +00:00
default_conf["disableparamexport"] = False
default_conf["user_data_dir"] = tmp_path
2021-06-30 04:33:40 +00:00
export_mock = mocker.patch("freqtrade.optimize.hyperopt_tools.HyperoptTools.export_params")
2023-11-05 15:23:22 +00:00
filename = tmp_path / f"{CURRENT_TEST_STRATEGY}.json"
2021-06-30 04:33:40 +00:00
assert not filename.is_file()
params = {
"params_details": {
2024-05-12 14:04:55 +00:00
"buy": {"buy_rsi": 30},
"sell": {"sell_rsi": 70},
"roi": {"0": 0.528, "346": 0.08499, "507": 0.049, "1595": 0},
2021-06-30 04:33:40 +00:00
},
"params_not_optimized": {
"stoploss": -0.05,
"trailing": {
"trailing_stop": False,
"trailing_stop_positive": 0.05,
"trailing_stop_positive_offset": 0.1,
2024-05-12 14:04:55 +00:00
"trailing_only_offset_is_reached": True,
2021-06-30 04:33:40 +00:00
},
},
FTHYPT_FILEVERSION: 2,
}
HyperoptTools.try_export_params(default_conf, "StrategyTestVXXX", params)
2021-06-30 04:33:40 +00:00
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, CURRENT_TEST_STRATEGY, params)
2021-06-30 04:33:40 +00:00
assert export_mock.call_count == 1
assert export_mock.call_args_list[0][0][1] == CURRENT_TEST_STRATEGY
2024-05-12 14:04:55 +00:00
assert export_mock.call_args_list[0][0][2].name == "strategy_test_v3.json"
2021-06-30 04:33:40 +00:00
def test_params_print(capsys):
params = {
2024-05-12 14:04:55 +00:00
"buy": {"buy_rsi": 30},
"sell": {"sell_rsi": 70},
}
non_optimized = {
2024-05-12 14:04:55 +00:00
"buy": {"buy_adx": 44},
"sell": {"sell_adx": 65},
"stoploss": {
"stoploss": -0.05,
},
"roi": {
"0": 0.05,
"20": 0.01,
},
"trailing": {
"trailing_stop": False,
"trailing_stop_positive": 0.05,
"trailing_stop_positive_offset": 0.1,
2024-05-12 14:04:55 +00:00
"trailing_only_offset_is_reached": True,
},
2024-05-12 14:04:55 +00:00
"max_open_trades": {"max_open_trades": 5},
}
2024-05-12 14:04:55 +00:00
HyperoptTools._params_pretty_print(params, "buy", "No header", non_optimized)
captured = capsys.readouterr()
assert re.search("# No header", captured.out)
assert re.search('"buy_rsi": 30,\n', captured.out)
assert re.search('"buy_adx": 44, # value loaded.*\n', captured.out)
assert not re.search("sell", captured.out)
2024-05-12 14:04:55 +00:00
HyperoptTools._params_pretty_print(params, "sell", "Sell Header", non_optimized)
captured = capsys.readouterr()
assert re.search("# Sell Header", captured.out)
assert re.search('"sell_rsi": 70,\n', captured.out)
assert re.search('"sell_adx": 65, # value loaded.*\n', captured.out)
2024-05-12 14:04:55 +00:00
HyperoptTools._params_pretty_print(params, "roi", "ROI Table:", non_optimized)
captured = capsys.readouterr()
assert re.search("# ROI Table: # value loaded.*\n", captured.out)
2024-05-12 14:04:55 +00:00
assert re.search("minimal_roi = {\n", captured.out)
assert re.search('"20": 0.01\n', captured.out)
2024-05-12 14:04:55 +00:00
HyperoptTools._params_pretty_print(params, "trailing", "Trailing stop:", non_optimized)
captured = capsys.readouterr()
assert re.search("# Trailing stop:", captured.out)
2024-05-12 14:04:55 +00:00
assert re.search("trailing_stop = False # value loaded.*\n", captured.out)
assert re.search("trailing_stop_positive = 0.05 # value loaded.*\n", captured.out)
assert re.search("trailing_stop_positive_offset = 0.1 # value loaded.*\n", captured.out)
assert re.search("trailing_only_offset_is_reached = True # value loaded.*\n", captured.out)
2021-07-03 06:38:55 +00:00
2024-05-12 14:04:55 +00:00
HyperoptTools._params_pretty_print(params, "max_open_trades", "Max Open Trades:", non_optimized)
captured = capsys.readouterr()
assert re.search("# Max Open Trades:", captured.out)
2024-05-12 14:04:55 +00:00
assert re.search("max_open_trades = 5 # value loaded.*\n", captured.out)
2021-07-03 06:38:55 +00:00
def test_hyperopt_serializer():
assert isinstance(hyperopt_serializer(np.int_(5)), int)
assert isinstance(hyperopt_serializer(np.bool_(True)), bool)
assert isinstance(hyperopt_serializer(np.bool_(False)), bool)