my code is not generating the correct ATR signals, can anyone help me in finding the error?

 
//+------------------------------------------------------------------+
//|                                                        MyATR.mq5 |
//|                                                 Ashwani Kushwaha |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Ashwani Kushwaha"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot ATR_Line
#property indicator_label1  "ATR_Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- input parameters
input int      ATR_PERIOD=14;
//--- indicator buffers
double         ATR_LineBuffer[],
               highLowDiff[],
               highPrevCloseDiff[],
               lowPrevCloseDiff[],
               TR[],
               T;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ATR_LineBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,highLowDiff,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,highPrevCloseDiff,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,lowPrevCloseDiff,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,TR,INDICATOR_CALCULATIONS);
 
   
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ATR_PERIOD);
   
   string short_name=StringFormat("myATR(%d)",ATR_PERIOD);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
   int start;
   
   if(rates_total<=ATR_PERIOD)
      {
         return(0);
      }
 
   if(prev_calculated==0)
      {
      
         TR[0]=0.0;
         ATR_LineBuffer[0]=0.0;
         
         for(int i=1;i<rates_total && !IsStopped();i++)
            {
               highLowDiff[i]=high[i]-low[i];
               lowPrevCloseDiff[i]=fabs(low[i]-close[i-1]);
               highPrevCloseDiff[i]=fabs(high[i]-close[i-1]);
               T=MathMax(highLowDiff[i],lowPrevCloseDiff[i]);
               TR[i]=MathMax(T,highPrevCloseDiff[i]);
            }
        
        double Sum=0.0;
        for(int i=1;i<=ATR_PERIOD;i++)
          {
             Sum+=TR[i];
             ATR_LineBuffer[i]=0.0;
          }    
          
          
          ATR_LineBuffer[ATR_PERIOD]=Sum/ATR_PERIOD;
          start=ATR_PERIOD+1;
          
          for(int i=start;i<rates_total && !IsStopped();i++)
            {
               highLowDiff[i]=high[i]-low[i];
               lowPrevCloseDiff[i]=fabs(low[i]-close[i-1]);
               highPrevCloseDiff[i]=fabs(high[i]-close[i-1]);
               T=MathMax(highLowDiff[i],lowPrevCloseDiff[i]);
               TR[i]=MathMax(T,highPrevCloseDiff[i]);
               ATR_LineBuffer[i]=((ATR_PERIOD-1)*ATR_LineBuffer[i-1]+TR[i])/ATR_PERIOD;
            }
          
      } 
   
   else
     {
         start=prev_calculated-1;
         for(int i=start;i<rates_total && !IsStopped();i++)
           {
               highLowDiff[i]=high[i]-low[i];
               lowPrevCloseDiff[i]=fabs(low[i]-close[i-1]);
               highPrevCloseDiff[i]=fabs(high[i]-close[i-1]);
               T=MathMax(highLowDiff[i],lowPrevCloseDiff[i]);
               TR[i]=MathMax(T,highPrevCloseDiff[i]);
               ATR_LineBuffer[i]=((ATR_PERIOD-1)*ATR_LineBuffer[i-1]+TR[i])/ATR_PERIOD;
               
           }
     }
     
    
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 
ASHWANI KUSHWAHA:

you will need to define what is wrong first before anyone can help...

 
First problem is you have not specified your indexing direction, both your buffers and the OnCalculate arrays to match your loop.
To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
          Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: