Add tests for new command

This commit is contained in:
Matthias 2021-10-30 17:05:12 +02:00
parent 72ecb45d86
commit 20904f1ca4
4 changed files with 34 additions and 17 deletions

View File

@ -3,11 +3,9 @@ from typing import Any, Dict
from freqtrade import constants
from freqtrade.configuration import setup_utils_configuration
from freqtrade.data.btanalysis import load_backtest_stats
from freqtrade.enums import RunMode
from freqtrade.exceptions import OperationalException
from freqtrade.misc import round_coin_value
from freqtrade.optimize.optimize_reports import show_backtest_results, show_filtered_pairlist
logger = logging.getLogger(__name__)
@ -63,11 +61,13 @@ def start_backtest_show(args: Dict[str, Any]) -> None:
config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
from freqtrade.optimize.optimize_reports import show_backtest_results, show_sorted_pairlist
from freqtrade.data.btanalysis import load_backtest_stats
results = load_backtest_stats(config['exportfilename'])
# print(results)
show_backtest_results(config, results)
show_filtered_pairlist(config, results)
show_sorted_pairlist(config, results)
def start_hyperopt(args: Dict[str, Any]) -> None:

View File

@ -737,7 +737,7 @@ def show_backtest_results(config: Dict, backtest_stats: Dict):
print('\nFor more details, please look at the detail tables above')
def show_filtered_pairlist(config: Dict, backtest_stats: Dict):
def show_sorted_pairlist(config: Dict, backtest_stats: Dict):
if config.get('backtest_show_pair_list', False):
for strategy, results in backtest_stats['strategy'].items():
print(f"Pairs for Strategy {strategy}: \n[")

View File

@ -8,12 +8,12 @@ from zipfile import ZipFile
import arrow
import pytest
from freqtrade.commands import (start_convert_data, start_convert_trades, start_create_userdir,
start_download_data, start_hyperopt_list, start_hyperopt_show,
start_install_ui, start_list_data, start_list_exchanges,
start_list_markets, start_list_strategies, start_list_timeframes,
start_new_strategy, start_show_trades, start_test_pairlist,
start_trading, start_webserver)
from freqtrade.commands import (start_backtest_show, start_convert_data, start_convert_trades,
start_create_userdir, start_download_data, start_hyperopt_list,
start_hyperopt_show, start_install_ui, start_list_data,
start_list_exchanges, start_list_markets, start_list_strategies,
start_list_timeframes, start_new_strategy, start_show_trades,
start_test_pairlist, start_trading, start_webserver)
from freqtrade.commands.deploy_commands import (clean_ui_subdir, download_and_install_ui,
get_ui_download_url, read_ui_version)
from freqtrade.configuration import setup_utils_configuration
@ -1389,3 +1389,19 @@ def test_show_trades(mocker, fee, capsys, caplog):
with pytest.raises(OperationalException, match=r"--db-url is required for this command."):
start_show_trades(pargs)
def test_backtest_show(mocker, testdatadir, capsys):
sbr = mocker.patch('freqtrade.optimize.optimize_reports.show_backtest_results')
args = [
"backtest-show",
"--export-filename",
f"{testdatadir / 'backtest-result_new.json'}",
"--show-pair-list"
]
pargs = get_args(args)
pargs['config'] = None
start_backtest_show(pargs)
assert sbr.call_count == 1
out, err = capsys.readouterr()
assert "Pairs for Strategy" in out

View File

@ -10,7 +10,8 @@ from arrow import Arrow
from freqtrade.configuration import TimeRange
from freqtrade.constants import DATETIME_PRINT_FORMAT, LAST_BT_RESULT_FN
from freqtrade.data import history
from freqtrade.data.btanalysis import get_latest_backtest_filename, load_backtest_data, load_backtest_stats
from freqtrade.data.btanalysis import (get_latest_backtest_filename, load_backtest_data,
load_backtest_stats)
from freqtrade.edge import PairInfo
from freqtrade.enums import SellType
from freqtrade.optimize.optimize_reports import (_get_resample_from_period, generate_backtest_stats,
@ -19,9 +20,9 @@ from freqtrade.optimize.optimize_reports import (_get_resample_from_period, gene
generate_periodic_breakdown_stats,
generate_sell_reason_stats,
generate_strategy_comparison,
generate_trading_stats, show_filtered_pairlist, store_backtest_stats,
text_table_bt_results, text_table_sell_reason,
text_table_strategy)
generate_trading_stats, show_sorted_pairlist,
store_backtest_stats, text_table_bt_results,
text_table_sell_reason, text_table_strategy)
from freqtrade.resolvers.strategy_resolver import StrategyResolver
from tests.data.test_history import _backup_file, _clean_test_file
@ -409,12 +410,12 @@ def test__get_resample_from_period():
_get_resample_from_period('noooo')
def test_show_filtered_pairlist(testdatadir, default_conf, capsys):
def test_show_sorted_pairlist(testdatadir, default_conf, capsys):
filename = testdatadir / "backtest-result_new.json"
bt_data = load_backtest_stats(filename)
default_conf['backtest_show_pair_list'] = True
show_filtered_pairlist(default_conf, bt_data)
show_sorted_pairlist(default_conf, bt_data)
out, err = capsys.readouterr()
assert 'Pairs for Strategy StrategyTestV2: \n[' in out