Please help me understanding this zigzag code!

 

Hi guys I´m a bit new to mql4, but I understand the most basic things in programming.

I want to create my own EA based on the highs and lows a zigzag indicator generates.

Thus I try to write a zigzag code who generates me the highs and lows because later I want to improve my EA constantly, so i don`t wanna copy/paste any zigzag indicator into my code.

I searched for a simple zigzag code and i tried to undertand it but I keep hanging around at this code because i don`t get it.

So, I would be very glad if someone or even more of you guys try to explain me this code by editing the code with some comments (//example) and make clear what or how the code is generating the highs and lows.

Here`s the code:

// PROPERTY-SETTINGS

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

// SETTINGS

extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;

// BUFFER-VARIABLES

double ExtMapBuffer[];
double ExtMapBuffer2[];

int init()
  {
  
   IndicatorBuffers(2);

// DRAWING-SETTINGS

   SetIndexStyle(0,DRAW_SECTION);

// BUFFER-SETTINGS

   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(0,0.0);
   ArraySetAsSeries(ExtMapBuffer,true);
   ArraySetAsSeries(ExtMapBuffer2,true);

// Shortname for indicator

   IndicatorShortName("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");

   return(0);
  }

int start()
  {
  
// VARIABLES
  
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;
   
// CALCULATING LOWEST BARS

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((Low[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;

// CALCULATING HIGHEST BARS

      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-High[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;
     }

// CALCULATING LOW&HIGH-VARIABLES
   
   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;
        }
     }
  }

Thanks everyone who can help me understanding this. :))


Happy trading.


I attached the file that you can add some comments and readd it that i can download it again. Thanks

Files:
 
no need to copy anything, use iCustom
 

Ok but how can I code somthing like that?:

If high reached -> sell

If low reached -> buy


How can i get the exact values where the lows and highs are?

 
this kind of thing has been discussed a lot of times have you tried to use the search button or google it
 

yes i have but i opened this thread to undertand the entire code :))

plz help me

 

OK, let's get started


lesson no. 1

#property indicator_chart_window // means show the indicator on the chart window & not in a separated window
#property indicator_buffers 1    // means that this indicator contains 1 buffer (there is a limitation of 8 buffers)
#property indicator_color1 Red   // means the color of this indicator is gonna be red (of course u can choose another color)

lesson no. 2: i will notify when it's gonna be..............

 
lets star at init()
 
FlashX:

yes i have but i opened this thread to undertand the entire code :))

plz help me

If you want to understand the code then you have to understand it. Take each function one at a time and read the Documentation about it . . if you get stuck ask for help.
 

RaptorUK:

FlashX:

yes i have but i opened this thread to undertand the entire code :))

plz help me


If you want to understand the code then you have to understand it. Take each function one at a time and read the Documentation about it . . if you get stuck ask for help.
or here
 

haha, this is the WORST code Ive ever seen in an indi. I rewrote the whole thing in less lines and less complexity. Spaghetti Italiano!

some people should never be let near a compiler!

Reason: