Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Fibonacci ZigZag - MetaTrader 5 Script - MQL5 #56619 - MT4/MT5 Resources

author EAcpu | 8 reads | 0 comments |

Fibonacci ZigZag - indicator for MetaTrader 5

Fibonacci ZigZag - indicator for MetaTrader 5

Fibonacci ZigZag - indicator for MetaTrader 5

Fibonacci ZigZag - indicator for MetaTrader 5

 # Property indicator buffer 2
#Property Indicator Chart 1
Enter double drawdown = 23.6 ; // drawdown amount
Enter double minSizeInAtrUnits= 0.0 ; //The minimum size of the wave in atr units
Enter the integer rolling AtrPeriod= 14 ; //Rolling atr period
input color color = clr dodge blue ; // wave color
Input integer width = 3 ; //Wave width
Input ENUM_LINE_STYLE style = style_entity ; //Wave style
//+------------------------------------------------------------------+
//|Custom indicator initialization function |
//+------------------------------------------------------------------+
//--- Double upWaves[],dwWaves[] for rising and falling waves ;

This upper wave array will store the high points and the dw wave array will store the lowest values

System variables:

We need to know the last wave type, start position, end position, distance from start to end in bars.

We then need the local high and local low variables and the cylindrical distance from each point.

 //--- Trace the Zigzag //--- The wave types we have are [0] None [1] Up [2] Down Integer Wave Type = 0 ;
//--- Wave price (starting price) double wave starting price = 0.0 ;
//--- Wave price (end price) double wave end price = 0.0 ;
//--- distance from the starting price (in bars) integer wave starting distance = 0 ;
//--- distance from the final price (in bars) integer wave end distance = 0 ;
//--- high price tracking double high memory = 0.0 ;
integer distance height = 0 ;
//--- low price tracking double low memory = 0.0 ;
Integer distance from low point = 0 ;
//--- scrolling atr double scrolling attribute = 0.0 ;
Integer scrolling Atrs = 0 ;

Finally, the rolling atr unit and how much are calculated

Then we create a system reset function:

 void resetSystem() {
Array filling (upWaves, 0 , array size (upWaves), 0.0 );
Array filling (dwWaves, 0 , array size (dwWaves), 0.0 );
wavetype = 0 ;
wave start price = 0.0 ;
wave end price = 0.0 ;
wave start distance = 0 ;
wave end distance = 0 ;
high memory = 0.0 ;
lowmemory = 0.0 ;
distance height = 0 ;
Distance from low point = 0 ;
scrollProperty = 0.0 ;
scrollAtrs= 0 ;
}

Standard stuff, fill the array with zeros and reset system variables.

On initialization, we set up the buffer, draw, and then call reset for the first time:

Set index buffer ( 0 , upwave, INDICATOR_DATA );
Set index buffer ( 1 ,dwWaves, INDICATOR_DATA );
plot index set double ( 0 , PLOT_EMPTY_VALUE , 0.0 );
Drawing index set integer ( 0 , drawing type , DRAW_ZIGZAG );
Drawing index set integer ( 0 , drawing line color , 0 , color);
Drawing index set integer ( 0 , drawing line width , width);
plot index set integer ( 0 , PLOT_LINE_STYLE , style);
  ResetSystem();

So let’s get straight to the math.

The first thing we need to deal with is rolling atr.

We won't do anything else until we have collected more bars than during the atr period.

The part that manages rolling atr is as follows:

 //--- manage atr rolling Atrs++;
if (rollingAtrs>rollingAtrPeriod){
Double new_portion=((High[i]-Low[i])/ _View )/(( Double ) rollingAtrPeriod);
//--- We delete the old part and add the new part rollingAtr=(rollingAtr)-(rollingAtr/(( double ) rollingAtrPeriod))+new_portion;
      }
Else if (rollingAtrs<=rollingAtrPeriod){
        rollAtr+=(high[i]-low[i])/ _view ;
if (rollingAtrs==rollingAtrPeriod){
          RollingAtr/=(( double )rollingAtrs);
//--- Start high and low memory and system high_mem=high[i];
            low_mem=low[i];
            distance height = 0 ;
            Distance from low point = 0 ;
          }
        }

Great, now another question.

The basis of this zigzag is the pullback.

But for a pullback to occur, there must be at least one wave.

But what will the first wave return? xD

To do this we will do the following:

This way we don't have a pullback as an initial wave, however, we have to start the sequence somehow.

Note that we could also choose to go with the classic fractal approach only for the first wave and then continue with the retracement.

As long as we don't have waves, this is what we do:

//--- if we don't have a tilde class yet {
//--- if we break the high instead of the low if (high[i]>high_mem&&low[i]>=low_mem){
Double new_wave_size_in_atr_units=((high[i]-low_mem)/ _view )/rollingAtr;
//--- If new wave size is valid if (new_wave_size_in_atr_units>=minSizeInAtrUnits){
//--- Start a new rising wave wave type = 1 ;
//--- The starting price is the lowest memory wave_start_price=low_mem;
            wave_start_distance=distance_from_low;
//--- The final price is a new high wave_end_price=highest price[i];
            wave end distance = 0 ;
//--- Draw waves dwWaves[i-wave_start_distance]=low_mem;
            upWaves[i]=high[i];
//--- Change the high bit high_mem=high[i];
            distance height = 0 ;
//--- Change the lowest value low_mem=low[i];
            Distance from low point = 0 ;
          }
          } 
//--- if we break the low instead of the high else if (low[i] Double new_wave_size_in_atr_units=((high_mem-low[i])/ _view )/rollingAtr;
//--- If new wave size is valid if (new_wave_size_in_atr_units>=minSizeInAtrUnits){  
//--- Start a new downgoing wave wave type =- 1 ;
//--- The starting price is the highest memory wave_start_price=high_mem;
            wave_start_distance=distance from high point;
//--- The final price is a new low wave_end_price=low[i];
            wave end distance = 0 ;
//--- draw waves upWaves[i-wave_start_distance]=high_mem;
            dwWaves[i]=low[i];
//--- Change the high bit high_mem=high[i];
            distance height = 0 ;
//--- Change the lowest value low_mem=low[i];
            Distance from low point = 0 ;
          }
          }
//--- if we both destroy other if (low[i] high_mem){
//--- change them high_mem=high[i];
            low_mem=low[i];
            distance height = 0 ;
            Distance from low point = 0 ;
          }
      }

Great. Now comes the final piece.

Here is the relevant code for the above:

//--- if we have a rising wave if (wavetype == 1 ){
//--- if the wave expands upward if (highest[i]>wave_end_price){
//--- Remove the previous final price from the array position (0.0=null) upWaves[i-wave_end_distance]= 0.0 ;
//--- put it in the new location upWaves[i]=high[i];
                wave_end_price=highest price[i];
                wave end distance = 0 ;
//--- Change the high bit high_mem=high[i];
                distance height = 0 ;
//--- Change the lowest value low_mem=low[i];
                Distance from low point = 0 ;
              }
//--- Check retracement if (low[i]  0 ){
              low_mem=low[i];
              Distance from low point = 0 ;
Double size_of_wave=(wave_end_price-wave_start_price)/ _view ;
Double size_of_retracement=(wave_end_price-low_mem)/ _view ;
if (wave size > 0.0 ){
Double retracement = (retracement size/wave size) * 100.0 ;
Double new_wave_size_in_atr_units=((wave_end_price-low_mem)/ _view )/rollingAtr;
//--- If new wave size is valid if (new_wave_size_in_atr_units>=minSizeInAtrUnits){
//--- If the retracement is significant, start a downward wave if (retracement >= retracement) {
//--- Start a new downgoing wave wave type =- 1 ;
//--- The starting price is the highest memory wave_start_price=high[i-distance_from_high];
                      wave_start_distance=distance from high point;
//--- The final price is a new low wave_end_price=low[i];
                      wave end distance = 0 ;
//--- draw waves upWaves[i-wave_start_distance]=high_mem;
                      dwWaves[i]=low[i];
//--- Change the high bit high_mem=high[i];
                      distance height = 0 ;
//--- Change the lowest value low_mem=low[i];
                      Distance from low point = 0 ;  
                                       }
                  }
                }
              }
          }

When the wave goes down, we do the opposite.

We are done and our retracement zigzag is ready.

This is the minimum size of the wave in the Zigzag 23.6% retracement and 0.0 atr units

Fibonacci ZigZag - indicator for MetaTrader 5

This is the same zigzag 3 minimum wave size (atr units)

Fibonacci ZigZag - indicator for MetaTrader 5


Attachment download

📎 fibozigzag.mq5 (13.54 KB)

Source: MQL5 #56619

MT5 MQL5 CodeBase Fibonacci ZigZag Script
Verification code Refresh