OPEN-SOURCE SCRIPT
Risk Dispersion Regime Indicator

Its primary objective is to measure the real-time correlation level (the lockstep movement) of a basket of assets to generate a dynamic multiplier. This multiplier adjusts your position sizing based on the current market risk regime.
⚙️ Configuration and Inputs
Customizable Basket: The script analyzes a basket of 3 to 20 assets (the slots). By default, it is preconfigured with major US sector and index ETFs (SPY, QQQ, XLK, etc.).
Analysis Windows: The user defines the lookback period for the correlation calculation (default 120 sessions) and the historical depth to establish the ranking (default 750 sessions).
Position Sizing Governor: Allows you to configure the amplitude of the position adjustments via a minimum multiplier (0.3x), a maximum multiplier (1.7x), and a global strength modifier.
🧮 Mathematical Mechanics and Logic
Standardization (Z-Score): The indicator converts the return of each asset in the basket into a Z-score (return standardized relative to its mean and standard deviation) to put all symbols on the exact same scale.
Group Correlation (Rho): Instead of calculating asset pairs one by one (which is computationally heavy), the code uses an optimized mathematical identity: it derives the average correlation of the basket directly from the variance of the assets' average.
Walk-Forward Ranking: The script calculates the percentile of the current correlation strictly against previously observed historical values. This method guarantees zero lookahead bias, making the indicator perfectly safe and robust for algorithmic backtesting.
🖥️ Visual Outputs and Interface
Main Plots: The indicator draws three lines on a separate lower pane: the correlation rank (percentile), the raw correlation, and the resulting position size multiplier.
Color-Coded Regimes: The chart background changes color dynamically. It turns red when the market moves as a monolith (rank > 0.90, signaling high systemic risk) and green when the market is highly dispersed (rank < 0.25, a favorable environment for stock-picking).
Diagnostic Dashboard: A table is displayed in the top right corner. It indicates the real-time market state (Very Tight, Loose, etc.), the recommended multiplier, and includes built-in safeguards (e.g., warning the user if the timeframe is not daily or if specific assets lack historical quote data).
Built-in Alerts: Alert conditions (alertcondition) are programmed to notify the user whenever the market crosses extreme correlation thresholds (90th and 25th percentiles).
⚙️ Configuration and Inputs
Customizable Basket: The script analyzes a basket of 3 to 20 assets (the slots). By default, it is preconfigured with major US sector and index ETFs (SPY, QQQ, XLK, etc.).
Analysis Windows: The user defines the lookback period for the correlation calculation (default 120 sessions) and the historical depth to establish the ranking (default 750 sessions).
Position Sizing Governor: Allows you to configure the amplitude of the position adjustments via a minimum multiplier (0.3x), a maximum multiplier (1.7x), and a global strength modifier.
🧮 Mathematical Mechanics and Logic
Standardization (Z-Score): The indicator converts the return of each asset in the basket into a Z-score (return standardized relative to its mean and standard deviation) to put all symbols on the exact same scale.
Group Correlation (Rho): Instead of calculating asset pairs one by one (which is computationally heavy), the code uses an optimized mathematical identity: it derives the average correlation of the basket directly from the variance of the assets' average.
Walk-Forward Ranking: The script calculates the percentile of the current correlation strictly against previously observed historical values. This method guarantees zero lookahead bias, making the indicator perfectly safe and robust for algorithmic backtesting.
🖥️ Visual Outputs and Interface
Main Plots: The indicator draws three lines on a separate lower pane: the correlation rank (percentile), the raw correlation, and the resulting position size multiplier.
Color-Coded Regimes: The chart background changes color dynamically. It turns red when the market moves as a monolith (rank > 0.90, signaling high systemic risk) and green when the market is highly dispersed (rank < 0.25, a favorable environment for stock-picking).
Diagnostic Dashboard: A table is displayed in the top right corner. It indicates the real-time market state (Very Tight, Loose, etc.), the recommended multiplier, and includes built-in safeguards (e.g., warning the user if the timeframe is not daily or if specific assets lack historical quote data).
Built-in Alerts: Alert conditions (alertcondition) are programmed to notify the user whenever the market crosses extreme correlation thresholds (90th and 25th percentiles).
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.
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.
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.