Navigation:Home > Content >

Linear_RegressionAppliedPrice.mq4

Time: 2012-12-02 | Download file:Linear_RegressionAppliedPrice.mq4

//+------------------------------------------------------------------+
//|                                            Linear Regression.mq4 |
//|                                       Copyright © 2006, tageiger |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, tageiger, aka fxid10t(at)yahoo.com"
#property link      "http://www.metaquotes.net"
#property indicator_chart_window

extern int period=0;
/*default 0 means the channel will use the open time from "x" bars back on which ever time period 
the indicator is attached to.  one can change to 1,5,15,30,60...etc to "lock" the start time to a specific 
period, and then view the "locked" channels on a different time period...*/
extern int line.width=2;
extern int LR.length=34;   // bars back regression begins
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 color  LR.c=Orange;
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;

int init(){return(0);}

int deinit(){ ObjectDelete(period+"m "+LR.length+" TL"); 
   ObjectDelete(period+"m "+LR.length+" +"+std.channel.1+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.1+"d");
   ObjectDelete(period+"m "+LR.length+" +"+std.channel.2+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.2+"d");
   ObjectDelete(period+"m "+LR.length+" +"+std.channel.3+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.3+"d");
return(0);}

int start(){//refresh chart
ObjectDelete(period+"m "+LR.length+" TL");
ObjectDelete(period+"m "+LR.length+" +"+std.channel.1+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.1+"d");
ObjectDelete(period+"m "+LR.length+" +"+std.channel.2+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.2+"d");
ObjectDelete(period+"m "+LR.length+" +"+std.channel.3+"d"); ObjectDelete(period+"m "+LR.length+" -"+std.channel.3+"d");
//linear regression calculation
int 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(LR.AppliedPrice, end.bar);
   sumy = value;
   for(int i=1; i0)  {
         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...
   ObjectCreate(period+"m "+LR.length+" +"+std.channel.1+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1+std.dev*std.channel.1,
                                        Time[end.bar],LR.price.2+std.dev*std.channel.1);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.1+"d",OBJPROP_COLOR,c.1);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.1+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.1+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LR.length+" -"+std.channel.1+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1-std.dev*std.channel.1,
                                             Time[end.bar],LR.price.2-std.dev*std.channel.1);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.1+"d",OBJPROP_COLOR,c.1);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.1+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.1+"d",OBJPROP_RAY,false);

   ObjectCreate(period+"m "+LR.length+" +"+std.channel.2+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1+std.dev*std.channel.2,
                                             Time[end.bar],LR.price.2+std.dev*std.channel.2);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.2+"d",OBJPROP_COLOR,c.2);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.2+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.2+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LR.length+" -"+std.channel.2+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1-std.dev*std.channel.2,
                                             Time[end.bar],LR.price.2-std.dev*std.channel.2);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.2+"d",OBJPROP_COLOR,c.2);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.2+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.2+"d",OBJPROP_RAY,false);

   ObjectCreate(period+"m "+LR.length+" +"+std.channel.3+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1+std.dev*std.channel.3,
                                             Time[end.bar],LR.price.2+std.dev*std.channel.3);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.3+"d",OBJPROP_COLOR,c.3);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.3+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" +"+std.channel.3+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LR.length+" -"+std.channel.3+"d",OBJ_TREND,0,iTime(Symbol(),period,start.bar),LR.price.1-std.dev*std.channel.3,
                                             Time[end.bar],LR.price.2-std.dev*std.channel.3);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.3+"d",OBJPROP_COLOR,c.3);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.3+"d",OBJPROP_WIDTH,line.width);
   ObjectSet(period+"m "+LR.length+" -"+std.channel.3+"d",OBJPROP_RAY,false);
      
return(0);}

double Price(int prMode, int myShift)
{

   double pr;
   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);
}


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

Recommend