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
|
2019-04-26 17:51:24 +00:00
|
|
|
from datetime import datetime
|
2019-09-08 07:54:15 +00:00
|
|
|
from pathlib import Path
|
2019-07-21 13:56:44 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock
|
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
|
|
|
|
2019-09-08 00:19:21 +00:00
|
|
|
from freqtrade import OperationalException
|
2018-12-15 14:34:25 +00:00
|
|
|
from freqtrade.data.converter import parse_ticker_dataframe
|
2018-12-13 05:35:28 +00:00
|
|
|
from freqtrade.data.history import load_tickerdata_file
|
2019-05-25 18:01:43 +00:00
|
|
|
from freqtrade.optimize import setup_configuration, start_hyperopt
|
2019-10-18 20:29:19 +00:00
|
|
|
from freqtrade.optimize.default_hyperopt import DefaultHyperOpt
|
2019-07-16 04:50:25 +00:00
|
|
|
from freqtrade.optimize.default_hyperopt_loss import DefaultHyperOptLoss
|
2019-07-21 14:07:06 +00:00
|
|
|
from freqtrade.optimize.hyperopt import Hyperopt
|
2019-09-08 07:54:15 +00:00
|
|
|
from freqtrade.resolvers.hyperopt_resolver import (HyperOptLossResolver,
|
|
|
|
HyperOptResolver)
|
2019-04-22 18:24:45 +00:00
|
|
|
from freqtrade.state import RunMode
|
2019-07-15 19:54:41 +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
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2018-07-30 10:26:54 +00:00
|
|
|
def hyperopt(default_conf, mocker):
|
2019-08-01 20:57:50 +00:00
|
|
|
default_conf.update({'spaces': ['all']})
|
2018-07-30 10:26:54 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
return Hyperopt(default_conf)
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2019-07-15 19:54:41 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def hyperopt_results():
|
|
|
|
return pd.DataFrame(
|
|
|
|
{
|
|
|
|
'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
|
2019-09-16 12:05:39 +00:00
|
|
|
'profit_percent': [-0.1, 0.2, 0.3],
|
|
|
|
'profit_abs': [-0.2, 0.4, 0.6],
|
2019-07-15 19:54:41 +00:00
|
|
|
'trade_duration': [10, 30, 10],
|
2019-09-16 12:05:39 +00:00
|
|
|
'sell_reason': [SellType.STOP_LOSS, SellType.ROI, SellType.ROI]
|
2019-07-15 19:54:41 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
# Functions for recurrent object patching
|
2019-09-08 07:54:15 +00:00
|
|
|
def create_trials(mocker, hyperopt, testdatadir) -> None:
|
2018-01-09 09:37:27 +00:00
|
|
|
"""
|
|
|
|
When creating trials, mock the hyperopt Trials so that *by default*
|
|
|
|
- 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
|
|
|
|
"""
|
2019-09-08 08:07:09 +00:00
|
|
|
hyperopt.trials_file = testdatadir / 'optimize/ut_trials.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()
|
|
|
|
stat_mock.st_size = PropertyMock(return_value=1)
|
|
|
|
mocker.patch.object(Path, "stat", MagicMock(return_value=False))
|
|
|
|
|
|
|
|
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)
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-06-24 12:27:53 +00:00
|
|
|
return [{'loss': 1, 'result': 'foo', 'params': {}}]
|
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 = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'hyperopt'
|
|
|
|
]
|
|
|
|
|
2019-05-25 18:01:43 +00:00
|
|
|
config = setup_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)
|
2019-04-22 18:24:45 +00:00
|
|
|
assert 'ticker_interval' 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 = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--datadir', '/foo/bar',
|
|
|
|
'hyperopt',
|
|
|
|
'--ticker-interval', '1m',
|
|
|
|
'--timerange', ':100',
|
|
|
|
'--enable-position-stacking',
|
|
|
|
'--disable-max-market-positions',
|
|
|
|
'--epochs', '1000',
|
|
|
|
'--spaces', 'all',
|
|
|
|
'--print-all'
|
|
|
|
]
|
|
|
|
|
2019-05-25 18:01:43 +00:00
|
|
|
config = setup_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)
|
2019-04-22 18:24:45 +00:00
|
|
|
assert 'ticker_interval' in config
|
2019-04-24 19:24:00 +00:00
|
|
|
assert log_has('Parameter -i/--ticker-interval detected ... Using ticker_interval: 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
|
|
|
|
|
|
|
|
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(
|
|
|
|
'freqtrade.resolvers.hyperopt_resolver.HyperOptResolver._load_hyperopt',
|
2019-10-18 20:29:19 +00:00
|
|
|
MagicMock(return_value=hyperopt(default_conf))
|
2019-01-06 18:38:32 +00:00
|
|
|
)
|
|
|
|
x = HyperOptResolver(default_conf, ).hyperopt
|
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)
|
2019-06-21 05:07:39 +00:00
|
|
|
assert hasattr(x, "ticker_interval")
|
2019-01-06 18:38:32 +00:00
|
|
|
|
|
|
|
|
2019-07-28 13:19:17 +00:00
|
|
|
def test_hyperoptresolver_wrongname(mocker, default_conf, caplog) -> None:
|
|
|
|
default_conf.update({'hyperopt': "NonExistingHyperoptClass"})
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'):
|
|
|
|
HyperOptResolver(default_conf, ).hyperopt
|
|
|
|
|
|
|
|
|
2019-07-16 04:50:25 +00:00
|
|
|
def test_hyperoptlossresolver(mocker, default_conf, caplog) -> None:
|
|
|
|
|
|
|
|
hl = DefaultHyperOptLoss
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.resolvers.hyperopt_resolver.HyperOptLossResolver._load_hyperoptloss',
|
|
|
|
MagicMock(return_value=hl)
|
|
|
|
)
|
2019-07-28 13:19:17 +00:00
|
|
|
x = HyperOptLossResolver(default_conf, ).hyperoptloss
|
|
|
|
assert hasattr(x, "hyperopt_loss_function")
|
|
|
|
|
|
|
|
|
|
|
|
def test_hyperoptlossresolver_wrongname(mocker, default_conf, caplog) -> None:
|
|
|
|
default_conf.update({'hyperopt_loss': "NonExistingLossClass"})
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r'Impossible to load HyperoptLoss.*'):
|
|
|
|
HyperOptLossResolver(default_conf, ).hyperopt
|
2019-07-16 04:50:25 +00:00
|
|
|
|
|
|
|
|
2019-09-25 09:54:57 +00:00
|
|
|
def test_start_not_installed(mocker, default_conf, caplog, 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 = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r"Please ensure that the hyperopt dependencies"):
|
|
|
|
start_hyperopt(args)
|
|
|
|
|
|
|
|
|
2018-03-06 06:02:03 +00:00
|
|
|
def test_start(mocker, default_conf, caplog) -> None:
|
|
|
|
start_mock = MagicMock()
|
2019-07-11 21:39:42 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_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 = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
2019-05-25 18:01:43 +00:00
|
|
|
start_hyperopt(args)
|
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
|
|
|
|
|
|
|
|
|
2019-05-14 06:23:09 +00:00
|
|
|
def test_start_no_data(mocker, default_conf, caplog) -> None:
|
2019-07-11 21:39:42 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
2019-10-23 18:27:51 +00:00
|
|
|
mocker.patch('freqtrade.data.history.load_pair_history', MagicMock(return_value=None))
|
2019-05-14 06:23:09 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
2019-10-23 18:27:51 +00:00
|
|
|
with pytest.raises(OperationalException, match='No data found. Terminating.'):
|
|
|
|
start_hyperopt(args)
|
2019-05-14 06:23:09 +00:00
|
|
|
|
|
|
|
|
2019-05-25 18:25:59 +00:00
|
|
|
def test_start_filelock(mocker, default_conf, caplog) -> None:
|
2019-07-21 14:07:06 +00:00
|
|
|
start_mock = MagicMock(side_effect=Timeout(Hyperopt.get_lock_filename(default_conf)))
|
2019-07-11 21:39:42 +00:00
|
|
|
patched_configuration_load_config_file(mocker, default_conf)
|
2019-05-25 18:25:59 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
|
|
|
start_hyperopt(args)
|
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
|
|
|
|
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
def test_loss_calculation_prefer_correct_trade_count(default_conf, hyperopt_results) -> None:
|
|
|
|
hl = HyperOptLossResolver(default_conf).hyperoptloss
|
|
|
|
correct = hl.hyperopt_loss_function(hyperopt_results, 600)
|
|
|
|
over = hl.hyperopt_loss_function(hyperopt_results, 600 + 100)
|
|
|
|
under = hl.hyperopt_loss_function(hyperopt_results, 600 - 100)
|
2018-03-02 13:46:32 +00:00
|
|
|
assert over > correct
|
|
|
|
assert under > correct
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
def test_loss_calculation_prefer_shorter_trades(default_conf, hyperopt_results) -> None:
|
2019-07-15 19:54:41 +00:00
|
|
|
resultsb = hyperopt_results.copy()
|
2019-07-27 20:24:06 +00:00
|
|
|
resultsb.loc[1, 'trade_duration'] = 20
|
2019-07-15 19:54:41 +00:00
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
hl = HyperOptLossResolver(default_conf).hyperoptloss
|
|
|
|
longer = hl.hyperopt_loss_function(hyperopt_results, 100)
|
|
|
|
shorter = hl.hyperopt_loss_function(resultsb, 100)
|
2018-03-02 13:46:32 +00:00
|
|
|
assert shorter < longer
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
def test_loss_calculation_has_limited_profit(default_conf, hyperopt_results) -> None:
|
2019-07-15 19:54:41 +00:00
|
|
|
results_over = hyperopt_results.copy()
|
|
|
|
results_over['profit_percent'] = hyperopt_results['profit_percent'] * 2
|
|
|
|
results_under = hyperopt_results.copy()
|
|
|
|
results_under['profit_percent'] = hyperopt_results['profit_percent'] / 2
|
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
hl = HyperOptLossResolver(default_conf).hyperoptloss
|
|
|
|
correct = hl.hyperopt_loss_function(hyperopt_results, 600)
|
|
|
|
over = hl.hyperopt_loss_function(results_over, 600)
|
|
|
|
under = hl.hyperopt_loss_function(results_under, 600)
|
2019-07-15 20:52:33 +00:00
|
|
|
assert over < correct
|
|
|
|
assert under > correct
|
2019-07-15 20:59:28 +00:00
|
|
|
|
2019-07-15 20:52:33 +00:00
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
def test_sharpe_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None:
|
2019-07-15 20:52:33 +00:00
|
|
|
results_over = hyperopt_results.copy()
|
|
|
|
results_over['profit_percent'] = hyperopt_results['profit_percent'] * 2
|
|
|
|
results_under = hyperopt_results.copy()
|
|
|
|
results_under['profit_percent'] = hyperopt_results['profit_percent'] / 2
|
|
|
|
|
2019-07-16 04:45:13 +00:00
|
|
|
default_conf.update({'hyperopt_loss': 'SharpeHyperOptLoss'})
|
|
|
|
hl = HyperOptLossResolver(default_conf).hyperoptloss
|
2019-07-25 17:54:12 +00:00
|
|
|
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
|
|
|
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
|
|
|
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
|
|
|
assert over < correct
|
|
|
|
assert under > correct
|
|
|
|
|
|
|
|
|
|
|
|
def test_onlyprofit_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None:
|
|
|
|
results_over = hyperopt_results.copy()
|
|
|
|
results_over['profit_percent'] = hyperopt_results['profit_percent'] * 2
|
|
|
|
results_under = hyperopt_results.copy()
|
|
|
|
results_under['profit_percent'] = hyperopt_results['profit_percent'] / 2
|
|
|
|
|
|
|
|
default_conf.update({'hyperopt_loss': 'OnlyProfitHyperOptLoss'})
|
|
|
|
hl = HyperOptLossResolver(default_conf).hyperoptloss
|
2019-07-16 04:45:13 +00:00
|
|
|
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
|
|
|
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
|
|
|
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
|
|
|
|
datetime(2019, 1, 1), datetime(2019, 5, 1))
|
2019-07-15 19:54:41 +00:00
|
|
|
assert over < correct
|
2018-03-02 13:46:32 +00:00
|
|
|
assert under > correct
|
2017-12-26 08:08:10 +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
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt.log_results(
|
|
|
|
{
|
|
|
|
'loss': 1,
|
2019-07-30 08:47:46 +00:00
|
|
|
'current_epoch': 1,
|
|
|
|
'results_explanation': 'foo.',
|
|
|
|
'is_initial_point': False
|
2018-03-02 13:46:32 +00:00
|
|
|
}
|
|
|
|
)
|
2018-03-25 20:57:40 +00:00
|
|
|
out, err = capsys.readouterr()
|
2019-05-11 07:17:46 +00:00
|
|
|
assert ' 2/2: foo. Objective: 1.00000' in out
|
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
|
|
|
|
hyperopt.log_results(
|
|
|
|
{
|
|
|
|
'loss': 3,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert caplog.record_tuples == []
|
2018-01-07 01:12:32 +00:00
|
|
|
|
|
|
|
|
2019-09-08 08:07:09 +00:00
|
|
|
def test_save_trials_saves_trials(mocker, hyperopt, testdatadir, caplog) -> None:
|
|
|
|
trials = create_trials(mocker, hyperopt, testdatadir)
|
2018-07-03 19:51:48 +00:00
|
|
|
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
2018-07-30 10:26:54 +00:00
|
|
|
hyperopt.trials = trials
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt.save_trials()
|
|
|
|
|
2019-09-08 08:07:09 +00:00
|
|
|
trials_file = testdatadir / 'optimize' / 'ut_trials.pickle'
|
|
|
|
assert log_has(f"Saving 1 evaluations to '{trials_file}'", caplog)
|
2018-03-02 13:46:32 +00:00
|
|
|
mock_dump.assert_called_once()
|
2018-01-09 10:19:44 +00:00
|
|
|
|
|
|
|
|
2019-09-08 08:07:09 +00:00
|
|
|
def test_read_trials_returns_trials_file(mocker, hyperopt, testdatadir, caplog) -> None:
|
|
|
|
trials = create_trials(mocker, hyperopt, testdatadir)
|
2018-07-03 19:51:48 +00:00
|
|
|
mock_load = mocker.patch('freqtrade.optimize.hyperopt.load', return_value=trials)
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt_trial = hyperopt.read_trials()
|
2019-09-08 08:07:09 +00:00
|
|
|
trials_file = testdatadir / 'optimize' / 'ut_trials.pickle'
|
|
|
|
assert log_has(f"Reading Trials from '{trials_file}'", caplog)
|
2018-03-02 13:46:32 +00:00
|
|
|
assert hyperopt_trial == trials
|
2018-01-09 10:19:44 +00:00
|
|
|
mock_load.assert_called_once()
|
2018-01-25 08:45:53 +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
|
|
|
|
|
|
|
|
2019-07-30 08:47:46 +00:00
|
|
|
def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None:
|
2018-07-03 19:09:59 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
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',
|
2019-08-20 20:28:16 +00:00
|
|
|
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result',
|
|
|
|
'params': {'buy': {}, 'sell': {}, 'roi': {}, 'stoploss': 0.0}}])
|
2018-06-24 12:27:53 +00:00
|
|
|
)
|
2018-06-17 20:32:56 +00:00
|
|
|
patch_exchange(mocker)
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2019-06-08 17:32:31 +00:00
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1, })
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
hyperopt = Hyperopt(default_conf)
|
2019-08-23 21:10:55 +00:00
|
|
|
hyperopt.backtesting.strategy.tickerdata_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
|
2019-06-08 17:32:31 +00:00
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
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")
|
|
|
|
assert hyperopt.max_open_trades == default_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
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_format_results(hyperopt):
|
2018-06-02 21:07:31 +00:00
|
|
|
# Test with BTC as stake_currency
|
2018-03-04 00:42:37 +00:00
|
|
|
trades = [
|
2018-02-03 16:15:40 +00:00
|
|
|
('ETH/BTC', 2, 2, 123),
|
|
|
|
('LTC/BTC', 1, 1, 123),
|
|
|
|
('XPR/BTC', -1, -2, -246)
|
2018-03-04 00:42:37 +00:00
|
|
|
]
|
2018-06-10 11:56:23 +00:00
|
|
|
labels = ['currency', 'profit_percent', 'profit_abs', 'trade_duration']
|
2018-02-08 19:49:43 +00:00
|
|
|
df = pd.DataFrame.from_records(trades, columns=labels)
|
2018-06-02 21:07:31 +00:00
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
result = hyperopt.format_results(df)
|
2018-06-02 21:07:31 +00:00
|
|
|
assert result.find(' 66.67%')
|
|
|
|
assert result.find('Total profit 1.00000000 BTC')
|
|
|
|
assert result.find('2.0000Σ %')
|
|
|
|
|
|
|
|
# Test with EUR as stake_currency
|
|
|
|
trades = [
|
|
|
|
('ETH/EUR', 2, 2, 123),
|
|
|
|
('LTC/EUR', 1, 1, 123),
|
|
|
|
('XPR/EUR', -1, -2, -246)
|
|
|
|
]
|
|
|
|
df = pd.DataFrame.from_records(trades, columns=labels)
|
2018-07-30 10:26:54 +00:00
|
|
|
result = hyperopt.format_results(df)
|
2018-06-02 21:07:31 +00:00
|
|
|
assert result.find('Total profit 1.00000000 EUR')
|
2018-02-08 19:49:43 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_has_space(hyperopt):
|
|
|
|
hyperopt.config.update({'spaces': ['buy', 'roi']})
|
|
|
|
assert hyperopt.has_space('roi')
|
|
|
|
assert hyperopt.has_space('buy')
|
|
|
|
assert not hyperopt.has_space('stoploss')
|
2018-03-04 08:51:22 +00:00
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
hyperopt.config.update({'spaces': ['all']})
|
|
|
|
assert hyperopt.has_space('buy')
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
|
2019-09-07 18:56:03 +00:00
|
|
|
def test_populate_indicators(hyperopt, testdatadir) -> None:
|
|
|
|
tick = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
|
2019-06-15 11:47:20 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
|
|
|
|
fill_missing=True)}
|
2019-08-23 21:10:55 +00:00
|
|
|
dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist)
|
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:
|
|
|
|
tick = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
|
2019-06-15 11:47:20 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
|
|
|
|
fill_missing=True)}
|
2019-08-23 21:10:55 +00:00
|
|
|
dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist)
|
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']
|
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_generate_optimizer(mocker, default_conf) -> None:
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.update({'config': 'config.json.example'})
|
|
|
|
default_conf.update({'timerange': None})
|
|
|
|
default_conf.update({'spaces': 'all'})
|
2019-05-01 12:55:56 +00:00
|
|
|
default_conf.update({'hyperopt_min_trades': 1})
|
2018-03-06 06:02:03 +00:00
|
|
|
|
|
|
|
trades = [
|
2019-10-30 08:20:56 +00:00
|
|
|
('TRX/BTC', 0.023117, 0.000233, 100)
|
2018-03-06 06:02:03 +00:00
|
|
|
]
|
2018-06-10 11:56:23 +00:00
|
|
|
labels = ['currency', 'profit_percent', 'profit_abs', 'trade_duration']
|
2018-03-06 06:02:03 +00:00
|
|
|
backtest_result = pd.DataFrame.from_records(trades, columns=labels)
|
|
|
|
|
|
|
|
mocker.patch(
|
2019-08-23 21:10:55 +00:00
|
|
|
'freqtrade.optimize.hyperopt.Backtesting.backtest',
|
2018-03-06 06:02:03 +00:00
|
|
|
MagicMock(return_value=backtest_result)
|
|
|
|
)
|
2018-10-16 18:32:33 +00:00
|
|
|
mocker.patch(
|
2018-11-04 12:43:09 +00:00
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
2019-07-15 19:54:41 +00:00
|
|
|
MagicMock(return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))
|
2018-10-16 18:32:33 +00:00
|
|
|
)
|
2018-06-17 20:32:56 +00:00
|
|
|
patch_exchange(mocker)
|
2018-07-03 19:09:59 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load', MagicMock())
|
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,
|
|
|
|
}
|
|
|
|
response_expected = {
|
|
|
|
'loss': 1.9840569076926293,
|
2019-11-06 18:33:15 +00:00
|
|
|
'results_explanation': (' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC '
|
|
|
|
'( 2.31\N{GREEK CAPITAL LETTER SIGMA}%). Avg duration 100.0 mins.'
|
|
|
|
).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'),
|
2019-08-03 16:13:18 +00:00
|
|
|
'params': optimizer_param,
|
|
|
|
'total_profit': 0.00023300
|
2018-03-06 06:02:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
hyperopt = Hyperopt(default_conf)
|
2019-09-16 18:53:19 +00:00
|
|
|
hyperopt.dimensions = hyperopt.hyperopt_space()
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_clean_hyperopt(mocker, default_conf, caplog):
|
|
|
|
patch_exchange(mocker)
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
})
|
|
|
|
mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True))
|
|
|
|
unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock())
|
2019-07-21 14:07:06 +00:00
|
|
|
h = Hyperopt(default_conf)
|
2019-07-15 18:27:34 +00:00
|
|
|
|
|
|
|
assert unlinkmock.call_count == 2
|
2019-08-18 13:00:12 +00:00
|
|
|
assert log_has(f"Removing `{h.tickerdata_pickle}`.", caplog)
|
2019-07-16 03:50:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_continue_hyperopt(mocker, default_conf, caplog):
|
|
|
|
patch_exchange(mocker)
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
'hyperopt_continue': True
|
|
|
|
})
|
|
|
|
mocker.patch("freqtrade.optimize.hyperopt.Path.is_file", MagicMock(return_value=True))
|
|
|
|
unlinkmock = mocker.patch("freqtrade.optimize.hyperopt.Path.unlink", MagicMock())
|
|
|
|
Hyperopt(default_conf)
|
|
|
|
|
|
|
|
assert unlinkmock.call_count == 0
|
2019-08-11 18:16:52 +00:00
|
|
|
assert log_has(f"Continuing on previous hyperopt results.", caplog)
|
2019-08-15 21:49:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None:
|
2019-08-16 01:20:12 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
|
|
|
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}])
|
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
'print_json': True,
|
|
|
|
})
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
2019-08-23 21:10:55 +00:00
|
|
|
hyperopt.backtesting.strategy.tickerdata_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 '{"params":{"mfi-value":null,"fastd-value":null,"adx-value":null,"rsi-value":null,"mfi-enabled":null,"fastd-enabled":null,"adx-enabled":null,"rsi-enabled":null,"trigger":null,"sell-mfi-value":null,"sell-fastd-value":null,"sell-adx-value":null,"sell-rsi-value":null,"sell-mfi-enabled":null,"sell-fastd-enabled":null,"sell-adx-enabled":null,"sell-rsi-enabled":null,"sell-trigger":null},"minimal_roi":{},"stoploss":null}' in out # noqa: E501
|
2019-08-16 01:20:12 +00:00
|
|
|
assert dumper.called
|
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
assert dumper.call_count == 2
|
2019-08-15 21:49:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) -> None:
|
2019-08-16 01:20:12 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
|
|
|
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}])
|
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'roi stoploss',
|
|
|
|
'hyperopt_jobs': 1,
|
|
|
|
'print_json': True,
|
|
|
|
})
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
2019-08-23 21:10:55 +00:00
|
|
|
hyperopt.backtesting.strategy.tickerdata_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
|
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
assert dumper.call_count == 2
|
2019-09-04 21:38:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_simplified_interface_roi_stoploss(mocker, default_conf, caplog, capsys) -> None:
|
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
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=[{
|
|
|
|
'loss': 1, 'results_explanation': 'foo result', 'params': {'stoploss': 0.0}}])
|
2019-09-04 21:38:15 +00:00
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'roi stoploss',
|
|
|
|
'hyperopt_jobs': 1, })
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
|
|
|
hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock()
|
|
|
|
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
|
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
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")
|
|
|
|
assert hyperopt.max_open_trades == default_conf['max_open_trades']
|
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
|
|
|
|
|
|
|
|
|
|
|
def test_simplified_interface_all_failed(mocker, default_conf, caplog, capsys) -> None:
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'all',
|
|
|
|
'hyperopt_jobs': 1, })
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
|
|
|
hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock()
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
def test_simplified_interface_buy(mocker, default_conf, caplog, capsys) -> None:
|
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
|
|
|
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}])
|
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'buy',
|
|
|
|
'hyperopt_jobs': 1, })
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
|
|
|
hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock()
|
|
|
|
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
|
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
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")
|
|
|
|
assert hyperopt.max_open_trades == default_conf['max_open_trades']
|
|
|
|
assert hasattr(hyperopt, "position_stacking")
|
|
|
|
|
|
|
|
|
|
|
|
def test_simplified_interface_sell(mocker, default_conf, caplog, capsys) -> None:
|
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
parallel = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
|
|
|
|
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}])
|
|
|
|
)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
|
|
|
'spaces': 'sell',
|
|
|
|
'hyperopt_jobs': 1, })
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
|
|
|
hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock()
|
|
|
|
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
|
|
|
|
# Should be called twice, once for tickerdata, once to save evaluations
|
|
|
|
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")
|
|
|
|
assert hyperopt.max_open_trades == default_conf['max_open_trades']
|
|
|
|
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'),
|
|
|
|
])
|
|
|
|
def test_simplified_interface_failed(mocker, default_conf, caplog, capsys, method, space) -> None:
|
2019-09-04 21:38:15 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
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(
|
|
|
|
'freqtrade.optimize.hyperopt.get_timeframe',
|
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
default_conf.update({'config': 'config.json.example',
|
|
|
|
'epochs': 1,
|
|
|
|
'timerange': None,
|
2019-09-06 12:08:44 +00:00
|
|
|
'spaces': space,
|
2019-09-04 21:38:15 +00:00
|
|
|
'hyperopt_jobs': 1, })
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(default_conf)
|
|
|
|
hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock()
|
|
|
|
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()
|