Swing_ZZ_001

Download script
Author: onix
Swing_ZZ_001

This script functions as a visual trading tool designed to identify and map out significant structural points in price movement, specifically focusing on "swing" highs and lows. It automates the detection of trend reversals by monitoring how price action behaves over a specific sequence of bars.

Core Logic and Behavior

1. Trend Detection Mechanism The script continuously scans historical price data to distinguish between upward and downward momentum. It does this by tracking several key metrics:

  • Price Extremes: It monitors the highest highs and lowest lows of recent price candles.
  • Bar Counting: The logic maintains counters to track how many consecutive bars are moving in a specific direction (up or down).
  • Minimum Bar Threshold: A user-defined parameter (minBars) acts as a filter. The script will only acknowledge a trend change if the movement persists for at least this many bars, which helps filter out minor market "noise" and highlights more significant structural shifts.

2. Identifying Swing Points (Highs and Lows) Once the script determines that a trend has sufficiently developed or reversed, it identifies specific "Swing" points:

  • High Points: When the price moves significantly upward and meets the required bar criteria, the script marks a high point.
  • Low Points: Conversely, when the price moves significantly downward and meets the same criteria, it marks a low point. These points represent the "peaks" and "valleys" of the market trend.

3. Dynamic Trend Reversal The logic is reactive. If the price begins to move aggressively against the current trend (e.g., a strong downward move occurring during an established uptrend), the script detects this conflict. If the downward movement exceeds the minimum bar requirement, the script automatically:

  • Identifies the reversal point.
  • Updates its internal "state" to recognize the new trend direction.
  • Places a marker at the peak of the previous move.

4. Visual Representation To make this data usable for a trader, the script handles three visual layers:

  • Sections: It draws lines connecting the identified highs and lows to form a visible "zigzag" path on the chart.
  • Arrows/Markers: It uses specific symbols (arrows) to pinpoint exactly where a new swing high or low was confirmed.
  • Color Coding: The script utilizes distinct colors (Aqua, Blue, and Red) to differentiate between the various data points it tracks, helping the user quickly distinguish between highs and lows at a glance.

Summary for the User

In simple terms, this tool looks at your price chart and says: "Ignore the small wobbles; tell me when the market has actually moved enough in one direction to be considered a real trend." When it finds those significant moves, it draws lines connecting the highest and lowest points, creating a clear "skeleton" of the market's structure.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
Swing_ZZ_001
//+------------------------------------------------------------------+
//|                                                     Swing_ZZ.mq4 |
//+------------------------------------------------------------------+
#property copyright "onix"
#property link      "http://onix-trade.net/forum/index.php?s=&showtopic=118&view=findpost&p=131476"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Blue
#property indicator_color3 Red

//---- indicator parameters
extern int    minBars = 2; 

//---- indicator buffers
double zzL[];
double zzH[];
double zz[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 //  IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexArrow(2,159);
   SetIndexArrow(1,159);
//---- indicator buffers mapping
   SetIndexBuffer(0,zz);
   SetIndexBuffer(1,zzH);
   SetIndexBuffer(2,zzL);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
     
//---- indicator short name
   IndicatorShortName("Swing_ZZ("+minBars+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
 int cbi;
 double lLast=0,hLast=0; 
 int fs=0; 
 int ai,bi,ai0,aip0,bi0,bip0; 
 datetime tai,tbi,taip,tbip,ti; 
 
 // Ïåðåìåííûå äëÿ Ñâèíãîâ Ãàííà
 double lLast_m=0, hLast_m=0;
 int countBarExt; // ñ÷åò÷èê âíåøíèõ áàðîâ
 int countBarl,countBarh;
 int i,n;

   ArrayInitialize(zz,0.0);
   ArrayInitialize(zzL,0.0);
   ArrayInitialize(zzH,0.0);



// lLast, hLast - ìèíèìóì è ìàêñèìóì àêòèâíîãî áàðà
// lLast_m, hLast_m - ìèíèìóì è ìàêñèìóì "ïðîìåæóòî÷íûõ" áàðîâ

//   cbi=Bars-IndicatorCounted()-1;
   cbi=Bars-1; 
//---------------------------------
   for (i=cbi; i>=0; i--) 
     {
//-------------------------------------------------
      // Óñòàíàâëèâàåì íà÷àëüíûå çíà÷åíèÿ ìèíèìóìà è ìàêñèìóìà áàðà
      if (lLast==0) {lLast=Low[i]; hLast=High[i]; ai=i; bi=i;}
      if (ti!=Time[i])
        {
         ti=Time[i];
         if (lLast_m==0 && hLast_m==0)
           {
            if (lLast>Low[i] && hLast<High[i]) // Âíåøíèé áàð
              {
               lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
               if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
               else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
               else {countBarl++;countBarh++;}
              }
            else if (lLast<=Low[i] && hLast<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
              {
               lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
               if (fs!=1) countBarh++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
              }
            else if (lLast>Low[i] && hLast>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
              {
               lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
               if (fs!=2) countBarl++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
              }
           }
         else  if (lLast_m>0 && hLast_m>0) // Âíåøíèé áàð (ïðåäûäóùèé)
           {
            if (lLast_m>Low[i] && hLast_m<High[i]) // Âíåøíèé áàð
              {
               lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
               if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
               else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
               else {countBarl++;countBarh++;}
              }
            else if (lLast_m<=Low[i] && hLast_m<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
              {
               lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
               if (fs!=1) countBarh++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
              }
            else if (lLast_m>Low[i] && hLast_m>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
              {
               lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
               if (fs!=2) countBarl++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
              }
           }
         else  if (lLast_m>0)
           {
            if (lLast_m>Low[i] && hLast<High[i]) // Âíåøíèé áàð
              {
               lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
               if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
               else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
               else {countBarl++;countBarh++;}
              }
            else if (lLast_m<=Low[i] && hLast<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
              {
               lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
               if (fs!=1) countBarh++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
              }
            else if (lLast_m>Low[i] && hLast>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
              {
               lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
               if (fs!=2) countBarl++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
              }
           }
         else  if (hLast_m>0)
           {
            if (lLast>Low[i] && hLast_m<High[i]) // Âíåøíèé áàð
              {
               lLast=Low[i];hLast=High[i];lLast_m=Low[i];hLast_m=High[i];countBarExt++;
               if (fs==1) {countBarl=countBarExt; ai=i; tai=Time[i];}
               else if (fs==2) {countBarh=countBarExt; bi=i; tbi=Time[i];}
               else {countBarl++;countBarh++;}
              }
            else if (lLast<=Low[i] && hLast_m<High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
              {
               lLast_m=0;hLast_m=High[i];countBarl=0;countBarExt=0;
               if (fs!=1) countBarh++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; ai=i; tai=Time[i];}
              }
            else if (lLast>Low[i] && hLast_m>=High[i]) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
              {
               lLast_m=Low[i];hLast_m=0;countBarh=0;countBarExt=0;
               if (fs!=2) countBarl++;
               else {lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0; bi=i; tbi=Time[i];}
              }
           }

         // Îïðåäåëÿåì íàïðàâëåíèå òðåíäà. 
         if (fs==0)
           {
            if (lLast<lLast_m && hLast>hLast_m) // âíóòðåííèé áàð
              {
               lLast=Low[i]; hLast=High[i]; ai=i; bi=i; countBarl=0;countBarh=0;countBarExt=0;
              }
              
            if (countBarh>countBarl && countBarh>countBarExt && countBarh>minBars)
              {
               lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
               fs=1;countBarh=0;countBarl=0;countBarExt=0;
               zz[bi]=Low[bi];
               zzL[bi]=Low[bi];
               zzH[bi]=0;
               ai=i;
               tai=Time[i];
              }
            else if (countBarl>countBarh && countBarl>countBarExt && countBarl>minBars)
              {
               lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;
               fs=2;countBarl=0;countBarh=0;countBarExt=0;
               zz[ai]=High[ai];
               zzH[ai]=High[ai];
               zzL[ai]=0;
               bi=i;
               tbi=Time[i];
              }
           }
         else
           {
            if (lLast_m==0 && hLast_m==0)
              {
               countBarl=0;countBarh=0;countBarExt=0;
              }

            // Òåíäåíöèÿ âîñõîäÿùàÿ
            if (fs==1)
              {
                  if (countBarl>countBarh && countBarl>countBarExt && countBarl>minBars) // Îïðåäåëÿåì òî÷êó ñìåíû òåíäåíöèè.
                    {
                     // çàïîìèíàåì çíà÷åíèå íàïðàâëåíèÿ òðåíäà fs íà ïðåäûäóùåì áàðå
                     ai0=iBarShift(Symbol(),0,tai); 
                     bi0=iBarShift(Symbol(),0,tbi);
                     fs=2;
                     countBarl=0;

                     zz[ai]=High[ai];
                     zzH[ai]=High[ai];
                     zzL[ai]=0;
                     bi=i;
                     tbi=Time[i];

                     lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;

                     for (n=0;countBarExt<minBars;n++) 
                       {
                        if (lLast<Low[i+n+1] && hLast>High[i+n+1]) {countBarExt++; countBarh++; lLast=Low[i+n+1]; hLast=High[i+n+1]; hLast_m=High[i];}
                        else break;
                       }

                     lLast=Low[i]; hLast=High[i];

                    }
              }

            // Òåíäåíöèÿ íèñõîäÿùàÿ
            if (fs==2)
              {
                  if (countBarh>countBarl && countBarh>countBarExt && countBarh>minBars) // Îïðåäåëÿåì òî÷êó ñìåíû òåíäåíöèè.
                    {
                     // çàïîìèíàåì çíà÷åíèå íàïðàâëåíèÿ òðåíäà fs íà ïðåäûäóùåì áàðå
                     ai0=iBarShift(Symbol(),0,tai); 
                     bi0=iBarShift(Symbol(),0,tbi);
                     fs=1;
                     countBarh=0;

                     zz[bi]=Low[bi];
                     zzL[bi]=Low[bi];
                     zzH[bi]=0;
                     ai=i;
                     tai=Time[i];

                     lLast=Low[i]; hLast=High[i]; lLast_m=0; hLast_m=0;

                     for (n=0;countBarExt<minBars;n++) 
                       {
                        if (lLast<Low[i+n+1] && hLast>High[i+n+1]) {countBarExt++; countBarl++; lLast=Low[i+n+1]; hLast=High[i+n+1]; lLast_m=Low[i];}
                        else break;
                       }

                     lLast=Low[i]; hLast=High[i];

                    }
              }
           } 
        } 
       if (i==0)
         {
          if (hLast<High[i] && fs==1) // Òåíäåíöèÿ íà òåêóùåì áàðå âîñõîäÿùàÿ
            {
             ai=i; tai=Time[i]; zz[ai]=High[ai]; zzH[ai]=High[ai]; zzL[ai]=0;
            }
          else if (lLast>Low[i] && fs==2) // Òåíäåíöèÿ íà òåêóùåì áàðå íèñõîäÿùàÿ
            {
             bi=i; tbi=Time[i]; zz[bi]=Low[bi]; zzL[bi]=Low[bi]; zzH[bi]=0;
            }
//===================================================================================================
      // Íóëåâîé áàð. Ðàñ÷åò ïåðâîãî ëó÷à ZigZag-a

          ai0=iBarShift(Symbol(),0,tai); 
          bi0=iBarShift(Symbol(),0,tbi);

          if (bi0>1) if (fs==1) {for (n=bi0-1; n>=0; n--) {zzH[n]=0.0; zz[n]=0.0;} zz[ai0]=High[ai0]; zzH[ai0]=High[ai0]; zzL[ai0]=0.0;}         
          if (ai0>1) if (fs==2) {for (n=ai0-1; n>=0; n--) {zzL[n]=0.0; zz[n]=0.0;} zz[bi0]=Low[bi0]; zzL[bi0]=Low[bi0]; zzH[bi0]=0.0;}

          if (ti<Time[1]) i=2;

         }
//====================================================================================================

     }
//--------------------------------------------
   
return(0);
}
//+------------------------------------------------------------------+

Comments