Add test for each analysis group, remove default table output if not indicator-list

This commit is contained in:
froggleston 2022-05-29 16:25:31 +01:00
parent 24b02127ec
commit 9a068c0b14
2 changed files with 119 additions and 34 deletions

View File

@ -173,7 +173,7 @@ def _print_results(analysed_trades, stratname, analysis_groups,
exit_reason_list = exit_reason_list.split(",") exit_reason_list = exit_reason_list.split(",")
bigdf = bigdf.loc[(bigdf['exit_reason'].isin(exit_reason_list))] bigdf = bigdf.loc[(bigdf['exit_reason'].isin(exit_reason_list))]
if indicator_list is not None: if indicator_list is not None and indicator_list != "":
if indicator_list == "all": if indicator_list == "all":
print(bigdf) print(bigdf)
else: else:
@ -183,8 +183,6 @@ def _print_results(analysed_trades, stratname, analysis_groups,
available_inds.append(ind) available_inds.append(ind)
ilist = ["pair", "enter_reason", "exit_reason"] + available_inds ilist = ["pair", "enter_reason", "exit_reason"] + available_inds
_print_table(bigdf[ilist], sortcols=['exit_reason'], show_index=False) _print_table(bigdf[ilist], sortcols=['exit_reason'], show_index=False)
else:
_print_table(bigdf[columns], sortcols=['pair'], show_index=False)
else: else:
print("\\_ No trades to show") print("\\_ No trades to show")

View File

@ -1,3 +1,4 @@
import logging
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock, PropertyMock from unittest.mock import MagicMock, PropertyMock
@ -19,6 +20,8 @@ def entryexitanalysis_cleanup() -> None:
def test_backtest_analysis_nomock(default_conf, mocker, caplog, testdatadir, tmpdir, capsys): def test_backtest_analysis_nomock(default_conf, mocker, caplog, testdatadir, tmpdir, capsys):
caplog.set_level(logging.INFO)
default_conf.update({ default_conf.update({
"use_exit_signal": True, "use_exit_signal": True,
"exit_profit_only": False, "exit_profit_only": False,
@ -26,22 +29,32 @@ def test_backtest_analysis_nomock(default_conf, mocker, caplog, testdatadir, tmp
"ignore_roi_if_entry_signal": False, "ignore_roi_if_entry_signal": False,
}) })
patch_exchange(mocker) patch_exchange(mocker)
result1 = pd.DataFrame({'pair': ['ETH/BTC', 'LTC/BTC'], result1 = pd.DataFrame({'pair': ['ETH/BTC', 'LTC/BTC', 'ETH/BTC', 'LTC/BTC'],
'profit_ratio': [0.0, 0.0], 'profit_ratio': [0.025, 0.05, -0.1, -0.05],
'profit_abs': [0.0, 0.0], 'profit_abs': [0.5, 2.0, -4.0, -2.0],
'open_date': pd.to_datetime(['2018-01-29 18:40:00', 'open_date': pd.to_datetime(['2018-01-29 18:40:00',
'2018-01-30 03:30:00', ], utc=True '2018-01-30 03:30:00',
'2018-01-30 08:10:00',
'2018-01-31 13:30:00', ], utc=True
), ),
'close_date': pd.to_datetime(['2018-01-29 20:45:00', 'close_date': pd.to_datetime(['2018-01-29 20:45:00',
'2018-01-30 05:35:00', ], utc=True), '2018-01-30 05:35:00',
'trade_duration': [235, 40], '2018-01-30 09:10:00',
'is_open': [False, False], '2018-01-31 15:00:00', ], utc=True),
'stake_amount': [0.01, 0.01], 'trade_duration': [235, 40, 60, 90],
'open_rate': [0.104445, 0.10302485], 'is_open': [False, False, False, False],
'close_rate': [0.104969, 0.103541], 'stake_amount': [0.01, 0.01, 0.01, 0.01],
"is_short": [False, False], 'open_rate': [0.104445, 0.10302485, 0.10302485, 0.10302485],
'enter_tag': ["enter_tag_long", "enter_tag_long"], 'close_rate': [0.104969, 0.103541, 0.102041, 0.102541],
'exit_reason': [ExitType.ROI, ExitType.ROI] "is_short": [False, False, False, False],
'enter_tag': ["enter_tag_long_a",
"enter_tag_long_b",
"enter_tag_long_a",
"enter_tag_long_b"],
'exit_reason': [ExitType.ROI,
ExitType.EXIT_SIGNAL,
ExitType.STOP_LOSS,
ExitType.TRAILING_STOP_LOSS]
}) })
backtestmock = MagicMock(side_effect=[ backtestmock = MagicMock(side_effect=[
@ -85,29 +98,103 @@ def test_backtest_analysis_nomock(default_conf, mocker, caplog, testdatadir, tmp
assert 'EXIT REASON STATS' in captured.out assert 'EXIT REASON STATS' in captured.out
assert 'LEFT OPEN TRADES REPORT' in captured.out assert 'LEFT OPEN TRADES REPORT' in captured.out
default_conf.update({ base_args = [
'analysis_groups': "0",
'enter_reason_list': "all",
'exit_reason_list': "all",
'indicator_list': "rsi"
})
args = [
'backtesting-analysis', 'backtesting-analysis',
'--config', 'config.json', '--config', 'config.json',
'--datadir', str(testdatadir), '--datadir', str(testdatadir),
'--user-data-dir', str(tmpdir), '--user-data-dir', str(tmpdir),
'--analysis-groups', '0',
'--indicator-list', 'rsi',
'--strategy',
'StrategyTestV3Analysis',
] ]
args = get_args(args) strat_args = ['--strategy', 'StrategyTestV3Analysis']
start_analysis_entries_exits(args)
# test group 0 and indicator list
args = get_args(base_args +
['--analysis-groups', '0',
'--indicator-list', 'close,rsi,profit_abs'] +
strat_args)
start_analysis_entries_exits(args)
captured = capsys.readouterr() captured = capsys.readouterr()
assert 'enter_tag_long' in captured.out
assert 'ETH/BTC' in captured.out
assert '34.049' in captured.out
assert 'LTC/BTC' in captured.out assert 'LTC/BTC' in captured.out
assert '54.3204' in captured.out assert 'ETH/BTC' in captured.out
assert 'enter_tag_long_a' in captured.out
assert 'enter_tag_long_b' in captured.out
assert 'exit_signal' in captured.out
assert 'roi' in captured.out
assert 'stop_loss' in captured.out
assert 'trailing_stop_loss' in captured.out
assert '0.5' in captured.out
assert '-4' in captured.out
assert '-2' in captured.out
assert '-3.5' in captured.out
assert '50' in captured.out
assert '0' in captured.out
assert '0.01616' in captured.out
assert '34.049' in captured.out
assert '0.104104' in captured.out
assert '47.0996' in captured.out
# test group 1
args = get_args(base_args + ['--analysis-groups', '1'] +
strat_args)
start_analysis_entries_exits(args)
captured = capsys.readouterr()
assert 'enter_tag_long_a' in captured.out
assert 'enter_tag_long_b' in captured.out
assert 'total_profit_pct' in captured.out
assert '-3.5' in captured.out
assert '-1.75' in captured.out
assert '-7.5' in captured.out
assert '-3.75' in captured.out
assert '0' in captured.out
# test group 2
args = get_args(base_args + ['--analysis-groups', '2'] +
strat_args)
start_analysis_entries_exits(args)
captured = capsys.readouterr()
assert 'enter_tag_long_a' in captured.out
assert 'enter_tag_long_b' in captured.out
assert 'exit_signal' in captured.out
assert 'roi' in captured.out
assert 'stop_loss' in captured.out
assert 'trailing_stop_loss' in captured.out
assert 'total_profit_pct' in captured.out
assert '-10' in captured.out
assert '-5' in captured.out
assert '2.5' in captured.out
# test group 3
args = get_args(base_args + ['--analysis-groups', '3'] +
strat_args)
start_analysis_entries_exits(args)
captured = capsys.readouterr()
assert 'LTC/BTC' in captured.out
assert 'ETH/BTC' in captured.out
assert 'enter_tag_long_a' in captured.out
assert 'enter_tag_long_b' in captured.out
assert 'total_profit_pct' in captured.out
assert '-7.5' in captured.out
assert '-3.75' in captured.out
assert '-1.75' in captured.out
assert '0' in captured.out
assert '2' in captured.out
# test group 4
args = get_args(base_args + ['--analysis-groups', '4'] +
strat_args)
start_analysis_entries_exits(args)
captured = capsys.readouterr()
assert 'LTC/BTC' in captured.out
assert 'ETH/BTC' in captured.out
assert 'enter_tag_long_a' in captured.out
assert 'enter_tag_long_b' in captured.out
assert 'exit_signal' in captured.out
assert 'roi' in captured.out
assert 'stop_loss' in captured.out
assert 'trailing_stop_loss' in captured.out
assert 'total_profit_pct' in captured.out
assert '-10' in captured.out
assert '-5' in captured.out
assert '-4' in captured.out
assert '0.5' in captured.out
assert '1' in captured.out
assert '2.5' in captured.out