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

Ideal ZigZag - MetaTrader 5 Script | Forex Indicator Download - MT4/MT5 Resources - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |

Ideal ZigZag - indicator for MetaTrader 5

This is a simple but very fast zigzag.

No pauses or false spikes. Peak retrieval has been time optimized.

Ideal ZigZag - indicator for MetaTrader 5

ZigZag is drawn based on the channel principle. Channel width can be defined in points (IdealZZ) or percentage (IdealZZP)

Peak search:

Input integer channel width = 100 ; #property Indicator_chart_window

Date and time last time; integer ZZ handle; //+--------------------------------------------------------------------------------+
//|Custom indicator initialization function |
//+------------------------------------------------------------------+
Blank initialization ()
  {
  last time = 0 ;
  ZZ handle = custom ( _symbol , period (), "Ideal ZZ" , channel width);
  } //+------------------------------------------------------------------+
//| GetValue |
//+------------------------------------------------------------------+
boolean GetValue( double_dir , integer_bar , integer_previous_bar , double_ &peak, integer &peakBar, datetime_peak_time_peak_time , const_date_time &T [ ])
  {
if (directory < 0 )
    {
doublet [ 1 ];
if ( 0 >= copybuffer (ZZhandles, 2 , bars , 1 , t)) return false ;
integer i = array B search (T, ( datetime )t[ 0 ]);

if (i==previous column)
        {
if ( 0 >= copy_buffer (ZZ_handle, 2 , bar+ 1 , 1 , t)) return false ;
        I = array B search (T, ( date time ) t [ 0 ]);
        }

double v[ 1 ];
if ( 0 >= copybuffer (ZZhandles, 1 , i , 1 , v)) return false ;

if (v[ 0 ]== empty_VALUE )
        {
if ( 0 >= copy_buffer (ZZ_handle, 2 , bar+ 1 , 1 , t)) return false ;
        I = array B search (T, ( date time ) t [ 0 ]);
if ( 0 >= copybuffer (ZZhandles, 1 , i , 1 , v)) return false ;
        }

      peak=v[ 0 ];
      peakbar=i;
      Peak time = ( date time )t[ 0 ];
    }
Else if (directory > 0 )
    {
doublet [ 1 ];
if ( 0 >= copybuffer (ZZhandle, 3 , bar , 1 , t)) return false ;
integer i = array B search (T, ( datetime )t[ 0 ]);

if (i==previous column)
        {
if ( 0 >= copy_buffer (ZZ_handles, 3 ,bar+s="number">1, 1 ,t)) return false ; i = array B search (T, ( datetime )t[ 0 ]); } double v[ 1 ]; if ( 0 >= copy_buffer (ZZ_handle, 0 , i, 1 , v)) return false ; if (v[ 0 ] == null_VALUE ) { if ( 0 >= copy_buffer (ZZ_handle, 3 , bar + 1 , 1 , t)) return false ; i = array B search (t, ( datetime )t[ 0 ]); if ( 0 >= copy buffer (ZZ handle, 0 , i, 1 , v)) return false ; } peak = v[ 0 ]; peakbar = i; peaktime = ( datetime )t[ 0 ]; } else { return ( false ); } return ( true ); } //+------------------------------------------------------------------+ //| GetValue | //+------------------------------------------------------------------+ Blank SetPoint( stringName , DoublePrice , DateTimeTime ) { Object Create ( 0 , Name, OBJ_ARROW , 0 , Time, Price); Object Set Integer ( 0 , Name, OBJPROP_ARROWCODE , 108 ); Object Set Double ( 0 , Name, OBJPROP_ARROWCODE , 108) , price); object set integer ( 0 , name, OBJPROP_TIME , time); } //+----------------------------------------------------------------------------------+ //|Customized indicator iteration function | //+------------------------------------------------------------------+ when calculating integer ( const integer rate_total, const integer previously calculated, const datetime &t[ ], const double&open[], const double&high[], const double & low [ ] , const double &close[], const long &tick_volume[], const long &volume[], const integer &spread[]) { if (last time == T [ 0 ]) return (rate_total); last time = T [ 0 ]; array set as series (T, true ); double dir_[ 1 ]; if ( 0 >= copy_buffer (ZZ_handle, 4 , 1 , 1 , dir_)) return rate_total; double dir = dir_[class="number">0 ]; double rdir=-dir; if (directory == empty_VALUE ) return (rate_total); double v1, v2, v3, v4, v5; integer i1, i2, i3, i4, i5; date time t1, t2, t3, t4, t5; if (get value (directory, 1 , 0 , v1, i1, t1, T) && GetValue(rdir, i1,0 ,v2,i2,t2,T) && GetValue(dir,i2,i1,v3,i3,t3,T) && GetValue(rdir,i3,i2,v4,i4,t4,T) && GetValue(directory,i4,i3,v5,i5,t5,T) ) { Set Point( "1" ,v1,t1); set point ( "2" ,v2,t2); set point ( "3" ,v3,t3); set point ( "4" ,v4,t4); set point ( "5" ,v5,t5); print (v1, " " ,v2, " " ,v3, " " ,v4, " " ,v5, " " ,i1, " " ,i2, " " ,i3, " " ,i4, " " ,i5); } else { print ( "There seems to be an error..." ); } return (rate_total); } //+----------------------------------------------------------------------------------+

This example is an indicator that marks (once per bar) the previous five peaks (including the currently formed peak).

attention! If zero bar mode is enabled, the code may not work properly

This mode can be enabled in the DrawZeroBar variable code. It is disabled by default. It is not recommended to enable it, especially when the indicator is used in Expert Advisor.

Enjoy using it. Please tell me about any exposed shortcomings.


Attachment download

📎 idealzz.mq5 (7.33 KB)

📎 idealzzp.mq5 (7.33 KB)

Source: MQL5 #925

Verification code Refresh