zigzagsignal

Download script
Author: Greshnik
zigzagsignal

This script is designed as a technical analysis tool for the MetaTrader 4 platform. It works by identifying specific geometric patterns within market price movements, specifically focusing on the "ZigZag" structure—a common visual representation of major highs and lows in trading.

Core Functionality

The primary goal of this logic is to filter out minor market "noise" and highlight significant trend reversals. Instead of reacting to every small price fluctuation, the script analyzes a sequence of five consecutive peaks or valleys (turning points) to determine if they meet specific structural requirements before providing a signal.

How the Logic Works

The script follows a multi-step verification process:

1. Data Collection The tool monitors the price action and extracts the coordinates of recent ZigZag peaks and valleys. It organizes these into a chronological history to evaluate how the market has moved over the last several significant turns.

2. Structural Filtering (The "Shape" Check) To ensure the signals are reliable, the script checks for a specific relationship between the heights of these peaks and valleys:

  • Proportional Growth: It calculates the ratio of height between different segments of the ZigZag. It looks for a specific consistency in how the price moves from one point to the next (using a minimum height ratio).
  • Deviation Limits: It ensures that certain "bottoms" or "tops" do not deviate too far from the established trend, maintaining the integrity of the pattern it is looking for.

3. Magnitude Verification Even if the geometric shape is correct, the script requires the price movement to be significant enough to matter. It checks the total distance covered by the current move against a user-defined "Minimum Height." If the move is too small, the script ignores it to avoid entering trades on insignificant price wiggles.

4. Visual Output Once all conditions are met—the correct shape, the correct proportions, and the required minimum size—the script places visual markers on your chart:

  • Buy Signals: Identified when a significant downward move is followed by a structural upward reversal that meets all criteria.
  • Sell Signals: Identified when a significant upward move is followed by a structural downward reversal that meets all criteria.

Summary for the User

In plain terms, this script acts as a sophisticated filter. It looks at the "skeleton" of the price chart (the ZigZag) and waits for a very specific type of structural pattern to form. Only when the market moves with enough strength and follows a specific geometric proportion will it highlight a potential entry point on your screen, helping you stay focused on high-probability trend shifts rather than minor price fluctuations.

Miscellaneous
Implements a curve of type %1
10 Views
1 Downloads
0 Favorites
zigzagsignal
//+------------------------------------------------------------------+
//|                                                ZigZag Signal.mq4 |
//|                                                         Greshnik |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Greshnik"
#property link      ""
#property version   "2.00"
#property strict

#property indicator_chart_window

#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red

input int      zigzag_Depft=4;
input int      zigzag_Deviation=2;
input int      zigzag_Backstep=2;
input int      MinHeight=60;//Ìèíèìàëüíàÿ âûñîòà 1-ãî êîëåíà ZigZag(ïóíêòû)
input double   MinHeightM234=1.15;//Ìèíèìàëüíîå îòíîøåíèå 2-é âåðøèíû ê 1-é è 3-é ê 4-é.
input double   Bottom=0.1;//Ìàêñèìàëüíîå îòêëîíåíèå 2-é âåðøèíû äíà îò 1-é ïî îòíîøåíèèþ ê âûñîòå 1-ãî êîëåíà

double   mBuffer0[];
double   mBuffer1[];
double   mBuffer2[];
double   mBuffer3[];

datetime last_time;
double points[5][2];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,mBuffer0);
   SetIndexStyle(0,DRAW_ARROW,0,2);
   SetIndexArrow(0,233);
   SetIndexEmptyValue(0,0.0);
   SetIndexLabel(0,"BUY");

   SetIndexBuffer(1,mBuffer1);
   SetIndexStyle(1,DRAW_ARROW,0,2);
   SetIndexArrow(1,234);
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel(1,"SELL");

   SetIndexBuffer(2,mBuffer2);
   SetIndexStyle(2,DRAW_ARROW,0,1);
   SetIndexArrow(2,174);
   SetIndexEmptyValue(2,0.0);
   SetIndexLabel(2,"zBuy");

   SetIndexBuffer(3,mBuffer3);
   SetIndexStyle(3,DRAW_ARROW,0,1);
   SetIndexArrow(3,174);
   SetIndexEmptyValue(3,0.0);
   SetIndexLabel(3,"zSell");

   last_time=0;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int bars;
   int i1,pos1;
   double d1,d2,d3;
   datetime t1;
   int filling;

   t1=Time[1];
   if(last_time>=t1)
     {
      return rates_total;
     }
   last_time=t1;

   bars=rates_total-prev_calculated;

   filling=5;
   for(pos1=0;((pos1<bars) || (filling>0)) && (pos1<rates_total);pos1++)
     {
      d1=iCustom(Symbol(),Period(),"Zigzag",zigzag_Depft,zigzag_Deviation,zigzag_Backstep,0,pos1);
      d2=iCustom(Symbol(),Period(),"Zigzag",zigzag_Depft,zigzag_Deviation,zigzag_Backstep,1,pos1);
      d3=iCustom(Symbol(),Period(),"Zigzag",zigzag_Depft,zigzag_Deviation,zigzag_Backstep,2,pos1);

      if(d1>0.0001)
        {
         filling--;
         for(i1=3;i1>=0;i1--)
           {
            points[i1+1][0]=points[i1][0];
            points[i1+1][1]=points[i1][1];
           }
         points[0][0]=pos1;
         points[0][1]=d1;

         if(filling<=0)
           {
            if(MathAbs(points[0][1]-points[1][1])<0.00000001)
              {
               filling=5;
               continue;
              }
            if(
               ((points[2][1]-points[1][1])/(points[0][1]-points[1][1])>=MinHeightM234)&&
               ((points[4][1]-points[3][1])/(points[2][1]-points[1][1])>=MinHeightM234)&&
               (MathAbs(points[3][1]-points[1][1])/MathAbs(points[0][1]-points[1][1])<=Bottom)
               )
              {
               if((points[0][1]-points[1][1])/Point>=MinHeight)
                 {
                  mBuffer1[(int)points[4][0]]=points[4][1]+MinHeight*Point/2;
                  for(i1=0;i1<4;i1++)
                    {
                     mBuffer3[(int)points[i1][0]]=points[i1][1];
                    }
                 }
               else
               if((points[0][1]-points[1][1])/Point<=-MinHeight)
                 {
                  mBuffer0[(int)points[4][0]]=points[4][1]-MinHeight*Point/2;
                  for(i1=0;i1<4;i1++)
                    {
                     mBuffer2[(int)points[i1][0]]=points[i1][1];
                    }
                 }
              }
            filling=0;
           }
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments