OPEN-SOURCE SCRIPT
Return Dispersion Matrix [The Quant Science]

Introduction
The Return Dispersion Matrix is a quantitative analysis tool that maps the temporal dependence of returns. In technical terms, performs a two-dimensional, 1-lag autocorrelation analysis, measuring the relationship and relative frequency between the return of the previous session (Yesterday) and that of the current session (Today) within specific volatility ranges.
It answers the key questions:
💭 🔑 “When a candle closes in the green or red, what is the most common range of returns?”
💭 🔑 “When the price did X yesterday, what does it probably do today?”
What does this indicator do?
1. Automatically calculate the probability of future returns for a given range of values and display the result in the right-hand column.

2. Instantly identify the market trend using the quadrants. Calculates the quadrant with the highest number of points and highlights it by lighting it up.

🟢 Q1

🟢 Q2

🔴 Q3

🔴 Q4

The horizontal X-axis represents the return of the previous candle (t-1), while the vertical Y-axis shows the return of the current candle (t). This allows you to visually determine whether a positive or negative day tends to be followed by one of the same sign (persistence) or the opposite sign (mean reversion).

X-axis
Y-axis
3. Automatically calculates volatility bands. It identifies the maximum range observed during the analysis period and divides the positive values into bands. These values are displayed in the left column in white.

4. Automatically calculates frequency clusters. The density and transparency of the points immediately reveal where most sessions are concentrated and which events represent anomalies or extreme outliers.

Uses
Identifying the Market Regime
Observing the concentration of points reveals the prevailing nature of the asset:
Assessment of Asymmetry and Risk Management (Fat Tails)
By comparing the vertical extent of the points on the positive and negative sides:
If, on the negative side (Q3 / Q4), the points frequently reach the extreme ranges (e.g., -L4 or -L5), while on the positive side they stop at +L2, the asset exhibits asymmetric downside volatility (crash risk). Essential for the correct calculation of position sizing and for the strategic placement of stop-loss orders.
Operational Execution Filter (Probabilistic Edge)
The tool acts as a statistical validator before entering the market:
Example: A trader wants to go long on an asset. The matrix shows that, under current conditions, Quadrant Q2 has a frequency of 45% (high probability that a red day will be followed by a green day). Instead of buying during the positive candle, the trader waits for a negative session to close before placing a buy order at the next opening, thereby maximizing expected return.
Settings
Traders can easily adjust the indicator's parameters using the user interface.

The Return Dispersion Matrix is a quantitative analysis tool that maps the temporal dependence of returns. In technical terms, performs a two-dimensional, 1-lag autocorrelation analysis, measuring the relationship and relative frequency between the return of the previous session (Yesterday) and that of the current session (Today) within specific volatility ranges.
It answers the key questions:
💭 🔑 “When a candle closes in the green or red, what is the most common range of returns?”
💭 🔑 “When the price did X yesterday, what does it probably do today?”
What does this indicator do?
1. Automatically calculate the probability of future returns for a given range of values and display the result in the right-hand column.
2. Instantly identify the market trend using the quadrants. Calculates the quadrant with the highest number of points and highlights it by lighting it up.
🟢 Q1
- A green candle followed by another green candle
- High concentration of points indicates that positive days tend to generate immediate additional momentum
- Suitable for trend-following and bullish breakout strategies
🟢 Q2
- A red candle followed by a green candle
- Heavy trading volume indicates a “resilient” market: buyers step in immediately when prices fall (“buy the dip”)
- Suitable for accumulation strategies on pullbacks
🔴 Q3
- A red candle followed by another red candle
- Indicates that sales generate further sales without immediate support
- Sign of underlying weakness or persistent downtrends
🔴 Q4
- A green candle followed by a red candle
- There are many indications that rallies are short-lived and lead to rapid profit-taking in the Suitable for “sell the rally” strategies
The horizontal X-axis represents the return of the previous candle (t-1), while the vertical Y-axis shows the return of the current candle (t). This allows you to visually determine whether a positive or negative day tends to be followed by one of the same sign (persistence) or the opposite sign (mean reversion).
X-axis
- To the right of 0: previous candle closed in positive territory (+)
- To the left of 0: previous candle closed in negative territory (-)
Y-axis
- Above 0: current candle is closing / has closed in positive territory (+)
- Below 0: current candle is closing / has closed in negative territory (-)
3. Automatically calculates volatility bands. It identifies the maximum range observed during the analysis period and divides the positive values into bands. These values are displayed in the left column in white.
4. Automatically calculates frequency clusters. The density and transparency of the points immediately reveal where most sessions are concentrated and which events represent anomalies or extreme outliers.
Uses
Identifying the Market Regime
Observing the concentration of points reveals the prevailing nature of the asset:
- Q1 / Q3 Prevalence (Trend / Inertia): The market tends toward continuation. The best strategy is to buy on breakouts or sell on support breaks.
- Prevalence of Q2 / Q4 (Mean Reversion): Buying at highs is statistically a losing strategy.
Assessment of Asymmetry and Risk Management (Fat Tails)
By comparing the vertical extent of the points on the positive and negative sides:
If, on the negative side (Q3 / Q4), the points frequently reach the extreme ranges (e.g., -L4 or -L5), while on the positive side they stop at +L2, the asset exhibits asymmetric downside volatility (crash risk). Essential for the correct calculation of position sizing and for the strategic placement of stop-loss orders.
Operational Execution Filter (Probabilistic Edge)
The tool acts as a statistical validator before entering the market:
Example: A trader wants to go long on an asset. The matrix shows that, under current conditions, Quadrant Q2 has a frequency of 45% (high probability that a red day will be followed by a green day). Instead of buying during the positive candle, the trader waits for a negative session to close before placing a buy order at the next opening, thereby maximizing expected return.
Settings
Traders can easily adjust the indicator's parameters using the user interface.
- Data Points Visualization: adjust the number of points within the matrix
- Matrix Offset: move the table to the right on the graph
- Levels Color: set the indicator colors
- Highlight Dominant Quadrant: apply automatic calculation of the winning quadrant
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.
🔥 Unlock the power of Symbiosis, the first Python XGBoost model natively built for Pine Script. Grab the last available spots on Patreon: patreon.com/c/xgboost_symbiosis
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.
🔥 Unlock the power of Symbiosis, the first Python XGBoost model natively built for Pine Script. Grab the last available spots on Patreon: patreon.com/c/xgboost_symbiosis
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.