Navigation:Home > Content >

Linear_Regression~.mq4

Time: 2012-07-18 | Download file:Linear_Regression~.mq4

//+------------------------------------------------------------------+
//|                                            Linear Regression.mq4 |
//|                Copyright © 2006, tageiger, aka [email protected] |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, tageiger, aka [email protected]"
#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 linewidth=2;
extern int LRlength=34;   // bars back regression begins
extern color  LRc=clrNONE;    //Orange;
extern double stdchannel1=0.618;        // 1st channel
extern color  c1=Gray;
extern double stdchannel2=1.618;        // 2nd channel
extern color  c2=Gray;
extern double stdchannel3=2.618;        // 3nd channel
extern color  c3=Gray;

int init(){return(0);}

int deinit(){ ObjectDelete(period+"m "+LRlength+" TL"); 
   ObjectDelete(period+"m "+LRlength+" +"+stdchannel1+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel1+"d");
   ObjectDelete(period+"m "+LRlength+" +"+stdchannel2+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel2+"d");
   ObjectDelete(period+"m "+LRlength+" +"+stdchannel3+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel3+"d");
return(0);}

int start(){//refresh chart
ObjectDelete(period+"m "+LRlength+" TL");
ObjectDelete(period+"m "+LRlength+" +"+stdchannel1+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel1+"d");
ObjectDelete(period+"m "+LRlength+" +"+stdchannel2+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel2+"d");
ObjectDelete(period+"m "+LRlength+" +"+stdchannel3+"d"); ObjectDelete(period+"m "+LRlength+" -"+stdchannel3+"d");
//linear regression calculation
int startbar=LRlength, endbar=0;
   int n=startbar-endbar+1;
//---- calculate price values
   double value=iClose(Symbol(),period,endbar);
   double a,b,c;
   double sumy=value;
   double sumx=0.0;
   double sumxy=0.0;
   double sumx2=0.0;
   for(int i=1; i0)  {
         xavg=(xavg+x)/i;
         xsumsquared+=(x-xavg)*(x-xavg);
         stddev=MathSqrt(xsumsquared/(startbar-1));  }  }
   //Print("LR.price.1 ",LR.price.1,"  LR.Price.2 ",LR.price.2," std.dev ",std.dev);
//...standard deviation channels...
   ObjectCreate(period+"m "+LRlength+" +"+stdchannel1+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1+stddev*stdchannel1,
                                        Time[endbar],LRprice2+stddev*stdchannel1);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel1+"d",OBJPROP_COLOR,c1);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel1+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel1+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LRlength+" -"+stdchannel1+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1-stddev*stdchannel1,
                                             Time[endbar],LRprice2-stddev*stdchannel1);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel1+"d",OBJPROP_COLOR,c1);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel1+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel1+"d",OBJPROP_RAY,false);

   ObjectCreate(period+"m "+LRlength+" +"+stdchannel2+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1+stddev*stdchannel2,
                                             Time[endbar],LRprice2+stddev*stdchannel2);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel2+"d",OBJPROP_COLOR,c2);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel2+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel2+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LRlength+" -"+stdchannel2+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1-stddev*stdchannel2,
                                             Time[endbar],LRprice2-stddev*stdchannel2);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel2+"d",OBJPROP_COLOR,c2);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel2+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel2+"d",OBJPROP_RAY,false);

   ObjectCreate(period+"m "+LRlength+" +"+stdchannel3+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1+stddev*stdchannel3,
                                             Time[endbar],LRprice2+stddev*stdchannel3);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel3+"d",OBJPROP_COLOR,c3);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel3+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" +"+stdchannel3+"d",OBJPROP_RAY,false);
   
   ObjectCreate(period+"m "+LRlength+" -"+stdchannel3+"d",OBJ_TREND,0,iTime(Symbol(),period,startbar),LRprice1-stddev*stdchannel3,
                                             Time[endbar],LRprice2-stddev*stdchannel3);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel3+"d",OBJPROP_COLOR,c3);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel3+"d",OBJPROP_WIDTH,linewidth);
   ObjectSet(period+"m "+LRlength+" -"+stdchannel3+"d",OBJPROP_RAY,false);
      
return(0);}
//+------------------------------------------------------------------+

Recommend