OPEN-SOURCE SCRIPT
MA Crossover with R Squared

This indicator enhances the classic Moving Average (MA) crossover strategy with statistical filtering and prediction capabilities.
Let me explain what it does:
Instead of just showing when a fast MA crosses above/below a slow MA, this indicator adds R² (R-squared) filtering to identify higher-quality crossovers and predicts future crossovers.
What is R²?
R² (Coefficient of Determination) is a statistical measure that shows how well one variable explains the movement of another variable. In simpler terms:
R² = 1.0: Perfect relationship - 100% of the movement in one MA is explained by the other MA
R² = 0.8: Strong relationship - 80%
R² = 0.5: Moderate relationship - 50%
R² = 0.0: No relationship - 0%
Imagine two cars driving on a highway:
High R² (0.9): Both cars are in the same lane, moving together consistently
Low R² (0.3): One car is weaving between lanes while the other stays straight - poor coordination.
Traditional MA crossovers often generate false signals during:
Choppy markets (price bouncing around)
Sideways/ranging markets
Low volatility periods
News events causing temporary spikes
The R² Solution:
R² acts as a "quality filter" that answers: "How meaningful this crossover is?"
What this means:
Before R² filtering: Every crossover generates a signal
After R² filtering: Only crossovers with R² > threshold generate signals
Result: Fewer but higher-quality signals.
MARKET REGIME DETECTION
High R² (> 0.7): Strong trending market - MA crossovers are reliable
Medium R² (0.4-0.7): Moderate trending - use with caution
Low R² (< 0.4): Choppy/range-bound market - avoid MA crossover signals
Increasing R²: MAs are converging/moving together more closely
Decreasing R²: MAs are diverging/losing coordination
Sudden R² drop: Potential market regime change.
Why Square the Correlation?
Correlation: Measures direction AND strength (-1 to +1)
R²: Measures strength ONLY (0 to 1)
In trading: We care about relationship strength, not direction
Direction is already indicated by crossover type (bullish/bearish)
Real-World Interpretation:
If R² = 0.64, it means:
64% of the variation in the fast MA is explained by the slow MA
36% is "noise" or unexplained movement
The MAs are moderately coordinated.
R² Trend Confirmation:
Entry: When crossover occurs AND R² is above threshold
Confirmation: R² continues rising after entry
Exit: R² drops below threshold (relationship weakening)
Multi-Timeframe R² Analysis
Check R² on higher timeframe for trend context
Use current timeframe for entry signals
Example: Daily R² > 0.7 gives bullish bias, use 1-hour for entries.
R² LIMITATIONS & CAUTIONS
1. Lagging Nature
R² is calculated from past data
By the time R² is high, the trend may already be established
2. Not a Standalone Indicator
R² should confirm other signals, not generate them alone
Always combine with price action, volume, support/resistance
3. Curve Fitting Risk
Don't over-optimize R² thresholds on historical data
What worked in the past may not work in the future
Use R² as a filter, not a predictor
4. Market-Specific Behavior
R² thresholds that work in trending stocks may fail in Forex
Cryptocurrencies may require different R² settings than commodities
Always test on your specific market/instrument
Before Taking Any Signal:
✅ Does the crossover have a colored circle? (R² > threshold)
✅ What's the R² number shown? (Higher = better)
✅ Is R² rising or falling? (Rising = strengthening relationship)
✅ Check history table - what happened with similar R² values?
✅ Consider prediction - does it align with current signal?
Simple R² Rules of Thumb:
R² > 0.8: Excellent signal quality
R² 0.6-0.8: Good signal quality
R² 0.4-0.6: Moderate - use additional confirmation
R² < 0.4: Poor - avoid or use extreme caution
Think of R² as:
A quality control inspector for MA crossovers
A relationship therapist for your moving averages
A statistical bouncer that only lets strong signals through
Higher win rate + Better risk/reward = More profitable trading
This script transforms the basic "when lines cross" approach into a sophisticated, statistically-validated trading system. R² is the secret sauce that separates random crossovers (Golden/Death) from meaningful trend changes.
DISCLAIMER: This information is provided for educational purposes only and should not be considered financial, investment, or trading advice. Please do boost if you like it. Happy Trading.
Let me explain what it does:
Instead of just showing when a fast MA crosses above/below a slow MA, this indicator adds R² (R-squared) filtering to identify higher-quality crossovers and predicts future crossovers.
What is R²?
R² (Coefficient of Determination) is a statistical measure that shows how well one variable explains the movement of another variable. In simpler terms:
R² = 1.0: Perfect relationship - 100% of the movement in one MA is explained by the other MA
R² = 0.8: Strong relationship - 80%
R² = 0.5: Moderate relationship - 50%
R² = 0.0: No relationship - 0%
Imagine two cars driving on a highway:
High R² (0.9): Both cars are in the same lane, moving together consistently
Low R² (0.3): One car is weaving between lanes while the other stays straight - poor coordination.
Traditional MA crossovers often generate false signals during:
Choppy markets (price bouncing around)
Sideways/ranging markets
Low volatility periods
News events causing temporary spikes
The R² Solution:
R² acts as a "quality filter" that answers: "How meaningful this crossover is?"
What this means:
Before R² filtering: Every crossover generates a signal
After R² filtering: Only crossovers with R² > threshold generate signals
Result: Fewer but higher-quality signals.
MARKET REGIME DETECTION
High R² (> 0.7): Strong trending market - MA crossovers are reliable
Medium R² (0.4-0.7): Moderate trending - use with caution
Low R² (< 0.4): Choppy/range-bound market - avoid MA crossover signals
Increasing R²: MAs are converging/moving together more closely
Decreasing R²: MAs are diverging/losing coordination
Sudden R² drop: Potential market regime change.
Why Square the Correlation?
Correlation: Measures direction AND strength (-1 to +1)
R²: Measures strength ONLY (0 to 1)
In trading: We care about relationship strength, not direction
Direction is already indicated by crossover type (bullish/bearish)
Real-World Interpretation:
If R² = 0.64, it means:
64% of the variation in the fast MA is explained by the slow MA
36% is "noise" or unexplained movement
The MAs are moderately coordinated.
R² Trend Confirmation:
Entry: When crossover occurs AND R² is above threshold
Confirmation: R² continues rising after entry
Exit: R² drops below threshold (relationship weakening)
Multi-Timeframe R² Analysis
Check R² on higher timeframe for trend context
Use current timeframe for entry signals
Example: Daily R² > 0.7 gives bullish bias, use 1-hour for entries.
R² LIMITATIONS & CAUTIONS
1. Lagging Nature
R² is calculated from past data
By the time R² is high, the trend may already be established
2. Not a Standalone Indicator
R² should confirm other signals, not generate them alone
Always combine with price action, volume, support/resistance
3. Curve Fitting Risk
Don't over-optimize R² thresholds on historical data
What worked in the past may not work in the future
Use R² as a filter, not a predictor
4. Market-Specific Behavior
R² thresholds that work in trending stocks may fail in Forex
Cryptocurrencies may require different R² settings than commodities
Always test on your specific market/instrument
Before Taking Any Signal:
✅ Does the crossover have a colored circle? (R² > threshold)
✅ What's the R² number shown? (Higher = better)
✅ Is R² rising or falling? (Rising = strengthening relationship)
✅ Check history table - what happened with similar R² values?
✅ Consider prediction - does it align with current signal?
Simple R² Rules of Thumb:
R² > 0.8: Excellent signal quality
R² 0.6-0.8: Good signal quality
R² 0.4-0.6: Moderate - use additional confirmation
R² < 0.4: Poor - avoid or use extreme caution
Think of R² as:
A quality control inspector for MA crossovers
A relationship therapist for your moving averages
A statistical bouncer that only lets strong signals through
Higher win rate + Better risk/reward = More profitable trading
This script transforms the basic "when lines cross" approach into a sophisticated, statistically-validated trading system. R² is the secret sauce that separates random crossovers (Golden/Death) from meaningful trend changes.
DISCLAIMER: This information is provided for educational purposes only and should not be considered financial, investment, or trading advice. Please do boost if you like it. Happy Trading.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.