Skip to main content

algobacktesting

PyPI CI License: MIT Python 3.11+ Coverage 100%

Is your backtest real? Overfitting checks for trading research.

The Sharpe and PBO modules are verified against their source papers. The cross-validation module is built from the standard definitions and is awaiting review against the book.

Try enough strategy variations and one of them will look brilliant by luck alone. algobacktesting implements statistical tests from Bailey & López de Prado's research that tell a real edge apart from an overfit one.

Not a programmer? The online check asks one question: are you actually profitable, or just lucky? Import a strategy's trades from TradingView, TopstepX, Quantower, NinjaTrader or thinkorswim, paste them, or enter a win rate. Its stop lab replays your entries on your own price bars to test other stop sizes, and counts every size tried so a lucky stop is not mistaken for a better one. A prop-firm simulator estimates your chance of passing a funded-account evaluation from your own trades. A strategy builder tests a rule you write as a sentence on your own bars, counting every version you try. Save strategies under a 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.

Question Tool
Is this Sharpe ratio still impressive after counting every strategy I tried? deflated_sharpe_ratio (DSR), probabilistic_sharpe_ratio (PSR), min_track_record_length
How likely is it that my "best" strategy is just the luckiest one? probability_of_backtest_overfitting (PBO, via combinatorially symmetric cross-validation)
How do I cross-validate on time series without leaking the future? purged_kfold (with embargo), combinatorial_purged_cv (CPCV)

Why this matters

I've consistently struggled with trading setups that had amazing winning streaks. They looked great, but my overall profitability was neglected because of my obsession with my strategies. The problem was that I didn't realize how unreliable my strategies really were until I started calculating them. My strategies had incredibly high reward-to-risk setups that happened frequently, and I launched them on Quantower using Python algorithms I wrote in Visual Studio 2022, so the code ran my strategies automatically. I would often watch around the peak hours of my setups and sometimes see them take off, and I told myself the winning trades would take care of the occasional losses. Little did I know I had a 33% win rate and my account was turning very negative.

When I saw the numbers, I didn't believe them. I blamed my brokerage for counting breakevens as negative, but in reality a breakeven is a negative trade: if your strategy has a real edge, you shouldn't need to worry about breakevens counting against you. Seeing my win rate broke my dreams. It made me feel like trading without professional experience is for losers. I called myself "exit liquidity", the target market for day-trading gurus, even though I had started with my own analysis of the market instead of listening to gurus who tell you that you can draw random patterns on a chart and that watching level 2 tick data is stupid. They say that to hide the truth: trading comes down to data.

Now, when I develop a strategy, I backtest it on historical market data and run the results through algobacktesting to see whether the strategy really has an edge.

Quickstart

import numpy as np
from algobacktesting import (
    column_sharpe_ratios,
    deflated_sharpe_ratio,
    probability_of_backtest_overfitting,
    return_stats,
)

returns = np.random.default_rng(42).normal(0, 0.01, size=(1008, 200))  # 200 strategies, no edge

sharpes = column_sharpe_ratios(returns)  # per-day Sharpe of each strategy
best = return_stats(returns[:, sharpes.argmax()])  # the backtest you'd be tempted to trade

dsr = deflated_sharpe_ratio(
    best.sharpe,
    best.n_obs,
    n_trials=200,
    sharpe_variance=sharpes.var(ddof=1),
    skew=best.skew,
    kurtosis=best.kurtosis,
)
pbo = probability_of_backtest_overfitting(returns, n_partitions=16).pbo

examples/overfit_demo.py runs this for three worlds: no edge, a modest real edge, and a strong real edge. Its output:

World Best backtest (annual Sharpe) Deflated Sharpe PBO
200 strategies, no edge 1.69 0.654 0.275
plus one modest real edge (true Sharpe ~1.3) 1.69 0.642 0.274
plus one strong real edge (true Sharpe ~2.4) 2.22 0.906 0.082
  • No edge: pure luck produced a 1.69 Sharpe. DSR refuses to call it real.
  • Modest edge: it exists, but the luckiest noise strategy still wins the search. Searching 200 variants buries a 1.3 Sharpe edge.
  • Strong edge: it wins, and PBO drops to 0.08, so the selection process works. DSR is still below 0.95: after 200 tries, four years of data is not enough to prove it.

Why purging matters

examples/leakage_demo.py cross-validates a model that has no real skill: a nearest-neighbour model predicting 20-day returns from pure noise. Averaged over 200 datasets:

Cross-validation scheme Out-of-sample correlation (honest value: 0)
Shuffled k-fold +0.816 (s.e. 0.002)
Contiguous k-fold, no purging +0.050 (s.e. 0.005)
Purged k-fold +0.000 (s.e. 0.005)
Purged k-fold with a 5% embargo -0.001 (s.e. 0.005)
Leak removed by purging (paired difference) +0.050 (s.e. 0.002)

Shuffling makes a worthless model look excellent. Without purging, even unshuffled folds leak at their edges. One dataset's score varies by about 0.07, so that leak is invisible in a single backtest while it inflates every one of them. Purged k-fold gives the honest answer.

Each function's docstring cites the equation and page it implements. docs/explainers/ has a plain-English guide to each module.

Installation

pip install algobacktesting

Requires Python 3.11+ and NumPy, which installs with it. For the latest version from GitHub instead (needs Git):

pip install git+https://github.com/TheMarketMakerHoldings/algobacktesting

Verification

  • Paper numbers: every number printed in the source papers that these functions compute is reproduced in the tests at the paper's printed precision. For example: DSR 0.9004, PSR 0.982 / 0.913 / 0.953, and minimum track record 2.73 / 2.83 / 3.24 / 4.99 years.
  • Independent checks: exact numerical integration, seeded Monte Carlo calibration, and a brute-force reference implementation for cross-validation, run on hundreds of random datasets.
  • Coverage and platforms: 100% line and branch coverage. CI runs the tests on Python 3.11–3.14, on Linux, Windows and macOS, and on the oldest supported NumPy (1.26).
  • Documentation: the code and numbers in this README are run and compared by the tests.

Documentation

If you want Read
Every function and its signature API reference
The ideas in plain English, with worked examples Explainers
The evidence: proofs, replication tables and errata Verification report
Runnable demonstrations examples/
What changed Changelog
To contribute or report a wrong number Contributing

References

  • Bailey, D. H. and López de Prado, M. (2012). The Sharpe Ratio Efficient Frontier. Journal of Risk 15(2).
  • Bailey, D. H. and López de Prado, M. (2014). The Deflated Sharpe Ratio: Correcting for Selection Bias, Backtest Overfitting and Non-Normality. Journal of Portfolio Management 40(5).
  • Bailey, D. H., Borwein, J., López de Prado, M. and Zhu, Q. J. (2017). The Probability of Backtest Overfitting. Journal of Computational Finance 20(4).
  • López de Prado, M. (2018). Advances in Financial Machine Learning. Wiley.

Disclaimer

Statistical tools for research. Not investment advice.

License

MIT © 2026 Daniel Heidary. Published by The Market Maker Holdings & Co.

Metadata

Release files for algobacktesting 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for algobacktesting 0.1.0
File Size Uploaded Provenance Actions
algobacktesting-0.1.0.tar.gz 18.2 kB With provenance Details for algobacktesting-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for algobacktesting 0.1.0
File Interpreter ABI Platform Actions
algobacktesting-0.1.0-py3-none-any.whl Python 3 none any Details for algobacktesting-0.1.0-py3-none-any.whl

Total release size: 38.1 kB

Release files / algobacktesting-0.1.0.tar.gz

Download URL algobacktesting-0.1.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4ae4f747b31bd3ac7a70a04b050a58cb3c19507e069e4c07de6f7d1d8112d8d6
BLAKE2b-256 checksum
How to use checksums
c7650c9fc0be8c45648c7de758644dbcc000af6ca1360771364722ba98caebfb
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 25, 2026.

Transparency log

Release files / algobacktesting-0.1.0-py3-none-any.whl

Download URL algobacktesting-0.1.0-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db18639db90fe6478595a874c3ba22fdcda12fb089ed9249c79cc1e3c3a71895
BLAKE2b-256 checksum
How to use checksums
17d21ae48ec3382fe4b1a22fc1f613ea636f3d38b2922d92356dc1f363fb716f
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 25, 2026.

Transparency log

Security for algobacktesting 0.1.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.

Select from 2 files

Attestation for algobacktesting-0.1.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 25, 2026.

Transparency log

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page