Fix some tests

This commit is contained in:
hroff-1902 2019-11-27 22:52:43 +03:00
parent c3d7411668
commit 8f9b5095b5
2 changed files with 47 additions and 10 deletions

View File

@ -181,7 +181,7 @@ class Hyperopt:
"""
Display details of the hyperopt result
"""
params = results['params_details']
params = results.get('params_details', {})
# Default header string
if header_str is None:
@ -335,6 +335,11 @@ class Hyperopt:
'end_date': max_date,
}
)
return self._get_results_dict(backtesting_results, min_date, max_date,
params_dict, params_details)
def _get_results_dict(self, backtesting_results, min_date, max_date,
params_dict, params_details):
results_metrics = self._calculate_results_metrics(backtesting_results)
results_explanation = self._format_results_explanation_string(results_metrics)

View File

@ -357,8 +357,9 @@ def test_onlyprofit_loss_prefers_higher_profits(default_conf, hyperopt_results)
def test_log_results_if_loss_improves(hyperopt, capsys) -> None:
hyperopt.current_best_loss = 2
hyperopt.total_epochs = 2
hyperopt.log_results(
hyperopt.print_results(
{
'is_best': False,
'loss': 1,
'current_epoch': 2, # This starts from 1 (in a human-friendly manner)
'results_explanation': 'foo.',
@ -371,8 +372,9 @@ def test_log_results_if_loss_improves(hyperopt, capsys) -> None:
def test_no_log_if_loss_does_not_improve(hyperopt, caplog) -> None:
hyperopt.current_best_loss = 2
hyperopt.log_results(
hyperopt.print_results(
{
'is_best': False,
'loss': 3,
'current_epoch': 1,
}
@ -400,8 +402,8 @@ def test_save_trials_saves_trials(mocker, hyperopt, testdatadir, caplog) -> None
def test_read_trials_returns_trials_file(mocker, hyperopt, testdatadir, caplog) -> None:
trials = create_trials(mocker, hyperopt, testdatadir)
mock_load = mocker.patch('freqtrade.optimize.hyperopt.load', return_value=trials)
hyperopt_trial = hyperopt.read_trials()
trials_file = testdatadir / 'optimize' / 'ut_trials.pickle'
hyperopt_trial = hyperopt._read_trials(trials_file)
assert log_has(f"Reading Trials from '{trials_file}'", caplog)
assert hyperopt_trial == trials
mock_load.assert_called_once()
@ -472,8 +474,8 @@ def test_format_results(hyperopt):
]
labels = ['currency', 'profit_percent', 'profit_abs', 'trade_duration']
df = pd.DataFrame.from_records(trades, columns=labels)
result = hyperopt.format_results(df)
results_metrics = hyperopt._calculate_results_metrics(df)
result = hyperopt._format_explanation_string(results_metrics, 1)
assert result.find(' 66.67%')
assert result.find('Total profit 1.00000000 BTC')
assert result.find('2.0000Σ %')
@ -485,7 +487,8 @@ def test_format_results(hyperopt):
('XPR/EUR', -1, -2, -246)
]
df = pd.DataFrame.from_records(trades, columns=labels)
result = hyperopt.format_results(df)
results_metrics = hyperopt._calculate_results_metrics(df)
result = hyperopt._format_explanation_string(results_metrics, 1)
assert result.find('Total profit 1.00000000 EUR')
@ -597,7 +600,35 @@ def test_generate_optimizer(mocker, default_conf) -> None:
'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'),
'params': optimizer_param,
'params_details': {'buy': {'adx-enabled': False,
'adx-value': 0,
'fastd-enabled': True,
'fastd-value': 35,
'mfi-enabled': False,
'mfi-value': 0,
'rsi-enabled': False,
'rsi-value': 0,
'trigger': 'macd_cross_signal'},
'roi': {0: 0.12000000000000001,
20.0: 0.02,
50.0: 0.01,
110.0: 0},
'sell': {'sell-adx-enabled': False,
'sell-adx-value': 0,
'sell-fastd-enabled': True,
'sell-fastd-value': 75,
'sell-mfi-enabled': False,
'sell-mfi-value': 0,
'sell-rsi-enabled': False,
'sell-rsi-value': 0,
'sell-trigger': 'macd_cross_signal'},
'stoploss': -0.4},
'params_dict': optimizer_param,
'results_metrics': {'avg_profit': 2.3117,
'duration': 100.0,
'profit': 2.3117,
'total_profit': 0.000233,
'trade_count': 1},
'total_profit': 0.00023300
}
@ -692,7 +723,8 @@ def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) ->
parallel = mocker.patch(
'freqtrade.optimize.hyperopt.Hyperopt.run_optimizer_parallel',
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {}}])
MagicMock(return_value=[{'loss': 1, 'results_explanation': 'foo result', 'params': {},
'params_details': {'roi': {}, 'stoploss': None}}])
)
patch_exchange(mocker)