t3_RSI_ZigZag_With_Channel





//+------------------------------------------------------------------+
//|                                                t3_RSI_ZigZag.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
//#property indicator_minimum 0
//#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
//---- input parameters
extern int RSIPeriod=14;
extern int t3_period=8;
extern double b=0.7;
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
extern int LowLevel=30;
extern int MedLevel=50;
extern int HighLevel=70;
extern int Window=1;
extern int Channel=0;
extern double STD.width=1.0;
extern int Price=0; // 0 = Close             
                    // 1 = Open
                    // 2 = High
                    // 3 = Low
                    // 4 = (H+L)/2
                    // 5 = (H+L+C)/3
                    // 6 = (H+L+C+C)/4
                    // other = Close                    
//---- buffers
double RSIBuffer[];
double t3_RSIBuffer[];
double ExtMapBuffer[];
double ExtMapBuffer2[];

double e1, e2, e3, e4, e5, e6, c1, c2, c3, c4, n, w1, w2, b2, b3;
int OldLastZigZag, OldPreviousZigZag;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(2,RSIBuffer);
   SetIndexBuffer(3,ExtMapBuffer2);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,t3_RSIBuffer);
   SetIndexStyle(1,DRAW_SECTION);
   SetIndexBuffer(1,ExtMapBuffer);
   SetIndexEmptyValue(1,0.0);
//---- name for DataWindow and indicator subwindow label
   short_name="RSI("+RSIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,RSIPeriod+t3_period);
   SetIndexDrawBegin(1,RSIPeriod+t3_period);
   SetLevelValue(0,LowLevel);
   SetLevelValue(1,MedLevel);
   SetLevelValue(2,HighLevel);
   b2=b*b;
   b3=b2*b;
   c1=-b3;
   c2=(3*(b2+b3));
   c3=-3*(2*b2+b+b3);
   c4=(1+3*b+b3+3*b2);
   n=t3_period;

   if (n<1) n=1;
   n = 1 + 0.5*(n-1);
   w1 = 2 / (n + 1);
   w2 = 1 - w1;
//----
   return(0);
  }
int deinit()
{
	ObjectDelete("Channel");
}  
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
//----   
   int    shift, back,lasthighpos,lastlowpos,index;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;

   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   if(Price<0 || Price>6) Price=0;
   for(; i>=0; i--)
    {
     RSIBuffer[i]=iRSI(NULL,0,RSIPeriod,Price,i);
    } 
   for(i=Bars-1; i>=0; i--)
    {
     e1 = w1*RSIBuffer[i] + w2*e1;
     e2 = w1*e1 + w2*e2;
     e3 = w1*e2 + w2*e3;
     e4 = w1*e3 + w2*e4;
     e5 = w1*e4 + w2*e5;
     e6 = w1*e5 + w2*e6;

     t3_RSIBuffer[i] = c1*e6 + c2*e5 + c3*e4 + c4*e3;
    }
   for(shift=Bars-1; shift>=0; shift--)
     {
      int n, sn, r, result;
      bool ft=true;
      for(n=1; n<ExtDepth; n++)
       {
        sn=shift+n;
        if(ft)
         {
          if(t3_RSIBuffer[shift]<t3_RSIBuffer[sn]) result=shift;
          else result=sn;
          r=result;
          ft=false;
         } 
        else
         {
          if(t3_RSIBuffer[r]<t3_RSIBuffer[sn]) result=r;
          else result=sn;
          r=result;
         }
       }    
      index=r;
      val=t3_RSIBuffer[index];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((t3_RSIBuffer[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0; 
              }
           }
        } 
      ExtMapBuffer[shift]=val;
      //--- high
      ft=true;
      for(n=1; n<ExtDepth; n++)
       {
        sn=shift+n;
        if(ft)
         {
          if(t3_RSIBuffer[shift]>t3_RSIBuffer[sn]) result=shift;
          else result=sn;
          r=result;
          ft=false;
         }
        else
         {
          if(t3_RSIBuffer[r]>t3_RSIBuffer[sn]) result=r;
          else result=sn;
          r=result;
         }
       }  
      index=r;
      val=t3_RSIBuffer[index];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-t3_RSIBuffer[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0; 
              } 
           }
        }
      ExtMapBuffer2[shift]=val;
     }
//---- final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
  
   for(shift=Bars-1; shift>=0; shift--)
     {
      if(shift>=Bars-ExtDepth) ExtMapBuffer[shift]=0.0;
      else
        {
         res=ExtMapBuffer2[shift];
         if(res!=0.0) ExtMapBuffer[shift]=res;
        }
     } 
   int LastZigZag, PreviousZigZag;
   int h=0;
   while (ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) h++;
   LastZigZag=h;
   h++;
   while(ExtMapBuffer[h]==0 && ExtMapBuffer2[h]==0) h++;
   PreviousZigZag=h;
   if (OldLastZigZag!=LastZigZag || OldPreviousZigZag!=PreviousZigZag)
    {
     OldLastZigZag=LastZigZag;
     OldPreviousZigZag=PreviousZigZag;
     if(Channel!=0)
      {
       ObjectDelete("Channel");
       ObjectCreate("Channel", OBJ_REGRESSION, Window, Time[PreviousZigZag], ExtMapBuffer[LastZigZag], Time[LastZigZag], ExtMapBuffer[PreviousZigZag]);
       ObjectSet("Channel",OBJPROP_COLOR,DeepSkyBlue);
       ObjectSet("Channel",OBJPROP_RAY,true);
      }
     else
      {  
       ObjectDelete("Channel");
       ObjectCreate("Channel", OBJ_STDDEVCHANNEL, Window, Time[PreviousZigZag], ExtMapBuffer[LastZigZag], Time[LastZigZag], ExtMapBuffer[PreviousZigZag]);
       ObjectSet("Channel",OBJPROP_DEVIATION,STD.width);
       ObjectSet("Channel",OBJPROP_COLOR,Blue);
       ObjectSet("Channel",OBJPROP_RAY,true);
      }
    }        
//----
   return(0);
  }
//+------------------------------------------------------------------+



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:


Implements a curve of type DRAW_LINE
Implements a curve of type DRAW_SECTION

Indicators Used:

Relative strength index


Custom Indicators Used:

Order Management characteristics:

Other Features: