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.
- 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.
- 📈 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.
Figure 1 – Distribution of future profit labels used in AI-based pair selection. The strong class imbalance posed a challenge to supervised models.
Figure 2 – Final dialog after backtesting completion, displaying capital summary and option to export trade logs as CSV.
Figure 3 – Main PyQt5 interface (gui.py) with configurable parameters, dynamic pair re-selection, real-time price/spread plots, and trade history tracking.
-
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
- 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.
- 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.
- Vidyamurthy, G. (2004). Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons.
- 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.
- 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.
⚠️ 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.


