Linear RegressionMAAppliedPrice





//+------------------------------------------------------------------+
//|                                            Linear Regression.mq4 |
//|                                       Copyright © 2006, tageiger |
//|                                        http://www.metaquotes.net |
//|                                                                  |
//| 01/12/2008 - Modified by Robert Hill to use Moving Average       |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, tageiger, aka fxid10t(at)yahoo.com"
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
#property  indicator_buffers 1
#property indicator_color1 Red      
#property indicator_width1 2

extern int     LR.length=34;   // bars back regression begins
extern string  m = "--Moving Average Types--";
extern string  m0 = " 0 = SMA";
extern string  m1 = " 1 = EMA";
extern string  m2 = " 2 = SMMA";
extern string  m3 = " 3 = LWMA";
extern int     LR.MovingAverage = 0;
extern int     LR.MA.Period = 14;
extern string  p = "--Applied Price Types--";
extern string  p0 = " 0 = close";
extern string  p1 = " 1 = open";
extern string  p2 = " 2 = high";
extern string  p3 = " 3 = low";
extern string  p4 = " 4 = median(high+low)/2";
extern string  p5 = " 5 = typical(high+low+close)/3";
extern string  p6 = " 6 = weighted(high+low+close+close)/4";
extern int     LR.AppliedPrice = 0;
extern int     LR.line.width=2;
extern color   LR.c=Orange;
extern bool    ShowChannel = true;
extern double  std.channel.1=0.618;        // 1st channel
extern color   c.1=Gray;
extern double  std.channel.2=1.618;        // 2nd channel
extern color   c.2=Gray;
extern double  std.channel.3=2.618;        // 3nd channel
extern color   c.3=Gray;

double ExtMapBuffer1[];
int    MAMode;
string strMAType;
int start.bar, end.bar;
string TrendID;
string ChannelUpper1, ChannelLower1;
string ChannelUpper2, ChannelLower2;
string ChannelUpper3, ChannelLower3;

int init()
{

   SetIndexBuffer(0,ExtMapBuffer1);
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);

switch (LR.MovingAverage)
   {
      case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
      case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
      case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
      default: strMAType="SMA"; MAMode=MODE_SMA; break;
   }
   IndicatorShortName( strMAType+ " (" +LR.MA.Period + ") ");

 TrendID = "m "+LR.length+" TL";
 ChannelUpper1 = "m "+LR.length+" +"+std.channel.1+"d";
 ChannelLower1 = "m "+LR.length+" -"+std.channel.1+"d";
 ChannelUpper2 = "m "+LR.length+" +"+std.channel.2+"d";
 ChannelLower2 = "m "+LR.length+" -"+std.channel.2+"d";
 ChannelUpper3 = "m "+LR.length+" +"+std.channel.3+"d";
 ChannelLower3 = "m "+LR.length+" -"+std.channel.3+"d";

return(0);
}

int deinit()
{
  RemoveChannelObjects();
  return(0);
}

void RemoveChannelObjects()
{
 ObjectDelete(TrendID); 
   if (ShowChannel)
   {
   ObjectDelete(ChannelUpper1); ObjectDelete(ChannelLower1);
   ObjectDelete(ChannelUpper2); ObjectDelete(ChannelLower2);
   ObjectDelete(ChannelUpper3); ObjectDelete(ChannelLower3);
   }
}
int start()
{
   int limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

   for(int i=limit; i>=0; i--)
   {
      ExtMapBuffer1[i] = Price(MAMode, LR.MA.Period, LR.AppliedPrice, i);
   }
   
//refresh chart
  RemoveChannelObjects();
  //linear regression calculation
   start.bar=LR.length; end.bar=0;
   int n=start.bar-end.bar+1;
//---- calculate price values
   double value;
   double a,b,c;
   double sumy;
   double sumx=0.0;
   double sumxy=0.0;
   double sumx2=0.0;
   value = Price(MAMode, LR.MA.Period, LR.AppliedPrice, end.bar);
//   ExtMapBuffer1[end.bar] = value;   
   sumy = value;
   for(i=1; i<n; i++)
     {
      value = Price(MAMode, LR.MA.Period, LR.AppliedPrice, end.bar + i);
//      ExtMapBuffer1[end.bar+i] = value;   
      sumy+=value;
      sumxy+=value*i;
      sumx+=i;
      sumx2+=i*i;
     }
   c=sumx2*n-sumx*sumx;
   if(c==0.0) return;
   b=(sumxy*n-sumx*sumy)/c;
   a=(sumy-sumx*b)/n;
   double LR.price.2=a;
   double LR.price.1=a+b*n;

//---- maximal deviation calculation (not used)
   double max.dev=0;
   double deviation=0;
   double dvalue=a;
   for(i=0; i<n; i++)
     {
      value = Price(MAMode, LR.MA.Period, LR.AppliedPrice, end.bar + i);
      dvalue+=b;
      deviation=MathAbs(value-dvalue);
      if(max.dev<=deviation) max.dev=deviation;
     }
//Linear regression trendline
   CreateLine (TrendID, LR.price.1, LR.price.2, LR.line.width, LR.c);
//...standard deviation...
if (ShowChannel)
{
   double x=0,x.sum=0,x.avg=0,x.sum.squared=0,std.dev=0;
   for(i=0; i<start.bar; i++)
   {
      x=MathAbs(iClose(Symbol(),0,i)-ObjectGetValueByShift("m "+LR.length+" TL",i));
      x.sum+=x;
      if(i>0)
        {
         x.avg=(x.avg+x)/i;
         x.sum.squared+=(x-x.avg)*(x-x.avg);
         std.dev=MathSqrt(x.sum.squared/(start.bar-1));
        }
   }
   //Print("LR.price.1 ",LR.price.1,"  LR.Price.2 ",LR.price.2," std.dev ",std.dev);
//...standard deviation channels...
   CreateLine(ChannelUpper1, LR.price.1+std.dev*std.channel.1,LR.price.2+std.dev*std.channel.1, LR.line.width, c.1);
   CreateLine(ChannelLower1, LR.price.1-std.dev*std.channel.1,LR.price.2-std.dev*std.channel.1, LR.line.width, c.1);

   CreateLine(ChannelUpper2, LR.price.1+std.dev*std.channel.2,LR.price.2+std.dev*std.channel.2, LR.line.width, c.2);
   CreateLine(ChannelLower2, LR.price.1-std.dev*std.channel.2,LR.price.2-std.dev*std.channel.2, LR.line.width, c.2);
   
   CreateLine(ChannelUpper3, LR.price.1+std.dev*std.channel.3,LR.price.2+std.dev*std.channel.3, LR.line.width, c.3);
   CreateLine(ChannelLower3, LR.price.1-std.dev*std.channel.3,LR.price.2-std.dev*std.channel.3, LR.line.width, c.3);
}      
return(0);
}

void CreateLine(string ChannelID, double pos1, double pos2, int mWidth, color c)
{
   ObjectCreate(ChannelID,OBJ_TREND,0,Time[start.bar],pos1,Time[end.bar],pos2);
   ObjectSet(ChannelID,OBJPROP_COLOR,c);
   ObjectSet(ChannelID,OBJPROP_WIDTH,mWidth);
   ObjectSet(ChannelID,OBJPROP_RAY,false);

}

double Price(int maMode, int maPeriod, int prMode, int myShift)
{

   double pr;
   pr = iMA(Symbol(), 0, maPeriod, 0, maMode, prMode, myShift);
//   switch (prMode)
//   {
//     case 0: pr = iClose(Symbol(), period, myShift);break;
//     case 1: pr = iOpen(Symbol(), period, myShift);break;
//     case 2: pr = iHigh(Symbol(), period, myShift);break;
//     case 3: pr = iLow(Symbol(), period, myShift);break;
//     case 4: pr = (iHigh(Symbol(), period, myShift) + iLow(Symbol(), period, myShift))/2;break;
//     case 5: pr = (iHigh(Symbol(), period, myShift) + iLow(Symbol(), period, myShift) + iClose(Symbol(), period, myShift))/3;break;
//    case 6: pr = (iHigh(Symbol(), period, myShift) + iLow(Symbol(), period, myShift) + 2 * iClose(Symbol(), period, myShift))/4;break;
//   }
     
     return (pr);
}


//+------------------------------------------------------------------+



Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar
Series array that contains open time of each bar


Indicator Curves created:


Implements a curve of type DRAW_LINE

Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:

Other Features: