SkillAgentSearch skills...

Quant Trading Strategy Backtester

A quantitative trading strategy backtester with an interactive dashboard. Enables users to implement, test, and visualise trading strategies using historical market data, featuring customisable parameters and key performance metrics. Developed with Python and Polars.

Install / Use

npx skills add IsaacCheng9/quant-trading-strategy-backtester

Installs into whichever agent you are using.

README

Quant Trading Strategy Backtester

Ruff Test

A quantitative trading strategy backtester with an interactive dashboard. Enables users to implement, test, and visualise trading strategies using historical market data, featuring customisable parameters and key performance metrics. Developed with Python.

Try the deployed app here on Streamlit Cloud!

Key Features

  • Multiple trading strategies – Buy and Hold, Mean Reversion, Moving Average Crossover, and Pairs Trading
  • Walk-forward validation – parameter optimisation with expanding training windows to reduce overfitting
  • Exploratory current-universe selection – grid search over parameter combinations and today's largest S&P 500 constituents
  • Transaction costs and slippage modelling – configurable fees and slippage for realistic performance estimates
  • Trade ledger and cost attribution – explicit trade events, turnover, gross/net returns, and cumulative cost drag
  • Cointegration-gated pairs selection – Engle-Granger filtering for automatic pairs trading selection and p-value diagnostics for manual pairs
  • Benchmark-relative reporting – SPY-relative excess return, beta, annualised alpha, and information ratio for the displayed backtest period
  • Efficient data processing – vectorised computation using Polars for improved performance
  • Interactive web-based dashboard – Streamlit UI for strategy configuration, backtesting, and analysis
  • Trade and spread visualisation – equity curves with trade markers and pairs spread z-score charts with entry/exit threshold bands
  • Performance metrics – total return, Sharpe, Sortino, Calmar, drawdown depth/duration, and monthly performance table with rolling returns
  • Real-time data fetching – adjusted historical market data from Yahoo Finance

Methodology Notes

  • Pairs trading uses a rolling hedge ratio to build the spread and exposure-normalised leg weights to calculate pair returns and transaction costs. Automatic pair selection first filters candidates using an Engle-Granger cointegration test on the training split
  • Ticker/pair selection uses a 70/30 train/test split – candidates are ranked on training data and evaluated on held-out test data, but the candidate universe itself is not point in time
  • Benchmark comparison aligns the strategy and SPY return streams by date over the displayed backtest period before calculating relative metrics
  • Optimisation reporting shows valid parameter combinations, candidate ticker/pair counts, and whether displayed results come from full-period, held-out, or walk-forward evaluation
  • Walk-forward validation uses expanding training windows for more robust out-of-sample evaluation – recommended over the default split
  • Automatic selection is exploratory only – it uses today's S&P 500 constituents and current market capitalisations for historical backtests
    • This introduces survivorship bias and future-information leakage
    • Held-out prices do not make these results point-in-time or research-grade out-of-sample evidence

Screenshots

Pairs Trading without Optimisation 1 Pairs Trading without Optimisation 2

<!-- markdownlint-disable-next-line MD033 --> <details> <!-- markdownlint-disable-next-line MD033 --> <summary>Pairs Trading with Strategy Parameter Optimisation</summary>

Pairs Trading with Walk-Forward Optimisation 1 Pairs Trading with Walk-Forward Optimisation 2

</details>

Performance Benchmark of pandas vs. Polars Implementation

I originally implemented the backtester and optimiser using pandas, but I wanted to explore the performance benefits of using Polars with lazy evaluation.

I benchmarked the two implementations on my local machine (Apple M1 Max with 10 CPU cores and 32 GPU cores, 32 GB unified memory). Data for all 190 ticker pairs was pre-downloaded to isolate computation from network I/O. Each run executed 38,000 backtests (190 pairs x 200 parameter combinations) for the pairs trading strategy over 2020/01/01 to 2023/12/31.

Polars was faster by 2.6x on average compared to pandas.

Benchmark Results

The full benchmark results can be found in the CSV files in the resources folder.

Usage

Installing Dependencies

Run the following command from the project root directory:

uv sync --all-extras --dev

Running the Application Locally

Run the following command from the project root directory:

uv run streamlit run src/quant_trading_strategy_backtester/app.py

Rate Limiting Issues with Yahoo Finance

Note that you may encounter rate limiting issues with Yahoo Finance resulting in slow data fetches in the app – unfortunately this is out of my control. You could work around this by using a VPN, or wait for a while before trying again. Sometimes upgrading the yfinance package to the latest version can also help.

Related Skills

View on GitHub
GitHub Stars43
CategoryDevelopment
Updated6d ago
Forks7

Languages

Python

Security Score

80/100

Audited on Aug 1, 2026

No findings