OPEN-SOURCE SCRIPT
Rolling Correlation & Breakdown Detector [LuxAlgo]

The Rolling Correlation & Breakdown Detector indicator is a specialized "trust meter" designed for statistical arbitrage and pairs trading that monitors the stability of the relationship between two assets to identify when mean-reversion strategies are likely to fail.
🔶 USAGE
This indicator functions as a decision filter rather than a standalone entry signal generator. It is intended to be used alongside spread indicators or Z-score models to determine if the underlying relationship between two assets is healthy enough to trade.
The script monitors two primary factors: the raw Pearson correlation and the stability of that correlation. This helps traders avoid the "divergence trap," where a price gap looks like a mean-reversion opportunity, but the assets have actually undergone a structural regime shift.
🔹 The Three-State System
The indicator categorizes the relationship into three distinct regimes:
🔹 Statistical Arbitrage Filtering
🔶 DETAILS
While most traders only look at the absolute correlation value, this indicator introduces the concept of Correlation Stability. A pair with a lower but stable correlation is often more tradable than a pair with high correlation that is rapidly collapsing.
The script calculates the standard deviation of the correlation over a "Stability Window." If the correlation fluctuates wildly, the stability metric will spike, triggering a Warning or Breakdown state even if the raw correlation value remains relatively high. This acts as a "circuit breaker" to protect traders from macro-driven divergences or structural news events.
🔶 SETTINGS
🔹 General Settings
🔹 Breakdown Detection Thresholds
🔹 Dashboard
* Dashboard: Toggles the visibility of the real-time information table.
* Position/Size: Customizes the location and scale of the on-screen dashboard.
🔶 USAGE
This indicator functions as a decision filter rather than a standalone entry signal generator. It is intended to be used alongside spread indicators or Z-score models to determine if the underlying relationship between two assets is healthy enough to trade.
The script monitors two primary factors: the raw Pearson correlation and the stability of that correlation. This helps traders avoid the "divergence trap," where a price gap looks like a mean-reversion opportunity, but the assets have actually undergone a structural regime shift.
🔹 The Three-State System
The indicator categorizes the relationship into three distinct regimes:
- Healthy (Green): The correlation is high and stable. This is the optimal environment for statistical arbitrage trades.
- Warning (Orange): The correlation is weakening or becoming volatile. New trades should be approached with caution, and existing positions should be managed tightly.
- Breakdown (Red): The assets have "divorced." The correlation has collapsed or is moving erratically. In this state, mean-reversion signals should be ignored as the assets are no longer moving in tandem.
🔹 Statistical Arbitrage Filtering
- Valid Trade: Your spread indicator shows an extreme (e.g., Z-Score > 2) AND the detector is Green.
- Invalid Trade: Your spread indicator shows an extreme, but the detector is Red. This suggests a "regime shift" where one asset is repricing independently of the other.
🔶 DETAILS
While most traders only look at the absolute correlation value, this indicator introduces the concept of Correlation Stability. A pair with a lower but stable correlation is often more tradable than a pair with high correlation that is rapidly collapsing.
The script calculates the standard deviation of the correlation over a "Stability Window." If the correlation fluctuates wildly, the stability metric will spike, triggering a Warning or Breakdown state even if the raw correlation value remains relatively high. This acts as a "circuit breaker" to protect traders from macro-driven divergences or structural news events.
🔶 SETTINGS
🔹 General Settings
- Asset B: The secondary symbol you wish to compare against the current chart symbol.
- Correlation Length: The rolling lookback window used for the Pearson correlation calculation.
- Stability Window: The window used to measure the volatility (standard deviation) of the correlation.
🔹 Breakdown Detection Thresholds
- Healthy Threshold: The minimum correlation value required to be considered in a "Healthy" state.
- Breakdown Threshold: The level below which the relationship is considered "Broken."
- Stability Threshold: The maximum allowed volatility in the correlation before the state shifts to a Warning or Breakdown.
🔹 Dashboard
* Dashboard: Toggles the visibility of the real-time information table.
* Position/Size: Customizes the location and scale of the on-screen dashboard.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Build your own edge with AI: luxalgo.com
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Build your own edge with AI: luxalgo.com
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.