Help require to setup EA indicator using global variables

 

Hi

I have an EA that uses ATR, and I have modified the standard metaquotes indicator so my EA can read the data from this indicator, I am new to coding and the Atr returns a value of 0.0000, you will see I have iATR as a backup also:

double Atr(int shift){
   string gvName    = "AtrForAntony";
   string gvSetting = "EaForAtrSetting";
   string gvReqwest = "EaForAtrRqwest";
   double res;

   int theAtrPeriod;
   if(Period() >= PERIOD_M5)
      theAtrPeriod = AtrPeriod2;
   if(Period() >= PERIOD_M1 && Period() < PERIOD_M5)
      theAtrPeriod = AtrPeriod;
      
   GlobalVariableSet(gvSetting,AtrPeriod);
   if(!GlobalVariableCheck(gvName)){
      Comment("You forgot to to set indicator ATR");
      res = iATR(Symbol(),Period(),theAtrPeriod,shift);

   return(NormalizeDouble(res,Digits));}
   return(res);
  
  }

Here is the modified ATR indicator:

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
string gvName    = "AtrForAntony";
string gvSetting = "EaForAtrSetting";
string gvReqwest = "EaForAtrRqwest";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 1 additional buffer used for counting.
   IndicatorBuffers(2);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBuffer);
   SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="ATR("+AtrPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,AtrPeriod);
//----
   if(!GlobalVariableCheck(gvName)){GlobalVariableSet(gvName,0);}
   if(!GlobalVariableCheck(gvSetting)){AtrPeriod = GlobalVariableGet(gvSetting);}
   return(0);
  }
//+------------------------------------------------------------------+
void deinit(){GlobalVariableDel(gvName);return;}
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=AtrPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=High[i];
      double low =Low[i];
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=Close[i+1];
         TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
        }
      i--;
     }
//----
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   for(i=0; i<limit; i++){AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);}
   if(!GlobalVariableCheck(gvReqwest)){
      int shift  = GlobalVariableGet(gvReqwest);
      GlobalVariableSet(gvName,AtrBuffer[shift]);
   }
//----
   return(0);
  }

Can someone please show me where I am going wrong?

Thanks

Antony

Reason: