Fractal ZigZag_v1

Download script
Author: ikovrigin
Fractal ZigZag_v1

This tool is designed to provide a visual map of the primary price movements on your trading chart. It functions by identifying significant "turning points" in the market—specifically the highs and lows where the price changes direction—and connecting them to form a clear trend line.

How the Logic Works

The script operates by scanning historical price data to identify unique peaks and valleys based on a user-defined sensitivity level. Here is a breakdown of its core functionality:

1. Identifying Significant Points (Fractals) Instead of showing every minor fluctuation in price, the script looks for specific "fractal" points. A point is only recognized as a significant peak if it is higher than all neighboring candles within a certain range, and a valley is only recognized if it is lower than all neighbors in that same range. This ensures that only major structural movements are highlighted.

2. Trend Detection and Filtering The logic maintains an internal "memory" of the current market direction (Upward or Downward). It does not simply draw a line every time a new high or low is touched; instead, it follows these rules:

  • In an Uptrend: The script looks for a new significant low. If it finds one that is lower than the previous low, it considers the trend broken and switches to a downward orientation.
  • In a Downtrend: The script looks for a new significant high. If it finds one that is higher than the previous high, it identifies a reversal and switches to an upward orientation.

3. Visual Representation The final output on your chart is a clean "ZigZag" structure:

  • Connection Lines: It draws continuous lines connecting these major highs and lows, making it easier to see the "skeleton" of the market trend.
  • Color Coding: By using distinct colors (Red and Green), the script helps you instantly distinguish between bullish moves and bearish retreats.

Key Benefit for the Trader

By filtering out the "noise" of minor price wicks and small fluctuations, this tool allows you to focus on the broader market structure. It helps in identifying support and resistance levels more clearly by highlighting the most significant structural pivots where the market has historically reversed direction.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
Fractal ZigZag_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                               Fractal ZigZag.mq4 |
//|                                                        ikovrigin |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "ikovrigin"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
//---- input parameters
extern int       Level=2;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,ExtMapBuffer2);
   
     
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//---- 
   

   int PU,PD,Trend=2;
   PU=0;
   PD=0;
   double FU,FD,F;
   FU=0;
   FD=0;
   int shift = Bars-Level;
   int n;
   if (shift>Level) shift=shift-Level-1;
   if (counted_bars==0)
   while (shift>Level-1)
   {
   ExtMapBuffer1[shift]=0;
   F=Low[shift];
   for (n=1; n<=Level; n++)
   {
     if (Low[shift+n]<Low[shift] || Low[shift-n]<Low[shift]) F=0;
   }
    if (F>0)
    {
      if (FD!=0)
      switch (Trend)
      {
        case 1:
           if (F<FD) 
           {
              ExtMapBuffer1[PU]=FU;
              Trend=2;
           }
           break;
        case 2:
           if (F>FD) 
           {
              ExtMapBuffer1[PD]=FD;
              Trend=1;
              if (PU>=PD)
              {
                FU=0;
                PU=0;
              }
           }
           break;
      }
      FD=F;
      PD=shift;
      ExtMapBuffer2[PD]=FD;
    }
    
    F=High[shift];
    for (n=1; n<=Level; n++)
    {
     if (High[shift+n]>High[shift] || High[shift-n]>High[shift]) F=0;    
    }
    if (F>0)
    {
      if (FU!=0) 
      switch (Trend)
      {
        case 1:
           if (F<FU )
           {
              ExtMapBuffer1[PU]=FU;
              Trend=2;
              if (PD>=PU)
              {
                FD=0;
                PD=0;
              }
           }
           break;
        case 2:
           if (F>FU)
           {
              ExtMapBuffer1[PD]=FD;
              Trend=1;
           }
           break;
      }
      FU=F;
      PU=shift;
      ExtMapBuffer2[PU]=FU;
    }
	shift--;
   }
   if (Trend==1) ExtMapBuffer1[PU]=FU; else ExtMapBuffer1[PD]=FD;
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments