/*+------------------------------------------------------------------+
                                            Linear Regression.mq4 
                               Copyright © 2010, Nondisclosure007 
                                               http://no.link.yet 

Graphs the slope of a line based on linear regression using least
   squares method.
+------------------------------------------------------------------*/
#property copyright "Copyright © 2010, Nondisclosure007"
#property link      "http://no.link.yet"

#property indicator_separate_window
#property indicator_buffers 1


//Note: the following values read from left to right.  For Linear regression
//      to opperate in the correct direction.
extern int       LRPeriod=4;
extern string    Stringy="0:Close,1:Open,2:High,3:Low,4:Median,5:Typcial,6:Weighted";
extern int       Type=5;

double Slope[];//,Bullseye[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("Linear Regression - Period="+LRPeriod);   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Blue);
   IndicatorDigits(Digits+1);
   SetIndexBuffer(0,Slope);
   SetIndexLabel(0,"Slope");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //Bar stuff
   int    counted_bars=IndicatorCounted(); int limit; 
   if(counted_bars<0) return(-1); 
   limit=Bars-counted_bars-1;
   for(int shift=0; shift<limit; shift++)
   {
      Slope[shift]=(procLinearReg(LRPeriod,shift))/Point; 
   } 
   return(0);
  }
//+------------------------------------------------------------------+

double procLinearReg(int p, int i)
{
   double Pi_n=0, Pi=0,SumX=0,b=0,tmpPrice=0,SumY=0,SumXY=0,SumX2=0,SumY2=0,y=0;
   int x=p;
   for(int c=1; c<=p; c++)
   {
      switch(Type){
         case 0:{y=Close[c+i]; break;}
         case 1:{y=Open[c+i]; break;}
         case 2:{y=High[c+i]; break;}
         case 3:{y=Low[c+i]; break;}
         case 4:{y=(High[c+i]+Low[c+i])/2; break;}
         case 5:{y=(High[c+i]+Low[c+i]+Close[c+i])/3; break;}
         case 6:{y=(High[c+i]+Low[c+i]+Close[c+i]+Close[c+i])/4; break;}
         default:{y=(High[c+i]+Low[c+i]+Close[c+i])/3; break;}
      }//switch(Type)
      SumY+=y;
      SumXY+=x*y;
      SumX+=x;
      SumX2+=x*x;
      SumY2+=y*y;
      x--;
   }
   b=((p*SumXY)-(SumX*SumY))/((p*SumX2)-(SumX*SumX));
   return(b);
}

