Setting different ATR values for different timeframes

 

Hello

I am working on an EA that uses ATR.

On a timeframe of 30min and higher I use ATR 14

On a timeframe of less than 30min I use ATR 10

I new to coding and have been trying something like this:

double res = iATR(Symbol(),Period(),AtrPeriod,shift);
   if(Period ()>= PERIOD_M30){return(AtrPeriod == 14);}

But with little success, can anyone give me any help with this please?

There is also the case of dealing with the variable:

extern    int       AtrPeriod                       = 10;

Thanks

Antony

 

I have also tried this but even though it compiles ok the EA stops working:

double Atr(int shift){

   double res;{
   if(Period() >= PERIOD_M30){return (res == iATR(Symbol(),Period(),AtrPeriod2,shift));}
  
   if(Period() < PERIOD_M30){return (res == iATR(Symbol(),Period(),AtrPeriod,shift));}
   }

   if(Digits == 4){return(NormalizeDouble(res,4));}
  }

Thanks

Antony

 

You have several coding errors, but you have tried! Maybe try something like

double Atr(int shift){

   double res;

   int theAtrPeriod;
   if(Period() >= PERIOD_M30)
      theAtrPeriod = AtrPeriod2;
   else
      theAtrPeriod = AtrPeriod;

   res = iATR(Symbol(),Period(),theAtrPeriod,shift);
   
   return(NormalizeDouble(res,Digits)); // want you want but I don't recommend
   return(res); // what I suggest, but hey, it's your EA!
  }

Note:

1) this is just copied/typed, not compiled or tested

2) choose which of the two 'return' lines you want - without change it does what you implied you wanted

*edit* fix theAtrPeriod typo

 

Hi

Thank you for your help you have been most helpful.

Thanks

Antony

Reason: