From 989ab646a9ec1367b3eef17d8740c6bc7c84a4f2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 9 Jan 2020 06:46:39 +0100 Subject: [PATCH] Add profit % to sell_reason table --- freqtrade/optimize/backtest_reports.py | 10 ++++++---- tests/optimize/test_backtest_reports.py | 12 ++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/freqtrade/optimize/backtest_reports.py b/freqtrade/optimize/backtest_reports.py index 5778747cf..555250760 100644 --- a/freqtrade/optimize/backtest_reports.py +++ b/freqtrade/optimize/backtest_reports.py @@ -66,11 +66,13 @@ def generate_text_table_sell_reason(data: Dict[str, Dict], results: DataFrame) - :return: pretty printed table with tabulate as string """ tabular_data = [] - headers = ['Sell Reason', 'Count', 'Profit', 'Loss'] + headers = ['Sell Reason', 'Count', 'Profit', 'Loss', 'Profit %'] for reason, count in results['sell_reason'].value_counts().iteritems(): - profit = len(results[(results['sell_reason'] == reason) & (results['profit_abs'] >= 0)]) - loss = len(results[(results['sell_reason'] == reason) & (results['profit_abs'] < 0)]) - tabular_data.append([reason.value, count, profit, loss]) + result = results.loc[results['sell_reason'] == reason] + profit = len(result[result['profit_abs'] >= 0]) + loss = len(result[results['profit_abs'] < 0]) + profit_mean = round(result['profit_percent'].mean() * 100.0, 2) + tabular_data.append([reason.value, count, profit, loss, profit_mean]) return tabulate(tabular_data, headers=headers, tablefmt="pipe") diff --git a/tests/optimize/test_backtest_reports.py b/tests/optimize/test_backtest_reports.py index 726202517..107389a42 100644 --- a/tests/optimize/test_backtest_reports.py +++ b/tests/optimize/test_backtest_reports.py @@ -39,8 +39,8 @@ def test_generate_text_table_sell_reason(default_conf, mocker): results = pd.DataFrame( { 'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'], - 'profit_percent': [0.1, 0.2, -0.3], - 'profit_abs': [0.2, 0.4, -0.5], + 'profit_percent': [0.1, 0.2, -0.1], + 'profit_abs': [0.2, 0.4, -0.2], 'trade_duration': [10, 30, 10], 'profit': [2, 0, 0], 'loss': [0, 0, 1], @@ -49,10 +49,10 @@ def test_generate_text_table_sell_reason(default_conf, mocker): ) result_str = ( - '| Sell Reason | Count | Profit | Loss |\n' - '|:--------------|--------:|---------:|-------:|\n' - '| roi | 2 | 2 | 0 |\n' - '| stop_loss | 1 | 0 | 1 |' + '| Sell Reason | Count | Profit | Loss | Profit % |\n' + '|:--------------|--------:|---------:|-------:|-----------:|\n' + '| roi | 2 | 2 | 0 | 15 |\n' + '| stop_loss | 1 | 0 | 1 | -10 |' ) assert generate_text_table_sell_reason( data={'ETH/BTC': {}}, results=results) == result_str