2017-12-26 08:08:10 +00:00
|
|
|
# pragma pylint: disable=missing-docstring,W0212,C0103
|
2019-11-06 18:33:15 +00:00
|
|
|
import locale
|
2020-04-28 19:56:19 +00:00
|
|
|
import logging
|
2020-10-05 06:07:53 +00:00
|
|
|
import re
|
2019-04-26 17:51:24 +00:00
|
|
|
from datetime import datetime
|
2019-09-08 07:54:15 +00:00
|
|
|
from pathlib import Path
|
2020-02-10 09:35:48 +00:00
|
|
|
from typing import Dict, List
|
2021-04-29 18:17:13 +00:00
|
|
|
from unittest.mock import ANY, MagicMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-02-08 19:49:43 +00:00
|
|
|
import pandas as pd
|
2018-05-02 20:49:55 +00:00
|
|
|
import pytest
|
2019-07-15 19:54:41 +00:00
|
|
|
from arrow import Arrow
|
|
|
|
from filelock import Timeout
|
2018-03-15 22:37:34 +00:00
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_hyperopt
|
2019-12-27 09:25:38 +00:00
|
|
|
from freqtrade.data.history import load_data
|
2021-02-12 19:20:32 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-07-21 14:07:06 +00:00
|
|
|
from freqtrade.optimize.hyperopt import Hyperopt
|
2021-03-29 17:27:19 +00:00
|
|
|
from freqtrade.optimize.hyperopt_auto import HyperOptAuto
|
2021-03-17 19:43:51 +00:00
|
|
|
from freqtrade.optimize.hyperopt_tools import HyperoptTools
|
2021-04-29 18:17:13 +00:00
|
|
|
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
2021-04-14 18:32:34 +00:00
|
|
|
from freqtrade.optimize.space import SKDecimal
|
2020-10-28 13:36:19 +00:00
|
|
|
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
|
2019-04-22 18:24:45 +00:00
|
|
|
from freqtrade.state import RunMode
|
2021-04-24 05:18:35 +00:00
|
|
|
from freqtrade.strategy.hyper import IntParameter
|
2021-04-29 18:17:13 +00:00
|
|
|
from freqtrade.strategy.interface import SellType
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
|
|
|
|
patched_configuration_load_config_file)
|
2018-01-07 11:54:00 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
from .hyperopts.default_hyperopt import DefaultHyperOpt
|
|
|
|
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
# Functions for recurrent object patching
|
2020-04-28 19:56:19 +00:00
|
|
|
def create_results(mocker, hyperopt, testdatadir) -> List[Dict]:
|
2018-01-09 09:37:27 +00:00
|
|
|
"""
|
2020-04-28 19:56:19 +00:00
|
|
|
When creating results, mock the hyperopt so that *by default*
|
2018-01-09 09:37:27 +00:00
|
|
|
- we don't create any pickle'd files in the filesystem
|
|
|
|
- we might have a pickle'd file so make sure that we return
|
|
|
|
false when looking for it
|
|
|
|
"""
|
2020-04-28 19:56:19 +00:00
|
|
|
hyperopt.results_file = testdatadir / 'optimize/ut_results.pickle'
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2019-07-21 13:56:44 +00:00
|
|
|
mocker.patch.object(Path, "is_file", MagicMock(return_value=False))
|
|
|
|
stat_mock = MagicMock()
|
2020-09-27 22:35:19 +00:00
|
|
|
stat_mock.st_size = 1
|
|
|
|
mocker.patch.object(Path, "stat", MagicMock(return_value=stat_mock))
|
2019-07-21 13:56:44 +00:00
|
|
|
|
|
|
|
mocker.patch.object(Path, "unlink", MagicMock(return_value=True))
|
2018-07-03 19:51:48 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2020-09-27 22:35:19 +00:00
|
|
|
return [{'loss': 1, 'result': 'foo', 'params': {}, 'is_best': True}]
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2019-04-22 18:24:45 +00:00
|
|
|
def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
2019-07-14 22:44:25 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
args = [
|
2019-09-16 04:44:07 +00:00
|
|
|
'hyperopt',
|
2019-04-22 18:24:45 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2019-04-22 18:24:45 +00:00
|
|
|
]
|
|
|
|
|
2020-01-26 12:33:13 +00:00
|
|
|
config = setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
|
2019-04-22 18:24:45 +00:00
|
|
|
assert 'max_open_trades' in config
|
|
|
|
assert 'stake_currency' in config
|
|
|
|
assert 'stake_amount' in config
|
|
|
|
assert 'exchange' in config
|
|
|
|
assert 'pair_whitelist' in config['exchange']
|
|
|
|
assert 'datadir' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog)
|
2020-06-01 18:33:26 +00:00
|
|
|
assert 'timeframe' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'position_stacking' not in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert not log_has('Parameter --enable-position-stacking detected ...', caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'timerange' not in config
|
|
|
|
assert 'runmode' in config
|
|
|
|
assert config['runmode'] == RunMode.HYPEROPT
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplog) -> None:
|
2019-07-14 22:44:25 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
2019-07-11 22:41:09 +00:00
|
|
|
mocker.patch(
|
2019-07-12 00:26:27 +00:00
|
|
|
'freqtrade.configuration.configuration.create_datadir',
|
|
|
|
lambda c, x: x
|
2019-07-11 22:41:09 +00:00
|
|
|
)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
args = [
|
2019-09-16 04:44:07 +00:00
|
|
|
'hyperopt',
|
2019-04-22 18:24:45 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2019-04-22 18:24:45 +00:00
|
|
|
'--datadir', '/foo/bar',
|
2020-06-01 18:47:27 +00:00
|
|
|
'--timeframe', '1m',
|
2019-04-22 18:24:45 +00:00
|
|
|
'--timerange', ':100',
|
|
|
|
'--enable-position-stacking',
|
|
|
|
'--disable-max-market-positions',
|
|
|
|
'--epochs', '1000',
|
2019-11-07 22:55:14 +00:00
|
|
|
'--spaces', 'default',
|
2019-04-22 18:24:45 +00:00
|
|
|
'--print-all'
|
|
|
|
]
|
|
|
|
|
2020-01-26 12:33:13 +00:00
|
|
|
config = setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
|
2019-04-22 18:24:45 +00:00
|
|
|
assert 'max_open_trades' in config
|
|
|
|
assert 'stake_currency' in config
|
|
|
|
assert 'stake_amount' in config
|
|
|
|
assert 'exchange' in config
|
|
|
|
assert 'pair_whitelist' in config['exchange']
|
|
|
|
assert 'datadir' in config
|
|
|
|
assert config['runmode'] == RunMode.HYPEROPT
|
|
|
|
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog)
|
2020-06-01 18:33:26 +00:00
|
|
|
assert 'timeframe' in config
|
2020-06-01 18:47:27 +00:00
|
|
|
assert log_has('Parameter -i/--timeframe detected ... Using timeframe: 1m ...',
|
2019-08-11 18:16:52 +00:00
|
|
|
caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'position_stacking' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Parameter --enable-position-stacking detected ...', caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'use_max_market_positions' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Parameter --disable-max-market-positions detected ...', caplog)
|
|
|
|
assert log_has('max_open_trades set to unlimited ...', caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'timerange' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'epochs' in config
|
2019-04-24 19:12:23 +00:00
|
|
|
assert log_has('Parameter --epochs detected ... Will run Hyperopt with for 1000 epochs ...',
|
2019-08-11 18:16:52 +00:00
|
|
|
caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'spaces' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Parameter -s/--spaces detected: {}'.format(config['spaces']), caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
assert 'print_all' in config
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Parameter --print-all detected ...', caplog)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
|
2021-02-26 18:48:06 +00:00
|
|
|
def test_setup_hyperopt_configuration_stake_amount(mocker, default_conf) -> None:
|
2020-03-10 09:42:31 +00:00
|
|
|
|
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'hyperopt',
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2021-02-26 18:48:06 +00:00
|
|
|
'--stake-amount', '1',
|
|
|
|
'--starting-balance', '2'
|
2020-03-10 09:42:31 +00:00
|
|
|
]
|
2021-02-12 19:20:32 +00:00
|
|
|
conf = setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
|
|
|
|
assert isinstance(conf, dict)
|
2020-03-10 09:42:31 +00:00
|
|
|
|
2021-02-26 18:48:06 +00:00
|
|
|
args = [
|
|
|
|
'hyperopt',
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--strategy', 'DefaultStrategy',
|
|
|
|
'--stake-amount', '1',
|
|
|
|
'--starting-balance', '0.5'
|
|
|
|
]
|
|
|
|
with pytest.raises(OperationalException, match=r"Starting balance .* smaller .*"):
|
|
|
|
setup_optimize_configuration(get_args(args), RunMode.HYPEROPT)
|
|
|
|
|
2020-03-10 09:42:31 +00:00
|
|
|
|
2019-01-06 18:38:32 +00:00
|
|
|
def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
|
2019-07-11 21:39:42 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
2019-01-06 18:38:32 +00:00
|
|
|
|
2019-10-18 20:29:19 +00:00
|
|
|
hyperopt = DefaultHyperOpt
|
2019-11-02 13:17:15 +00:00
|
|
|
delattr(hyperopt, 'populate_indicators')
|
2019-10-18 20:29:19 +00:00
|
|
|
delattr(hyperopt, 'populate_buy_trend')
|
|
|
|
delattr(hyperopt, 'populate_sell_trend')
|
2019-01-06 18:38:32 +00:00
|
|
|
mocker.patch(
|
2019-12-24 12:54:46 +00:00
|
|
|
'freqtrade.resolvers.hyperopt_resolver.HyperOptResolver.load_object',
|
2019-10-18 20:29:19 +00:00
|
|
|
MagicMock(return_value=hyperopt(default_conf))
|
2019-01-06 18:38:32 +00:00
|
|
|
)
|
2019-10-20 17:54:38 +00:00
|
|
|
default_conf.update({'hyperopt': 'DefaultHyperOpt'})
|
2019-12-23 09:06:19 +00:00
|
|
|
x = HyperOptResolver.load_hyperopt(default_conf)
|
2019-11-02 13:17:15 +00:00
|
|
|
assert not hasattr(x, 'populate_indicators')
|
2019-01-06 18:38:32 +00:00
|
|
|
assert not hasattr(x, 'populate_buy_trend')
|
|
|
|
assert not hasattr(x, 'populate_sell_trend')
|
2019-11-02 13:17:15 +00:00
|
|
|
assert log_has("Hyperopt class does not provide populate_indicators() method. "
|
|
|
|
"Using populate_indicators from the strategy.", caplog)
|
2019-09-18 19:57:37 +00:00
|
|
|
assert log_has("Hyperopt class does not provide populate_sell_trend() method. "
|
|
|
|
"Using populate_sell_trend from the strategy.", caplog)
|
|
|
|
assert log_has("Hyperopt class does not provide populate_buy_trend() method. "
|
|
|
|
"Using populate_buy_trend from the strategy.", caplog)
|
2020-06-02 08:02:55 +00:00
|
|
|
assert hasattr(x, "ticker_interval") # DEPRECATED
|
|
|
|
assert hasattr(x, "timeframe")
|
2019-01-06 18:38:32 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_hyperoptresolver_wrongname(default_conf) -> None:
|
2019-07-28 13:19:17 +00:00
|
|
|
default_conf.update({'hyperopt': "NonExistingHyperoptClass"})
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'):
|
2019-12-23 09:06:19 +00:00
|
|
|
HyperOptResolver.load_hyperopt(default_conf)
|
2019-10-10 01:37:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_hyperoptresolver_noname(default_conf):
|
|
|
|
default_conf['hyperopt'] = ''
|
|
|
|
with pytest.raises(OperationalException,
|
2019-10-14 17:42:28 +00:00
|
|
|
match="No Hyperopt set. Please use `--hyperopt` to specify "
|
2019-10-10 01:37:32 +00:00
|
|
|
"the Hyperopt class to use."):
|
2019-12-23 09:06:19 +00:00
|
|
|
HyperOptResolver.load_hyperopt(default_conf)
|
2019-07-28 13:19:17 +00:00
|
|
|
|
|
|
|
|
2020-08-06 07:00:28 +00:00
|
|
|
def test_start_not_installed(mocker, default_conf, import_fails) -> None:
|
2019-09-24 12:39:28 +00:00
|
|
|
start_mock = MagicMock()
|
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'hyperopt',
|
2019-09-29 14:25:25 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2020-08-06 07:00:28 +00:00
|
|
|
'--hyperopt-path',
|
|
|
|
str(Path(__file__).parent / "hyperopts"),
|
2020-10-28 13:36:19 +00:00
|
|
|
'--epochs', '5',
|
|
|
|
'--hyperopt-loss', 'SharpeHyperOptLossDaily',
|
2019-09-24 12:39:28 +00:00
|
|
|
]
|
2020-02-10 09:35:48 +00:00
|
|
|
pargs = get_args(args)
|
2019-09-24 12:39:28 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"Please ensure that the hyperopt dependencies"):
|
2020-02-10 09:35:48 +00:00
|
|
|
start_hyperopt(pargs)
|
2019-09-24 12:39:28 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_start(mocker, hyperopt_conf, caplog) -> None:
|
2018-03-06 06:02:03 +00:00
|
|
|
start_mock = MagicMock()
|
2020-08-06 06:51:01 +00:00
|
|
|
patched_configuration_load_config_file(mocker, hyperopt_conf)
|
2018-03-06 06:02:03 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
2018-06-17 20:32:56 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2018-03-06 06:02:03 +00:00
|
|
|
args = [
|
|
|
|
'hyperopt',
|
2019-09-16 04:44:07 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2020-10-10 12:22:29 +00:00
|
|
|
'--hyperopt-loss', 'SharpeHyperOptLossDaily',
|
2018-03-06 06:02:03 +00:00
|
|
|
'--epochs', '5'
|
|
|
|
]
|
2020-02-10 09:35:48 +00:00
|
|
|
pargs = get_args(args)
|
|
|
|
start_hyperopt(pargs)
|
2018-03-06 06:02:03 +00:00
|
|
|
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has('Starting freqtrade in Hyperopt mode', caplog)
|
2018-03-06 06:02:03 +00:00
|
|
|
assert start_mock.call_count == 1
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_start_no_data(mocker, hyperopt_conf) -> None:
|
|
|
|
patched_configuration_load_config_file(mocker, hyperopt_conf)
|
2019-12-04 05:57:44 +00:00
|
|
|
mocker.patch('freqtrade.data.history.load_pair_history', MagicMock(return_value=pd.DataFrame))
|
2019-05-14 06:23:09 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-05-14 06:23:09 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'hyperopt',
|
2019-09-16 04:44:07 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2020-10-10 12:22:29 +00:00
|
|
|
'--hyperopt-loss', 'SharpeHyperOptLossDaily',
|
2019-05-14 06:23:09 +00:00
|
|
|
'--epochs', '5'
|
|
|
|
]
|
2020-02-10 09:35:48 +00:00
|
|
|
pargs = get_args(args)
|
2019-10-23 18:27:51 +00:00
|
|
|
with pytest.raises(OperationalException, match='No data found. Terminating.'):
|
2020-02-10 09:35:48 +00:00
|
|
|
start_hyperopt(pargs)
|
2019-05-14 06:23:09 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_start_filelock(mocker, hyperopt_conf, caplog) -> None:
|
2021-02-01 10:00:55 +00:00
|
|
|
hyperopt_mock = MagicMock(side_effect=Timeout(Hyperopt.get_lock_filename(hyperopt_conf)))
|
2020-08-06 06:51:01 +00:00
|
|
|
patched_configuration_load_config_file(mocker, hyperopt_conf)
|
2021-02-01 10:00:55 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.__init__', hyperopt_mock)
|
2019-05-25 18:25:59 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'hyperopt',
|
2019-09-16 04:44:07 +00:00
|
|
|
'--config', 'config.json',
|
2019-10-20 17:54:38 +00:00
|
|
|
'--hyperopt', 'DefaultHyperOpt',
|
2020-10-10 12:22:29 +00:00
|
|
|
'--hyperopt-loss', 'SharpeHyperOptLossDaily',
|
2019-05-25 18:25:59 +00:00
|
|
|
'--epochs', '5'
|
|
|
|
]
|
2020-02-10 09:35:48 +00:00
|
|
|
pargs = get_args(args)
|
|
|
|
start_hyperopt(pargs)
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has("Another running instance of freqtrade Hyperopt detected.", caplog)
|
2019-05-25 18:25:59 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_log_results_if_loss_improves(hyperopt, capsys) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt.current_best_loss = 2
|
2019-07-30 08:47:46 +00:00
|
|
|
hyperopt.total_epochs = 2
|
2020-03-01 02:11:00 +00:00
|
|
|
|
2019-11-27 19:52:43 +00:00
|
|
|
hyperopt.print_results(
|
2018-03-02 13:46:32 +00:00
|
|
|
{
|
|
|
|
'loss': 1,
|
2020-03-01 02:11:00 +00:00
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
},
|
|
|
|
'total_profit': 0,
|
2019-11-23 09:00:43 +00:00
|
|
|
'current_epoch': 2, # This starts from 1 (in a human-friendly manner)
|
2020-03-01 02:11:00 +00:00
|
|
|
'is_initial_point': False,
|
|
|
|
'is_best': True
|
2018-03-02 13:46:32 +00:00
|
|
|
}
|
|
|
|
)
|
2018-03-25 20:57:40 +00:00
|
|
|
out, err = capsys.readouterr()
|
2020-03-03 00:14:56 +00:00
|
|
|
assert all(x in out
|
2020-03-04 19:51:09 +00:00
|
|
|
for x in ["Best", "2/2", " 1", "0.10%", "0.00100000 BTC (1.00%)", "20.0 m"])
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_no_log_if_loss_does_not_improve(hyperopt, caplog) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt.current_best_loss = 2
|
2019-11-27 19:52:43 +00:00
|
|
|
hyperopt.print_results(
|
2018-03-02 13:46:32 +00:00
|
|
|
{
|
2019-11-27 19:52:43 +00:00
|
|
|
'is_best': False,
|
2018-03-02 13:46:32 +00:00
|
|
|
'loss': 3,
|
2019-11-23 09:00:43 +00:00
|
|
|
'current_epoch': 1,
|
2018-03-02 13:46:32 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
assert caplog.record_tuples == []
|
2018-01-07 01:12:32 +00:00
|
|
|
|
|
|
|
|
2020-04-28 19:56:19 +00:00
|
|
|
def test_save_results_saves_epochs(mocker, hyperopt, testdatadir, caplog) -> None:
|
|
|
|
epochs = create_results(mocker, hyperopt, testdatadir)
|
2018-07-03 19:51:48 +00:00
|
|
|
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
2020-09-27 14:50:22 +00:00
|
|
|
mock_dump_json = mocker.patch('freqtrade.optimize.hyperopt.file_dump_json', return_value=None)
|
2020-04-28 19:56:19 +00:00
|
|
|
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
2019-11-23 09:30:49 +00:00
|
|
|
|
2020-04-28 19:56:19 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
hyperopt.epochs = epochs
|
|
|
|
hyperopt._save_results()
|
|
|
|
assert log_has(f"1 epoch saved to '{results_file}'.", caplog)
|
2018-03-02 13:46:32 +00:00
|
|
|
mock_dump.assert_called_once()
|
2020-09-27 14:50:22 +00:00
|
|
|
mock_dump_json.assert_called_once()
|
2018-01-09 10:19:44 +00:00
|
|
|
|
2020-04-28 19:56:19 +00:00
|
|
|
hyperopt.epochs = epochs + epochs
|
|
|
|
hyperopt._save_results()
|
|
|
|
assert log_has(f"2 epochs saved to '{results_file}'.", caplog)
|
2019-11-23 09:30:49 +00:00
|
|
|
|
2018-01-09 10:19:44 +00:00
|
|
|
|
2020-04-28 19:56:19 +00:00
|
|
|
def test_read_results_returns_epochs(mocker, hyperopt, testdatadir, caplog) -> None:
|
|
|
|
epochs = create_results(mocker, hyperopt, testdatadir)
|
2021-05-02 06:59:21 +00:00
|
|
|
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
2020-04-28 19:56:19 +00:00
|
|
|
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
2021-03-17 19:43:51 +00:00
|
|
|
hyperopt_epochs = HyperoptTools._read_results(results_file)
|
2020-04-28 19:56:19 +00:00
|
|
|
assert log_has(f"Reading epochs from '{results_file}'", caplog)
|
|
|
|
assert hyperopt_epochs == epochs
|
2018-01-09 10:19:44 +00:00
|
|
|
mock_load.assert_called_once()
|
2018-01-25 08:45:53 +00:00
|
|
|
|
|
|
|
|
2020-09-27 22:35:19 +00:00
|
|
|
def test_load_previous_results(mocker, hyperopt, testdatadir, caplog) -> None:
|
|
|
|
epochs = create_results(mocker, hyperopt, testdatadir)
|
2021-05-02 06:59:21 +00:00
|
|
|
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
2020-09-27 22:35:19 +00:00
|
|
|
mocker.patch.object(Path, 'is_file', MagicMock(return_value=True))
|
|
|
|
statmock = MagicMock()
|
|
|
|
statmock.st_size = 5
|
|
|
|
# mocker.patch.object(Path, 'stat', MagicMock(return_value=statmock))
|
|
|
|
|
|
|
|
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
|
|
|
|
2021-03-17 19:43:51 +00:00
|
|
|
hyperopt_epochs = HyperoptTools.load_previous_results(results_file)
|
2020-09-27 22:35:19 +00:00
|
|
|
|
|
|
|
assert hyperopt_epochs == epochs
|
|
|
|
mock_load.assert_called_once()
|
|
|
|
|
|
|
|
del epochs[0]['is_best']
|
2021-05-02 06:59:21 +00:00
|
|
|
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
2020-09-27 22:35:19 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2021-03-17 19:43:51 +00:00
|
|
|
HyperoptTools.load_previous_results(results_file)
|
2020-09-27 22:35:19 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_roi_table_generation(hyperopt) -> None:
|
2018-01-25 08:45:53 +00:00
|
|
|
params = {
|
|
|
|
'roi_t1': 5,
|
|
|
|
'roi_t2': 10,
|
|
|
|
'roi_t3': 15,
|
|
|
|
'roi_p1': 1,
|
|
|
|
'roi_p2': 2,
|
|
|
|
'roi_p3': 3,
|
|
|
|
}
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-03-22 08:27:13 +00:00
|
|
|
assert hyperopt.custom_hyperopt.generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0}
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None:
|
2018-07-03 19:09:59 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
|
|
|
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-05-14 06:23:09 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-05-14 06:23:09 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
2018-06-24 12:27:53 +00:00
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:24:04 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result',
|
|
|
|
'params': {'buy': {}, 'sell': {}, 'roi': {}, 'stoploss': 0.0},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
},
|
|
|
|
}])
|
2018-06-24 12:27:53 +00:00
|
|
|
)
|
2018-06-17 20:32:56 +00:00
|
|
|
patch_exchange(mocker)
|
2020-03-08 10:35:31 +00:00
|
|
|
# Co-test loading timeframe from strategy
|
2020-08-06 06:51:01 +00:00
|
|
|
del hyperopt_conf['timeframe']
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-08-01 20:57:50 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
hyperopt.start()
|
2019-07-30 08:47:46 +00:00
|
|
|
|
2018-06-24 12:27:53 +00:00
|
|
|
parallel.assert_called_once()
|
2019-07-30 08:47:46 +00:00
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
2019-08-03 08:19:36 +00:00
|
|
|
assert 'Best result:\n\n* 1/1: foo result Objective: 1.00000\n' in out
|
2018-07-03 19:09:59 +00:00
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-06-08 17:32:31 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-09-18 19:57:37 +00:00
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
|
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
|
2019-07-15 18:28:55 +00:00
|
|
|
assert hasattr(hyperopt, "max_open_trades")
|
2020-08-06 06:51:01 +00:00
|
|
|
assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
|
2019-07-16 03:50:27 +00:00
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
|
2021-04-29 18:17:13 +00:00
|
|
|
def test_hyperopt_format_results(hyperopt):
|
|
|
|
|
|
|
|
bt_result = {
|
|
|
|
'results': pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
|
|
|
|
"UNITTEST/BTC", "UNITTEST/BTC"],
|
|
|
|
"profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780],
|
|
|
|
"profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
|
|
|
|
"open_date": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 21, 36, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 12, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 44, 00).datetime],
|
|
|
|
"close_date": [Arrow(2017, 11, 14, 21, 35, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 10, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 43, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 58, 00).datetime],
|
|
|
|
"open_rate": [0.002543, 0.003003, 0.003089, 0.003214],
|
|
|
|
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
|
|
|
|
"trade_duration": [123, 34, 31, 14],
|
|
|
|
"is_open": [False, False, False, True],
|
|
|
|
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
|
|
|
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
|
|
|
|
SellType.ROI, SellType.FORCE_SELL]
|
|
|
|
}),
|
|
|
|
'config': hyperopt.config,
|
|
|
|
'locks': [],
|
|
|
|
'final_balance': 0.02,
|
|
|
|
'backtest_start_time': 1619718665,
|
|
|
|
'backtest_end_time': 1619718665,
|
|
|
|
}
|
|
|
|
results_metrics = generate_strategy_stats({'XRP/BTC': None}, '', bt_result,
|
|
|
|
Arrow(2017, 11, 14, 19, 32, 00),
|
|
|
|
Arrow(2017, 12, 14, 19, 32, 00), market_change=0)
|
|
|
|
|
2021-05-01 07:05:46 +00:00
|
|
|
results_explanation = HyperoptTools.format_results_explanation_string(results_metrics, 'BTC')
|
2021-04-29 18:17:13 +00:00
|
|
|
total_profit = results_metrics['profit_total_abs']
|
2019-12-01 15:01:59 +00:00
|
|
|
|
|
|
|
results = {
|
|
|
|
'loss': 0.0,
|
|
|
|
'params_dict': None,
|
|
|
|
'params_details': None,
|
|
|
|
'results_metrics': results_metrics,
|
|
|
|
'results_explanation': results_explanation,
|
|
|
|
'total_profit': total_profit,
|
|
|
|
'current_epoch': 1,
|
|
|
|
'is_initial_point': True,
|
|
|
|
}
|
2018-06-02 21:07:31 +00:00
|
|
|
|
2021-03-17 19:43:51 +00:00
|
|
|
result = HyperoptTools._format_explanation_string(results, 1)
|
2021-04-29 18:17:13 +00:00
|
|
|
assert ' 0.71%' in result
|
|
|
|
assert 'Total profit 0.00003100 BTC' in result
|
|
|
|
assert '0:50:00 min' in result
|
2018-02-08 19:49:43 +00:00
|
|
|
|
|
|
|
|
2019-11-07 22:55:14 +00:00
|
|
|
@pytest.mark.parametrize("spaces, expected_results", [
|
|
|
|
(['buy'],
|
|
|
|
{'buy': True, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False}),
|
|
|
|
(['sell'],
|
|
|
|
{'buy': False, 'sell': True, 'roi': False, 'stoploss': False, 'trailing': False}),
|
|
|
|
(['roi'],
|
|
|
|
{'buy': False, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False}),
|
|
|
|
(['stoploss'],
|
|
|
|
{'buy': False, 'sell': False, 'roi': False, 'stoploss': True, 'trailing': False}),
|
|
|
|
(['trailing'],
|
|
|
|
{'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': True}),
|
|
|
|
(['buy', 'sell', 'roi', 'stoploss'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}),
|
|
|
|
(['buy', 'sell', 'roi', 'stoploss', 'trailing'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}),
|
|
|
|
(['buy', 'roi'],
|
|
|
|
{'buy': True, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False}),
|
|
|
|
(['all'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}),
|
|
|
|
(['default'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}),
|
|
|
|
(['default', 'trailing'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}),
|
|
|
|
(['all', 'buy'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}),
|
|
|
|
(['default', 'buy'],
|
|
|
|
{'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}),
|
|
|
|
])
|
2021-05-01 14:36:35 +00:00
|
|
|
def test_has_space(hyperopt_conf, spaces, expected_results):
|
2019-11-07 22:55:14 +00:00
|
|
|
for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing']:
|
2021-05-01 14:36:35 +00:00
|
|
|
hyperopt_conf.update({'spaces': spaces})
|
|
|
|
assert HyperoptTools.has_space(hyperopt_conf, s) == expected_results[s]
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
|
2019-09-07 18:56:03 +00:00
|
|
|
def test_populate_indicators(hyperopt, testdatadir) -> None:
|
2020-03-08 10:35:31 +00:00
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], fill_up_missing=True)
|
|
|
|
dataframes = hyperopt.backtesting.strategy.ohlcvdata_to_dataframe(data)
|
2018-03-22 08:27:13 +00:00
|
|
|
dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'],
|
|
|
|
{'pair': 'UNITTEST/BTC'})
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
# Check if some indicators are generated. We will not test all of them
|
|
|
|
assert 'adx' in dataframe
|
2018-06-24 12:27:53 +00:00
|
|
|
assert 'mfi' in dataframe
|
|
|
|
assert 'rsi' in dataframe
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
|
2019-09-07 18:56:03 +00:00
|
|
|
def test_buy_strategy_generator(hyperopt, testdatadir) -> None:
|
2020-03-08 10:35:31 +00:00
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], fill_up_missing=True)
|
|
|
|
dataframes = hyperopt.backtesting.strategy.ohlcvdata_to_dataframe(data)
|
2018-03-22 08:27:13 +00:00
|
|
|
dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'],
|
|
|
|
{'pair': 'UNITTEST/BTC'})
|
2018-03-05 08:35:42 +00:00
|
|
|
|
2018-03-22 08:27:13 +00:00
|
|
|
populate_buy_trend = hyperopt.custom_hyperopt.buy_strategy_generator(
|
2018-03-05 08:35:42 +00:00
|
|
|
{
|
2018-06-24 12:27:53 +00:00
|
|
|
'adx-value': 20,
|
|
|
|
'fastd-value': 20,
|
|
|
|
'mfi-value': 20,
|
|
|
|
'rsi-value': 20,
|
|
|
|
'adx-enabled': True,
|
|
|
|
'fastd-enabled': True,
|
|
|
|
'mfi-enabled': True,
|
|
|
|
'rsi-enabled': True,
|
|
|
|
'trigger': 'bb_lower'
|
2018-03-05 08:35:42 +00:00
|
|
|
}
|
|
|
|
)
|
2018-07-29 19:07:21 +00:00
|
|
|
result = populate_buy_trend(dataframe, {'pair': 'UNITTEST/BTC'})
|
2018-03-05 08:35:42 +00:00
|
|
|
# Check if some indicators are generated. We will not test all of them
|
2018-03-06 06:02:03 +00:00
|
|
|
assert 'buy' in result
|
|
|
|
assert 1 in result['buy']
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_sell_strategy_generator(hyperopt, testdatadir) -> None:
|
|
|
|
data = load_data(testdatadir, '1m', ['UNITTEST/BTC'], fill_up_missing=True)
|
|
|
|
dataframes = hyperopt.backtesting.strategy.ohlcvdata_to_dataframe(data)
|
|
|
|
dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'],
|
|
|
|
{'pair': 'UNITTEST/BTC'})
|
|
|
|
|
|
|
|
populate_sell_trend = hyperopt.custom_hyperopt.sell_strategy_generator(
|
|
|
|
{
|
|
|
|
'sell-adx-value': 20,
|
|
|
|
'sell-fastd-value': 75,
|
|
|
|
'sell-mfi-value': 80,
|
|
|
|
'sell-rsi-value': 20,
|
|
|
|
'sell-adx-enabled': True,
|
|
|
|
'sell-fastd-enabled': True,
|
|
|
|
'sell-mfi-enabled': True,
|
|
|
|
'sell-rsi-enabled': True,
|
|
|
|
'sell-trigger': 'sell-bb_upper'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = populate_sell_trend(dataframe, {'pair': 'UNITTEST/BTC'})
|
|
|
|
# Check if some indicators are generated. We will not test all of them
|
|
|
|
print(result)
|
|
|
|
assert 'sell' in result
|
|
|
|
assert 1 in result['sell']
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_optimizer(mocker, hyperopt_conf) -> None:
|
|
|
|
hyperopt_conf.update({'spaces': 'all',
|
|
|
|
'hyperopt_min_trades': 1,
|
|
|
|
})
|
2018-03-06 06:02:03 +00:00
|
|
|
|
2021-04-30 05:40:07 +00:00
|
|
|
backtest_result = {
|
|
|
|
'results': pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
|
|
|
|
"UNITTEST/BTC", "UNITTEST/BTC"],
|
|
|
|
"profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780],
|
|
|
|
"profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
|
|
|
|
"open_date": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 21, 36, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 12, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 44, 00).datetime],
|
|
|
|
"close_date": [Arrow(2017, 11, 14, 21, 35, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 10, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 43, 00).datetime,
|
|
|
|
Arrow(2017, 11, 14, 22, 58, 00).datetime],
|
|
|
|
"open_rate": [0.002543, 0.003003, 0.003089, 0.003214],
|
|
|
|
"close_rate": [0.002546, 0.003014, 0.003103, 0.003217],
|
|
|
|
"trade_duration": [123, 34, 31, 14],
|
|
|
|
"is_open": [False, False, False, True],
|
|
|
|
"stake_amount": [0.01, 0.01, 0.01, 0.01],
|
|
|
|
"sell_reason": [SellType.ROI, SellType.STOP_LOSS,
|
|
|
|
SellType.ROI, SellType.FORCE_SELL]
|
|
|
|
}),
|
|
|
|
'config': hyperopt_conf,
|
|
|
|
'locks': [],
|
|
|
|
'final_balance': 1000,
|
|
|
|
}
|
2021-04-29 18:17:13 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', return_value=backtest_result)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.get_timerange',
|
|
|
|
return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))
|
2018-06-17 20:32:56 +00:00
|
|
|
patch_exchange(mocker)
|
2021-04-29 18:17:13 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load', return_value={'XRP/BTC': None})
|
2018-03-06 06:02:03 +00:00
|
|
|
|
|
|
|
optimizer_param = {
|
2018-06-24 12:27:53 +00:00
|
|
|
'adx-value': 0,
|
|
|
|
'fastd-value': 35,
|
|
|
|
'mfi-value': 0,
|
|
|
|
'rsi-value': 0,
|
|
|
|
'adx-enabled': False,
|
|
|
|
'fastd-enabled': True,
|
|
|
|
'mfi-enabled': False,
|
|
|
|
'rsi-enabled': False,
|
|
|
|
'trigger': 'macd_cross_signal',
|
2019-01-06 09:35:18 +00:00
|
|
|
'sell-adx-value': 0,
|
|
|
|
'sell-fastd-value': 75,
|
|
|
|
'sell-mfi-value': 0,
|
|
|
|
'sell-rsi-value': 0,
|
|
|
|
'sell-adx-enabled': False,
|
|
|
|
'sell-fastd-enabled': True,
|
|
|
|
'sell-mfi-enabled': False,
|
|
|
|
'sell-rsi-enabled': False,
|
|
|
|
'sell-trigger': 'macd_cross_signal',
|
2018-03-06 06:02:03 +00:00
|
|
|
'roi_t1': 60.0,
|
|
|
|
'roi_t2': 30.0,
|
|
|
|
'roi_t3': 20.0,
|
2018-06-24 12:27:53 +00:00
|
|
|
'roi_p1': 0.01,
|
|
|
|
'roi_p2': 0.01,
|
|
|
|
'roi_p3': 0.1,
|
2018-03-06 06:02:03 +00:00
|
|
|
'stoploss': -0.4,
|
2019-11-13 20:32:37 +00:00
|
|
|
'trailing_stop': True,
|
|
|
|
'trailing_stop_positive': 0.02,
|
2019-12-10 11:06:17 +00:00
|
|
|
'trailing_stop_positive_offset_p1': 0.05,
|
2019-11-13 20:32:37 +00:00
|
|
|
'trailing_only_offset_is_reached': False,
|
2018-03-06 06:02:03 +00:00
|
|
|
}
|
|
|
|
response_expected = {
|
2021-04-29 18:17:13 +00:00
|
|
|
'loss': 1.9147239021396234,
|
|
|
|
'results_explanation': (' 4 trades. 4/0/0 Wins/Draws/Losses. '
|
|
|
|
'Avg profit 0.77%. Median profit 0.71%. Total profit '
|
|
|
|
'0.00003100 BTC ( 0.00\N{GREEK CAPITAL LETTER SIGMA}%). '
|
|
|
|
'Avg duration 0:50:00 min.'
|
2019-11-06 18:33:15 +00:00
|
|
|
).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'),
|
2019-11-27 19:52:43 +00:00
|
|
|
'params_details': {'buy': {'adx-enabled': False,
|
|
|
|
'adx-value': 0,
|
|
|
|
'fastd-enabled': True,
|
|
|
|
'fastd-value': 35,
|
|
|
|
'mfi-enabled': False,
|
|
|
|
'mfi-value': 0,
|
|
|
|
'rsi-enabled': False,
|
|
|
|
'rsi-value': 0,
|
|
|
|
'trigger': 'macd_cross_signal'},
|
|
|
|
'roi': {0: 0.12000000000000001,
|
|
|
|
20.0: 0.02,
|
|
|
|
50.0: 0.01,
|
|
|
|
110.0: 0},
|
|
|
|
'sell': {'sell-adx-enabled': False,
|
|
|
|
'sell-adx-value': 0,
|
|
|
|
'sell-fastd-enabled': True,
|
|
|
|
'sell-fastd-value': 75,
|
|
|
|
'sell-mfi-enabled': False,
|
|
|
|
'sell-mfi-value': 0,
|
|
|
|
'sell-rsi-enabled': False,
|
|
|
|
'sell-rsi-value': 0,
|
|
|
|
'sell-trigger': 'macd_cross_signal'},
|
2019-12-04 22:08:38 +00:00
|
|
|
'stoploss': {'stoploss': -0.4},
|
|
|
|
'trailing': {'trailing_only_offset_is_reached': False,
|
|
|
|
'trailing_stop': True,
|
|
|
|
'trailing_stop_positive': 0.02,
|
2019-12-10 11:06:17 +00:00
|
|
|
'trailing_stop_positive_offset': 0.07}},
|
2019-11-27 19:52:43 +00:00
|
|
|
'params_dict': optimizer_param,
|
2021-04-29 18:17:13 +00:00
|
|
|
'results_metrics': ANY,
|
|
|
|
'total_profit': 3.1e-08
|
2018-03-06 06:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2021-05-01 11:33:12 +00:00
|
|
|
hyperopt.min_date = Arrow(2017, 12, 10)
|
|
|
|
hyperopt.max_date = Arrow(2017, 12, 13)
|
2021-05-02 07:35:10 +00:00
|
|
|
hyperopt.init_spaces()
|
|
|
|
hyperopt.dimensions = hyperopt.dimensions
|
2018-06-24 12:27:53 +00:00
|
|
|
generate_optimizer_value = hyperopt.generate_optimizer(list(optimizer_param.values()))
|
2018-03-06 06:02:03 +00:00
|
|
|
assert generate_optimizer_value == response_expected
|
2019-07-15 18:27:34 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_clean_hyperopt(mocker, hyperopt_conf, caplog):
|
2019-07-15 18:27:34 +00:00
|
|
|
patch_exchange(mocker)
|
2020-08-06 06:51:01 +00:00
|
|
|
|
2019-07-15 18:27:34 +00:00
|
|
|
mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True))
|
|
|
|
unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock())
|
2020-08-06 06:51:01 +00:00
|
|
|
h = Hyperopt(hyperopt_conf)
|
2019-07-15 18:27:34 +00:00
|
|
|
|
|
|
|
assert unlinkmock.call_count == 2
|
2020-03-08 10:35:31 +00:00
|
|
|
assert log_has(f"Removing `{h.data_pickle_file}`.", caplog)
|
2019-07-16 03:50:27 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_print_json_spaces_all(mocker, hyperopt_conf, capsys) -> None:
|
2019-08-16 01:20:12 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:34:47 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
|
|
|
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-08-15 21:49:49 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-08-15 21:49:49 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:11:00 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {},
|
|
|
|
'params_details': {
|
|
|
|
'buy': {'mfi-value': None},
|
|
|
|
'sell': {'sell-mfi-value': None},
|
|
|
|
'roi': {}, 'stoploss': {'stoploss': None},
|
|
|
|
'trailing': {'trailing_stop': None}
|
|
|
|
},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-08-15 21:49:49 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
'print_json': True,
|
|
|
|
})
|
2019-08-15 21:49:49 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-08-15 21:49:49 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
2020-03-01 02:11:00 +00:00
|
|
|
result_str = (
|
|
|
|
'{"params":{"mfi-value":null,"sell-mfi-value":null},"minimal_roi"'
|
|
|
|
':{},"stoploss":null,"trailing_stop":null}'
|
|
|
|
)
|
|
|
|
assert result_str in out # noqa: E501
|
2019-08-16 01:20:12 +00:00
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-08-16 01:20:12 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-08-15 21:49:49 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_print_json_spaces_default(mocker, hyperopt_conf, capsys) -> None:
|
2019-11-13 20:09:05 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-11-13 20:09:05 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-11-13 20:09:05 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:11:00 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {},
|
|
|
|
'params_details': {
|
|
|
|
'buy': {'mfi-value': None},
|
|
|
|
'sell': {'sell-mfi-value': None},
|
|
|
|
'roi': {}, 'stoploss': {'stoploss': None}
|
|
|
|
},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-11-13 20:09:05 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'print_json': True})
|
2019-11-13 20:09:05 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-11-13 20:09:05 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
2019-12-01 15:34:25 +00:00
|
|
|
assert '{"params":{"mfi-value":null,"sell-mfi-value":null},"minimal_roi":{},"stoploss":null}' in out # noqa: E501
|
2019-11-13 20:09:05 +00:00
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-11-13 20:09:05 +00:00
|
|
|
assert dumper.call_count == 2
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_print_json_spaces_roi_stoploss(mocker, hyperopt_conf, capsys) -> None:
|
2019-08-16 01:20:12 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-08-15 21:49:49 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-08-15 21:49:49 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:11:00 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {},
|
|
|
|
'params_details': {'roi': {}, 'stoploss': {'stoploss': None}},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-08-15 21:49:49 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'roi stoploss',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
'print_json': True,
|
|
|
|
})
|
2019-08-15 21:49:49 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-08-15 21:49:49 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert '{"minimal_roi":{},"stoploss":null}' in out
|
2019-08-16 01:20:12 +00:00
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-08-16 01:20:12 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-09-04 21:38:15 +00:00
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_simplified_interface_roi_stoploss(mocker, hyperopt_conf, capsys) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-09-04 21:38:15 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2019-09-06 13:09:03 +00:00
|
|
|
MagicMock(return_value=[{
|
2020-03-01 02:11:00 +00:00
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {'stoploss': 0.0},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-09-04 21:38:15 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'roi stoploss'})
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
del hyperopt.custom_hyperopt.__class__.buy_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.indicator_space
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_indicator_space
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert 'Best result:\n\n* 1/1: foo result Objective: 1.00000\n' in out
|
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-09-04 21:38:15 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-09-18 19:57:37 +00:00
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
|
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "max_open_trades")
|
2020-08-06 06:51:01 +00:00
|
|
|
assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_simplified_interface_all_failed(mocker, hyperopt_conf) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-09-04 21:38:15 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'all', })
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
del hyperopt.custom_hyperopt.__class__.buy_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.indicator_space
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_indicator_space
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"The 'buy' space is included into *"):
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_simplified_interface_buy(mocker, hyperopt_conf, capsys) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-09-04 21:38:15 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:11:00 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-09-04 21:38:15 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'buy'})
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
# TODO: sell_strategy_generator() is actually not called because
|
|
|
|
# run_optimizer_parallel() is mocked
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.sell_indicator_space
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert 'Best result:\n\n* 1/1: foo result Objective: 1.00000\n' in out
|
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-09-04 21:38:15 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-09-18 19:57:37 +00:00
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
|
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "max_open_trades")
|
2020-08-06 06:51:01 +00:00
|
|
|
assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
|
|
|
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_simplified_interface_sell(mocker, hyperopt_conf, capsys) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-09-04 21:38:15 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
2020-03-01 02:11:00 +00:00
|
|
|
MagicMock(return_value=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {},
|
|
|
|
'results_metrics':
|
|
|
|
{
|
|
|
|
'trade_count': 1,
|
|
|
|
'avg_profit': 0.1,
|
|
|
|
'total_profit': 0.001,
|
|
|
|
'profit': 1.0,
|
|
|
|
'duration': 20.0
|
|
|
|
}
|
|
|
|
}])
|
2019-09-04 21:38:15 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': 'sell', })
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
|
|
|
# TODO: buy_strategy_generator() is actually not called because
|
|
|
|
# run_optimizer_parallel() is mocked
|
|
|
|
del hyperopt.custom_hyperopt.__class__.buy_strategy_generator
|
|
|
|
del hyperopt.custom_hyperopt.__class__.indicator_space
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
|
|
|
|
parallel.assert_called_once()
|
|
|
|
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert 'Best result:\n\n* 1/1: foo result Objective: 1.00000\n' in out
|
|
|
|
assert dumper.called
|
2020-03-08 10:35:31 +00:00
|
|
|
# Should be called twice, once for historical candle data, once to save evaluations
|
2019-09-04 21:38:15 +00:00
|
|
|
assert dumper.call_count == 2
|
2019-09-18 19:57:37 +00:00
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_sell")
|
|
|
|
assert hasattr(hyperopt.backtesting.strategy, "advise_buy")
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "max_open_trades")
|
2020-08-06 06:51:01 +00:00
|
|
|
assert hyperopt.max_open_trades == hyperopt_conf['max_open_trades']
|
2019-09-04 21:38:15 +00:00
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
|
|
|
|
|
|
|
|
2019-09-06 12:08:44 +00:00
|
|
|
@pytest.mark.parametrize("method,space", [
|
|
|
|
('buy_strategy_generator', 'buy'),
|
|
|
|
('indicator_space', 'buy'),
|
|
|
|
('sell_strategy_generator', 'sell'),
|
|
|
|
('sell_indicator_space', 'sell'),
|
|
|
|
])
|
2020-08-06 06:51:01 +00:00
|
|
|
def test_simplified_interface_failed(mocker, hyperopt_conf, method, space) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2020-09-27 17:48:11 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
2019-10-27 09:56:38 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.load_bt_data',
|
|
|
|
MagicMock(return_value=(MagicMock(), None)))
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch(
|
2019-12-17 22:06:03 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timerange',
|
2019-09-04 21:38:15 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt_conf.update({'spaces': space})
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2020-08-06 06:51:01 +00:00
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
hyperopt.backtesting.strategy.ohlcvdata_to_dataframe = MagicMock()
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={})
|
|
|
|
|
2019-09-06 12:08:44 +00:00
|
|
|
delattr(hyperopt.custom_hyperopt.__class__, method)
|
2019-09-04 21:38:15 +00:00
|
|
|
|
2019-09-06 12:08:44 +00:00
|
|
|
with pytest.raises(OperationalException, match=f"The '{space}' space is included into *"):
|
2019-09-04 21:38:15 +00:00
|
|
|
hyperopt.start()
|
2020-10-05 06:07:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_print_epoch_details(capsys):
|
|
|
|
test_result = {
|
|
|
|
'params_details': {
|
|
|
|
'trailing': {
|
|
|
|
'trailing_stop': True,
|
|
|
|
'trailing_stop_positive': 0.02,
|
|
|
|
'trailing_stop_positive_offset': 0.04,
|
|
|
|
'trailing_only_offset_is_reached': True
|
|
|
|
},
|
|
|
|
'roi': {
|
|
|
|
0: 0.18,
|
|
|
|
90: 0.14,
|
|
|
|
225: 0.05,
|
|
|
|
430: 0},
|
|
|
|
},
|
|
|
|
'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-03-17 19:43:51 +00:00
|
|
|
HyperoptTools.print_epoch_details(test_result, 5, False, no_header=True)
|
2020-10-05 06:07:53 +00:00
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert '# Trailing stop:' in captured.out
|
|
|
|
# re.match(r"Pairs for .*", captured.out)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-03-29 17:27:19 +00:00
|
|
|
|
|
|
|
|
2021-04-25 06:25:12 +00:00
|
|
|
def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None:
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
|
2021-03-29 17:27:19 +00:00
|
|
|
(Path(tmpdir) / 'hyperopt_results').mkdir(parents=True)
|
|
|
|
# No hyperopt needed
|
|
|
|
del hyperopt_conf['hyperopt']
|
|
|
|
hyperopt_conf.update({
|
|
|
|
'strategy': 'HyperoptableStrategy',
|
|
|
|
'user_data_dir': Path(tmpdir),
|
|
|
|
})
|
|
|
|
hyperopt = Hyperopt(hyperopt_conf)
|
|
|
|
assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto)
|
2021-04-24 05:18:35 +00:00
|
|
|
assert isinstance(hyperopt.backtesting.strategy.buy_rsi, IntParameter)
|
|
|
|
|
2021-05-01 14:36:53 +00:00
|
|
|
assert hyperopt.backtesting.strategy.buy_rsi.in_space is True
|
2021-04-24 05:18:35 +00:00
|
|
|
assert hyperopt.backtesting.strategy.buy_rsi.value == 35
|
|
|
|
buy_rsi_range = hyperopt.backtesting.strategy.buy_rsi.range
|
|
|
|
assert isinstance(buy_rsi_range, range)
|
|
|
|
# Range from 0 - 50 (inclusive)
|
|
|
|
assert len(list(buy_rsi_range)) == 51
|
2021-03-29 17:27:19 +00:00
|
|
|
|
|
|
|
hyperopt.start()
|
2021-04-09 20:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_SKDecimal():
|
|
|
|
space = SKDecimal(1, 2, decimals=2)
|
|
|
|
assert 1.5 in space
|
|
|
|
assert 2.5 not in space
|
|
|
|
assert space.low == 100
|
|
|
|
assert space.high == 200
|
|
|
|
|
|
|
|
assert space.inverse_transform([200]) == [2.0]
|
|
|
|
assert space.inverse_transform([100]) == [1.0]
|
|
|
|
assert space.inverse_transform([150, 160]) == [1.5, 1.6]
|
|
|
|
|
|
|
|
assert space.transform([1.5]) == [150]
|
|
|
|
assert space.transform([2.0]) == [200]
|
|
|
|
assert space.transform([1.0]) == [100]
|
|
|
|
assert space.transform([1.5, 1.6]) == [150, 160]
|