ZigZagOnFractals

Download script
Author: Scriptong
ZigZagOnFractals

This tool is designed to analyze market price action by identifying and connecting significant structural pivot points. Rather than simply drawing a static line, it uses a dynamic logic system to determine where the "true" peaks and valleys of the market are occurring based on price breakouts.

Core Logic Overview

The script operates by monitoring two primary types of historical markers: Upper Fractals (local ceilings) and Lower Fractals (local floors). Instead of connecting every single minor fluctuation, it looks for specific breaches of these established levels to decide when a new trend point has been formed.

How the Analysis Works

  1. Identifying Key Levels: The logic constantly scans recent historical data to find the most recent Upper and Lower Fractals. These represent the "boundaries" of the current price movement.

  2. Detecting Breakouts: The tool waits for the market price to break through one of these established boundaries:

    • Upside Breakout: If the current market high exceeds a previous Upper Fractal, the script recognizes this as a potential shift in momentum.
    • Downside Breakout: If the current market low falls below a previous Lower Fractal, it signals a downward shift.
  3. Locating the Turning Point (The "ZigZag" Effect): Once a breakout is detected, the script doesn't just mark the exact moment of the break. Instead, it performs a retrospective search:

    • If an Upside Breakout occurs, the logic looks back to find the lowest price point since the last recognized high. This becomes the new "bottom" of the current move.
    • If a Downside Breakout occurs, it looks back to find the highest price point since the last recognized low. This becomes the new "peak."
  4. Dynamic Updating: The tool is designed to be reactive. It constantly refreshes its calculations every time a new candle forms on the chart. It ensures that the visual line (the ZigZag) updates in real-time as new highs or lows are confirmed, providing a clear path of the market's structural " xương" (skeleton).

Summary for the User

Essentially, this script filters out market "noise" by ignoring minor price wiggles. It only draws a new connection when the market proves it has moved significantly enough to break past previous historical levels, automatically finding the most relevant extreme points to map out the overall trend clearly on your chart.

Indicators Used
Fractals
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
ZigZagOnFractals
//+------------------------------------------------------------------+
//|                                             ZigZagOnFractals.mq4 |
//|                                                        Scriptong |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link      "http://fortrader.ru/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
double Buf[];
int LastZZ;
double FUp, FDn;
datetime LastBar, ZeroBarBit;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,Buf);
   SetIndexEmptyValue(0,0.0);
   LastBar = 0;
   ZeroBarBit = 0;
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Ïîèñê ïîñëåäíèõ äâóõ ôðàêòàëîâ (îäèí Up- è îäèí Down-ôðàêòàë)    |
//+------------------------------------------------------------------+ 
void Fract(int Num)
{
 FUp = 0; FDn = 0;
 for(int i = Num+1; i <= LastZZ; i++)
   {
    double F = iFractals(Symbol(), 0, MODE_UPPER, i);
    if(F != 0 && FUp == 0)
      FUp = F;
    F = iFractals(Symbol(), 0, MODE_LOWER, i);
    if(F != 0 && FDn == 0)
      FDn = F;
    if(FUp != 0 && FDn != 0)
      return;  
   }
}  
 
//+------------------------------------------------------------------+
//| Íîðìàëèçàöèÿ çíà÷åíèé äî òî÷íîñòè êîòèðîâîê äàííîé âàëþòû        |
//+------------------------------------------------------------------+  
double ND(double A)
{
 return(NormalizeDouble(A, Digits));
}  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(LastBar != Time[0])
     {
      ArrayInitialize(Buf, 0.0);
      LastZZ = Bars-2;
      for(int i = Bars-3; i > 0; i--)
        {
         Fract(i);  //  ïåðåìåííûå FUp è FDn çàíîñèì çíà÷åíèÿ ïîñëåäíèõ äâóõ ôðàêòàëîâ
         // Åñëè áûëî ïðîáèòèå Up-ôðàêòàëà, òî íàõîäèì ñàìóþ íèæíþþ òî÷êó îò òåêóùåãî ìîìåíòà äî ïðåäûäóùåé âåðøèíû
         // Ýòî è áóäåò ëîêàëüíûé ìèíèìóì
         if(ND(High[i]) > ND(FUp) && ND(FUp) != 0 && ND(Buf[LastZZ]) != ND(Low[LastZZ]))
            if(LastZZ-i-1 > 0)
              {
               int Ext = iLowest(Symbol(), 0, MODE_LOW, LastZZ-i-1, i+1);
               Buf[Ext] = Low[Ext];
               LastZZ = Ext;
               continue;
              }
         // Åñëè áûëî ïðîáèòèå Down-ôðàêòàëà, òî íàõîäèì ñàìóþ âûñøóþ òî÷êó îò òåêóùåãî ìîìåíòà äî ïðåäûäóùåé âïàäèíû
         // Ýòî è áóäåò ëîêàëüíûé ìàêñèìóì
         if(ND(Low[i]) < ND(FDn) && ND(FDn) != 0 && ND(Buf[LastZZ]) != ND(High[LastZZ]))
            if(LastZZ-i-1 > 0)
              { 
               Ext = iHighest(Symbol(), 0, MODE_HIGH, LastZZ-i-1, i+1);
               Buf[Ext] = High[Ext];
               LastZZ = Ext;
              }
        }
     }
   
   // Îòñëåæèâàåì ïðîáèòèå íà íóëåâîì áàðå
   if(ZeroBarBit != Time[0])
     {
      Fract(2);
      if(ND(High[0]) > ND(FUp) && ND(FUp) != 0 && ND(Buf[LastZZ]) != ND(Low[LastZZ]))
        {
         Ext = iLowest(Symbol(), 0, MODE_LOW, LastZZ-1, 1);
         Buf[Ext] = Low[Ext];
         ZeroBarBit = Time[0];
         return(0);
        } 
      if(ND(Low[0]) < ND(FDn) && ND(FDn) != 0 && ND(Buf[LastZZ]) != ND(High[LastZZ]))
        {
         Ext = iHighest(Symbol(), 0, MODE_HIGH, LastZZ-1, 1);
         Buf[Ext] = High[Ext];
         ZeroBarBit = Time[0];
        }  
     }
   // ------------------------------------
//   Print("i = ",i, ", FUp = ", FUp, ", FDn = ", FDn, ", LastZZ = ", LastZZ);
     
   LastBar = Time[0];  
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments