feat: add SQN calculation as backtest metric

This commit is contained in:
Matthias
2025-03-02 15:41:43 +01:00
parent 13e9f8a98e
commit e1f6702932
2 changed files with 66 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ from freqtrade.data.metrics import (
calculate_max_drawdown,
calculate_sharpe,
calculate_sortino,
calculate_sqn,
calculate_underwater,
combine_dataframes_with_mean,
combined_dataframes_with_rel_mean,
@@ -457,6 +458,42 @@ def test_calculate_calmar(testdatadir):
assert pytest.approx(calmar) == 559.040508
def test_calculate_sqn(testdatadir):
filename = testdatadir / "backtest_results/backtest-result.json"
bt_data = load_backtest_data(filename)
sqn = calculate_sqn(DataFrame(), 0)
assert sqn == 0.0
sqn = calculate_sqn(
bt_data,
0.01,
)
assert isinstance(sqn, float)
assert pytest.approx(sqn) == 3.2991
@pytest.mark.parametrize(
"profits,starting_balance,expected_sqn,description",
[
([1.0, -0.5, 2.0, -1.0, 0.5, 1.5, -0.5, 1.0], 100, 1.3229, "Mixed profits/losses"),
([], 100, 0.0, "Empty dataframe"),
([1.0, 0.5, 2.0, 1.5, 0.8], 100, 4.3657, "All winning trades"),
([-1.0, -0.5, -2.0, -1.5, -0.8], 100, -4.3657, "All losing trades"),
([1.0], 100, -100, "Single trade"),
],
)
def test_calculate_sqn_cases(profits, starting_balance, expected_sqn, description):
"""
Test SQN calculation with various scenarios:
"""
trades = DataFrame({"profit_abs": profits})
sqn = calculate_sqn(trades, starting_balance=starting_balance)
assert isinstance(sqn, float)
assert pytest.approx(sqn, rel=1e-4) == expected_sqn
@pytest.mark.parametrize(
"start,end,days, expected",
[