From 876a9e4f44c599358f2a323b7a0bcf45cf78845e Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 25 May 2020 07:08:15 +0200 Subject: [PATCH] finish refactor of sell_reason table --- freqtrade/optimize/optimize_reports.py | 18 +++++++++--------- tests/optimize/test_optimize_reports.py | 8 ++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/freqtrade/optimize/optimize_reports.py b/freqtrade/optimize/optimize_reports.py index e0c66b7c3..09c862847 100644 --- a/freqtrade/optimize/optimize_reports.py +++ b/freqtrade/optimize/optimize_reports.py @@ -1,7 +1,7 @@ import logging from datetime import timedelta from pathlib import Path -from typing import Dict, Tuple, List +from typing import Dict, Tuple, List, Any from pandas import DataFrame from tabulate import tabulate @@ -112,11 +112,10 @@ def generate_text_table(data: Dict[str, Dict], stake_currency: str, max_open_tra floatfmt=floatfmt, tablefmt="orgtbl", stralign="right") # type: ignore -def generate_sell_reason_stats(stake_currency: str, max_open_trades: int, +def generate_sell_reason_stats(max_open_trades: int, results: DataFrame) -> List[Dict]: """ Generate small table outlining Backtest results - :param stake_currency: Stakecurrency used :param max_open_trades: Max_open_trades parameter :param results: Dataframe containing the backtest result for one strategy :return: List of Dicts containing the metrics per Sell reason @@ -148,8 +147,7 @@ def generate_sell_reason_stats(stake_currency: str, max_open_trades: int, return tabular_data -def generate_text_table_sell_reason(stake_currency: str, - max_open_trades: int, results: DataFrame) -> str: +def generate_text_table_sell_reason(sell_reason_stats: Dict[str, Any], stake_currency: str) -> str: """ Generate small table outlining Backtest results :param stake_currency: Stakecurrency used @@ -168,7 +166,7 @@ def generate_text_table_sell_reason(stake_currency: str, f'Tot Profit {stake_currency}', 'Tot Profit %', ] - sell_reason_stats = generate_sell_reason_stats(stake_currency, max_open_trades, results) + output = [[ t['sell_reason'], t['trades'], t['wins'], t['draws'], t['losses'], t['profit_mean_pct'], t['profit_sum_pct'], t['profit_total_abs'], t['profit_pct_total'], @@ -249,9 +247,11 @@ def show_backtest_results(config: Dict, btdata: Dict[str, DataFrame], print(' BACKTESTING REPORT '.center(len(table.splitlines()[0]), '=')) print(table) - table = generate_text_table_sell_reason(stake_currency=config['stake_currency'], - max_open_trades=config['max_open_trades'], - results=results) + sell_reason_stats = generate_sell_reason_stats(max_open_trades=config['max_open_trades'], + results=results) + table = generate_text_table_sell_reason(sell_reason_stats=sell_reason_stats, + stake_currency=config['stake_currency'], + ) if isinstance(table, str): print(' SELL REASON STATS '.center(len(table.splitlines()[0]), '=')) print(table) diff --git a/tests/optimize/test_optimize_reports.py b/tests/optimize/test_optimize_reports.py index e0782146a..94c404b38 100644 --- a/tests/optimize/test_optimize_reports.py +++ b/tests/optimize/test_optimize_reports.py @@ -6,6 +6,7 @@ from arrow import Arrow from freqtrade.edge import PairInfo from freqtrade.optimize.optimize_reports import ( generate_edge_table, generate_text_table, generate_text_table_sell_reason, + generate_sell_reason_stats, generate_text_table_strategy, store_backtest_result) from freqtrade.strategy.interface import SellType from tests.conftest import patch_exchange @@ -65,8 +66,11 @@ def test_generate_text_table_sell_reason(default_conf, mocker): '| stop_loss | 1 | 0 | 0 | 1 |' ' -10 | -10 | -0.2 | -5 |' ) - assert generate_text_table_sell_reason(stake_currency='BTC', max_open_trades=2, - results=results) == result_str + + sell_reason_stats = generate_sell_reason_stats(max_open_trades=2, + results=results) + assert generate_text_table_sell_reason(sell_reason_stats=sell_reason_stats, + stake_currency='BTC') == result_str def test_generate_text_table_strategy(default_conf, mocker):