2017-12-26 08:08:10 +00:00
|
|
|
# pragma pylint: disable=missing-docstring,W0212,C0103
|
2019-04-22 18:24:45 +00:00
|
|
|
import json
|
2018-03-02 13:46:32 +00:00
|
|
|
import os
|
2019-04-26 17:51:24 +00:00
|
|
|
from datetime import datetime
|
2018-03-02 13:46:32 +00:00
|
|
|
from unittest.mock import MagicMock
|
2019-05-25 18:25:59 +00:00
|
|
|
from filelock import Timeout
|
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
|
2018-03-15 22:37:34 +00:00
|
|
|
|
2019-04-22 18:24:45 +00:00
|
|
|
from freqtrade import DependencyException
|
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-01-06 18:38:32 +00:00
|
|
|
from freqtrade.optimize.default_hyperopt import DefaultHyperOpts
|
2019-05-25 18:25:59 +00:00
|
|
|
from freqtrade.optimize.hyperopt import Hyperopt, HYPEROPT_LOCKFILE
|
2019-05-25 18:01:43 +00:00
|
|
|
from freqtrade.optimize import setup_configuration, start_hyperopt
|
2019-05-25 14:53:35 +00:00
|
|
|
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
|
2019-04-22 18:24:45 +00:00
|
|
|
from freqtrade.state import RunMode
|
2019-04-24 19:24:00 +00:00
|
|
|
from freqtrade.tests.conftest import log_has, log_has_re, patch_exchange
|
2018-03-06 06:02:03 +00:00
|
|
|
from freqtrade.tests.optimize.test_backtesting import get_args
|
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):
|
|
|
|
patch_exchange(mocker)
|
|
|
|
return Hyperopt(default_conf)
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
# Functions for recurrent object patching
|
2018-07-30 10:26:54 +00:00
|
|
|
def create_trials(mocker, hyperopt) -> 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
|
|
|
|
"""
|
2018-07-30 10:26:54 +00:00
|
|
|
hyperopt.trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle')
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=False)
|
2018-03-06 06:02:03 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1)
|
2018-03-02 13:46:32 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.os.remove', 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:
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
|
|
|
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
|
|
|
|
assert log_has(
|
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'ticker_interval' in config
|
2019-04-24 19:24:00 +00:00
|
|
|
assert not log_has_re('Parameter -i/--ticker-interval detected .*', caplog.record_tuples)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'live' not in config
|
|
|
|
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'position_stacking' not in config
|
|
|
|
assert not log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'refresh_pairs' not in config
|
|
|
|
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
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:
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
mocker.patch('freqtrade.configuration.Configuration._create_datadir', lambda s, c, x: x)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--datadir', '/foo/bar',
|
|
|
|
'hyperopt',
|
|
|
|
'--ticker-interval', '1m',
|
|
|
|
'--timerange', ':100',
|
|
|
|
'--refresh-pairs-cached',
|
|
|
|
'--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
|
|
|
|
|
|
|
|
assert log_has(
|
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
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 ...',
|
|
|
|
caplog.record_tuples)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'position_stacking' in config
|
|
|
|
assert log_has('Parameter --enable-position-stacking detected ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'use_max_market_positions' in config
|
|
|
|
assert log_has('Parameter --disable-max-market-positions detected ...', caplog.record_tuples)
|
|
|
|
assert log_has('max_open_trades set to unlimited ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'refresh_pairs' in config
|
|
|
|
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'timerange' in config
|
|
|
|
assert log_has(
|
|
|
|
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
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 ...',
|
|
|
|
caplog.record_tuples)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
assert 'spaces' in config
|
|
|
|
assert log_has(
|
|
|
|
'Parameter -s/--spaces detected: {}'.format(config['spaces']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'print_all' in config
|
2019-04-24 19:12:23 +00:00
|
|
|
assert log_has('Parameter --print-all detected ...', caplog.record_tuples)
|
2019-04-22 18:24:45 +00:00
|
|
|
|
|
|
|
|
2019-01-06 18:38:32 +00:00
|
|
|
def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
hyperopts = DefaultHyperOpts
|
|
|
|
delattr(hyperopts, 'populate_buy_trend')
|
|
|
|
delattr(hyperopts, 'populate_sell_trend')
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.resolvers.hyperopt_resolver.HyperOptResolver._load_hyperopt',
|
|
|
|
MagicMock(return_value=hyperopts)
|
|
|
|
)
|
|
|
|
x = HyperOptResolver(default_conf, ).hyperopt
|
|
|
|
assert not hasattr(x, 'populate_buy_trend')
|
|
|
|
assert not hasattr(x, 'populate_sell_trend')
|
|
|
|
assert log_has("Custom Hyperopt does not provide populate_sell_trend. "
|
|
|
|
"Using populate_sell_trend from DefaultStrategy.", caplog.record_tuples)
|
|
|
|
assert log_has("Custom Hyperopt does not provide populate_buy_trend. "
|
|
|
|
"Using populate_buy_trend from DefaultStrategy.", caplog.record_tuples)
|
|
|
|
|
|
|
|
|
2018-03-06 06:02:03 +00:00
|
|
|
def test_start(mocker, default_conf, caplog) -> None:
|
|
|
|
start_mock = MagicMock()
|
2018-06-16 11:49:03 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: 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
|
|
|
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(caplog.record_tuples)
|
|
|
|
|
|
|
|
assert log_has(
|
|
|
|
'Starting freqtrade in Hyperopt mode',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert start_mock.call_count == 1
|
|
|
|
|
|
|
|
|
2019-05-14 06:23:09 +00:00
|
|
|
def test_start_no_data(mocker, default_conf, caplog) -> None:
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock(return_value={}))
|
|
|
|
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-05-25 18:01:43 +00:00
|
|
|
start_hyperopt(args)
|
2019-05-14 06:23:09 +00:00
|
|
|
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(caplog.record_tuples)
|
|
|
|
|
|
|
|
assert log_has('No data found. Terminating.', caplog.record_tuples)
|
|
|
|
|
|
|
|
|
2018-09-29 11:49:38 +00:00
|
|
|
def test_start_failure(mocker, default_conf, caplog) -> None:
|
|
|
|
start_mock = MagicMock()
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--strategy', 'TestStrategy',
|
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
2019-04-22 18:24:45 +00:00
|
|
|
with pytest.raises(DependencyException):
|
2019-05-25 18:01:43 +00:00
|
|
|
start_hyperopt(args)
|
2018-09-29 11:49:38 +00:00
|
|
|
assert log_has(
|
|
|
|
"Please don't use --strategy for hyperopt.",
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-05-25 18:25:59 +00:00
|
|
|
def test_start_filelock(mocker, default_conf, caplog) -> None:
|
|
|
|
start_mock = MagicMock(side_effect=Timeout(HYPEROPT_LOCKFILE))
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: 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)
|
|
|
|
start_hyperopt(args)
|
|
|
|
assert log_has(
|
|
|
|
"Another running instance of freqtrade Hyperopt detected.",
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_loss_calculation_prefer_correct_trade_count(hyperopt) -> None:
|
2017-12-26 08:08:10 +00:00
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
correct = hyperopt.calculate_loss(1, hyperopt.target_trades, 20)
|
|
|
|
over = hyperopt.calculate_loss(1, hyperopt.target_trades + 100, 20)
|
|
|
|
under = hyperopt.calculate_loss(1, hyperopt.target_trades - 100, 20)
|
|
|
|
assert over > correct
|
|
|
|
assert under > correct
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_loss_calculation_prefer_shorter_trades(hyperopt) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
shorter = hyperopt.calculate_loss(1, 100, 20)
|
|
|
|
longer = hyperopt.calculate_loss(1, 100, 30)
|
|
|
|
assert shorter < longer
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_loss_calculation_has_limited_profit(hyperopt) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
correct = hyperopt.calculate_loss(hyperopt.expected_max_profit, hyperopt.target_trades, 20)
|
|
|
|
over = hyperopt.calculate_loss(hyperopt.expected_max_profit * 2, hyperopt.target_trades, 20)
|
|
|
|
under = hyperopt.calculate_loss(hyperopt.expected_max_profit / 2, hyperopt.target_trades, 20)
|
|
|
|
assert over == correct
|
|
|
|
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
|
|
|
|
hyperopt.log_results(
|
|
|
|
{
|
|
|
|
'loss': 1,
|
|
|
|
'current_tries': 1,
|
|
|
|
'total_tries': 2,
|
2019-05-11 07:17:46 +00:00
|
|
|
'result': 'foo.',
|
|
|
|
'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
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_save_trials_saves_trials(mocker, hyperopt, caplog) -> None:
|
|
|
|
trials = create_trials(mocker, hyperopt)
|
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()
|
|
|
|
|
2018-03-26 11:14:36 +00:00
|
|
|
trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle')
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-06-24 12:27:53 +00:00
|
|
|
'Saving 1 evaluations to \'{}\''.format(trials_file),
|
2018-03-02 13:46:32 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
mock_dump.assert_called_once()
|
2018-01-09 10:19:44 +00:00
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_read_trials_returns_trials_file(mocker, hyperopt, caplog) -> None:
|
|
|
|
trials = create_trials(mocker, hyperopt)
|
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()
|
2018-03-26 11:14:36 +00:00
|
|
|
trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle')
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-03-26 11:14:36 +00:00
|
|
|
'Reading Trials from \'{}\''.format(trials_file),
|
2018-03-02 13:46:32 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_start_calls_optimizer(mocker, default_conf, caplog) -> None:
|
2018-07-03 19:09:59 +00:00
|
|
|
dumper = mocker.patch('freqtrade.optimize.hyperopt.dump', MagicMock())
|
2018-03-02 13:46:32 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
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',
|
|
|
|
MagicMock(return_value=[{'loss': 1, 'result': 'foo result', 'params': {}}])
|
|
|
|
)
|
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)
|
2018-10-17 17:47:19 +00:00
|
|
|
hyperopt.strategy.tickerdata_to_dataframe = MagicMock()
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
hyperopt.start()
|
2018-06-24 12:27:53 +00:00
|
|
|
parallel.assert_called_once()
|
2019-06-08 17:32:31 +00:00
|
|
|
assert log_has('Best result:\nfoo result\nwith values:\n', caplog.record_tuples)
|
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
|
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
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_populate_indicators(hyperopt) -> None:
|
2018-03-24 09:21:59 +00:00
|
|
|
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
|
2018-12-31 18:15:05 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', fill_missing=True)}
|
2018-10-17 17:47:19 +00:00
|
|
|
dataframes = hyperopt.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
|
|
|
|
|
|
|
|
2018-07-30 10:26:54 +00:00
|
|
|
def test_buy_strategy_generator(hyperopt) -> None:
|
2018-03-24 09:21:59 +00:00
|
|
|
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
|
2018-12-31 18:15:05 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', fill_missing=True)}
|
2018-10-17 17:47:19 +00:00
|
|
|
dataframes = hyperopt.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 = [
|
2018-02-03 16:15:40 +00:00
|
|
|
('POWR/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(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.backtest',
|
|
|
|
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',
|
2018-10-16 18:32:33 +00:00
|
|
|
MagicMock(return_value=(datetime(2017, 12, 10), datetime(2017, 12, 13)))
|
|
|
|
)
|
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-05-12 18:19:20 +00:00
|
|
|
'result': ' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC '
|
|
|
|
'( 2.31Σ%). Avg duration 100.0 mins.',
|
2018-06-24 12:27:53 +00:00
|
|
|
'params': optimizer_param
|
2018-03-06 06:02:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
hyperopt = Hyperopt(default_conf)
|
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
|