algobacktesting 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- algobacktesting-0.1.0/LICENSE +21 -0
- algobacktesting-0.1.0/PKG-INFO +204 -0
- algobacktesting-0.1.0/README.md +175 -0
- algobacktesting-0.1.0/pyproject.toml +105 -0
- algobacktesting-0.1.0/pyproject.toml.orig +77 -0
- algobacktesting-0.1.0/src/algobacktesting/__init__.py +51 -0
- algobacktesting-0.1.0/src/algobacktesting/cv.py +251 -0
- algobacktesting-0.1.0/src/algobacktesting/pbo.py +252 -0
- algobacktesting-0.1.0/src/algobacktesting/py.typed +0 -0
- algobacktesting-0.1.0/src/algobacktesting/sharpe.py +337 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Heidary
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: algobacktesting
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Is your backtest real? Deflated Sharpe Ratio, Probability of Backtest Overfitting, and purged cross-validation for trading research.
|
|
5
|
+
Keywords: backtesting,overfitting,deflated sharpe ratio,probability of backtest overfitting,quantitative finance,cross-validation
|
|
6
|
+
Author: Daniel Heidary
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Dist: numpy>=1.26
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Project-URL: Homepage, https://algobacktesting.com/
|
|
24
|
+
Project-URL: Repository, https://github.com/TheMarketMakerHoldings/algobacktesting
|
|
25
|
+
Project-URL: Documentation, https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/api.md
|
|
26
|
+
Project-URL: Changelog, https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CHANGELOG.md
|
|
27
|
+
Project-URL: Issues, https://github.com/TheMarketMakerHoldings/algobacktesting/issues
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# algobacktesting
|
|
31
|
+
|
|
32
|
+
[](https://pypi.org/project/algobacktesting/)
|
|
33
|
+
[](https://github.com/TheMarketMakerHoldings/algobacktesting/actions/workflows/ci.yml)
|
|
34
|
+
[](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/LICENSE)
|
|
35
|
+

|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
**Is your backtest real? Overfitting checks for trading research.**
|
|
39
|
+
|
|
40
|
+
> The Sharpe and PBO modules are verified against their source papers. The cross-validation
|
|
41
|
+
> module is built from the standard definitions and is awaiting review against the book.
|
|
42
|
+
|
|
43
|
+
Try enough strategy variations and one of them will look brilliant by luck alone.
|
|
44
|
+
`algobacktesting` implements statistical tests from Bailey & López de Prado's research that
|
|
45
|
+
tell a real edge apart from an overfit one.
|
|
46
|
+
|
|
47
|
+
**Not a programmer?** The [online check](https://algobacktesting.com/)
|
|
48
|
+
asks one question: are you actually profitable, or just lucky? Import a strategy's trades from
|
|
49
|
+
TradingView, TopstepX, Quantower, NinjaTrader or thinkorswim, paste them, or enter a win rate. Its stop lab
|
|
50
|
+
replays your entries on your own price bars to test other stop sizes, and counts every size
|
|
51
|
+
tried so a lucky stop is not mistaken for a better one. A prop-firm simulator estimates your
|
|
52
|
+
chance of passing a funded-account evaluation from your own trades. A strategy builder tests a rule you
|
|
53
|
+
write as a sentence on your own bars, counting every version you try. Save strategies under a
|
|
54
|
+
name and compare any two side by side, and share a result with a link that holds no trades. It runs in the browser, and nothing is uploaded.
|
|
55
|
+
|
|
56
|
+
| Question | Tool |
|
|
57
|
+
| --- | --- |
|
|
58
|
+
| Is this Sharpe ratio still impressive after counting every strategy I tried? | `deflated_sharpe_ratio` (DSR), `probabilistic_sharpe_ratio` (PSR), `min_track_record_length` |
|
|
59
|
+
| How likely is it that my "best" strategy is just the luckiest one? | `probability_of_backtest_overfitting` (PBO, via combinatorially symmetric cross-validation) |
|
|
60
|
+
| How do I cross-validate on time series without leaking the future? | `purged_kfold` (with embargo), `combinatorial_purged_cv` (CPCV) |
|
|
61
|
+
|
|
62
|
+
## Why this matters
|
|
63
|
+
|
|
64
|
+
I've consistently struggled with trading setups that had amazing winning streaks. They
|
|
65
|
+
looked great, but my overall profitability was neglected because of my obsession with my
|
|
66
|
+
strategies. The problem was that I didn't realize how unreliable my strategies really were
|
|
67
|
+
until I started calculating them. My strategies had incredibly high reward-to-risk setups
|
|
68
|
+
that happened frequently, and I launched them on Quantower using Python algorithms I wrote in
|
|
69
|
+
Visual Studio 2022, so the code ran my strategies automatically. I would often watch around
|
|
70
|
+
the peak hours of my setups and sometimes see them take off, and I told myself the winning
|
|
71
|
+
trades would take care of the occasional losses. Little did I know I had a 33% win rate and
|
|
72
|
+
my account was turning very negative.
|
|
73
|
+
|
|
74
|
+
When I saw the numbers, I didn't believe them. I blamed my brokerage for counting breakevens
|
|
75
|
+
as negative, but in reality a breakeven is a negative trade: if your strategy has a real edge,
|
|
76
|
+
you shouldn't need to worry about breakevens counting against you. Seeing my win rate broke
|
|
77
|
+
my dreams. It made me feel like trading without professional experience is for losers. I
|
|
78
|
+
called myself "exit liquidity", the target market for day-trading gurus, even though I had
|
|
79
|
+
started with my own analysis of the market instead of listening to gurus who tell you that
|
|
80
|
+
you can draw random patterns on a chart and that watching level 2 tick data is stupid. They
|
|
81
|
+
say that to hide the truth: trading comes down to data.
|
|
82
|
+
|
|
83
|
+
Now, when I develop a strategy, I backtest it on historical market data and run the results
|
|
84
|
+
through `algobacktesting` to see whether the strategy really has an edge.
|
|
85
|
+
|
|
86
|
+
## Quickstart
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import numpy as np
|
|
90
|
+
from algobacktesting import (
|
|
91
|
+
column_sharpe_ratios,
|
|
92
|
+
deflated_sharpe_ratio,
|
|
93
|
+
probability_of_backtest_overfitting,
|
|
94
|
+
return_stats,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
returns = np.random.default_rng(42).normal(0, 0.01, size=(1008, 200)) # 200 strategies, no edge
|
|
98
|
+
|
|
99
|
+
sharpes = column_sharpe_ratios(returns) # per-day Sharpe of each strategy
|
|
100
|
+
best = return_stats(returns[:, sharpes.argmax()]) # the backtest you'd be tempted to trade
|
|
101
|
+
|
|
102
|
+
dsr = deflated_sharpe_ratio(
|
|
103
|
+
best.sharpe,
|
|
104
|
+
best.n_obs,
|
|
105
|
+
n_trials=200,
|
|
106
|
+
sharpe_variance=sharpes.var(ddof=1),
|
|
107
|
+
skew=best.skew,
|
|
108
|
+
kurtosis=best.kurtosis,
|
|
109
|
+
)
|
|
110
|
+
pbo = probability_of_backtest_overfitting(returns, n_partitions=16).pbo
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`examples/overfit_demo.py` runs this for three worlds: no edge, a modest real edge, and a
|
|
114
|
+
strong real edge. Its output:
|
|
115
|
+
|
|
116
|
+
| World | Best backtest (annual Sharpe) | Deflated Sharpe | PBO |
|
|
117
|
+
| --- | --- | --- | --- |
|
|
118
|
+
| 200 strategies, no edge | 1.69 | 0.654 | 0.275 |
|
|
119
|
+
| plus one modest real edge (true Sharpe ~1.3) | 1.69 | 0.642 | 0.274 |
|
|
120
|
+
| plus one strong real edge (true Sharpe ~2.4) | 2.22 | 0.906 | 0.082 |
|
|
121
|
+
|
|
122
|
+
- **No edge:** pure luck produced a 1.69 Sharpe. DSR refuses to call it real.
|
|
123
|
+
- **Modest edge:** it exists, but the luckiest noise strategy still wins the search.
|
|
124
|
+
Searching 200 variants buries a 1.3 Sharpe edge.
|
|
125
|
+
- **Strong edge:** it wins, and PBO drops to 0.08, so the selection process works. DSR is
|
|
126
|
+
still below 0.95: after 200 tries, four years of data is not enough to *prove* it.
|
|
127
|
+
|
|
128
|
+
### Why purging matters
|
|
129
|
+
|
|
130
|
+
`examples/leakage_demo.py` cross-validates a model that has **no real skill**: a
|
|
131
|
+
nearest-neighbour model predicting 20-day returns from pure noise. Averaged over 200
|
|
132
|
+
datasets:
|
|
133
|
+
|
|
134
|
+
| Cross-validation scheme | Out-of-sample correlation (honest value: 0) |
|
|
135
|
+
| --- | --- |
|
|
136
|
+
| Shuffled k-fold | +0.816 (s.e. 0.002) |
|
|
137
|
+
| Contiguous k-fold, no purging | +0.050 (s.e. 0.005) |
|
|
138
|
+
| Purged k-fold | +0.000 (s.e. 0.005) |
|
|
139
|
+
| Purged k-fold with a 5% embargo | -0.001 (s.e. 0.005) |
|
|
140
|
+
| Leak removed by purging (paired difference) | +0.050 (s.e. 0.002) |
|
|
141
|
+
|
|
142
|
+
Shuffling makes a worthless model look excellent. Without purging, even unshuffled
|
|
143
|
+
folds leak at their edges. One dataset's score varies by about 0.07, so that leak is
|
|
144
|
+
invisible in a single backtest while it inflates every one of them. Purged k-fold
|
|
145
|
+
gives the honest answer.
|
|
146
|
+
|
|
147
|
+
Each function's docstring cites the equation and page it implements. `docs/explainers/`
|
|
148
|
+
has a plain-English guide to each module.
|
|
149
|
+
|
|
150
|
+
## Installation
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
pip install algobacktesting
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Requires Python 3.11+ and NumPy, which installs with it. For the latest version from GitHub
|
|
157
|
+
instead (needs Git):
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
pip install git+https://github.com/TheMarketMakerHoldings/algobacktesting
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Verification
|
|
164
|
+
|
|
165
|
+
- **Paper numbers:** every number printed in the source papers that these functions
|
|
166
|
+
compute is reproduced in the tests at the paper's printed precision. For example: DSR
|
|
167
|
+
0.9004, PSR 0.982 / 0.913 / 0.953, and minimum track record 2.73 / 2.83 / 3.24 / 4.99
|
|
168
|
+
years.
|
|
169
|
+
- **Independent checks:** exact numerical integration, seeded Monte Carlo calibration,
|
|
170
|
+
and a brute-force reference implementation for cross-validation, run on hundreds of
|
|
171
|
+
random datasets.
|
|
172
|
+
- **Coverage and platforms:** 100% line and branch coverage. CI runs the tests on Python
|
|
173
|
+
3.11–3.14, on Linux, Windows and macOS, and on the oldest supported NumPy (1.26).
|
|
174
|
+
- **Documentation:** the code and numbers in this README are run and compared by the tests.
|
|
175
|
+
|
|
176
|
+
## Documentation
|
|
177
|
+
|
|
178
|
+
| If you want | Read |
|
|
179
|
+
| --- | --- |
|
|
180
|
+
| Every function and its signature | [API reference](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/api.md) |
|
|
181
|
+
| The ideas in plain English, with worked examples | [Explainers](https://github.com/TheMarketMakerHoldings/algobacktesting/tree/main/docs/explainers) |
|
|
182
|
+
| The evidence: proofs, replication tables and errata | [Verification report](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/paper/verification-report.md) |
|
|
183
|
+
| Runnable demonstrations | [`examples/`](https://github.com/TheMarketMakerHoldings/algobacktesting/tree/main/examples) |
|
|
184
|
+
| What changed | [Changelog](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CHANGELOG.md) |
|
|
185
|
+
| To contribute or report a wrong number | [Contributing](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CONTRIBUTING.md) |
|
|
186
|
+
|
|
187
|
+
## References
|
|
188
|
+
|
|
189
|
+
- Bailey, D. H. and López de Prado, M. (2012). The Sharpe Ratio Efficient Frontier.
|
|
190
|
+
*Journal of Risk* 15(2).
|
|
191
|
+
- Bailey, D. H. and López de Prado, M. (2014). The Deflated Sharpe Ratio: Correcting for
|
|
192
|
+
Selection Bias, Backtest Overfitting and Non-Normality. *Journal of Portfolio Management*
|
|
193
|
+
40(5).
|
|
194
|
+
- Bailey, D. H., Borwein, J., López de Prado, M. and Zhu, Q. J. (2017). The Probability of
|
|
195
|
+
Backtest Overfitting. *Journal of Computational Finance* 20(4).
|
|
196
|
+
- López de Prado, M. (2018). *Advances in Financial Machine Learning*. Wiley.
|
|
197
|
+
|
|
198
|
+
## Disclaimer
|
|
199
|
+
|
|
200
|
+
Statistical tools for research. Not investment advice.
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT © 2026 Daniel Heidary. Published by The Market Maker Holdings & Co.
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# algobacktesting
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/algobacktesting/)
|
|
4
|
+
[](https://github.com/TheMarketMakerHoldings/algobacktesting/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/LICENSE)
|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
**Is your backtest real? Overfitting checks for trading research.**
|
|
10
|
+
|
|
11
|
+
> The Sharpe and PBO modules are verified against their source papers. The cross-validation
|
|
12
|
+
> module is built from the standard definitions and is awaiting review against the book.
|
|
13
|
+
|
|
14
|
+
Try enough strategy variations and one of them will look brilliant by luck alone.
|
|
15
|
+
`algobacktesting` implements statistical tests from Bailey & López de Prado's research that
|
|
16
|
+
tell a real edge apart from an overfit one.
|
|
17
|
+
|
|
18
|
+
**Not a programmer?** The [online check](https://algobacktesting.com/)
|
|
19
|
+
asks one question: are you actually profitable, or just lucky? Import a strategy's trades from
|
|
20
|
+
TradingView, TopstepX, Quantower, NinjaTrader or thinkorswim, paste them, or enter a win rate. Its stop lab
|
|
21
|
+
replays your entries on your own price bars to test other stop sizes, and counts every size
|
|
22
|
+
tried so a lucky stop is not mistaken for a better one. A prop-firm simulator estimates your
|
|
23
|
+
chance of passing a funded-account evaluation from your own trades. A strategy builder tests a rule you
|
|
24
|
+
write as a sentence on your own bars, counting every version you try. Save strategies under a
|
|
25
|
+
name and compare any two side by side, and share a result with a link that holds no trades. It runs in the browser, and nothing is uploaded.
|
|
26
|
+
|
|
27
|
+
| Question | Tool |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| Is this Sharpe ratio still impressive after counting every strategy I tried? | `deflated_sharpe_ratio` (DSR), `probabilistic_sharpe_ratio` (PSR), `min_track_record_length` |
|
|
30
|
+
| How likely is it that my "best" strategy is just the luckiest one? | `probability_of_backtest_overfitting` (PBO, via combinatorially symmetric cross-validation) |
|
|
31
|
+
| How do I cross-validate on time series without leaking the future? | `purged_kfold` (with embargo), `combinatorial_purged_cv` (CPCV) |
|
|
32
|
+
|
|
33
|
+
## Why this matters
|
|
34
|
+
|
|
35
|
+
I've consistently struggled with trading setups that had amazing winning streaks. They
|
|
36
|
+
looked great, but my overall profitability was neglected because of my obsession with my
|
|
37
|
+
strategies. The problem was that I didn't realize how unreliable my strategies really were
|
|
38
|
+
until I started calculating them. My strategies had incredibly high reward-to-risk setups
|
|
39
|
+
that happened frequently, and I launched them on Quantower using Python algorithms I wrote in
|
|
40
|
+
Visual Studio 2022, so the code ran my strategies automatically. I would often watch around
|
|
41
|
+
the peak hours of my setups and sometimes see them take off, and I told myself the winning
|
|
42
|
+
trades would take care of the occasional losses. Little did I know I had a 33% win rate and
|
|
43
|
+
my account was turning very negative.
|
|
44
|
+
|
|
45
|
+
When I saw the numbers, I didn't believe them. I blamed my brokerage for counting breakevens
|
|
46
|
+
as negative, but in reality a breakeven is a negative trade: if your strategy has a real edge,
|
|
47
|
+
you shouldn't need to worry about breakevens counting against you. Seeing my win rate broke
|
|
48
|
+
my dreams. It made me feel like trading without professional experience is for losers. I
|
|
49
|
+
called myself "exit liquidity", the target market for day-trading gurus, even though I had
|
|
50
|
+
started with my own analysis of the market instead of listening to gurus who tell you that
|
|
51
|
+
you can draw random patterns on a chart and that watching level 2 tick data is stupid. They
|
|
52
|
+
say that to hide the truth: trading comes down to data.
|
|
53
|
+
|
|
54
|
+
Now, when I develop a strategy, I backtest it on historical market data and run the results
|
|
55
|
+
through `algobacktesting` to see whether the strategy really has an edge.
|
|
56
|
+
|
|
57
|
+
## Quickstart
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import numpy as np
|
|
61
|
+
from algobacktesting import (
|
|
62
|
+
column_sharpe_ratios,
|
|
63
|
+
deflated_sharpe_ratio,
|
|
64
|
+
probability_of_backtest_overfitting,
|
|
65
|
+
return_stats,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
returns = np.random.default_rng(42).normal(0, 0.01, size=(1008, 200)) # 200 strategies, no edge
|
|
69
|
+
|
|
70
|
+
sharpes = column_sharpe_ratios(returns) # per-day Sharpe of each strategy
|
|
71
|
+
best = return_stats(returns[:, sharpes.argmax()]) # the backtest you'd be tempted to trade
|
|
72
|
+
|
|
73
|
+
dsr = deflated_sharpe_ratio(
|
|
74
|
+
best.sharpe,
|
|
75
|
+
best.n_obs,
|
|
76
|
+
n_trials=200,
|
|
77
|
+
sharpe_variance=sharpes.var(ddof=1),
|
|
78
|
+
skew=best.skew,
|
|
79
|
+
kurtosis=best.kurtosis,
|
|
80
|
+
)
|
|
81
|
+
pbo = probability_of_backtest_overfitting(returns, n_partitions=16).pbo
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`examples/overfit_demo.py` runs this for three worlds: no edge, a modest real edge, and a
|
|
85
|
+
strong real edge. Its output:
|
|
86
|
+
|
|
87
|
+
| World | Best backtest (annual Sharpe) | Deflated Sharpe | PBO |
|
|
88
|
+
| --- | --- | --- | --- |
|
|
89
|
+
| 200 strategies, no edge | 1.69 | 0.654 | 0.275 |
|
|
90
|
+
| plus one modest real edge (true Sharpe ~1.3) | 1.69 | 0.642 | 0.274 |
|
|
91
|
+
| plus one strong real edge (true Sharpe ~2.4) | 2.22 | 0.906 | 0.082 |
|
|
92
|
+
|
|
93
|
+
- **No edge:** pure luck produced a 1.69 Sharpe. DSR refuses to call it real.
|
|
94
|
+
- **Modest edge:** it exists, but the luckiest noise strategy still wins the search.
|
|
95
|
+
Searching 200 variants buries a 1.3 Sharpe edge.
|
|
96
|
+
- **Strong edge:** it wins, and PBO drops to 0.08, so the selection process works. DSR is
|
|
97
|
+
still below 0.95: after 200 tries, four years of data is not enough to *prove* it.
|
|
98
|
+
|
|
99
|
+
### Why purging matters
|
|
100
|
+
|
|
101
|
+
`examples/leakage_demo.py` cross-validates a model that has **no real skill**: a
|
|
102
|
+
nearest-neighbour model predicting 20-day returns from pure noise. Averaged over 200
|
|
103
|
+
datasets:
|
|
104
|
+
|
|
105
|
+
| Cross-validation scheme | Out-of-sample correlation (honest value: 0) |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| Shuffled k-fold | +0.816 (s.e. 0.002) |
|
|
108
|
+
| Contiguous k-fold, no purging | +0.050 (s.e. 0.005) |
|
|
109
|
+
| Purged k-fold | +0.000 (s.e. 0.005) |
|
|
110
|
+
| Purged k-fold with a 5% embargo | -0.001 (s.e. 0.005) |
|
|
111
|
+
| Leak removed by purging (paired difference) | +0.050 (s.e. 0.002) |
|
|
112
|
+
|
|
113
|
+
Shuffling makes a worthless model look excellent. Without purging, even unshuffled
|
|
114
|
+
folds leak at their edges. One dataset's score varies by about 0.07, so that leak is
|
|
115
|
+
invisible in a single backtest while it inflates every one of them. Purged k-fold
|
|
116
|
+
gives the honest answer.
|
|
117
|
+
|
|
118
|
+
Each function's docstring cites the equation and page it implements. `docs/explainers/`
|
|
119
|
+
has a plain-English guide to each module.
|
|
120
|
+
|
|
121
|
+
## Installation
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
pip install algobacktesting
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Requires Python 3.11+ and NumPy, which installs with it. For the latest version from GitHub
|
|
128
|
+
instead (needs Git):
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pip install git+https://github.com/TheMarketMakerHoldings/algobacktesting
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Verification
|
|
135
|
+
|
|
136
|
+
- **Paper numbers:** every number printed in the source papers that these functions
|
|
137
|
+
compute is reproduced in the tests at the paper's printed precision. For example: DSR
|
|
138
|
+
0.9004, PSR 0.982 / 0.913 / 0.953, and minimum track record 2.73 / 2.83 / 3.24 / 4.99
|
|
139
|
+
years.
|
|
140
|
+
- **Independent checks:** exact numerical integration, seeded Monte Carlo calibration,
|
|
141
|
+
and a brute-force reference implementation for cross-validation, run on hundreds of
|
|
142
|
+
random datasets.
|
|
143
|
+
- **Coverage and platforms:** 100% line and branch coverage. CI runs the tests on Python
|
|
144
|
+
3.11–3.14, on Linux, Windows and macOS, and on the oldest supported NumPy (1.26).
|
|
145
|
+
- **Documentation:** the code and numbers in this README are run and compared by the tests.
|
|
146
|
+
|
|
147
|
+
## Documentation
|
|
148
|
+
|
|
149
|
+
| If you want | Read |
|
|
150
|
+
| --- | --- |
|
|
151
|
+
| Every function and its signature | [API reference](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/api.md) |
|
|
152
|
+
| The ideas in plain English, with worked examples | [Explainers](https://github.com/TheMarketMakerHoldings/algobacktesting/tree/main/docs/explainers) |
|
|
153
|
+
| The evidence: proofs, replication tables and errata | [Verification report](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/paper/verification-report.md) |
|
|
154
|
+
| Runnable demonstrations | [`examples/`](https://github.com/TheMarketMakerHoldings/algobacktesting/tree/main/examples) |
|
|
155
|
+
| What changed | [Changelog](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CHANGELOG.md) |
|
|
156
|
+
| To contribute or report a wrong number | [Contributing](https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CONTRIBUTING.md) |
|
|
157
|
+
|
|
158
|
+
## References
|
|
159
|
+
|
|
160
|
+
- Bailey, D. H. and López de Prado, M. (2012). The Sharpe Ratio Efficient Frontier.
|
|
161
|
+
*Journal of Risk* 15(2).
|
|
162
|
+
- Bailey, D. H. and López de Prado, M. (2014). The Deflated Sharpe Ratio: Correcting for
|
|
163
|
+
Selection Bias, Backtest Overfitting and Non-Normality. *Journal of Portfolio Management*
|
|
164
|
+
40(5).
|
|
165
|
+
- Bailey, D. H., Borwein, J., López de Prado, M. and Zhu, Q. J. (2017). The Probability of
|
|
166
|
+
Backtest Overfitting. *Journal of Computational Finance* 20(4).
|
|
167
|
+
- López de Prado, M. (2018). *Advances in Financial Machine Learning*. Wiley.
|
|
168
|
+
|
|
169
|
+
## Disclaimer
|
|
170
|
+
|
|
171
|
+
Statistical tools for research. Not investment advice.
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT © 2026 Daniel Heidary. Published by The Market Maker Holdings & Co.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "algobacktesting"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Is your backtest real? Deflated Sharpe Ratio, Probability of Backtest Overfitting, and purged cross-validation for trading research."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = ["numpy>=1.26"]
|
|
10
|
+
keywords = [
|
|
11
|
+
"backtesting",
|
|
12
|
+
"overfitting",
|
|
13
|
+
"deflated sharpe ratio",
|
|
14
|
+
"probability of backtest overfitting",
|
|
15
|
+
"quantitative finance",
|
|
16
|
+
"cross-validation",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
21
|
+
"Intended Audience :: Science/Research",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Programming Language :: Python :: 3.14",
|
|
28
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
29
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[[project.authors]]
|
|
34
|
+
name = "Daniel Heidary"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://algobacktesting.com/"
|
|
38
|
+
Repository = "https://github.com/TheMarketMakerHoldings/algobacktesting"
|
|
39
|
+
Documentation = "https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/api.md"
|
|
40
|
+
Changelog = "https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CHANGELOG.md"
|
|
41
|
+
Issues = "https://github.com/TheMarketMakerHoldings/algobacktesting/issues"
|
|
42
|
+
|
|
43
|
+
[build-system]
|
|
44
|
+
requires = ["uv_build>=0.11.16,<0.14.0"]
|
|
45
|
+
build-backend = "uv_build"
|
|
46
|
+
|
|
47
|
+
[dependency-groups]
|
|
48
|
+
dev = [
|
|
49
|
+
"hypothesis>=6.170.15",
|
|
50
|
+
"mypy>=2.4.1",
|
|
51
|
+
"pre-commit>=4.7.0",
|
|
52
|
+
"pytest>=9.2.0",
|
|
53
|
+
"pytest-cov>=7.1.0",
|
|
54
|
+
"ruff>=0.17.5",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.ruff]
|
|
58
|
+
line-length = 100
|
|
59
|
+
target-version = "py311"
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint]
|
|
62
|
+
select = [
|
|
63
|
+
"E",
|
|
64
|
+
"W",
|
|
65
|
+
"F",
|
|
66
|
+
"I",
|
|
67
|
+
"B",
|
|
68
|
+
"UP",
|
|
69
|
+
"SIM",
|
|
70
|
+
"RUF",
|
|
71
|
+
"N",
|
|
72
|
+
"D",
|
|
73
|
+
"NPY",
|
|
74
|
+
"PT",
|
|
75
|
+
"ANN",
|
|
76
|
+
]
|
|
77
|
+
ignore = [
|
|
78
|
+
"D203",
|
|
79
|
+
"D213",
|
|
80
|
+
"D105",
|
|
81
|
+
"D107",
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
[tool.ruff.lint.per-file-ignores]
|
|
85
|
+
"tests/**" = [
|
|
86
|
+
"D",
|
|
87
|
+
"ANN",
|
|
88
|
+
"S101",
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
[tool.mypy]
|
|
92
|
+
strict = true
|
|
93
|
+
python_version = "3.11"
|
|
94
|
+
|
|
95
|
+
[tool.pytest.ini_options]
|
|
96
|
+
testpaths = [
|
|
97
|
+
"tests",
|
|
98
|
+
"src",
|
|
99
|
+
]
|
|
100
|
+
addopts = "-ra --strict-markers --doctest-modules --import-mode=importlib"
|
|
101
|
+
pythonpath = ["src"]
|
|
102
|
+
|
|
103
|
+
[tool.coverage.run]
|
|
104
|
+
source = ["algobacktesting"]
|
|
105
|
+
branch = true
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "algobacktesting"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Is your backtest real? Deflated Sharpe Ratio, Probability of Backtest Overfitting, and purged cross-validation for trading research."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "Daniel Heidary" }]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = ["numpy>=1.26"]
|
|
11
|
+
keywords = [
|
|
12
|
+
"backtesting",
|
|
13
|
+
"overfitting",
|
|
14
|
+
"deflated sharpe ratio",
|
|
15
|
+
"probability of backtest overfitting",
|
|
16
|
+
"quantitative finance",
|
|
17
|
+
"cross-validation",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 3 - Alpha",
|
|
21
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
22
|
+
"Intended Audience :: Science/Research",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
29
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
30
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://algobacktesting.com/"
|
|
36
|
+
Repository = "https://github.com/TheMarketMakerHoldings/algobacktesting"
|
|
37
|
+
Documentation = "https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/docs/api.md"
|
|
38
|
+
Changelog = "https://github.com/TheMarketMakerHoldings/algobacktesting/blob/main/CHANGELOG.md"
|
|
39
|
+
Issues = "https://github.com/TheMarketMakerHoldings/algobacktesting/issues"
|
|
40
|
+
|
|
41
|
+
[build-system]
|
|
42
|
+
requires = ["uv_build>=0.11.16,<0.14.0"]
|
|
43
|
+
build-backend = "uv_build"
|
|
44
|
+
|
|
45
|
+
[dependency-groups]
|
|
46
|
+
dev = [
|
|
47
|
+
"hypothesis>=6.170.15",
|
|
48
|
+
"mypy>=2.4.1",
|
|
49
|
+
"pre-commit>=4.7.0",
|
|
50
|
+
"pytest>=9.2.0",
|
|
51
|
+
"pytest-cov>=7.1.0",
|
|
52
|
+
"ruff>=0.17.5",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 100
|
|
57
|
+
target-version = "py311"
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
select = ["E", "W", "F", "I", "B", "UP", "SIM", "RUF", "N", "D", "NPY", "PT", "ANN"]
|
|
61
|
+
ignore = ["D203", "D213", "D105", "D107"]
|
|
62
|
+
|
|
63
|
+
[tool.ruff.lint.per-file-ignores]
|
|
64
|
+
"tests/**" = ["D", "ANN", "S101"]
|
|
65
|
+
|
|
66
|
+
[tool.mypy]
|
|
67
|
+
strict = true
|
|
68
|
+
python_version = "3.11"
|
|
69
|
+
|
|
70
|
+
[tool.pytest.ini_options]
|
|
71
|
+
testpaths = ["tests", "src"]
|
|
72
|
+
addopts = "-ra --strict-markers --doctest-modules --import-mode=importlib"
|
|
73
|
+
pythonpath = ["src"]
|
|
74
|
+
|
|
75
|
+
[tool.coverage.run]
|
|
76
|
+
source = ["algobacktesting"]
|
|
77
|
+
branch = true
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""algobacktesting: tools for telling real trading edges from backtest overfitting.
|
|
2
|
+
|
|
3
|
+
Published by The Market Maker Holdings & Co. Not investment advice.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from algobacktesting.cv import (
|
|
7
|
+
CPCVResult,
|
|
8
|
+
Split,
|
|
9
|
+
combinatorial_purged_cv,
|
|
10
|
+
purged_kfold,
|
|
11
|
+
)
|
|
12
|
+
from algobacktesting.pbo import (
|
|
13
|
+
PBOResult,
|
|
14
|
+
column_sharpe_ratios,
|
|
15
|
+
probability_of_backtest_overfitting,
|
|
16
|
+
)
|
|
17
|
+
from algobacktesting.sharpe import (
|
|
18
|
+
EULER_GAMMA,
|
|
19
|
+
ReturnStats,
|
|
20
|
+
annualize_sharpe,
|
|
21
|
+
deannualize_sharpe,
|
|
22
|
+
deflated_sharpe_ratio,
|
|
23
|
+
expected_max_sharpe,
|
|
24
|
+
min_track_record_length,
|
|
25
|
+
probabilistic_sharpe_ratio,
|
|
26
|
+
return_stats,
|
|
27
|
+
sharpe_ratio,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__version__ = "0.1.0"
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"EULER_GAMMA",
|
|
34
|
+
"CPCVResult",
|
|
35
|
+
"PBOResult",
|
|
36
|
+
"ReturnStats",
|
|
37
|
+
"Split",
|
|
38
|
+
"__version__",
|
|
39
|
+
"annualize_sharpe",
|
|
40
|
+
"column_sharpe_ratios",
|
|
41
|
+
"combinatorial_purged_cv",
|
|
42
|
+
"deannualize_sharpe",
|
|
43
|
+
"deflated_sharpe_ratio",
|
|
44
|
+
"expected_max_sharpe",
|
|
45
|
+
"min_track_record_length",
|
|
46
|
+
"probabilistic_sharpe_ratio",
|
|
47
|
+
"probability_of_backtest_overfitting",
|
|
48
|
+
"purged_kfold",
|
|
49
|
+
"return_stats",
|
|
50
|
+
"sharpe_ratio",
|
|
51
|
+
]
|