R-Squared_v11

Download script
Author: Copyright � 2006, TrendLaboratory
R-Squared_v11

This tool is designed to measure the strength and reliability of trends within a financial market. It provides a visual representation of how well the current price action follows a consistent pattern, helping traders distinguish between high-quality, stable trends and noisy, unpredictable price movements.

Core Logic and Functionality

The script operates by analyzing historical price data over a specific timeframe to determine "trend quality." Here is a breakdown of how it processes information:

1. Data Selection and Input The indicator allows you to choose which specific price point to analyze (such as the closing price, opening price, or high/low prices). It then looks at a defined window of past data points (the "Length") to calculate the current market state.

2. The R-Squared Calculation At its heart, the script uses a statistical method called "R-Squared." In plain terms:

  • It attempts to map out a linear progression of the prices over your chosen period.
  • It then calculates how closely the actual market prices hug that "line" of progression.
  • The result is expressed as a percentage (on a scale of 0 to 100). A higher value indicates that the price is moving in a very consistent, predictable manner, while a lower value suggests that the price action is erratic or lacks a clear direction.

3. Trend Smoothing Because raw statistical data can often be "jittery" (jumping up and down rapidly), the script includes a secondary smoothing process. It takes the R-Squared results and averages them over a set period. This creates a much cleaner, more readable line on your chart, making it easier to identify when a trend is gaining strength or beginning to lose its integrity without being distracted by minor fluctuations.

How to Interpret the Results

The indicator provides two distinct lines:

  • R-Squared: Represents the immediate statistical correlation of the current price movement.
  • Smoothed Line: Provides a clearer picture of the prevailing trend's health over time.

By observing these values, you can see whether a move in the market is "strong" (the line moves toward 100, indicating high reliability) or "weak" (the line stays low, indicating that while the price might be moving, it is doing so without a consistent underlying pattern).

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
R-Squared_v11
//+------------------------------------------------------------------+
//|                                                 R-Squared_v1.mq4 |
//|                                Copyright © 2006, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 SkyBlue
#property indicator_color2 Tomato
#property indicator_width1 2
#property indicator_width2 1
#property indicator_minimum  -1
#property indicator_maximum 101
//---- input parameters
extern int     Price          =  0;  //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 14;  //Period of indicator 
extern int     Smooth         = 14;  //Period of smoothing
//---- indicator buffers
double RSquared[];
double Smoothed[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSquared);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Smoothed);   
   string short_name;
//---- indicator line
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="R-Squared("+Length+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"R-Squared");
   SetIndexLabel(1,"Smoothed");
//----
   SetIndexDrawBegin(0,Length+Smooth);
   SetIndexDrawBegin(1,Length+Smooth);
//----
   
   return(0);
  }

//+------------------------------------------------------------------+
//| R-Squared_v1                                                      |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-Length-1; 
   if ( counted_bars < 1 ) 
   for(i=1;i<Length;i++) {RSquared[Bars-i]=0;Smoothed[Bars-i]=0;}    
     
   double SumX = 0;
   for(i=0;i<=Length-1;i++) SumX += i+1;
   
   double SumX2 = 0;
   for(i=0;i<=Length-1;i++) SumX2 += (i+1)*(i+1);
   
   for(shift=limit;shift>=0;shift--) 
   {	
   double SumY = 0;
   for(i=0;i<=Length-1;i++) SumY += iMA(NULL,0,1,0,1,Price,i+shift);

   double SumY2 = 0;
   for(i=0;i<=Length-1;i++) SumY2 += MathPow(iMA(NULL,0,1,0,1,Price,i+shift),2);
   
   double SumXY = 0;
   for(i=0;i<=Length-1;i++) SumXY += (i+1)*iMA(NULL,0,1,0,1,Price,i+shift);

   
   double Q1 = SumXY - SumX*SumY/Length;
   double Q2 = SumX2 - SumX*SumX/Length;
   double Q3 = SumY2 - SumY*SumY/Length;
   
   if( Q2*Q3 != 0 ) 
	RSquared[shift] = 100*Q1*Q1/(Q2*Q3);
   else 
	RSquared[shift] = 0; 
      
   double SumR = 0;
   for(i=0;i<=Smooth-1;i++) SumR += RSquared[shift+i];    
   Smoothed[shift]=SumR/Smooth;
   }
//----
	return(0);	
}

Comments