Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Request for modification of zig zag indicator. (https://www.forexfactory.com/showthread.php?t=176155)

deedeed Jun 16, 2009 2:17am | Post# 1

Request for modification of zig zag indicator.
 
2 Attachment(s)
Thank You for taking your time out for checking out my request.

I have attached the mql of the normal zig zag indicator that comes with metatrader.

I needed a couple of modifications with this piece of code.



Details of the two images attached:-
zig zag 1 - I have attached the zig zag indicator into the chart here.

zig zag 2 - The indicator draws the line but I needed some text and values printed on it.

As you can clearly see if the swing high was higher than the previous swing high I have typed HH meaning Higher High and the value of the swing high.

LL - Lower Low
HL - Higher Low
SH - Same High
SL - Same Low

Can any of you expert programmers here please modify the code for me so that the swing highs and lows and the corresponding values automatically show up as soon as the indicator is attached.?

Cheers
D
Click to Enlarge

Name: zig zag 1.gif
Size: 19 KB
Click to Enlarge

Name: zig zag 2.gif
Size: 19 KB

Zen_Leow Jun 16, 2009 3:54am | Post# 2

"Modification" suggests there's something given to modify. you've not given that "something" yet.

post the zig zag indicator's codes first.

deedeed Jun 16, 2009 4:04am | Post# 3

1 Attachment(s)
Oops.... :-)

I have attached the zigzag indicator with this post.
ZigZag.mq4

Zen_Leow Jun 16, 2009 1:13pm | Post# 4

1 Attachment(s)
here. not sure if it'll work though.
ZigZag_wLabel.mq4

deedeed Jun 16, 2009 1:35pm | Post# 5

2 Attachment(s)
here. not sure if it'll work though.

Thank You Zen,

It works but when the same high happens it shows LH where as it must show SH.

And the same problem with "Same Low"

Cheers
D
Click to Enlarge

Name: samehighproblem.gif
Size: 14 KB
Click to Enlarge

Name: samelowproblem.gif
Size: 11 KB

deedeed Jun 16, 2009 11:43pm | Post# 6

Thanks once again Zen
 
fixed it.... that was a simple correction :-)
PHP Code:
//+------------------------------------------------------------------+
//|                                                       Zigzag.mq4 |
//|                 Copyright  2005-2007, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright  2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black
//---- indicator parameters
extern int ExtDepth=2;
extern int ExtDeviation=2;
extern int ExtBackstep=1;
extern color LabelColor Blue;
extern int Labeldistance 30;
//---- indicator buffers
double ZigzagBuffer[];
double HighMapBuffer[];
double LowMapBuffer[];
int level=3// recounting's depth 
bool downloadhistory=false;

int PipFactor 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
// Cater for fractional pips
   
if (Digits == || Digits == 5)
   {
      
PipFactor 10;
   }
   
ObjectsDeleteAll(0,OBJ_TEXT);
   
IndicatorBuffers(3);
//---- drawing settings
   
SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
   
SetIndexBuffer(0,ZigzagBuffer);
   
SetIndexBuffer(1,HighMapBuffer);
   
SetIndexBuffer(2,LowMapBuffer);
   
SetIndexEmptyValue(0,0.0);

//---- indicator short name
   
IndicatorShortName("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   
return(0);
  }
  
  
int deinit()
  {
    
ObjectsDeleteAll(0,OBJ_TEXT);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   
int icounted_bars IndicatorCounted();
   
int limit,counterZ,whatlookfor;
   
int shift,back,lasthighpos,lastlowpos;
   
double val,res;
   
double curlow,curhigh,lasthigh,lastlow;

   if (
counted_bars==&& downloadhistory// history was downloaded
     
{
      
ArrayInitialize(ZigzagBuffer,0.0);
      
ArrayInitialize(HighMapBuffer,0.0);
      
ArrayInitialize(LowMapBuffer,0.0);
     }
   if (
counted_bars==0
     {
      
limit=Bars-ExtDepth;
      
downloadhistory=true;
     }
   if (
counted_bars>0
     {
      while (
counterZ<level && i<100)
        {
         
res=ZigzagBuffer[i];
         if (
res!=0counterZ++;
         
i++;
        }
      
i--;
      
limit=i;
      if (
LowMapBuffer[i]!=0
        {
         
curlow=LowMapBuffer[i];
         
whatlookfor=1;
        }
      else
        {
         
curhigh=HighMapBuffer[i];
         
whatlookfor=-1;
        }
      for (
i=limit-1;i>=0;i--)  
        {
         
ZigzagBuffer[i]=0.0;  
         
LowMapBuffer[i]=0.0;
         
HighMapBuffer[i]=0.0;
        }
     }
      
   for(
shift=limitshift>=0shift--)
     {
      
val=Low[iLowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(
val==lastlowval=0.0;
      else 
        { 
         
lastlow=val
         if((
Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(
back=1back<=ExtBackstepback++)
              {
               
res=LowMapBuffer[shift+back];
               if((
res!=0)&&(res>val)) LowMapBuffer[shift+back]=0.0
              }
           }
        } 
      if (
Low[shift]==valLowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0;
      
//--- high
      
val=High[iHighest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(
val==lasthighval=0.0;
      else 
        {
         
lasthigh=val;
         if((
val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(
back=1back<=ExtBackstepback++)
              {
               
res=HighMapBuffer[shift+back];
               if((
res!=0)&&(res<val)) HighMapBuffer[shift+back]=0.0
              } 
           }
        }
      if (
High[shift]==valHighMapBuffer[shift]=val; else HighMapBuffer[shift]=0.0;
     }

   
// final cutting 
   
if (whatlookfor==0)
     {
      
lastlow=0;
      
lasthigh=0;  
     }
   else
     {
      
lastlow=curlow;
      
lasthigh=curhigh;
     }
   for (
shift=limit;shift>=0;shift--)
     {
      
res=0.0;
      switch(
whatlookfor)
        {
         case 
0// look for peak or lawn 
            
if (lastlow==&& lasthigh==0)
              {
               if (
HighMapBuffer[shift]!=0)
                 {
                  
lasthigh=High[shift];
                  
lasthighpos=shift;
                  
whatlookfor=-1;
                  
ZigzagBuffer[shift]=lasthigh;
                  
DrawHighLabel(shift);
                  
res=1;
                 }
               if (
LowMapBuffer[shift]!=0)
                 {
                  
lastlow=Low[shift];
                  
lastlowpos=shift;
                  
whatlookfor=1;
                  
ZigzagBuffer[shift]=lastlow;
                  
DrawLowLabel(shift);
                  
res=1;
                 }
              }
             break;  
         case 
1// look for peak
            
if (LowMapBuffer[shift]!=0.0 && LowMapBuffer[shift]<lastlow && HighMapBuffer[shift]==0.0)
              {
               
ZigzagBuffer[lastlowpos]=0.0;
               
RemoveLabel(lastlowpos);
               
lastlowpos=shift;
               
lastlow=LowMapBuffer[shift];
               
ZigzagBuffer[shift]=lastlow;
               
DrawLowLabel(shift);
               
res=1;
              }
            if (
HighMapBuffer[shift]!=0.0 && LowMapBuffer[shift]==0.0)
              {
               
lasthigh=HighMapBuffer[shift];
               
lasthighpos=shift;
               
ZigzagBuffer[shift]=lasthigh;
               
DrawHighLabel(shift);
               
whatlookfor=-1;
               
res=1;
              }   
            break;               
         case -
1// look for lawn
            
if (HighMapBuffer[shift]!=0.0 && HighMapBuffer[shift]>lasthigh && LowMapBuffer[shift]==0.0)
              {
               
ZigzagBuffer[lasthighpos]=0.0;
               
RemoveLabel(lasthighpos);
               
lasthighpos=shift;
               
lasthigh=HighMapBuffer[shift];
               
ZigzagBuffer[shift]=lasthigh;
               
DrawHighLabel(shift);
               
res=1;
              }
            if (
LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0)
              {
               
lastlow=LowMapBuffer[shift];
               
lastlowpos=shift;
               
ZigzagBuffer[shift]=lastlow;
               
DrawLowLabel(shift);
               
whatlookfor=1;
               
res=1;
              }   
            break;               
         default: return; 
        }
     }

   return(
0);
  }
  
void DrawHighLabel(int shift)
{
   
string status "";
   
double currentHigh HighMapBuffer[shift];
   
double previousHigh;
   
// find the previous high
   
for (int i=shift+1i<shift+1000i++)
   {
      if (
HighMapBuffer[i] != && ZigzagBuffer[i] != 0)
      {
         
previousHigh HighMapBuffer[i];
         break;
      }
   }
   if (
currentHigh previousHigh)
   {
     
// status = "HH : "+DoubleToStr(currentHigh,Digits);
      
status "HH";
   }
   else
   if (
currentHigh == previousHigh)
   {
     
// status = "SH : "+DoubleToStr(currentHigh,Digits);
      
status "SH";
   }
   else
   {
      
//status = "LH : "+DoubleToStr(currentHigh,Digits);
      
status "LH";
   }
   
double position HighMapBuffer[shift] + (Labeldistance Point PipFactor);
   
ObjectCreate"statusLabel"+Time[shift], OBJ_TEXT0Time[shift], position);
   
ObjectSetText("statusLabel"+Time[shift], status10"Arial"LabelColor);
}

void DrawLowLabel(int shift)
{
   
string status "";
   
double currentLow LowMapBuffer[shift];
   
double previousLow;
   
// find the previous Low
   
for (int i=shift+1i<shift+1000i++)
   {
      if (
LowMapBuffer[i] != && ZigzagBuffer[i] != 0)
      {
         
previousLow LowMapBuffer[i];
         break;
      }
   }
   if (
currentLow previousLow)
   {
      
//status = "HL : "+DoubleToStr(currentLow,Digits);
      
status "HL";
   }
   else
   if (
currentLow == previousLow)
   {
      
//status = "SL : "+DoubleToStr(currentLow,Digits);
      
status "SL";
   }
   else
   {
      
//status = "LL : "+DoubleToStr(currentLow,Digits);
      
status "LL";
   }
   
double position LowMapBuffer[shift] - (Labeldistance/Point PipFactor);
   
ObjectCreate"statusLabel"+Time[shift], OBJ_TEXT0Time[shift], position);
   
ObjectSetText("statusLabel"+Time[shift], status10"Arial"LabelColor);
}


void RemoveLabel(int shift)
{
   
ObjectDelete("statusLabel"+Time[shift]);
}
//+------------------------------------------------------------------+ 

Zen_Leow Jun 17, 2009 11:05am | Post# 7

that fix actually won't work 100% because of the way the code works in the earlier portions. but since its enough for you, then I don't need to probe further. =)


© Forex Factory