OPEN-SOURCE SCRIPT
Telah dikemas kini

Staccked SMA - Regime Switching & Persistance Statistics

139
This indicator is designed to identify the prevailing market regime by analyzing the behavior of a "stack" of Simple Moving Averages (SMAs). It helps you understand whether the market is currently trending, mean-reverting, or moving randomly.

Core Concept: SMA Correlation
At its heart, the indicator examines the relationship between a set of nine SMAs with different lengths (3, 5, 8, 13, 21, 34, 55, 89, 144) and the lengths themselves.

In a strong trending market (either up or down), the SMAs will be neatly "stacked" in order of their length. The shortest SMA will be furthest from the longest SMA, creating a strong, almost linear visual pattern. When we measure the statistical correlation between the SMA values and their corresponding lengths, we get a value close to +1 (perfect uptrend stack) or -1 (perfect downtrend stack). The absolute value of this correlation will be very high (close to 1).

In a mean-reverting or sideways market, the SMAs will be tangled and crisscrossing each other. There is no clear order, and the relationship between an SMA's length and its price value is weak. The correlation will be close to 0.

This indicator calculates this Pearson correlation on every bar, giving a continuous measure of how ordered or "trendy" the SMAs are. An absolute correlation above 0.8 is considered strongly trending, while a value between 0.4 and 0.8 suggests a mean-reverting character. Below 0.4, the market is likely random or choppy.

Regime Classification and Statistics
The indicator doesn't just look at the current correlation; it analyzes its behavior over a user-defined lookback window (default is 252 bars) to classify the overall market "regime."

It presents its findings in a clear table:

📊 |SMA Correlation| Regime Table: This main table provides a snapshot of the current market character.

Median: Shows the median absolute correlation over the lookback period, giving a central tendency of the market's behavior.

% > 0.80: The percentage of time the market was in a strong trend during the lookback period.

% < 0.80 & > 0.40: The percentage of time the market showed mean-reverting characteristics.

🧠 Regime: The final classification. It's labeled "📈 Trend-Dominant" if the median correlation is high and it has spent a significant portion of the time trending. It's labeled "🔄 Mean-Reverting" if the median is in the middle range and it has spent significant time in that state. Otherwise, it's considered "⚖️ Random/ Choppy".

📐 Regime Significance: This tells you how statistically confident you can be in the current regime classification, using a Z-score to compare its occurrence against random chance. ⭐⭐⭐ indicates high confidence (99%), while "❌ Not Significant" means the pattern could be random.

Regime Transition Probabilities
Optionally, a second table can be displayed that shows the historical probability of the market transitioning from one regime to another over different time horizons (t+5, t+10, t+15, and t+20 bars).

📈 → 🔄 → ⚖️ Transition Table: This table answers questions like, "If the market is trending now (From: 📈), what is the probability it will be mean-reverting (→ 🔄) in 10 bars?"

This provides powerful insights into the market's cyclical nature, helping you anticipate future behavior based on past patterns. For example, you might find that after a period of strong trending, a transition to a choppy state is more likely than a direct switch to a mean-reverting

Indicator Settings
Lookback Window for Regime Classification: This sets the number of recent bars (default is 252) the script analyzes to determine the current market regime (Trending, Mean-Reverting, or Random). A larger number provides a more stable, long-term view, while a smaller number makes the classification more sensitive to recent price action.

Show Regime Transition Table: A simple toggle (on/off) to show or hide the table that displays the probabilities of the market switching from one regime to another.

Lookback Offset for Starting Regime: This determines the "starting point" in the past for calculating regime transitions. The default is 20 bars ago. The script looks at the regime at this point and then checks what it became at later points.

Step 1, 2, 3, 4 Offset (bars): These define the future time intervals (5, 10, 15, and 20 bars by default) for the transition probability table. For example, the script checks the regime at the "Lookback Offset" and then sees what it transitioned to 5, 10, 15, and 20 bars later.

Significance Filter Settings
Use Regime Significance Filter: When enabled, this filter ensures that the regime transition statistics only count transitions that were "statistically significant." This helps to filter out noise and focus on more reliable patterns.

Min Stars Required (1=90%, 2=95%, 3=99%): This sets the minimum confidence level required for a regime to be included in the transition statistics when the significance filter is on.

1 ⭐: Requires at least 90% confidence.

2 ⭐⭐: Requires at least 95% confidence (default).

3 ⭐⭐⭐: Requires at least 99% confidence.
Nota Keluaran
Minor edit .
Nota Keluaran
Added some tool tip and warnings for incorrect lengths
Nota Keluaran
Changed text color to blue to be visible on both dark and white mode

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.