Can anyone please explain what is wrong here?

 

Hi

I have an EA that uses ATR, but because of issues in accuracy I am having with the iATR I have decided to use the metaquotes indicator for the EA to reference, but iATR as backup incase the indicator is not attached to the same chart.

The metaquotes indicator is returning a value of 0.0000 for the EA, can anyone see what is wrong here please?

Here is the code for the EA, but I am a newbie at coding:

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

Thanks

Antony

 

hey Im a newbie to but I can see that

if(Period() >= PERIOD_M1 && Period() < PERIOD_M5) is the same as just writing if(Period() == PERIOD_M1 )

what kind of variable is theAtrPeriod and theAtrPeriod2 ? i think global variable may be trying to operate with wrong variable ? cant comment on this until I know.

What accuracy issues are you having with iATR ? It probably isnt iATR but your code. If you attach the whole EA code, it might be easier to see the problem

 

I think its to do with the GlobalVariableGet but I am new to this so I am unsure.

 
MickGlancy:

hey Im a newbie to but I can see that

if(Period() >= PERIOD_M1 && Period() < PERIOD_M5) is the same as just writing if(Period() == PERIOD_M1 )

what kind of variable is theAtrPeriod and theAtrPeriod2 ? i think global variable may be trying to operate with wrong variable ? cant comment on this until I know.

What accuracy issues are you having with iATR ? It probably isnt iATR but your code. If you attach the whole EA code, it might be easier to see the problem


Hi

The different atr periods are for use with different timeframes. I will sort the condition statements for the time periods.

The iATR seems to round value up, so for example, the iATR may show a value of 0.0022 and then the metaquotes will show 0.0021 which is the correct reading. My EA does rely on accurate ATR which is why I would like to use the values of the metaquotes ATR.

Unless there is a different way to does this?

Antony

 
tonyjms2005:


Hi

The different atr periods are for use with different timeframes. I will sort the condition statements for the time periods.

The iATR seems to round value up, so for example, the iATR may show a value of 0.0022 and then the metaquotes will show 0.0021 which is the correct reading. My EA does rely on accurate ATR which is why I would like to use the values of the metaquotes ATR.

Unless there is a different way to does this?

Antony

You can set the time frames like this

double iATR1 = iATR (symbol(), PERIOD_M1, int period, 0 )

double iATR2 = iATR (symbol(), PERIOD_M1, int period,1)

double iATR3 = iATR (symbol(), PERIOD_M5, int period,0)

double iATR4 = iATR (symbol(), PERIOD_M5, int period, 1 )

and as I have done, name the variables differently. Its a bit easier to work with I think.

why are you using GlobalVariableSet ?

and I think the digits variable may perform differently in different settings, I would just use the number 5 there.

"The iATR seems to round value up,"

It does not round up behind the scenes, so if you assume the value and use it elsewhere, and you dont pass it through other variables unneccesarily it should work. it is difficult to see what you are doing without seeing all the code. I have a few ATR EA's you could look through for ideas if you pm me your email address I will send them

 

Hi

The site wont let me post the EA as it is too long, but the Atr posted above is how it is calulated then used in condition statements in the EA.

If I divide the iATR by 1.0000000046511560159 it will output the correct value, so that number is the diffence.

Thanks

Antony

 

"why are you using GlobalVariableSet ?"

I am new to coding so I have done thsi by looking at example, the actual code means nothing to me :(

Thanks

Antony

 
MickGlancy:

You can set the time frames like this

double iATR1 = iATR (symbol(), PERIOD_M1, int period, 0 )

double iATR2 = iATR (symbol(), PERIOD_M1, int period,1)

double iATR3 = iATR (symbol(), PERIOD_M5, int period,0)

double iATR4 = iATR (symbol(), PERIOD_M5, int period, 1 )

You can not get bar zero values in the tester for other pairs/TFs.
 
Besides that, iATR calls built-in indicator, while your modified indicator must be called with iCustom,
 
Drave:
Besides that, iATR calls built-in indicator, while your modified indicator must be called with iCustom,


Will I have to use iCustom to call the metaquotes atr indicator that will be attached to the same chart?

Thanks

Antony

 

The iAtr does work but I want the EA to call on the metaquotes indicator that is attached to the same chart as the EA, so what is the problem with the current global variables setup that I have?

Thanks

Antony

Reason: