Why I need to compile all the time the indicator?

 

Hi, I was trying to normalize the ATR between the values 0 and 100. 0 - to the lowest value and 100 to the greatest.

I did the formula, but all the time, the indicator change to zero, and only the end start to move. I don' t have Idea why it’s happening. It only works when I compile it, but I need to do it all the time.

Can anyone help me.

//+------------------------------------------------------------------+
//|                                            Q ATR Normalizado.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Green

//----- Q ATR Normalizado Inputs -----//
extern int periodo_ATR=14;            //
//------------------------------------//

//---------- Buffers ----------//
double ATR[];                  //
double ATR_Normalizado[];      //
//-----------------------------//

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

      IndicatorBuffers(2); 

      SetIndexStyle(0, DRAW_LINE);
      SetIndexBuffer(0,ATR_Normalizado);
      
      SetIndexStyle(1, DRAW_NONE);
      SetIndexBuffer(1,ATR); 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int  limit;
//----

//---- Data ckecking ----//

   if (counted_bars==0)
      {
      limit=Bars-1;
      }
   if (counted_bars>0)
      {
      limit=Bars-counted_bars;
      } 
      
//---- Indicator Calculus ----//
      
   for(int i=limit;i>=0;i--) // Count down for one pass
    {
    
      ATR[i]=iATR(NULL,
                  0,
                  periodo_ATR,
                  i);
               
      
      ATR_Normalizado[i] = (ATR[i]-ATR[ArrayMinimum(ATR)]) / (ATR[ArrayMaximum(ATR)] - ATR[ArrayMinimum(ATR)])* 100;
                  
    
    }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
rodrigosm:

Hi, I was trying to normalize the ATR between the values 0 and 100. 0 - to the lowest value and 100 to the greatest.

I did the formula, but all the time, the indicator change to zero, and only the end start to move. I don' t have Idea why it’s happening. It only works when I compile it, but I need to do it all the time.

Can anyone help me.


Any help?
 
 ATR_Normalizado[i] = (ATR[i]-ATR[ArrayMinimum(ATR)]) 
                      / (ATR[ArrayMaximum(ATR)] - ATR[ArrayMinimum(ATR)])* 100;
This won't work because at the time of the call ATR[i-1] .. ATR[0] have not been calculated yet, so ArrayMin/Max don't return good values.
 for(int i=limit;i>=0;i--) // Count down for one pass
      ATR[i]=iATR(NULL, 0, periodo_ATR, i);
 double min = ATR[ArrayMinimum(ATR)],
        max = ATR[ArrayMaximum(ATR)],
        rng = 100./(max-min);
 for(i=limit;i>=0;i--) // Count down for one pass
      ATR_Normalizado[i] = (ATR[i]-min) * rng;