ZigZag_Real_Sep

Download script
ZigZag_Real_Sep

This tool is designed to analyze price movements on a trading chart by identifying significant "swings"—the peak highs and valley lows that define the overall direction of a market trend. Rather than showing every minor price fluctuation, it filters out the noise to highlight the structural turning points of the market.

Core Logic and Functionality

1. Trend Identification (The ZigZag Mechanism) The script scans through historical price data to establish a "ZigZag" pattern. It looks for specific price extremes by comparing a set number of candles against each other. When it detects that a price has moved significantly enough to form a new high or a new low, it marks that point as a structural pivot.

2. Dynamic Peak and Valley Tracking As the script moves through time, it keeps track of the highest highs and lowest lows. If the price continues to move in one direction (e.g., hitting new highs), the script updates the "peak" coordinates. If the price reverses and breaks past a previous level in the opposite direction, the script recognizes this as a change in trend and creates a new connection point.

3. Visual Indicators The tool provides three distinct visual layers:

  • Trend Lines: It draws lines connecting the major highs and lows, allowing you to see the "skeleton" of the market movement clearly.
  • Buy/Sell Markers: The script automatically places specific arrow symbols on the chart when it identifies a definitive reversal. These arrows act as visual cues for potential entry points based on trend changes.

4. Automated Alerts To ensure you don't have to watch the charts constantly, the script includes a notification system. When the logic detects a new confirmed structural reversal (a "Buy" or "Sell" signal), it triggers a pop-up alert on your platform. This ensures that you are notified of significant trend shifts in real-time.

Summary for the User

Think of this tool as an automatic "map maker" for price action. It ignores the small, jagged movements of the market and draws a clear path showing where the price has actually turned. By identifying these major pivots and alerting you to them, it helps you stay aligned with the broader market direction rather than getting distracted by minor volatility.

Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
ZigZag_Real_Sep
//+------------------------------------------------------------------+
//|                                             ZigZag_Real_Sep.mq4  |
//|                                                                  |
//+------------------------------------------------------------------+


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Blue
#property indicator_color3 Red

//---- input parameters
extern bool Alert_message = true;
extern int Length=6;
extern int CountBars=300;
//---- buffers

double LL,HH,BH,BL,NH,NL;

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double AlertTime=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator line
   IndicatorBuffers(4);
   SetIndexBuffer(0, Buffer1);
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(1, Buffer2);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,233);
   SetIndexBuffer(2, Buffer3);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,234);
   SetIndexBuffer(3, Buffer4);
//---- name for DataWindow and indicator subwindow label
   //short_name="";
   //IndicatorShortName(short_name);
   //SetIndexLabel(0,short_name);
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| z                                                       |
//+------------------------------------------------------------------+
int start()
  {
   if (CountBars>Bars) CountBars=Bars;
   SetIndexDrawBegin(0,Bars-CountBars+1);
   SetIndexDrawBegin(1,Bars-CountBars+1);
   int shift,i,Swing_n,Swing,uzl,zu,zd,counted_bars=IndicatorCounted();
   int a,b;
   
   if(Bars<=CountBars-1) return(0);

//---- initial zero
//   if(counted_bars<1)
//      for(i=1;i<=SMA_Period;i++) MomBuffer[Bars-i]=0.0;
//----
   Swing_n=0;Swing=0;//uzl=0;
   BH =High[CountBars];BL=Low[CountBars];zu=CountBars;zd=CountBars; 
   shift=CountBars-1;
//   if(counted_bars>=SMA_Period) i=Bars-counted_bars-1;
   while(shift>=0)
     {
     
     
LL=10000000;HH=-100000000;
for (i=shift+Length ; i>=shift+1; i--)

{ 
if (Low[i]<LL) LL=Low[i];
if (High[i]>HH) HH=High[i];
}



if ((Low[shift]<LL) && (High[shift]>HH))
{ 
Swing=2; 
if (Swing_n==1) zu=shift+1;
if (Swing_n==-1) zd=shift+1;
} 
else 
{ 
if (Low[shift]<LL) Swing=-1;
if (High[shift]>HH) Swing=1;
}

if ((Swing != Swing_n) && (Swing_n != 0))
{ 
if (Swing==2) {Swing=-Swing_n;BH = High[shift];BL = Low[shift];}

if (Swing == 1) {Buffer4[shift]=BL;Buffer1[zd]=BL;}
if (Swing ==- 1) {Buffer4[shift]=BH;Buffer1[zu]=BH;}

BH = High[shift];BL = Low[shift]; 
}

if (Swing == 1) { if (High[shift] >= BH) {BH=High[shift]; zu=shift;} } 
if (Swing == -1) { if (Low[shift]<=BL) {BL=Low[shift]; zd=shift;} } 
Swing_n=Swing;      
      

      shift--;
     }
     
for (i=CountBars; i>=0; i--)
{      
   Buffer2[i]=EMPTY_VALUE; Buffer3[i]=EMPTY_VALUE;
   if (Buffer4[i]>=High[i]) Buffer3[i]=Buffer4[i];
   if (Buffer4[i]<=Low [i]) Buffer2[i]=Buffer4[i];
}     
//---- Alert
     if (Alert_message==true && CurTime()>AlertTime && Buffer2[0]!=EMPTY_VALUE)
     { 
     Alert(" ",Symbol()," - ZigZag_Real(", Period(), ")  -  BUY !!!  ");
     AlertTime=CurTime() + (Period() - MathMod(TimeMinute(CurTime()), Period()))*60;
     }
     
     if (Alert_message==true && CurTime()>AlertTime && Buffer3[0]!=EMPTY_VALUE)
     { 
     Alert(" ",Symbol()," - ZigZag_Real(", Period(), ")  -  SELL !!!  ");
     AlertTime=CurTime() + (Period() - MathMod(TimeMinute(CurTime()), Period()))*60;
     }
//---- 
   return(0);
  }
//+------------------------------------------------------------------+

Comments