How to use ATR values in EA without plotting the indicator

I developed a scalping trading system, and used fixed tick intervals.

Now I am trying to upgrade for a variable interval using ATR as reference.

I am 100% blind on this.

I´ve never used indicators before and don´t now how to do it. After some reading and search, my best try was:

Thanks in advance.

nt ATR;


void OnInit() {
   
  ATR = iATR(_Symbol,PERIOD_M5,10);

}
  
void CheckATR() {

 ArraySetAsSeries(_atr, true);
  
 if( CopyBuffer(ATR,0,0,10,_atr) < 0) {Print("CopyBufferATR error =",GetLastError());}
  
 Print("the ATR is",_atr);

}

Bro,

you should always keep this inside the loop of section of your code OnStart(),OnTick or … if you are going to use this value regularly.

the indicator is not important. the way of loading should be like below :

double   ma[1];

   ExtHandle=iMA(_Symbol,_Period,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE); // can be placed inside OnInit as well
   if(CopyBuffer(ExtHandle,0,0,1,ma)!=1)
     {
      Print("CopyBuffer from iMA failed, no data");
      return;
     }

or you have to use your function CheckATR() inside these loop. i hope i understood your need properly

int ATR; <---- double ATR[];


void OnInit() {
   
  ATR = iATR(_Symbol,PERIOD_M5,10);

}
  
void CheckATR() {

 ArraySetAsSeries(_atr, true);
  
 if( CopyBuffer(ATR,0,0,10,_atr) < 0) {Print("CopyBufferATR error =",GetLastError());}
  
 Print("the ATR is",_atr[0 to 9]);

}

I don´t need to check the value for every tick. Just before opening new trader orders. that´s whay I am using an function. On my concepts, of course, i could be wrong.

thanks a lot.

Hi, thanks for the adjusments.

I will try this way.

The stdlib has some pretty decent indicator wrappers which will make your life a whole lot easier.

#include <Indicators\Indicators.mqh>
CiATR atr;
int OnInit()
{
   if(!atr.Create(_Symbol, PERIOD_M5, 10))
      return INIT_FAILED; 
   EventSetMillisecondTimer(1);
   return(INIT_SUCCEEDED);
}
void OnTick(){}
void OnTimer()
{
   atr.Refresh();
   if(atr.BarsCalculated() > 1){
      printf("ATR = %d points", 
         int(round(atr.Main(0) / _Point))
      );
      EventKillTimer();
   }
}

https://www.mql5.com/en/docs/standardlibrary/technicalindicators/oscillatorindicators/ciatr

Hi,

The stdlib fits perfectly. Forsure the best way to implement what I needed;

I did not use the OnTimer(), but works great for me.

thank you.

Rgds.