Static analysis tool for detecting look-ahead bias, data leakage, and statistical errors in Python backtesting code.
Project description
backtest-audit
Static analysis for Python backtesting code.
Catches look-ahead bias, data leakage, and statistical errors — before they invalidate your results.
pip install backtest-audit
backtest-audit check ./strategies/
Why
A backtest that looks profitable is often built on data the strategy could never have had.
Look-ahead bias, data leakage, and missing transaction costs are the three most common ways
a backtest lies to you. backtest-audit catches these at the code level, without running
your strategy.
Output
── strategies/mean_reversion.py ─────────────────────── 2 issues ──
[LAB001] Negative shift — future data pulled into current timestep line 34
df['signal'] = df['close'].shift(-5)
↳ Replace shift(-n) with shift(n). Negative periods reference future rows, invalidating the backtest.
[LEAK002] fit_transform() detected — ensure this is called only on training data line 61
X_scaled = scaler.fit_transform(X)
↳ Replace fit_transform(X) with fit(X_train).transform(X_train), then transform(X_test) separately.
──────────────────────────────────────────────────────────────────────
1 error, 1 warning
Rules
| Code | Category | Description |
|---|---|---|
| LAB001 | Look-Ahead Bias | shift(-n) pulls future prices into current signal |
| LAB002 | Look-Ahead Bias | pct_change(-n) computes forward returns |
| LEAK001 | Data Leakage | fit() called with no train_test_split detected |
| LEAK002 | Data Leakage | fit_transform() may be called on unsplit data |
| STAT001 | Statistical Malpractice | Sharpe ratio assigned without annualization factor |
| STAT002 | Statistical Malpractice | No transaction cost or slippage variable detected |
Full documentation with explanations and code examples: docs/rules/
Usage
# Check a single file
backtest-audit check my_backtest.py
# Check a directory
backtest-audit check ./strategies/
# Output as JSON (for CI pipelines)
backtest-audit check ./strategies/ --format json
# Ignore specific rules
backtest-audit check ./strategies/ --ignore LAB002,STAT002
# List all rules
backtest-audit rules
Installation
pip install backtest-audit
Requires Python 3.8+. The only dependency is click.
How It Works
backtest-audit parses your Python source files into an abstract syntax tree (AST)
and walks the tree looking for known problematic patterns. It does not execute your code.
Each rule is a pattern matcher — it looks for a specific construct that is known to
produce invalid backtest results.
This means it is fast, safe to run on any codebase, and produces no false positives from runtime behavior.
Limitations
- Does not run your backtest or evaluate strategy profitability
- Cannot detect all forms of look-ahead bias — only statically visible patterns
- LEAK001 checks for
train_test_splitpresence in the file, not correct application - Manual chronological splits (e.g.,
df[:split_date]) are not yet recognized - Does not support non-Python backtesting frameworks
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file backtest_audit-1.0.0.tar.gz.
File metadata
- Download URL: backtest_audit-1.0.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7db5b3cb6aa251ea01513983d7482b59489d24dde49afe998149f744d704f0e5
|
|
| MD5 |
ff501d86dd4c955ba29204583357ac1e
|
|
| BLAKE2b-256 |
c1e0cf6a71b14fdd7e3ce5b8393e02ef6abe8392f034524e7e73085e4364db52
|
File details
Details for the file backtest_audit-1.0.0-py3-none-any.whl.
File metadata
- Download URL: backtest_audit-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d38db58ef15db35c5545191824e32a970423310ac53a64c248b79d71e6cbf42c
|
|
| MD5 |
0e0d80d0b6a4829491980cf68e500f69
|
|
| BLAKE2b-256 |
7c4d67108f6349d9bae74859a8714c68f77d9463a72c5bc96b0789d4cbf4a187
|