2017-12-26 08:08:10 +00:00
|
|
|
# pragma pylint: disable=missing-docstring,W0212,C0103
|
2018-03-02 13:46:32 +00:00
|
|
|
import os
|
2018-04-09 19:12:48 +00:00
|
|
|
import signal
|
2018-03-02 13:46:32 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from unittest.mock import 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
|
2018-03-15 22:37:34 +00:00
|
|
|
|
2018-03-05 08:35:42 +00:00
|
|
|
from freqtrade.optimize.__init__ import load_tickerdata_file
|
2018-03-06 06:02:03 +00:00
|
|
|
from freqtrade.optimize.hyperopt import Hyperopt, start
|
2018-03-24 17:11:21 +00:00
|
|
|
from freqtrade.strategy.resolver import StrategyResolver
|
2018-04-08 09:11:14 +00:00
|
|
|
from freqtrade.tests.conftest import log_has
|
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-03-02 13:46:32 +00:00
|
|
|
# Avoid to reinit the same object again and again
|
2018-04-08 09:11:14 +00:00
|
|
|
_HYPEROPT_INITIALIZED = False
|
|
|
|
_HYPEROPT = None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def init_hyperopt(default_conf, mocker):
|
|
|
|
global _HYPEROPT_INITIALIZED, _HYPEROPT
|
|
|
|
if not _HYPEROPT_INITIALIZED:
|
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True))
|
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock())
|
|
|
|
_HYPEROPT = Hyperopt(default_conf)
|
|
|
|
_HYPEROPT_INITIALIZED = True
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
# Functions for recurrent object patching
|
|
|
|
def create_trials(mocker) -> 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-03-02 15:22:00 +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)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
|
|
|
|
2017-12-26 08:08:10 +00:00
|
|
|
return mocker.Mock(
|
2018-03-02 13:46:32 +00:00
|
|
|
results=[
|
|
|
|
{
|
|
|
|
'loss': 1,
|
|
|
|
'result': 'foo',
|
|
|
|
'status': 'ok'
|
|
|
|
}
|
|
|
|
],
|
2018-01-09 09:37:27 +00:00
|
|
|
best_trial={'misc': {'vals': {'adx': 999}}}
|
2017-12-26 08:08:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
# Unit tests
|
2018-03-06 06:02:03 +00:00
|
|
|
def test_start(mocker, default_conf, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test start() function
|
|
|
|
"""
|
|
|
|
start_mock = MagicMock()
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
|
2018-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.exchange.validate_pairs', MagicMock())
|
|
|
|
|
2018-03-06 06:02:03 +00:00
|
|
|
args = [
|
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-03-06 06:02:03 +00:00
|
|
|
'hyperopt',
|
|
|
|
'--epochs', '5'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
2018-03-24 19:44:04 +00:00
|
|
|
StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-03-06 06:02:03 +00:00
|
|
|
start(args)
|
|
|
|
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(caplog.record_tuples)
|
|
|
|
|
|
|
|
assert log_has(
|
|
|
|
'Starting freqtrade in Hyperopt mode',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert start_mock.call_count == 1
|
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_loss_calculation_prefer_correct_trade_count(init_hyperopt) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.calculate_loss()
|
|
|
|
"""
|
|
|
|
hyperopt = _HYPEROPT
|
2018-03-24 19:44:04 +00:00
|
|
|
StrategyResolver({'strategy': 'DefaultStrategy'})
|
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-04-08 09:11:14 +00:00
|
|
|
def test_loss_calculation_prefer_shorter_trades(init_hyperopt) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.calculate_loss()
|
|
|
|
"""
|
|
|
|
hyperopt = _HYPEROPT
|
2017-12-26 08:08:10 +00:00
|
|
|
|
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-04-08 09:11:14 +00:00
|
|
|
def test_loss_calculation_has_limited_profit(init_hyperopt) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = _HYPEROPT
|
2017-12-26 08:08:10 +00:00
|
|
|
|
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-05-13 11:38:29 +00:00
|
|
|
def test_log_results_if_loss_improves(init_hyperopt, capsys) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = _HYPEROPT
|
|
|
|
hyperopt.current_best_loss = 2
|
|
|
|
hyperopt.log_results(
|
|
|
|
{
|
|
|
|
'loss': 1,
|
|
|
|
'current_tries': 1,
|
|
|
|
'total_tries': 2,
|
|
|
|
'result': 'foo'
|
|
|
|
}
|
|
|
|
)
|
2018-03-25 20:57:40 +00:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert ' 1/2: foo. Loss 1.00000'in out
|
2017-12-26 08:08:10 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_no_log_if_loss_does_not_improve(init_hyperopt, caplog) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = _HYPEROPT
|
|
|
|
hyperopt.current_best_loss = 2
|
|
|
|
hyperopt.log_results(
|
|
|
|
{
|
|
|
|
'loss': 3,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert caplog.record_tuples == []
|
2018-01-07 01:12:32 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_fmin_best_results(mocker, init_hyperopt, default_conf, caplog) -> None:
|
2018-01-07 01:12:32 +00:00
|
|
|
fmin_result = {
|
2018-01-16 11:31:45 +00:00
|
|
|
"macd_below_zero": 0,
|
2018-01-08 20:00:10 +00:00
|
|
|
"adx": 1,
|
|
|
|
"adx-value": 15.0,
|
|
|
|
"fastd": 1,
|
|
|
|
"fastd-value": 40.0,
|
|
|
|
"green_candle": 1,
|
|
|
|
"mfi": 0,
|
|
|
|
"over_sar": 0,
|
|
|
|
"rsi": 1,
|
|
|
|
"rsi-value": 37.0,
|
|
|
|
"trigger": 2,
|
|
|
|
"uptrend_long_ema": 1,
|
|
|
|
"uptrend_short_ema": 0,
|
|
|
|
"uptrend_sma": 0,
|
|
|
|
"stoploss": -0.1,
|
2018-01-24 15:58:44 +00:00
|
|
|
"roi_t1": 1,
|
|
|
|
"roi_t2": 2,
|
|
|
|
"roi_t3": 3,
|
|
|
|
"roi_p1": 1,
|
|
|
|
"roi_p2": 2,
|
|
|
|
"roi_p3": 3,
|
2018-01-07 01:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.update({'config': 'config.json.example'})
|
|
|
|
conf.update({'epochs': 1})
|
|
|
|
conf.update({'timerange': None})
|
2018-03-04 08:51:22 +00:00
|
|
|
conf.update({'spaces': 'all'})
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
2018-01-07 01:12:32 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value=fmin_result)
|
2018-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.exchange.validate_pairs', MagicMock())
|
2018-01-07 01:12:32 +00:00
|
|
|
|
2018-03-24 19:44:04 +00:00
|
|
|
StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = Hyperopt(conf)
|
|
|
|
hyperopt.trials = create_trials(mocker)
|
|
|
|
hyperopt.tickerdata_to_dataframe = MagicMock()
|
|
|
|
hyperopt.start()
|
2018-01-07 01:12:32 +00:00
|
|
|
|
|
|
|
exists = [
|
2018-03-02 13:46:32 +00:00
|
|
|
'Best parameters:',
|
2018-01-07 01:12:32 +00:00
|
|
|
'"adx": {\n "enabled": true,\n "value": 15.0\n },',
|
2018-03-02 13:46:32 +00:00
|
|
|
'"fastd": {\n "enabled": true,\n "value": 40.0\n },',
|
2018-01-07 01:12:32 +00:00
|
|
|
'"green_candle": {\n "enabled": true\n },',
|
2018-03-02 13:46:32 +00:00
|
|
|
'"macd_below_zero": {\n "enabled": false\n },',
|
2018-01-07 01:12:32 +00:00
|
|
|
'"mfi": {\n "enabled": false\n },',
|
2018-03-02 13:46:32 +00:00
|
|
|
'"over_sar": {\n "enabled": false\n },',
|
|
|
|
'"roi_p1": 1.0,',
|
|
|
|
'"roi_p2": 2.0,',
|
|
|
|
'"roi_p3": 3.0,',
|
|
|
|
'"roi_t1": 1.0,',
|
|
|
|
'"roi_t2": 2.0,',
|
|
|
|
'"roi_t3": 3.0,',
|
|
|
|
'"rsi": {\n "enabled": true,\n "value": 37.0\n },',
|
|
|
|
'"stoploss": -0.1,',
|
2018-01-16 11:31:45 +00:00
|
|
|
'"trigger": {\n "type": "faststoch10"\n },',
|
2018-03-02 13:46:32 +00:00
|
|
|
'"uptrend_long_ema": {\n "enabled": true\n },',
|
|
|
|
'"uptrend_short_ema": {\n "enabled": false\n },',
|
|
|
|
'"uptrend_sma": {\n "enabled": false\n }',
|
2018-03-04 00:42:37 +00:00
|
|
|
'ROI table:\n{0: 6.0, 3.0: 3.0, 5.0: 1.0, 6.0: 0}',
|
2018-03-02 13:46:32 +00:00
|
|
|
'Best Result:\nfoo'
|
2018-01-07 01:12:32 +00:00
|
|
|
]
|
|
|
|
for line in exists:
|
|
|
|
assert line in caplog.text
|
2018-01-08 01:45:31 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_fmin_throw_value_error(mocker, init_hyperopt, default_conf, caplog) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
2018-01-08 01:45:31 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.fmin', side_effect=ValueError())
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.update({'config': 'config.json.example'})
|
|
|
|
conf.update({'epochs': 1})
|
|
|
|
conf.update({'timerange': None})
|
2018-03-04 08:51:22 +00:00
|
|
|
conf.update({'spaces': 'all'})
|
2018-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.exchange.validate_pairs', MagicMock())
|
|
|
|
|
2018-03-24 19:44:04 +00:00
|
|
|
StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = Hyperopt(conf)
|
|
|
|
hyperopt.trials = create_trials(mocker)
|
|
|
|
hyperopt.tickerdata_to_dataframe = MagicMock()
|
|
|
|
|
|
|
|
hyperopt.start()
|
2018-01-08 01:45:31 +00:00
|
|
|
|
|
|
|
exists = [
|
|
|
|
'Best Result:',
|
|
|
|
'Sorry, Hyperopt was not able to find good parameters. Please try with more epochs '
|
|
|
|
'(param: -e).',
|
|
|
|
]
|
|
|
|
|
|
|
|
for line in exists:
|
|
|
|
assert line in caplog.text
|
2018-01-09 09:37:27 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_resuming_previous_hyperopt_results_succeeds(mocker, init_hyperopt, default_conf) -> None:
|
2018-01-09 09:37:27 +00:00
|
|
|
trials = create_trials(mocker)
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.update({'config': 'config.json.example'})
|
|
|
|
conf.update({'epochs': 1})
|
|
|
|
conf.update({'timerange': None})
|
2018-03-04 08:51:22 +00:00
|
|
|
conf.update({'spaces': 'all'})
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=True)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.len', return_value=len(trials.results))
|
|
|
|
mock_read = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.read_trials',
|
|
|
|
return_value=trials
|
|
|
|
)
|
|
|
|
mock_save = mocker.patch(
|
|
|
|
'freqtrade.optimize.hyperopt.Hyperopt.save_trials',
|
|
|
|
return_value=None
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.sorted', return_value=trials.results)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
2018-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock())
|
2018-03-02 13:46:32 +00:00
|
|
|
|
2018-03-24 19:44:04 +00:00
|
|
|
StrategyResolver({'strategy': 'DefaultStrategy'})
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = Hyperopt(conf)
|
|
|
|
hyperopt.trials = trials
|
|
|
|
hyperopt.tickerdata_to_dataframe = MagicMock()
|
|
|
|
|
|
|
|
hyperopt.start()
|
2018-01-09 09:37:27 +00:00
|
|
|
|
|
|
|
mock_read.assert_called_once()
|
|
|
|
mock_save.assert_called_once()
|
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
current_tries = hyperopt.current_tries
|
|
|
|
total_tries = hyperopt.total_tries
|
2018-01-09 09:37:27 +00:00
|
|
|
|
|
|
|
assert current_tries == len(trials.results)
|
|
|
|
assert total_tries == (current_tries + len(trials.results))
|
|
|
|
|
2018-01-09 10:19:44 +00:00
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_save_trials_saves_trials(mocker, init_hyperopt, caplog) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
create_trials(mocker)
|
|
|
|
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.pickle.dump', return_value=None)
|
2018-01-09 10:19:44 +00:00
|
|
|
|
2018-03-02 13:46:32 +00:00
|
|
|
hyperopt = _HYPEROPT
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.open', return_value=hyperopt.trials_file)
|
2018-01-09 10:19:44 +00:00
|
|
|
|
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-03-26 11:14:36 +00:00
|
|
|
'Saving Trials 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-04-08 09:11:14 +00:00
|
|
|
def test_read_trials_returns_trials_file(mocker, init_hyperopt, caplog) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
trials = create_trials(mocker)
|
|
|
|
mock_load = mocker.patch('freqtrade.optimize.hyperopt.pickle.load', return_value=trials)
|
|
|
|
mock_open = mocker.patch('freqtrade.optimize.hyperopt.open', return_value=mock_load)
|
|
|
|
|
|
|
|
hyperopt = _HYPEROPT
|
|
|
|
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_open.assert_called_once()
|
|
|
|
mock_load.assert_called_once()
|
2018-01-25 08:45:53 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_roi_table_generation(init_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
|
|
|
|
|
|
|
hyperopt = _HYPEROPT
|
2018-03-04 00:42:37 +00:00
|
|
|
assert hyperopt.generate_roi_table(params) == {0: 6, 15: 3, 25: 1, 30: 0}
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_start_calls_fmin(mocker, init_hyperopt, default_conf) -> None:
|
2018-03-02 13:46:32 +00:00
|
|
|
trials = create_trials(mocker)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.sorted', return_value=trials.results)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock())
|
2018-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock())
|
2018-03-02 13:46:32 +00:00
|
|
|
mock_fmin = mocker.patch('freqtrade.optimize.hyperopt.fmin', return_value={})
|
|
|
|
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.update({'config': 'config.json.example'})
|
|
|
|
conf.update({'epochs': 1})
|
|
|
|
conf.update({'timerange': None})
|
2018-03-04 08:51:22 +00:00
|
|
|
conf.update({'spaces': 'all'})
|
2018-03-02 13:46:32 +00:00
|
|
|
|
|
|
|
hyperopt = Hyperopt(conf)
|
|
|
|
hyperopt.trials = trials
|
|
|
|
hyperopt.tickerdata_to_dataframe = MagicMock()
|
|
|
|
|
|
|
|
hyperopt.start()
|
|
|
|
mock_fmin.assert_called_once()
|
|
|
|
|
|
|
|
|
2018-06-02 21:07:31 +00:00
|
|
|
def test_format_results(init_hyperopt):
|
2018-03-04 00:42:37 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.format_results()
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
result = _HYPEROPT.format_results(df)
|
|
|
|
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)
|
|
|
|
result = _HYPEROPT.format_results(df)
|
|
|
|
assert result.find('Total profit 1.00000000 EUR')
|
2018-02-08 19:49:43 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_signal_handler(mocker, init_hyperopt):
|
2018-03-04 00:42:37 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.signal_handler()
|
|
|
|
"""
|
2018-02-08 19:49:43 +00:00
|
|
|
m = MagicMock()
|
|
|
|
mocker.patch('sys.exit', m)
|
2018-03-04 00:42:37 +00:00
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.save_trials', m)
|
|
|
|
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.log_trials_result', m)
|
|
|
|
|
|
|
|
hyperopt = _HYPEROPT
|
2018-04-09 19:12:48 +00:00
|
|
|
hyperopt.signal_handler(signal.SIGTERM, None)
|
2018-02-08 19:49:43 +00:00
|
|
|
assert m.call_count == 3
|
2018-02-09 18:59:06 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_has_space(init_hyperopt):
|
2018-03-04 08:51:22 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.has_space() method
|
|
|
|
"""
|
|
|
|
_HYPEROPT.config.update({'spaces': ['buy', 'roi']})
|
|
|
|
assert _HYPEROPT.has_space('roi')
|
|
|
|
assert _HYPEROPT.has_space('buy')
|
|
|
|
assert not _HYPEROPT.has_space('stoploss')
|
|
|
|
|
|
|
|
_HYPEROPT.config.update({'spaces': ['all']})
|
|
|
|
assert _HYPEROPT.has_space('buy')
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_populate_indicators(init_hyperopt) -> None:
|
2018-03-05 08:35:42 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.populate_indicators()
|
|
|
|
"""
|
2018-03-24 09:21:59 +00:00
|
|
|
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
|
2018-02-03 16:15:40 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': tick}
|
2018-03-05 08:35:42 +00:00
|
|
|
dataframes = _HYPEROPT.tickerdata_to_dataframe(tickerlist)
|
2018-02-03 16:15:40 +00:00
|
|
|
dataframe = _HYPEROPT.populate_indicators(dataframes['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
|
|
|
|
assert 'ao' in dataframe
|
|
|
|
assert 'cci' in dataframe
|
|
|
|
|
|
|
|
|
2018-04-08 09:11:14 +00:00
|
|
|
def test_buy_strategy_generator(init_hyperopt) -> None:
|
2018-03-05 08:35:42 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.buy_strategy_generator()
|
|
|
|
"""
|
2018-03-24 09:21:59 +00:00
|
|
|
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
|
2018-02-03 16:15:40 +00:00
|
|
|
tickerlist = {'UNITTEST/BTC': tick}
|
2018-03-05 08:35:42 +00:00
|
|
|
dataframes = _HYPEROPT.tickerdata_to_dataframe(tickerlist)
|
2018-02-03 16:15:40 +00:00
|
|
|
dataframe = _HYPEROPT.populate_indicators(dataframes['UNITTEST/BTC'])
|
2018-03-05 08:35:42 +00:00
|
|
|
|
|
|
|
populate_buy_trend = _HYPEROPT.buy_strategy_generator(
|
|
|
|
{
|
2018-03-06 06:02:03 +00:00
|
|
|
'uptrend_long_ema': {
|
|
|
|
'enabled': True
|
|
|
|
},
|
|
|
|
'macd_below_zero': {
|
|
|
|
'enabled': True
|
|
|
|
},
|
|
|
|
'uptrend_short_ema': {
|
|
|
|
'enabled': True
|
|
|
|
},
|
|
|
|
'mfi': {
|
|
|
|
'enabled': True,
|
|
|
|
'value': 20
|
|
|
|
},
|
|
|
|
'fastd': {
|
|
|
|
'enabled': True,
|
|
|
|
'value': 20
|
|
|
|
},
|
2018-03-05 08:35:42 +00:00
|
|
|
'adx': {
|
|
|
|
'enabled': True,
|
|
|
|
'value': 20
|
|
|
|
},
|
2018-03-06 06:02:03 +00:00
|
|
|
'rsi': {
|
|
|
|
'enabled': True,
|
|
|
|
'value': 20
|
|
|
|
},
|
|
|
|
'over_sar': {
|
|
|
|
'enabled': True,
|
|
|
|
},
|
|
|
|
'green_candle': {
|
|
|
|
'enabled': True,
|
|
|
|
},
|
|
|
|
'uptrend_sma': {
|
|
|
|
'enabled': True,
|
|
|
|
},
|
|
|
|
|
2018-03-05 08:35:42 +00:00
|
|
|
'trigger': {
|
|
|
|
'type': 'lower_bb'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = populate_buy_trend(dataframe)
|
|
|
|
# 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-04-08 09:11:14 +00:00
|
|
|
def test_generate_optimizer(mocker, init_hyperopt, default_conf) -> None:
|
2018-03-06 06:02:03 +00:00
|
|
|
"""
|
|
|
|
Test Hyperopt.generate_optimizer() function
|
|
|
|
"""
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.update({'config': 'config.json.example'})
|
|
|
|
conf.update({'timerange': None})
|
|
|
|
conf.update({'spaces': 'all'})
|
|
|
|
|
|
|
|
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-04-08 09:11:14 +00:00
|
|
|
mocker.patch('freqtrade.exchange.validate_pairs', MagicMock())
|
2018-03-06 06:02:03 +00:00
|
|
|
|
|
|
|
optimizer_param = {
|
|
|
|
'adx': {'enabled': False},
|
|
|
|
'fastd': {'enabled': True, 'value': 35.0},
|
|
|
|
'green_candle': {'enabled': True},
|
|
|
|
'macd_below_zero': {'enabled': True},
|
|
|
|
'mfi': {'enabled': False},
|
|
|
|
'over_sar': {'enabled': False},
|
|
|
|
'roi_p1': 0.01,
|
|
|
|
'roi_p2': 0.01,
|
|
|
|
'roi_p3': 0.1,
|
|
|
|
'roi_t1': 60.0,
|
|
|
|
'roi_t2': 30.0,
|
|
|
|
'roi_t3': 20.0,
|
|
|
|
'rsi': {'enabled': False},
|
|
|
|
'stoploss': -0.4,
|
|
|
|
'trigger': {'type': 'macd_cross_signal'},
|
|
|
|
'uptrend_long_ema': {'enabled': False},
|
|
|
|
'uptrend_short_ema': {'enabled': True},
|
|
|
|
'uptrend_sma': {'enabled': True}
|
|
|
|
}
|
|
|
|
|
|
|
|
response_expected = {
|
|
|
|
'loss': 1.9840569076926293,
|
|
|
|
'result': ' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC '
|
|
|
|
'(0.0231Σ%). Avg duration 100.0 mins.',
|
|
|
|
'status': 'ok'
|
|
|
|
}
|
|
|
|
|
|
|
|
hyperopt = Hyperopt(conf)
|
|
|
|
generate_optimizer_value = hyperopt.generate_optimizer(optimizer_param)
|
|
|
|
assert generate_optimizer_value == response_expected
|