How to use Zigzag values to make custom ATR indicator

 

The following code for an indicator just kept crashing / suspending (infinite loop, too many calcultion???)

Can someone suggest a better coding practise to get a better result.

Code is not tidy as it has been copy and pasted from different places and played with to see if I can get a quick overview of result / problems.

It looks like the icustom() function is not actually acessing the zigzag indicator even though I have checked that it is present, something to do with the new mt4 compiler I am not aware of perhaps?


#property  indicator_separate_window
#property indicator_buffers 2       
#property indicator_color1 Blue     
#property indicator_color2 Red      

double Buf_0[],Buf_1[];
int ExtDepth =12;
int ExtDeviation=5 ;
int ExtBackstep=3 ;
double P0,P1,P2,P3,P4,P5 ;             
//--------------------------------------------------------------------
int init()                          
  {
//--------------------------------------------------------------------
   SetIndexBuffer(0,Buf_0);         
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
//--------------------------------------------------------------------
   SetIndexBuffer(1,Buf_1);         
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1);  //SetIndexStyle(0,DRAW_LINE);
//--------------------------------------------------------------------
   return(0);                          
  }
//--------------------------------------------------------------------
int start()                         
  {
   int i,                           
       Counted_bars;                
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); 
   i=Bars-Counted_bars-1;           
   while(i>=0)                      
     {      
      Buf_0[i]=call_zigzag(i) ;
      Buf_1[i]=iATR(NULL,0,300,i) ;
      i--;                          
     }
     Comment(DoubleToStr(call_zigzag(0),Digits)) ;
//--------------------------------------------------------------------
   return(0);                          
  }
//--------------------------------------------------------------------
double call_zigzag (int shift)
{
//This function calls the custom indicator zigzag and returns it´s values. 
//THE INDICATOR ZIGZAG MUST BE IN THE FOLDER C:\...\MetaTrader 4\experts\indicators AND MUST BE NAMED "zigzag"!!!!
   double result=0.0 ;
   int n=0, i = shift;
      while(n<6 || i<=shift+1000)
      {
       if(P0>0) {P5=P4; P4=P3; P3=P2; P2=P1; P1=P0; }
       P0=iCustom(Symbol(),0,"zigzag",ExtDepth,ExtDeviation,ExtBackstep,0,shift+i);
       if(P0>0) {n+=1; }
       i++;
      }
      if (n==5) 
        {
         result=(MathAbs(P0-P1)+MathAbs(P1-P2)+MathAbs(P2-P3)+MathAbs(P3-P4)+MathAbs(P4-P4))/5 ;
        }
 return(result) ;      
}
 
      while(n<6 || i<=shift+1000)
  1. Maybe not letting i go through Bars (array range exceeded)
  2. You exit the loop when you get to 6 fractals but only return a result if there are EXACTLY 5 fractals.
  3. Change the title. You are not calculating ATR (by its definition) You are calculating average of a trend channel width (or something similar.)
  4. What is the value of
    MathAbs(P4-P4)
 

Thanks - as I said just getting a look and feel of the nature of the problem as far as the title is concerned.


I think there is a better stratergy to get at the solution but at the moment I cant see it.