Skip to content

This repository showcases Pairs trading bot exploiting mean reversion in cointegrated assets. Fetches data, runs Engle-Granger tests, generates signals (Z-Score/Bollinger), and backtests results. A PyQt GUI provides interactive configuration and real-time visualization.

Notifications You must be signed in to change notification settings

Hyperakan/Quantitative-Arbitrage

Repository files navigation

Quantitative Arbitrage: Multi-Asset Cointegration & Statistical Trading

This repository implements a pairs trading bot that exploits mean-reverting relationships between cointegrated assets. While examples emphasize BIST100, the approach is asset-agnostic and can be applied to any liquid market.


Project Overview

1. Core Idea

  • Detect statistically significant cointegration between asset pairs using the Engle-Granger test.
  • Go long one asset and short the other when their spread or ratio diverges.
  • Profit when the price relationship mean-reverts.

2. Workflow

  • 📈 Data: Download & clean 10 years of historical prices (e.g., from Yahoo Finance).
  • 🔍 Cointegration: Identify statistically stable candidate pairs.
  • 🧠 Signals: Use Z-Scores or Bollinger Bands to trigger trades.
  • 📊 Backtesting: Simulate trades, log positions, and track profitability.
  • 🖥️ GUI: A PyQt-based trading simulator with configurable strategy settings and real-time charts.

Screenshots & Visuals

1. Data Imbalance in ML Model

Figure 1 – Distribution of future profit labels used in AI-based pair selection. The strong class imbalance posed a challenge to supervised models.


2. GUI – Final Simulation Results

Figure 2 – Final dialog after backtesting completion, displaying capital summary and option to export trade logs as CSV.


3. GUI – Trading Dashboard

Figure 3 – Main PyQt5 interface (gui.py) with configurable parameters, dynamic pair re-selection, real-time price/spread plots, and trade history tracking.


File Structure

  • data_preprocessing.py
    Cleans the raw data: filters incomplete records and forward-fills missing values.

  • cointegration_tests.py
    Applies the Engle-Granger test to discover statistically cointegrated pairs.

  • pair_selection.py
    Ranks cointegrated pairs using half-life, volatility, or profit simulations.

  • trading_signals.py
    Generates entry/exit signals using:

    • Ratio Z-Score
    • Spread Z-Score
    • Bollinger Bands
  • backtester.py
    Executes trades in a simulated environment, logs results, and computes:

    • Total Return
    • Sharpe Ratio
    • Max Drawdown
    • Win Rate
  • gui.py
    Launches a user-friendly PyQt5 simulator interface for live visualization.

  • main.py
    Entry point:

    • --mode backtest: Run full backtest via CLI
    • --mode gui: Open simulator GUI

Key Results

  • Spread Z-Score method achieved 234.8% return and 3.16 Sharpe Ratio during backtesting.
  • Outperformed a simple BIST100 buy-and-hold benchmark (25.4% return).
  • Preliminary AI-based pair selection underperformed due to data imbalance.
  • A dynamic GUI enables rapid experimentation with parameters and strategies.

References

  1. Gatev, E., Goetzmann, W. N., & Rouwenhorst, K. G. (2006). Pairs trading: Performance of a relative-value arbitrage rule. Review of Financial Studies, 19(3), 797–827.
  2. Vidyamurthy, G. (2004). Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons.
  3. Caldeira, J. F., & Moura, O. A. (2013). Selection of pairs of stocks based on cointegration: A statistical arbitrage strategy. Brazilian Review of Finance, 11(3), 369–402.
  4. Krauss, C., Do, X., & Huck, N. (2017). Deep neural networks, gradient-boosted trees, random forests: Statistical arbitrage on the S&P 500. European Journal of Operational Research, 259(2), 689–702.

Disclaimer

⚠️ This repository is intended for research and educational purposes. Financial data used in simulations may be incomplete or noisy. Do not use in live trading environments without further validation, slippage modeling, and risk management procedures.

About

This repository showcases Pairs trading bot exploiting mean reversion in cointegrated assets. Fetches data, runs Engle-Granger tests, generates signals (Z-Score/Bollinger), and backtests results. A PyQt GUI provides interactive configuration and real-time visualization.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published