//+------------------------------------------------------------------+
//|                                                 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  0
#property indicator_maximum 100
//---- 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) 
//int            Length         =  10;  //Period of indicator  
extern int     Lengthtop      = 1340;
extern int     Lengthbutt     = 5;
int            MAXRS          = 0;
int            OPTIL          = 0;  
  
//---- indicator buffers
double RSquared[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSquared);
   SetIndexStyle(1,DRAW_LINE);
   string short_name;
//---- indicator line
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="R-Squared=("+OPTIL+")OPTIL=("+MAXRS+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"R-Squared");
//----
   SetIndexDrawBegin(0,OPTIL);
//----
   
   return(0);
  }

//+------------------------------------------------------------------+
//| R-Squared_v1                                                      |
//+------------------------------------------------------------------+
int start()
{
int i,j,shift,counted_bars=IndicatorCounted();

if (counted_bars < 0 )         return;
if (Lengthbutt >= Lengthtop)   return ;

   for   (j = Lengthbutt; j <= Lengthtop; j++)
{          
   for(i=1;i<Lengthtop;i++) {RSquared[i]=0;}
   
   double SumX = 0;
   for(i=0;i<=j-1;i++) SumX += i+1;
   
   double SumX2 = 0;
   for(i=0;i<=j-1;i++) SumX2 += (i+1)*(i+1);
   
   for(shift=j;shift>=0;shift--)
{	
   double SumY = 0;
   for(i=0;i<=j-1;i++) SumY += iMA(NULL,0,1,0,1,Price,i+j);

   double SumY2 = 0;
   for(i=0;i<=j-1;i++) SumY2 += MathPow(iMA(NULL,0,1,0,1,Price,i+j),2);
   
   double SumXY = 0;
   for(i=0;i<=j-1;i++) SumXY += (i+1)*iMA(NULL,0,1,0,1,Price,i+j);

   
   double Q1 = SumXY - SumX*SumY/j;
   double Q2 = SumX2 - SumX*SumX/j;
   double Q3 = SumY2 - SumY*SumY/j;
   
   if( Q2*Q3 != 0 ) 
	RSquared[shift] = 100*Q1*Q1/(Q2*Q3);
   else 
	RSquared[shift] = 100*Q1*Q1/(0.000001); 
//Comment ("OPTIL0x");	
//}
if (RSquared[shift] < MAXRS) return;
   else
    MAXRS = RSquared[shift];
    OPTIL = shift;
}    
return;   
}
  return;
//ANZEIGE IM CHART  
// Comment("R-Squared=", MAXRS, ", Length=", "OPTIL");   
//}   
//return;   
//ANZEIGE IM CHART  
//   Comment("R-Squared=", MAXRS, ", Length=", "OPTIL");   
//  }
//----
//	return(0);	
//ANZEIGE IM CHART  
// Comment("R-Squared=", MAXRS, ", Length=", "XX0");
//}
//ANZEIGE IM CHART  
//   Comment("R-Squared=", MAXRS, ", Length=", "XX0");
return(MAXRS);

}

