Skip to content

IsaacCheng9/quant-trading-strategy-backtester

Repository files navigation

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
  • Automatic strategy optimisation – grid search over parameter combinations and stock selection from S&P 500
  • Transaction costs and slippage modelling – configurable fees and slippage for realistic performance estimates
  • 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 Ratio, Max Drawdown, and monthly performance table with rolling returns
  • Real-time data fetching – historical market data from Yahoo Finance

Screenshots

Pairs Trading without Optimisation 1 Pairs Trading without Optimisation 2

Pairs Trading with Strategy Parameter Optimisation

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

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.

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages