From 955a63725a4e0210afc9faf17d16af23bc13aded Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 22 Jul 2023 19:43:20 +0200 Subject: [PATCH] Improve resiliance when showing older backtest results --- freqtrade/data/metrics.py | 2 +- freqtrade/optimize/optimize_reports/bt_output.py | 5 +++-- tests/data/test_btanalysis.py | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/freqtrade/data/metrics.py b/freqtrade/data/metrics.py index 2cc95fa79..d4746c168 100644 --- a/freqtrade/data/metrics.py +++ b/freqtrade/data/metrics.py @@ -198,7 +198,7 @@ def calculate_expectancy(trades: pd.DataFrame) -> Tuple[float, float]: """ Calculate expectancy :param trades: DataFrame containing trades (requires columns close_date and profit_abs) - :return: expectancy + :return: expectancy, expectancy_ratio """ expectancy = 0 diff --git a/freqtrade/optimize/optimize_reports/bt_output.py b/freqtrade/optimize/optimize_reports/bt_output.py index 85e91dc2a..07f23a5fa 100644 --- a/freqtrade/optimize/optimize_reports/bt_output.py +++ b/freqtrade/optimize/optimize_reports/bt_output.py @@ -233,8 +233,9 @@ def text_table_add_metrics(strat_results: Dict) -> str: ('Calmar', f"{strat_results['calmar']:.2f}" if 'calmar' in strat_results else 'N/A'), ('Profit factor', f'{strat_results["profit_factor"]:.2f}' if 'profit_factor' in strat_results else 'N/A'), - ('Expectancy (Ratio)', f"{strat_results['expectancy']:.2f} " - f"({strat_results['expectancy_ratio']:.2f})"), + ('Expectancy (Ratio)', ( + f"{strat_results['expectancy']:.2f} ({strat_results['expectancy_ratio']:.2f})" if + 'expectancy_ratio' in strat_results else 'N/A')), ('Trades per day', strat_results['trades_per_day']), ('Avg. daily profit %', f"{(strat_results['profit_total'] / strat_results['backtest_days']):.2%}"), diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index 6a100904c..f11e5d772 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -353,6 +353,15 @@ def test_calculate_expectancy(testdatadir): assert pytest.approx(expectancy) == 5.820687070932315e-06 assert pytest.approx(expectancy_ratio) == 0.07151374226574791 + data = { + 'profit_abs': [100, 200, 50, -150, 300, -100, 80, -30] + } + df = DataFrame(data) + expectancy, expectancy_ratio = calculate_expectancy(df) + + assert pytest.approx(expectancy) == 56.25 + assert pytest.approx(expectancy_ratio) == 0.60267857 + def test_calculate_sortino(testdatadir): filename = testdatadir / "backtest_results/backtest-result.json"