zig zag indicator with trendline on each top and bottom .with a problem

 

hi

i have a zig zag indicator  that iv modified   to draw trendline on each top and bottom  to the right  over a specific number of bars  ,   , but with one problem . the trend line seem to be short when there  are a holiday (Saturday and Sunday )….the code are attached and a picture  which show the problem

p.s  1.( the problem seem to be in future and current bars  as i notice that there  are no short trendlines in the past bars).

       2. this part is my modification on the code:

            int yyy2;
     int xxx2=Barsahead12+Barsahead12*PeriodSeconds();
      
      int start_bar_shift2=iBarShift(Symbol(),0,t22);
  int end_bar_shift2=start_bar_shift2-Barsahead12;
    if(end_bar_shift2>=0)
   yyy2=Time[end_bar_shift2];
  else
    yyy2= t22+xxx2;
     
       
       ObjectDelete (ObjName2);
       ObjectCreate(ObjName2,OBJ_TREND,0,t22,LabelPos2,yyy2,LabelPos2);//
      ObjectSet(ObjName2,OBJPROP_RAY, false); 
      ObjectSet(ObjName2,OBJPROP_COLOR,TrendLineColor2);
             
        int LineTime2=Barsahead12;
       ObjectDelete (LineTime2);
       
       ObjectCreate("LineTime2", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("LineTime2",DoubleToStr(LineTime2,0),14, "Arial",Aqua);
      ObjectSet("LineTime2", OBJPROP_XDISTANCE,234 );
      ObjectSet("LineTime2", OBJPROP_YDISTANCE, 19);
      ObjectSet("LineTime2", OBJPROP_CORNER, 1);

any help is appreciated and thanks 

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link    "https://www.mql5.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Yellow
//---- indicator parameters
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;
extern color TrendLineColor2=Yellow;
extern color TextLabelColor=White;
extern int Barsahead12= 45;

//---- indicator buffers
double ZigzagBuffer2[];
double HighMapBuffer2[];
double LowMapBuffer2[];
int level2=3; // recounting's depth 
bool downloadhistory2=false;
double Pip2;
bool Started2=False;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
//---- indicator buffers mapping
   SetIndexBuffer(0,ZigzagBuffer2);
   SetIndexBuffer(1,HighMapBuffer2);
   SetIndexBuffer(2,LowMapBuffer2);
   SetIndexEmptyValue(0,0.0);

//---- indicator short name
   IndicatorShortName("ZigZag2("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
   if(Digits==3 || Digits==5) Pip2 = 10*Point;
   else Pip2 = Point;   
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
    DelObj2();
    return(0);
  }

void DelObj2()
  {
   string ObjName2;
   for(int i2=ObjectsTotal()-1; i2>=0; i2--)
   {
     ObjName2 = ObjectName(i2);
     if(StringFind(ObjName2,"ZZLabel2",0)>=0)
       ObjectDelete(ObjName2);
   }
   return;
  }

int start()
  {
   int i2, counted_bars2 = IndicatorCounted();
   int limit2,counterZ2,whatlookfor2;
   int shift2,back2,lasthighpos2,lastlowpos2;
   double val2,res2;
   double curlow2,curhigh2,lasthigh2,lastlow2;

   if (counted_bars2==0 && downloadhistory2) // history was downloaded
     {
      ArrayInitialize(ZigzagBuffer2,0.0);
      ArrayInitialize(HighMapBuffer2,0.0);
      ArrayInitialize(LowMapBuffer2,0.0);
     }
   if (counted_bars2==0) 
     {
      limit2=Bars-ExtDepth;
      downloadhistory2=true;
     }
   if (counted_bars2>0) 
     {
      while (counterZ2<level2 && i2<100)
        {
         res2=ZigzagBuffer2[i2];
         if (res2!=0) counterZ2++;
         i2++;
        }
      i2--;
      limit2=i2;
      if (LowMapBuffer2[i2]!=0) 
        {
         curlow2=LowMapBuffer2[i2];
         whatlookfor2=1;
        }
      else
        {
         curhigh2=HighMapBuffer2[i2];
         whatlookfor2=-1;
        }
      for (i2=limit2-1;i2>=0;i2--)  
        {
         ZigzagBuffer2[i2]=0.0;  
         LowMapBuffer2[i2]=0.0;
         HighMapBuffer2[i2]=0.0;
        }
     }
      
   for(shift2=limit2; shift2>=0; shift2--)
     {
      val2=Low[iLowest(NULL,0,MODE_LOW,ExtDepth,shift2)];
      if(val2==lastlow2) val2=0.0;
      else 
        { 
         lastlow2=val2; 
         if((Low[shift2]-val2)>(ExtDeviation*Point)) val2=0.0;
         else
           {
            for(back2=1; back2<=ExtBackstep; back2++)
              {
               res2=LowMapBuffer2[shift2+back2];
               if((res2!=0)&&(res2>val2)) LowMapBuffer2[shift2+back2]=0.0; 
              }
           }
        } 
      if (Low[shift2]==val2) LowMapBuffer2[shift2]=val2; else LowMapBuffer2[shift2]=0.0;
      //--- high
      val2=High[iHighest(NULL,0,MODE_HIGH,ExtDepth,shift2)];
      if(val2==lasthigh2) val2=0.0;
      else 
        {
         lasthigh2=val2;
         if((val2-High[shift2])>(ExtDeviation*Point)) val2=0.0;
         else
           {
            for(back2=1; back2<=ExtBackstep; back2++)
              {
               res2=HighMapBuffer2[shift2+back2];
               if((res2!=0)&&(res2<val2)) HighMapBuffer2[shift2+back2]=0.0; 
              } 
           }
        }
      if (High[shift2]==val2) HighMapBuffer2[shift2]=val2; else HighMapBuffer2[shift2]=0.0;
     }

   // final cutting 
   if (whatlookfor2==0)
     {
      lastlow2=0;
      lasthigh2=0;  
     }
   else
     {
      lastlow2=curlow2;
      lasthigh2=curhigh2;
     }
   for (shift2=limit2;shift2>=0;shift2--)
     {
      res2=0.0;
      bool OutOfForLoop2=False;
      switch(whatlookfor2)
        {
         case 0: // look for peak or lawn 
            if (lastlow2==0 && lasthigh2==0)
              {
               if (HighMapBuffer2[shift2]!=0)
                 {
                  lasthigh2=High[shift2];
                  lasthighpos2=shift2;
                  whatlookfor2=-1;
                  ZigzagBuffer2[shift2]=lasthigh2;
                  res2=1;
                 }
               if (LowMapBuffer2[shift2]!=0)
                 {
                  lastlow2=Low[shift2];
                  lastlowpos2=shift2;
                  whatlookfor2=1;
                  ZigzagBuffer2[shift2]=lastlow2;
                  res2=1;
                 }
              }
             break;  
         case 1: // look for peak
            if (LowMapBuffer2[shift2]!=0.0 && LowMapBuffer2[shift2]<lastlow2 && HighMapBuffer2[shift2]==0.0)
              {
               ZigzagBuffer2[lastlowpos2]=0.0;
               lastlowpos2=shift2;
               lastlow2=LowMapBuffer2[shift2];
               ZigzagBuffer2[shift2]=lastlow2;
               res2=1;
              }
            if (HighMapBuffer2[shift2]!=0.0 && LowMapBuffer2[shift2]==0.0)
              {
               lasthigh2=HighMapBuffer2[shift2];
               lasthighpos2=shift2;
               ZigzagBuffer2[shift2]=lasthigh2;
               whatlookfor2=-1;
               res2=1;
              }   
            break;               
         case -1: // look for lawn
            if (HighMapBuffer2[shift2]!=0.0 && HighMapBuffer2[shift2]>lasthigh2 && LowMapBuffer2[shift2]==0.0)
              {
               ZigzagBuffer2[lasthighpos2]=0.0;
               lasthighpos2=shift2;
               lasthigh2=HighMapBuffer2[shift2];
               ZigzagBuffer2[shift2]=lasthigh2;
              }
            if (LowMapBuffer2[shift2]!=0.0 && HighMapBuffer2[shift2]==0.0)
              {
               lastlow2=LowMapBuffer2[shift2];
               lastlowpos2=shift2;
               ZigzagBuffer2[shift2]=lastlow2;
               whatlookfor2=1;
              }   
            break;               
         default: OutOfForLoop2=True; 
        }
        if(OutOfForLoop2) break;
     }
     //////////////////
     if(counted_bars2<1)
     {
       DelObj2();     
       limit2 = Bars-1;
     }
     else limit2 = 0;
     for(i2 = limit2; i2>=0; i2--)
     {
     int k2 = i2;
     double zz2;
     double d12=0,d22=0,d32=0;
     datetime t12=0,t22=0,t32=0;
     while(k2<Bars-2)
     {
       zz2 = ZigzagBuffer2[k2];
       if(zz2!=0)
       {         
         d12 = d22; d22 = d32; d32 = zz2;
         t12 = t22; t22 = t32; t32 = Time[k2];
       }
       if(d12>0) break;
       k2++;  
     }
     if(d12==0) continue;
     double LabelPos2;
     int ib2 = iBarShift(NULL,0,t12);
     if(d12>d22) LabelPos2 = NormalizeDouble(High[ib2]*iATR(NULL,0,10,ib2),Digits);//+0.5
     else LabelPos2 = Low[ib2];
     string ObjName2 = "ZZLabel_Leg12";
     if(ObjectFind(ObjName2)<0)
      ObjectCreate(ObjName2,OBJ_LABEL,0,0,LabelPos2);
      // ObjectCreate(ObjName,OBJ_TREND,t1,LabelPos);
     else
       ObjectMove(ObjName2,0,t12,LabelPos2);
     ObjectSetText(ObjName2,DoubleToStr(MathAbs(d22-d12)/Pip2,1),10,"Arial",Yellow);       
     //////////////////////
     if(i2==0)
     {
     LabelPos2 = Close[0];
     ObjName2 = "ZZLabel_Bid2";
     int rOffset2 = 5*Period()*60;
     if(ObjectFind(ObjName2)<0)
       ObjectCreate(ObjName2,OBJ_TEXT,0,Time[0]+rOffset2,LabelPos2);
     else
       ObjectMove(ObjName2,0,Time[0]+rOffset2,LabelPos2);                   
     ObjectSetText(ObjName2,DoubleToStr(MathAbs(Close[0]-d12)/Pip2,1),10,"Arial",Yellow);     
     }
     //////////////////////     
     ib2 = iBarShift(NULL,0,t22);
     if(d22>d32) LabelPos2 = NormalizeDouble(High[ib2]+0.00005*iATR(NULL,0,10,ib2),Digits);//0.5
     else LabelPos2 = Low[ib2];
     ObjName2 = "ZZLabel2"+t12;
     if(ObjectFind(ObjName2)<0)
    // {
     
     
     
                int yyy2;
     int xxx2=Barsahead12+Barsahead12*PeriodSeconds();
      
      int start_bar_shift2=iBarShift(Symbol(),0,t22);
  int end_bar_shift2=start_bar_shift2-Barsahead12;
    if(end_bar_shift2>=0)
   yyy2=Time[end_bar_shift2];
  else
    yyy2= t22+xxx2;
     
       
       ObjectDelete (ObjName2);
       ObjectCreate(ObjName2,OBJ_TREND,0,t22,LabelPos2,yyy2,LabelPos2);//
      ObjectSet(ObjName2,OBJPROP_RAY, false); 
      ObjectSet(ObjName2,OBJPROP_COLOR,TrendLineColor2);
             
        int LineTime2=Barsahead12;
       ObjectDelete (LineTime2);
       
       ObjectCreate("LineTime2", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("LineTime2",DoubleToStr(LineTime2,0),14, "Arial",Aqua);
      ObjectSet("LineTime2", OBJPROP_XDISTANCE,234 );
      ObjectSet("LineTime2", OBJPROP_YDISTANCE, 19);
      ObjectSet("LineTime2", OBJPROP_CORNER, 1);
      
       
       
   //  }     
    

     }
     
   //////////////////

   return(0);
  }
//+------------------------------------------------------------------+
Files:
untitledz.jpg  474 kb
 
abd-:

hi

i have a zig zag indicator  that iv modified   to draw trendline on each top and bottom  to the right  over a specific number of bars  ,   , but with one problem . the trend line seem to be short when there  are a holiday (Saturday and Sunday )….the code are attached and a picture  which show the problem

p.s  1.( the problem seem to be in future and current bars  as i notice that there  are no short trendlines in the past bars).

       2. this part is my modification on the code:

any help is appreciated and thanks 

You need your code to take weekend days into account. There are few market hours on Saturday and Sunday, but PeriodSeconds() doesn't know that.

You can either incorporate Sunday bars into Monday or specifically omit trendlines on Saturday/Sunday by using DayOfWeek() as a filter:

DayOfWeek - Date and Time - MQL4 Reference

DayOfWeek - Date and Time - MQL4 Reference
DayOfWeek - Date and Time - MQL4 Reference
  • docs.mql4.com
DayOfWeek - Date and Time - MQL4 Reference
 
Ryan L Johnson #:

You can either incorporate Sunday bars into Monday or specifically omit trendlines on Saturday/Sunday by using DayOfWeek() as a filter:

DayOfWeek - Date and Time - MQL4 Reference

hi

thanks for your help: i have a question befor traying DayOfWeek() function.

you said: either incorporate Sunday bars into Monday:

1.what about Saturday?


am asking you because am not a programmer but traying to write some simple code

thanks for you

 
abd- #:
1.what about Saturday?

That depends... Do you have price data appearing on Saturday?

 
Ryan L Johnson #:

That depends... Do you have price data appearing on Saturday?

Ryan L Johnson #: I don't see any price data on Saturday in my platform

so I must take in to consideration adding to days to my code   86400x2 second ........as I understand the idea

thanks for you 

zig zag indicator with trendline on each top and bottom .with a problem
zig zag indicator with trendline on each top and bottom .with a problem
  • 2025.02.04
  • abd-
  • www.mql5.com
hi i have a zig zag indicator that iv modified to draw trendline on each top and bottom to the right over a specific number of bars , , but with on...