ZZS

This tool is designed to identify and visualize the significant "peaks" and "valleys" in financial market price movements, creating a clean visual path of the market's primary trends. Instead of showing every minor price fluctuation, it filters out the noise to highlight only the most important structural turns.

Core Logic and Behavior

The script operates by scanning historical price data to identify major turning points based on three specific user-defined parameters: Depth, Deviation, and Backstep. Here is how it processes that information:

1. Identifying Extremes (The Filtering Phase) The tool constantly looks back over a set window of recent price bars (the "Depth"). It searches for the highest highs and lowest lows within that period. However, to prevent the chart from being cluttered with every tiny movement, it applies two filters:

  • Deviation: The price must move a significant enough distance from the previous point to be considered a "new" peak or valley. If the change is too small, the script ignores it.
  • Backstep: This ensures that the line remains smooth by checking immediate preceding points. It prevents the indicator from creating multiple jagged points in a very short timeframe, ensuring that only one distinct point is registered for a specific trend direction.

2. Trend Confirmation (The Smoothing Phase) Once potential peaks and valleys are identified, the script performs a secondary "cleaning" process. It looks at the sequence of these points to ensure they follow a logical order. If it detects a new peak that is higher than a previously recorded one in the same sequence, it intelligently updates the path to reflect the most significant price level. This ensures that the resulting line represents the most prominent structural trend rather than a series of minor corrections.

3. Final Visualization The final result is a continuous line (drawn in red) that connects these primary structural highs and lows. For the user, this translates to a clear visual map of market momentum:

  • When the line moves upward, it highlights a confirmed bullish trend.
  • When the line moves downward, it highlights a confirmed bearish trend.
  • The "zig-zag" nature of the line allows traders to see where the market has reversed direction and how far those reversals have extended.

Summary for the User

In simple terms, this script acts as a "trend filter." It ignores the "jitter" of minute price changes and only draws lines when the market makes a meaningful move in a specific direction. By using the Depth and Deviation settings, you can control whether you want to see micro-trends (more frequent lines) or macro-trends (fewer, larger moves).

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
ZZS
//+------------------------------------------------------------------+
//|                                                      ZigZagS.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
extern int nBars=150;
//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(2);
//---- drawing settings
   SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(0,0.0);
   ArraySetAsSeries(ExtMapBuffer,true);
   ArraySetAsSeries(ExtMapBuffer2,true);
//---- indicator short name
   IndicatorShortName("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start() {
  int    shift, back, lasthighpos, lastlowpos, LoopBegin,k;
  double val, res;
  double curlow, curhigh, lasthigh, lastlow;

 	if (nBars==0) LoopBegin=Bars-ExtDepth;
  else LoopBegin=nBars;
  LoopBegin=MathMin(Bars-ExtDepth, LoopBegin);

  for(shift=LoopBegin; 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;
      //--- high
      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;
     }

   // 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;k=0;}
      else
        {
         res=ExtMapBuffer2[shift];
         if(res!=0.0) ExtMapBuffer[shift]=res;
        }
     }
  }

Comments