backtest-leakage-audit
Catch the two most common lies a trading backtest tells: look-ahead bias and overfitting.
This is the companion code for the tutorial How to Catch Look-Ahead Bias and Overfitting in Your Trading Backtests with Python (link added once published).
Everything runs on synthetic data, so no market data, API key or broker account is needed. On a driftless random walk the true Sharpe ratio of every strategy is zero, so any profit is a bug or luck.
What it shows
| Problem | What goes wrong on pure noise | What catches it |
|---|---|---|
Signal reads the future (shift(-1), centred windows, full-sample stats) |
Sharpe ratios up to 10.81 | Future-scramble test on the signal |
Engine trades on the same bar it decides (missing shift(1)) |
Sharpe jumps from 0.52 to 4.86 | Future-scramble test on the positions |
| Picking the best of 466 parameter sets | Mean in-sample Sharpe 0.98, out-of-sample 0.08 | Deflated Sharpe ratio (fooled 0% of the time vs 42% for a plain test) |
A control dataset with a real edge passes every check when it is the only idea tested, so the tools are not simply rejecting everything. Searching many ideas raises the bar: see How much data you need.
Install
pip install backtest-leakage-audit
Python 3.11 or newer.
Quick start
Save your signal as a function in a file, for example my_signals.py:
import numpy as np
def my_signal(prices):
return np.sign(prices["close"].pct_change())
Then audit it against a CSV with dates in the first column and a close column:
backtest-leakage-audit prices.csv --signal my_signals:my_signal --cost-bps 5 --all-rows
The exit code is 0 when every check passes, 1 when one fails and 2 for bad input, so it can gate a CI job.
Add --trials trials.csv with one column of daily returns per strategy you tried, including this one, to
run the bootstrap data-snooping test.
Sample audit report:
Audit: best_crossover (annualised Sharpe 0.84)
[OK] signal uses no future data 0/50 values changed
[OK] position is decided before the bar it trades 0/50 values changed
[FAIL] edge survives multiple testing DSR over 466 trials = 0.215
Verdict: DO NOT TRUST THIS BACKTEST
Using it from Python
audit_backtest takes a prices DataFrame with a close column, plus any other columns your signal reads.
from backtest_leakage_audit import audit_backtest, parameter_grid, trial_return_matrix
trials = trial_return_matrix(prices, parameter_grid(), cost_bps=5) # or build your own: one column per idea
audit_backtest(
prices,
my_signal,
cost_bps=5, # audit after transaction costs
n_checks=None, # test every row, which catches leaks that only touch a few rows
trial_returns=trials, # every strategy you tried, including this one
)
Columns named open, high, low, adj_close or vwap are rescaled with the scrambled close. Every other
column has its future shuffled.
How much data you need
Both multiple-testing checks need a long history once you have searched many ideas. Here is how often each one passed in 15 to 30 synthetic histories, searching 466 moving-average pairs. The "Real edge" columns add one rule that truly works to the search:
| Test (pass at 5%) | Noise, 3 years | Real edge, 3 years | Noise, 10 years | Real edge, 10 years |
|---|---|---|---|---|
| Deflated Sharpe ratio | 3% | 7% | 0% | 73% |
| SPA test | 7% | 23% | 0% | 87% |
A failed check on a short history can mean "not enough evidence yet". It does not prove there is no edge.
Project layout
| File | Purpose |
|---|---|
src/backtest_leakage_audit/data.py |
Synthetic prices: a driftless random walk and an AR(1) control with a real edge |
src/backtest_leakage_audit/engine.py |
A tiny vectorised backtest engine, plus the deliberately leaky version |
src/backtest_leakage_audit/signals.py |
One honest signal and three that leak the future |
src/backtest_leakage_audit/lookahead.py |
The future-scramble test for look-ahead bias |
src/backtest_leakage_audit/overfitting.py |
Probabilistic and deflated Sharpe ratios, grid search, walk-forward |
src/backtest_leakage_audit/snooping.py |
Hansen's SPA test across every strategy you tried |
src/backtest_leakage_audit/audit.py |
Runs every check and prints a verdict |
src/backtest_leakage_audit/cli.py |
The backtest-leakage-audit command |
examples/ |
The scripts behind every number in the tutorial |
expected_output/ |
Exact output of each script, checked by tests/test_golden_outputs.py |
Reproducibility
git clone https://github.com/Iyanuoluwa007/backtest-leakage-audit.git
cd backtest-leakage-audit
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -e ".[dev]"
python examples/demo_leaks.py # leaky signals and the future-scramble test
python examples/demo_overfitting.py # grid search, deflated Sharpe ratio, walk-forward
python examples/monte_carlo.py # 100 noise histories plus a control (about 1 minute)
python examples/audit_demo.py # one plain-text verdict per strategy
python -m pytest -q # unit tests and golden-output tests
Every number in the tutorial is checked in CI. The golden-output tests run each script and compare its
output byte for byte with expected_output/. Tested on Python 3.11 with pandas 2.x, and on Python 3.14 and
3.15 with pandas 3.x. The SPA tests skip on 3.15, where arch has no wheel yet. The one-minute Monte Carlo
check runs with RUN_SLOW=1 python -m pytest -q.
The tutorial's code matches the v1.0.0
tag. See the changelog for what
has changed since.
What these checks can't catch
Survivorship bias, data that was revised after publication, leaks from data outside the prices DataFrame, an
under-counted number of trials, and execution costs beyond a flat cost_bps. See the tutorial for details.
Reference
Bailey, D. H. and Lopez de Prado, M. (2014). The Deflated Sharpe Ratio: Correcting for Selection Bias, Backtest Overfitting, and Non-Normality. Journal of Portfolio Management, 40(5), 94-107. SSRN 2460551
Hansen, P. R. (2005). A Test for Superior Predictive Ability. Journal of Business and Economic Statistics, 23(4), 365-380.
White, H. (2000). A Reality Check for Data Snooping. Econometrica, 68(5), 1097-1126.
License
Metadata
Release files for backtest-leakage-audit 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| backtest_leakage_audit-2.0.0.tar.gz | 23.5 kB | With provenance | Details for backtest_leakage_audit-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| backtest_leakage_audit-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for backtest_leakage_audit-2.0.0-py3-none-any.whl |
Total release size: 43.3 kB
Release files / backtest_leakage_audit-2.0.0.tar.gz
| Download URL | backtest_leakage_audit-2.0.0.tar.gz |
|---|---|
| Size | 23.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91b1429644dcf760a170aac7e297904a981eac4f1a8581029cb49b159a1b6575
|
|
BLAKE2b-256 checksum How to use checksums |
6db260f154d3e78e93a1b25ebe6a8e912bffb93e4f17c256c31957fa698d1bea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.
Transparency logRelease files / backtest_leakage_audit-2.0.0-py3-none-any.whl
| Download URL | backtest_leakage_audit-2.0.0-py3-none-any.whl |
|---|---|
| Size | 19.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24e94fde00b2e4ca2caae5c7a22e964595486a91512f408793bd96378782ba1e
|
|
BLAKE2b-256 checksum How to use checksums |
d21a4a2c06d4eadac24e6ccb6812fcd93bea905ba4c8f4baedddb9bf82573701
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.
Transparency logSecurity for backtest-leakage-audit 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for backtest_leakage_audit-2.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.