ATR indicator for EA returning 0.0000

 

Hi

I have an EA that uses ATR, because it relies on accuracy I found using the indicator to provide the values is best so here is the code in the EA:

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);
  
  }

And here is the modified ATR indicator:

#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);
  }

The problem is that I am getting an ATR value of 0.0000 for the current and then 1.0000 for the previous candle.

Can anyone suggest what is causing this?

Thanks

Antony

 
   if(!GlobalVariableCheck(gvName)){
      Comment("You forgot to to set indicator ATR");
      res = iATR(Symbol(),Period(),theAtrPeriod,shift);

   return(NormalizeDouble(res,Digits));}
your the the iATR value only when GlobalVariableCheck==false.


BTw, why using such a complicated way trough Globalvariables?
 

Hi

I find the iATR can be slightly inaccurate, and this causes my EA to miss around 3-4 valid trades per weeks, for example the Atr indicator may show 0.0022 and the iAtr will show it as 0.0023, this doesn't seem like a lot but in my strategy it makes all the difference.

So I would like the EA to read data from the Atr idicator attached to the chart, if I forget to add the indicator then the EA can resort back to iATR is back up.

But right now when using the Atr idicator with the EA, it just returns 0.0000 and then 0.0001 for the previous candle.

Thanks

Antony

 

It seems that in this function you never read the value from the custom indicator. I don't see a GlobalVariableGet() anywhere. And you never set the shift value to the global variable. (note that the indicator will update only at the next tick AFTER the variables have been set)

Also the "normal" way would be to get the custom indicator value trough the iCustom() function, this has also the advantage that you don't need to manually add the indicator.

Another even better solution is to calculate the Atr value inside the EA, if you know the math and don't need a visual drawing this might be the best solution of all.

 

Hi

I will have to look into this as I am new to coding, many things are still beyond my knowledge, and any examples will be very helpful.

Thanks

Antony

 
I have read quickly trough your modified ATR and the standart ATR, the calculations is the same, i assume you copy'ed from there. What modifications have you made in order to be more reliable as you said?
 
zzuegg:
I have read quickly trough your modified ATR and the standart ATR, the calculations is the same, i assume you copy'ed from there. What modifications have you made in order to be more reliable as you said?


Hi

The Atr idicator is just the standard one that comes with metatrader, the calculations are the same but it seems they round off different, please see image:

You can see the difference.

Thanks

Antony

 

i don't see any rounding operations in any of the indicators, try printing the values with DoubleToStr(value,5);

Also i can't see the code in your indicator which is responsible for printing the values in your indicator window...

 

Hi

This is the code I use in the EA for displaying the values:

string d_str(double in){return(DoubleToStr(in,Digits+1));}

Then for the Atr vales etc, I use this:

  comm=comm+"\nCurrent Rsi: "+d_str(Rsi(0))+" ";
comm=comm+"\nPrevious Rsi: "+d_str(Rsi(0+1))+" ";
  comm=comm+"\nCurrent Macd main: "+d_str(Macd(0,0))+" Signal: "+d_str(Macd(0,1))+"";
   comm=comm+"\nPrevious Macd main: "+d_str(Macd(0+1,0))+ " Signal: "+d_str(Macd(0+1,1))+" ";
    comm=comm+"\nCurrent Atr: "+d_str(Atr(0))+" ";
     comm=comm+"\nPrevious Atr: "+d_str(Atr(0+1))+" ";

Because I use this EA accross many pairs, to get the correct readings by using the Digits+1

At the point shown in the attached image, I traded that pattern manually and, it was a valid trade, but because of issue with the Atr the EA completely missed it. This has been apparent for a few recent trades.

For a while I found just dividing the iAtr by 1.0000000046511560159, corrected the issue on most trades, but there is still a difference, not a lot as you can see by the number but enough to cause major problems for my EA.

As i have traded manually for some time I know that the Standard Atr indicator is correct and the EA is incorrect, this is why I would prefer my EA to read from the standard Atr.

The previous bars shown is just a mod I did to aid manual trading.

Thanks

Antony

Reason: