ZigZagFirst_v1

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
ZigZagFirst_v1

This script functions as a specialized visual tool designed to identify and map significant price structural points—specifically the "peaks" and "valleys"—on a trading chart. It simplifies complex market noise into a clean set of coordinates, helping traders visualize major turning points in price movement.

How the Logic Works

The tool operates through a multi-stage filtering process to ensure that only meaningful price extremes are highlighted:

1. Initial Price Scouting The script scans the historical data based on three customizable settings: Depth, Deviation, and Backstep.

  • Depth determines how far back into the past the script looks to find a high or low point.
  • Deviation acts as a sensitivity filter; if a price move is too small, the script ignores it, preventing the chart from becoming cluttered with insignificant "wiggles."
  • Backstep allows the script to verify these points against neighboring candles to ensure they are distinct.

2. Identifying Peaks and Valleys The logic separately tracks high points (peaks) and low points (valleys). It looks for instances where the price reaches a local maximum or minimum within the defined depth. By comparing the current price against previous results, it ensures that only a significant "new" peak or valley is recorded.

3. Dynamic Refinement and Correction Once the initial points are identified, the script performs a "final cutting" pass. This is a refinement step where the tool compares new peaks/valleys to the ones it just found.

  • If a new high point is discovered that is higher than the previously recorded "current" high, the script effectively updates the data by discarding the older, lower point.
  • This ensures that the final output only shows the most prominent structural turns of the market, rather than every tiny fluctuation.

4. Visual Synchronization In the final phase, the script aligns these refined points to create a cohesive visual structure. It prioritizes the high points and maps them onto the chart in a way that emphasizes the primary trend direction. The end result is a simplified map of the market's "skeleton," highlighting where the price has decisively reversed from one direction to another.

Summary for the User

Essentially, this script acts as an automated "ZigZag" filter. It ignores the minor noise of everyday trading and draws out the core path of the market. By identifying the most significant highs and lows, it provides a clearer picture of the overall price action, making it easier to see where major trend shifts are occurring.

Miscellaneous
Implements a curve of type %1
19 Views
0 Downloads
0 Favorites
ZigZagFirst_v1
//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 White
//---- indicator parameters
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
//---- 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("ZigZagFirstBar("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;

   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)) continue;//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)) continue;//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.0)||(curhigh==0.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;
           };//z
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           };//z 
         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;
         //if(res>0.0) ExtMapBuffer[shift]=res;
        }
     }
  }

Comments