Script to get value of indicator ATR

 

Hi,

 I try to program  a simple script.

 My question: How do I get the value of the ATR (average true range) indicator in a script? I've found the function "iATR" (https://www.mql5.com/en/docs/indicators/iatr) but it does not return the current ATR value. Any ideas?

 

Thank you.

Best,

Chris 

Documentation on MQL5: Technical Indicators / iATR
Documentation on MQL5: Technical Indicators / iATR
  • www.mql5.com
Technical Indicators / iATR - Documentation on MQL5
 
zubr:

Hi,

 I try to program  a simple script.

 My question: How do I get the value of the ATR (average true range) indicator in a script? I've found the function "iATR" (https://www.mql5.com/en/docs/indicators/iatr) but it does not return the current ATR value. Any ideas?

It doesn't return the ATR value,  it returns a handle . . .  read the documentation and sample code.
 
RaptorUK:
It doesn't return the ATR value,  it returns a handle . . .  read the documentation and sample code.
Do you know how to get the ATR value? I haven't found the function yet.
 

This is the simple code to get ATR Value :

int      MA_Period = 15;               // The value of the averaging period for the indicator calculation
int      Count = 5;                    // Amount to copy
int      ATRHandle;                    // Variable to store the handle of ATR
double   ATRValue[];                   // Variable to store the value of ATR    
ATRHandle = iATR(_Symbol,0,MA_Period); // returns a handle for ATR
ArraySetAsSeries( ATRValue,true );     // Set the ATRValue to timeseries, 0 is the oldest. 
if( CopyBuffer( ATRHandle,0,0,Count,ATRValue ) > 0 ) // Copy value of ATR to ATRValue
   {
      for( int i=0;i<Count;i++ )
         {
            Print(ATRValue[i]); // Print the value of the ATR
         } 
   }
 
zubr:
Do you know how to get the ATR value? I haven't found the function yet.

Yes,  I already told you what to do . . .

RaptorUK:
   read the documentation and sample code.
 
biantoro:

This is the simple code to get ATR Value :

Thank you!
 
Biantoro Kunarto:

This is the simple code to get ATR Value :

Hi I write the same code but it returns zero. every element of the array is zero
 
I am using the same code, it prints "1.7975931348623157E + 308"
it happens on the first few ticks, then it goes normal

I have iATR() returning handle 14, no error
I have CopyBuffer() returning 1, no error

How could we know if iATR is ready or not?
Any check for MQL5 like in MQL4 that 4066/4073?

I have tried this but no luck, it goes infinite loop
while(BarsCalculated(handle) <= 0) Sleep(50);

And I am thinking is the best practice like this:
int OnInit(){
  int handle = iATR(NULL, 0, 14)
  return 0;
}

int OnCalculate(...){
  if((BarsCalculated(handle) <= 0) return 0;
  double buf[];
  if(CopyBuffer(handle, 0, shift, 1, buf) > 0)
    Print(buf[0]);
  return 0;
}
 
@Yu Pang Chan #:
I am using the same code, it prints "1.7975931348623157E + 308"
it happens on the first few ticks, then it goes normal

I have iATR() returning handle 14, no error
I have CopyBuffer() returning 1, no error

How could we know if iATR is ready or not?
Any check for MQL5 like in MQL4 that 4066/4073?
I would assume that BarsCalculated() should provide the answer — Returns the amount of calculated data in the indicator buffer or -1 in the case of error (data not calculated yet)
Documentation on MQL5: Timeseries and Indicators Access / BarsCalculated
Documentation on MQL5: Timeseries and Indicators Access / BarsCalculated
  • www.mql5.com
Returns the number of calculated data for the specified indicator. Parameters indicator_handle [in]  The indicator handle, returned by the...
 
Fernando Carreiro #:
I would assume that BarsCalculated() should provide the answer — Returns the amount of calculated data in the indicator buffer or -1 in the case of error (data not calculated yet)

Thanks Fernando, you are faster than my edit:

Do you think the best practice is like this:

int OnInit(){
  int handle = iATR(NULL, 0, 14)
  return 0;
}

int OnCalculate(...){
  if((BarsCalculated(handle) <= 0) return 0;
  double buf[];
  if(CopyBuffer(handle, 0, shift, 1, buf) > 0)
    Print(buf[0]);
  return 0;
}
 
@Yu Pang Chan #Thanks Fernando, you are faster than my edit: Do you think the best practice is like this:

Probably, but if you look at the example code provided by MetaQuotes in the "Examples" folders, you will see several ways of how to use it.

Here is a sample from the example indicator "MQL5\Indicators\Examples\MACD.mq5" ...

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(rates_total<InpSignalSMA)
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(ExtFastMaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtFastMaHandle is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(ExtSlowMaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtSlowMaHandle is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }
//--- get Fast EMA buffer
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0)
     {
      Print("Getting fast EMA is failed! Error ",GetLastError());
      return(0);
     }
//--- get SlowSMA buffer
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
     {
      Print("Getting slow SMA is failed! Error ",GetLastError());
      return(0);