From a4ab6cb27d6910178e2f0d22be8ab4b17f9ffd6d Mon Sep 17 00:00:00 2001 From: TomaszLakota <48764424+TomaszLakota@users.noreply.github.com> Date: Mon, 8 Jun 2020 17:30:23 +0200 Subject: [PATCH 1/4] Add profit factor to stats --- backtesting/backtesting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 7d1a3041..fdd049e8 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -945,6 +945,7 @@ def _round_timedelta(value, _period=_data_period(df)): s.loc['Avg. Trade [%]'] = mean_return * 100 s.loc['Max. Trade Duration'] = _round_timedelta(durations.max()) s.loc['Avg. Trade Duration'] = _round_timedelta(durations.mean()) + s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) s.loc['Expectancy [%]'] = ((returns[returns > 0].mean() * win_rate - returns[returns < 0].mean() * (100 - win_rate))) pl = pl.dropna() From 16266503997f9f3929cb9aa14f26c8e7aba85782 Mon Sep 17 00:00:00 2001 From: TomaszLakota <48764424+TomaszLakota@users.noreply.github.com> Date: Mon, 8 Jun 2020 17:51:01 +0200 Subject: [PATCH 2/4] add mock profit factor value in test result --- backtesting/test/_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backtesting/test/_test.py b/backtesting/test/_test.py index 7e8ebc28..47fe8525 100644 --- a/backtesting/test/_test.py +++ b/backtesting/test/_test.py @@ -256,6 +256,7 @@ def test_compute_stats(self): 'Max. Drawdown Duration': pd.Timedelta('584 days 00:00:00'), 'Max. Drawdown [%]': -48.15069053929621, 'Max. Trade Duration': pd.Timedelta('183 days 00:00:00'), + 'Profit Factor': 5.0, 'Return [%]': 426.2429346696951, 'SQN': 0.91553210127173, 'Sharpe Ratio': 0.23169782960690408, From 99b6b5906fea1af24267b480a171d7631808c205 Mon Sep 17 00:00:00 2001 From: TomaszLakota <48764424+TomaszLakota@users.noreply.github.com> Date: Mon, 8 Jun 2020 17:56:06 +0200 Subject: [PATCH 3/4] add correct profit factor value to assertion --- backtesting/test/_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtesting/test/_test.py b/backtesting/test/_test.py index 47fe8525..3cb8d794 100644 --- a/backtesting/test/_test.py +++ b/backtesting/test/_test.py @@ -256,7 +256,7 @@ def test_compute_stats(self): 'Max. Drawdown Duration': pd.Timedelta('584 days 00:00:00'), 'Max. Drawdown [%]': -48.15069053929621, 'Max. Trade Duration': pd.Timedelta('183 days 00:00:00'), - 'Profit Factor': 5.0, + 'Profit Factor': 2.060450149412349, 'Return [%]': 426.2429346696951, 'SQN': 0.91553210127173, 'Sharpe Ratio': 0.23169782960690408, From e2410c80eafa099ab78e837ba334dc66df29db9a Mon Sep 17 00:00:00 2001 From: TomaszLakota <48764424+TomaszLakota@users.noreply.github.com> Date: Mon, 8 Jun 2020 18:06:34 +0200 Subject: [PATCH 4/4] suppress lint warning --- backtesting/backtesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index fdd049e8..85a4aa76 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -945,7 +945,7 @@ def _round_timedelta(value, _period=_data_period(df)): s.loc['Avg. Trade [%]'] = mean_return * 100 s.loc['Max. Trade Duration'] = _round_timedelta(durations.max()) s.loc['Avg. Trade Duration'] = _round_timedelta(durations.mean()) - s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) + s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) # noqa: E501 s.loc['Expectancy [%]'] = ((returns[returns > 0].mean() * win_rate - returns[returns < 0].mean() * (100 - win_rate))) pl = pl.dropna()