Skip to content

Instantly share code, notes, and snippets.

@18182324
Created November 29, 2025 16:07
Show Gist options
  • Select an option

  • Save 18182324/feac54e561b1249495bcc23ac79c8394 to your computer and use it in GitHub Desktop.

Select an option

Save 18182324/feac54e561b1249495bcc23ac79c8394 to your computer and use it in GitHub Desktop.
AI prompt pairs trading system
You are a senior quantitative developer and trading strategist.
Your task is to design and implement, in Python, a modular PAIRS TRADING backtesting and evaluation engine that I can extend later.
General requirements:
- Use Python 3, pandas, NumPy and statsmodels, and clearly list all required packages.
- Write clean, well commented code that a professional trader can understand.
- Organize the logic into functions or simple classes, not a single script blob.
- Assume I provide daily adjusted close prices for multiple tickers in a CSV or via a DataFrame.
1) Data handling
- Implement a function to:
- Load price data for a universe of tickers from CSV.
- Convert prices to log returns.
- Align dates and handle missing data in a transparent way (e.g. forward fill, with a warning).
2) Pair selection modules
Implement separate modules (functions) for at least these methods:
- Distance method:
- Compute Euclidean distance between normalized cumulative return series over a formation window.
- Return a ranked list of top N pairs.
- Cointegration method:
- Use Engle–Granger tests to find cointegrated pairs.
- Return pairs that pass significance thresholds, with estimated hedge ratios.
- Correlation based selection:
- Compute Pearson, Spearman and Kendall correlations on returns.
- Return pairs sorted by Kendall correlation.
- (Optional, sketched only) Copula or PCA based selection:
- Provide a placeholder function and comments describing how it would be integrated.
3) Trading rules
- Implement a generic pair trading rule that can work with any selected pair:
- Inputs:
- Price series of X and Y,
- Hedge ratio,
- Formation window length,
- Entry Z score threshold (e.g. 2),
- Exit Z score threshold (e.g. 0),
- Side: trade both long–short directions.
- Logic:
- Construct the spread: spread = price_X – beta * price_Y.
- Standardize spread using rolling mean and rolling standard deviation.
- Enter trades when Z score crosses the entry threshold.
- Exit trades when Z score reverts toward zero or when a max holding period is reached.
- Include:
- Per trade transaction costs,
- Slippage parameter,
- Position sizing in dollar terms or as a fixed notional.
4) Backtest engine
- Implement a backtest function that:
- Takes:
- A list of pairs with hedge ratios,
- A trading rule configuration,
- Start and end dates,
- Transaction cost assumptions.
- Iterates over all pairs and over time, simulates trades, and records:
- Entry and exit dates,
- Direction (long spread or short spread),
- PnL per trade net of costs,
- Max drawdown within each trade,
- Holding period in days.
5) Performance evaluation
- Implement a performance module that aggregates results across all pairs and computes:
- Total return,
- Annualized return,
- Annualized volatility,
- Sharpe ratio,
- Max drawdown,
- Win rate,
- Average profit per trade,
- Average holding period,
- Turnover.
- Provide a simple textual report and a few key plots:
- Equity curve over time,
- Distribution of trade returns,
- Histogram of holding periods.
6) Configuration and extension
- Show a short example script that:
- Loads sample data,
- Selects pairs using:
- Distance method, then
- Cointegration method,
- Runs the backtest for each,
- Prints and compares Sharpe ratio, max drawdown and win rate across methods.
- Comment in the code where I could later:
- Plug in machine learning based pair selection,
- Add copula based signals,
- Attach a RAG or MCP style layer that pulls configuration and parameters from external files or APIs.
Output format:
- First, show the high level module and function structure.
- Then provide the full Python code, split into logical sections with comments.
- Finally, provide a short “how to run this” section with example commands.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment