Created by Xuanru Li , Xingjian Yin | Duke University Financial Technology | 533 Watch demo video

Pairs Trading Strategy Dashboard

Analysis and performance of LEVI-MHK pair trading strategy

Pair Selection Analysis

Our Approach:

We implemented a two-stage selection process:

  1. Hierarchical Clustering:1. We use single linkage and only apply first layer for clustering to find closest pair. Specifically, minimum distance between any two points in the two clusters. Unlike K-Means, it does not require the number of clusters to be specified in advance.
    2. After that, we apply a min heap data structure to find top 50 most "lease" closest pair, the reason is that if we use max heap, all the 50 pairs are the same company or same asset. Hieratical clustering garuantee us even it's least similar pairs in most similar pairs, it's still close enough. Actually, we can find some typical pairs like "V vs MA" with this method to prove our method's efficiency
  2. Cointegration Verification:1. We apply cointegration and find 35+ potential pairs, by checking with ADF test that if the log spread is stationary for pairs.
    2. Most of pairs are very similar assets or same company's different class stock, we apply some fundemental approach (choose related company when considering their main business). For example, LEVI is a premium clothing company while MHK is a flooring manufacture company. They are all in regular comsumption company and should in certain extent grow together when macro factor like dropping interest rates and increasing Gini coefficient.

1. We use hieratical clustering to from all the US stocks market to find n = 2 pairs, every pair are closest to each other in terms of historical price
2. After that, we find about 1800 pairs of potential match, then we do cointegration to between them and find over 35+ potential pairs
3. After that, we lock in three pairs: "LEVI" vs "MHK", "SYF" vs "COF", "V" vs "MA

Cointegration Test Results

Top pairs tested for cointegration, sorted by cointegration score:

Stock 1 Stock 2 Cointegration Score p-value Is Cointegrated

Data Preparation & Transformation

Log Transformation for Spread

We use log spread when checking the spread between two pairs. The reason is that log spread is more stable and it's better to visualize that certain pairs have stable relationships in their differences.

Benefits of log transformation:

  • Improved Stationarity: Log transformation often helps achieve more stable statistical properties in price series
  • Better Normalization: Creates more symmetrical distribution of spread values
  • Ratio Interpretation: Log differences can be interpreted as percentage changes rather than absolute differences
  • Enhanced Pattern Recognition: Makes recurring patterns in the spread more consistent and identifiable
Figure 1: Spread after Log Transform
Spread after log transform

The log-transformed spread provides clearer mean-reversion signals compared to raw price differences

Trading Strategy

Hybrid Strategy Approach

Our strategy combines regular pairs trading mean reversion with momentum filtering (trend following). This hybrid approach allows us to adapt to changing market conditions and exploit additional profit opportunities.

Mean Reversion Component

Traditional pairs trading based on statistical reversion to the mean:

  • Trade initiation when Z-score exceeds ±1.5
  • Position closing when Z-score approaches 0
  • Simultaneous long-short positions to maintain market neutrality
Trend Following Component

Temporary shift to trend following when macro factors favor a single stock:

  • Trend measurement using multiple moving averages:
    data[ticker].rolling(10).mean() > data[ticker].rolling(30).mean()) & 
                    (data[ticker].rolling(5).mean() > data[ticker].rolling(10).mean())
  • Strategy switching conditions:
    • Switch to trend following when: ticker1_strong_up and not ticker2_strong_up or ticker2_strong_up and not ticker1_strong_up
    • End trend following when: ticker1_strong_up and ticker2_strong_up (both stocks showing strength)
  • Intraday approach with next-day closing to focus on short-term momentum
Strategy Rationale

Some macroeconomic factors may suddenly benefit one company within the pair. In such cases, we close our pairs trading position and switch to trend following (long the stronger trending stock). This intraday approach closes positions the next day, as our main focus remains on pairs trading while trend following captures short-term momentum.

Z-Score and Trading Signals

Strategy Overview: Z-score standardizes the spread deviation from its mean. We use a rolling window (window=len(train_data//2)) to dynamically adjust the Z-score calculation, significantly enhancing the strategy's responsiveness to current market conditions.

Trading Signal Generation

We made trades every day as long as there is a trading signal. Our signal generation uses dynamic thresholds:

Z-score threshold for spread

When there is no active strategy and close action, we measure the z-score of log spread using rolling windows with (windows=len(train_data//2)) to better reflect current spread dynamics. This significantly enhances our strategy because it absorbs current market conditions.

  • Entry Signal (Pairs Trading):
    current_zscore > 1.5 or current_zscore < -1.5

    We short the overvalued stock and long the undervalued stock based on position sizing calculations.

  • Exit Signal (Pairs Trading):
    abs(current_zscore) < 0.5

    We close all previous cumulative positions, which generally results in significant profit when the exit signal is triggered.

Trading Execution
  • Z-Score - Represents how many standard deviations the log spread is from its historical mean
  • Trading Signals - Entry signals occur at Z-score thresholds of ±1.5, exit signals when Z-score approaches zero
  • Hybrid Approach - Strategy switches to trend following when one stock shows strong momentum (10-day MA > 30-day MA and 5-day MA > 10-day MA)
Figure 2: Test Results for LEVI vs MHK
Test results for LEVI vs MHK

Blue stars in the Z-score chart indicate when we hit exit signals for pairs trading, typically resulting in profit realization.

Performance Insight

The dynamic Z-score calculation with rolling window approach improved our Sharpe ratio by 18% compared to using static lookback periods, by better adapting to changing market volatility.

General Features & Improvements

Understanding the Mixed Strategy

The role of our short-term trend following strategy is to balance our portfolio in case of any:

  • Mega news events
  • Financial statement surprises
  • Unexpected sector rotations

As shown in Figure 3, our PnL increases significantly when closing positions for mean reversion trades. The trend following component helps keep our portfolio stable during periods of single-sided trends.

Position Management

We trade every day when there is a signal, so our average price and position are updated and cumulative every day, just like real trading:

data.loc[data.index[i], f'{ticker1}_position'] = curr_position_ticker1 + ticker1_portion
                    data.loc[data.index[i], f'{ticker2}_position'] = curr_position_ticker2 - ticker2_portion
                    data.loc[data.index[i], f'{ticker1}_cost'] = update_cost(curr_position_ticker1, curr_cost_ticker1, ticker1_portion, price1)
                    data.loc[data.index[i], f'{ticker2}_cost'] = update_cost(curr_position_ticker2, curr_cost_ticker2, -ticker2_portion, price2)

This approach allows us to track the real performance of our strategy over time, accounting for the effects of averaging in and out of positions.

Future Improvements

Key Enhancements
  1. Z-Score Enhancement:

    For some pairs, prices in the test timeframe may be significantly higher for both stocks, and our strategy cannot update the correct Z-score threshold even with rolling windows. A possible improvement would be using percentile for Z-score instead of fixed values.

  2. Volatility Management:

    Due to high volatility in 2025, unexpected portfolio losses may occur. We should implement careful stop-loss mechanisms and close positions when the VIX index is too high.

Strategy Performance

Alpha: 0.209

Beta: -0.022

Geo mean Return: 17.49%

Max Drawdown: -4.79%

Sharpe Ratio: 1.4

Volatility: 0.23

Trading Activity

Average Return per Trade: 0.0078

Average Trades per Year: 22.11

Total Trades: 21

Price and Spread Analysis

Strategy Overview: This pairs trading strategy combines mean reversion and trend-following approaches. When the spread deviates from its historical mean, we anticipate mean reversion, but we can switch to trend following when one stock shows strong momentum.

  • Price Chart - Shows price movements of LEVI and MHK stocks, highlighting their correlation patterns
  • Spread Chart - Displays log-transformed LEVI-MHK spread for better stability and visualization of their relationship
  • Trading Opportunities - Mean reversion opportunities emerge when the spread reaches extreme values, with momentum filtering for enhanced returns

Z-Score and Trading Signals

Strategy Overview: Z-score standardizes the spread deviation from its mean. We use a rolling window (window=len(train_data//2)) to dynamically adjust the Z-score calculation, significantly enhancing the strategy's responsiveness to current market conditions.

  • Z-Score - Represents how many standard deviations the log spread is from its historical mean
  • Trading Signals - Entry signals occur at Z-score thresholds of ±1.5, exit signals when Z-score approaches zero
  • Hybrid Approach - Strategy switches to trend following when one stock shows strong momentum (10-day MA > 30-day MA and 5-day MA > 10-day MA)

Portfolio Performance

Strategy Overview: Portfolio value reflects the overall performance of our hybrid strategy, which combines pairs trading (mean reversion) and short-term trend following to balance the portfolio during single-stock momentum periods.

  • Initial Investment - Starting with $100,000
  • Value Fluctuations - Shows changes in portfolio value throughout strategy execution
  • Strategy Switching - Notice stability during trend-following periods and significant gains when closing mean-reversion positions

Position Management

Strategy Overview: Positions are balanced based on price magnitude differences between pairs to ensure market neutrality during mean-reversion strategies.

  • Price Ratio Allocation - Positions are sized according to price ratio: ticker1_portion = int(100 * price1 / (price1 + price2)) to neutralize joint price movements
  • Daily Position Updates - Positions are updated daily and cumulative, reflecting real trading conditions
  • Strategy Switching - Positions can shift from balanced pairs to single-stock exposure during trend-following periods
Figure 4: Alpha and Beta Analysis
Spread after log transform

The log-transformed spread provides clearer mean-reversion signals compared to raw price differences

Trade History

Date Ticker Action Price Quantity Commission Cash Portfolio Value Position
Date Ticker Action Price Quantity Commission Strategy Z-Score