PROTECTED SOURCE SCRIPT
ZScore correlation volatility spread pac

This is a complete multi-strategy dashboard for statistical arbitrage (pairs trading). It is designed to solve the biggest challenge in pairs trading: knowing when to trade mean-reversion and when to trade a regime break.
This indicator automatically analyzes the stability of the pair's relationship using two critical filters (a Volatility Ratio filter and a Correlation Z-Score filter). It then provides clear, actionable signals for two opposite strategies based on the current market "regime."
The Regime "Traffic Light" System
The indicator's background color tells you which strategy is currently active.
• 🟢 GREEN Background (Stable Regime): This is the "Mean Reversion" regime. It means both the volatility and correlation filters are stable. The pair is behaving predictably, and you can trust the Z-Score to revert to its mean.
• 🔴 RED Background (Unstable Regime): This is the "Divergence" or "Breakout" regime. It means the pair's relationship has failed (correlation has broken down OR volatility has exploded). In this regime, the Z-Score is not expected to revert and may continue to diverge.
How to Use: The Two Strategies
The indicator will plot text labels on your chart for four specific signals.
📈 Strategy 1: Mean Reversion (Green Regime 🟢)
This is the classic pairs trading strategy. You only take these signals when the background is GREEN.
• LONG Signal: "ACHAT MOYENNE" (Buy Mean)
• What it means: The Z-Score (blue line) has crossed below the lower band (e.g., -2.0) while the regime is stable.
• Your Bet: The spread is statistically "too cheap" and will rise back to the 0-line.
• Action: Buy the Spread (e.g., Buy MES, Sell MNQ).
• SHORT Signal: "VENTE MOYENNE" (Sell Mean)
• What it means: The Z-Score (blue line) has crossed above the upper band (e.g., +2.0) while the regime is stable.
• Your Bet: The spread is statistically "too expensive" and will fall back to the 0-line.
• Action: Sell the Spread (e.g., Sell MES, Buy MNQ).
• Exit Target: Close your position when the Z-Score (blue line) returns to 0.
🚀 Strategy 2: Divergence / Momentum (Red Regime 🔴)
This is a momentum strategy that bets on the continuation of a regime break. These signals appear on the exact bar the background turns RED.
• LONG Signal: "ACHAT ÉCART" (Buy Divergence)
• What it means: The regime just broke (turned RED) at the same time the Z-Score was already rising.
• Your Bet: The pair's relationship is broken, and the spread will continue to "rip" higher, diverging further from the mean.
• Action: Buy the Spread (e.g., Buy MES, Sell MNQ) and hold for momentum.
• SHORT Signal: "VENTE ÉCART" (Sell Divergence)
• What it means: The regime just broke (turned RED) at the same time the Z-Score was already falling.
• Your Bet: The pair's relationship is broken, and the spread will continue to "crash" lower, diverging further from the mean.
• Action: Sell the Spread (e.g., Sell MES, Buy MNQ) and hold for momentum.
• Exit Target: This is a momentum trade, so the exit is not the 0-line. Use a trailing stop or exit when the regime becomes stable again (turns GREEN).
The 3 Indicator Panes
1. Pane 1: Main Dashboard (Signal Pane)
• Z-Score PRIX (Blue Line): Your main signal. Shows the spread's deviation.
• Regime (Background Color): Your "traffic light" (Green for Mean Reversion, Red for Divergence).
• Trade Labels: The explicit entry signals.
2. Pane 2: Volatility Ratio (Diagnostic Pane)
• This pane shows the ratio of the two assets' volatility (Orange Line) vs. its long-term average (Gray Line).
• It is one of the two filters used to decide if the regime is "stable." If the orange line moves too far from the gray line, the regime turns RED.
3. Pane 3: Correlation Z-Score (Diagnostic Pane)
• This is the most critical filter. It measures the Z-Score of the rolling correlation itself.
• If this Purple Line drops below the Red Dashed Line (the "Danger Threshold"), it means the pair's correlation has statistically broken. This is the primary trigger for the RED "Divergence" regime.
Settings
• Symbol 1 & 2 Tickers: Set the two assets for the filters (e.g., "MES1!" and "MNQ1!"). Note: You must still load the spread chart itself (e.g., MES1!-MNQ1!) for the Price Z-Score to work.
• Z-Score Settings: Adjust the lookback period and bands for the Price Z-Score.
• Volatility Filter Settings: Adjust the ATR period, the MA period, and the deviation threshold.
• Correlation Filter Settings: Adjust the lookback periods and the "danger threshold" for the Correlation Z-Score.
Disclaimer: This indicator is for educational and informational purposes only. It does not constitute financial advice. All trading involves significant risk. Past performance is not indicative of future results.
This indicator automatically analyzes the stability of the pair's relationship using two critical filters (a Volatility Ratio filter and a Correlation Z-Score filter). It then provides clear, actionable signals for two opposite strategies based on the current market "regime."
The Regime "Traffic Light" System
The indicator's background color tells you which strategy is currently active.
• 🟢 GREEN Background (Stable Regime): This is the "Mean Reversion" regime. It means both the volatility and correlation filters are stable. The pair is behaving predictably, and you can trust the Z-Score to revert to its mean.
• 🔴 RED Background (Unstable Regime): This is the "Divergence" or "Breakout" regime. It means the pair's relationship has failed (correlation has broken down OR volatility has exploded). In this regime, the Z-Score is not expected to revert and may continue to diverge.
How to Use: The Two Strategies
The indicator will plot text labels on your chart for four specific signals.
📈 Strategy 1: Mean Reversion (Green Regime 🟢)
This is the classic pairs trading strategy. You only take these signals when the background is GREEN.
• LONG Signal: "ACHAT MOYENNE" (Buy Mean)
• What it means: The Z-Score (blue line) has crossed below the lower band (e.g., -2.0) while the regime is stable.
• Your Bet: The spread is statistically "too cheap" and will rise back to the 0-line.
• Action: Buy the Spread (e.g., Buy MES, Sell MNQ).
• SHORT Signal: "VENTE MOYENNE" (Sell Mean)
• What it means: The Z-Score (blue line) has crossed above the upper band (e.g., +2.0) while the regime is stable.
• Your Bet: The spread is statistically "too expensive" and will fall back to the 0-line.
• Action: Sell the Spread (e.g., Sell MES, Buy MNQ).
• Exit Target: Close your position when the Z-Score (blue line) returns to 0.
🚀 Strategy 2: Divergence / Momentum (Red Regime 🔴)
This is a momentum strategy that bets on the continuation of a regime break. These signals appear on the exact bar the background turns RED.
• LONG Signal: "ACHAT ÉCART" (Buy Divergence)
• What it means: The regime just broke (turned RED) at the same time the Z-Score was already rising.
• Your Bet: The pair's relationship is broken, and the spread will continue to "rip" higher, diverging further from the mean.
• Action: Buy the Spread (e.g., Buy MES, Sell MNQ) and hold for momentum.
• SHORT Signal: "VENTE ÉCART" (Sell Divergence)
• What it means: The regime just broke (turned RED) at the same time the Z-Score was already falling.
• Your Bet: The pair's relationship is broken, and the spread will continue to "crash" lower, diverging further from the mean.
• Action: Sell the Spread (e.g., Sell MES, Buy MNQ) and hold for momentum.
• Exit Target: This is a momentum trade, so the exit is not the 0-line. Use a trailing stop or exit when the regime becomes stable again (turns GREEN).
The 3 Indicator Panes
1. Pane 1: Main Dashboard (Signal Pane)
• Z-Score PRIX (Blue Line): Your main signal. Shows the spread's deviation.
• Regime (Background Color): Your "traffic light" (Green for Mean Reversion, Red for Divergence).
• Trade Labels: The explicit entry signals.
2. Pane 2: Volatility Ratio (Diagnostic Pane)
• This pane shows the ratio of the two assets' volatility (Orange Line) vs. its long-term average (Gray Line).
• It is one of the two filters used to decide if the regime is "stable." If the orange line moves too far from the gray line, the regime turns RED.
3. Pane 3: Correlation Z-Score (Diagnostic Pane)
• This is the most critical filter. It measures the Z-Score of the rolling correlation itself.
• If this Purple Line drops below the Red Dashed Line (the "Danger Threshold"), it means the pair's correlation has statistically broken. This is the primary trigger for the RED "Divergence" regime.
Settings
• Symbol 1 & 2 Tickers: Set the two assets for the filters (e.g., "MES1!" and "MNQ1!"). Note: You must still load the spread chart itself (e.g., MES1!-MNQ1!) for the Price Z-Score to work.
• Z-Score Settings: Adjust the lookback period and bands for the Price Z-Score.
• Volatility Filter Settings: Adjust the ATR period, the MA period, and the deviation threshold.
• Correlation Filter Settings: Adjust the lookback periods and the "danger threshold" for the Correlation Z-Score.
Disclaimer: This indicator is for educational and informational purposes only. It does not constitute financial advice. All trading involves significant risk. Past performance is not indicative of future results.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.