Getting an indicator value

To add comments, please log in or register
linux80s
48
linux80s  

Hi, I am new to MT5 and confused.

In MT4 it was easy to get an indicator value, for example:

double atr=iATR(Symbol(),PERIOD_CURRENT,10,0);

The documentation is kinda difficult for me.

How can I get the "atr" value in MT5? How does the above code look like in MT5?

Thanks

Konstantin Nikitin
12324
Konstantin Nikitin  

iATR

CopyBuffer

input int   atr_period=14;   // period of calculation

double iATRBuffer[];
int    handle;
//+------------------------------------------------------------------+
int OnInit()
  {
   handle=iATR(_Symbol,PERIOD_CURRENT,atr_period);
//--- if the handle is not created
   if(handle==INVALID_HANDLE)
     {
      Print("Error creating \"ATR\" indicator");
      ExpertRemove();
     }
   ArraySetAsSeries(iATRBuffer,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnTick(void)
  {
   if(CopyBuffer(handle,0,0,10,iATRBuffer)!=10)
      return;
//--- work
   for(int i=0; i<10; i++)
      Print("ATR ",i,": ",iATRBuffer[i]);
  }
linux80s
48
linux80s  

Thank you for the answer.

What is the problem with my code?

I got this in the MT5: "cannot load indicator 'Average True Range' [4002]".

4002 is "Wrong parameter in the inner call of the client terminal function". I can't see what is wrong.

int     hATR;
double  valATR[];
double  atr;
int     atrPeriod=14;

int OnInit()
  {
   chartSymbol=_Symbol;    
   hATR=iATR(chartSymbol,PERIOD_CURRENT,atrPeriod);
   if(hATR==INVALID_HANDLE) return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }
  
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(CopyBuffer(hATR,0,0,1,valATR)>0) atr=valATR[0];
   return(rates_total);
  }
Konstantin Nikitin
12324
Konstantin Nikitin  
Pay attention to ArraySetAsSeries. The order of the elements of indicator arrays differs in MQL4/5.
And it is desirable to clear the array.
ZeroMemory(valATR);
if(CopyBuffer(hATR,0,0,1,valATR)>0) atr=valATR[0];
Alain Verleyen
38403
Alain Verleyen  
Konstantin Nikitin:
Pay attention to ArraySetAsSeries. The order of the elements of indicator arrays differs in MQL4/5.
And it is desirable to clear the array.

I don't see a reason why it's desirable. If the function succeeds you have the right data, if it fails you don't have to use the data.

Konstantin Nikitin
12324
Konstantin Nikitin  
Alain Verleyen:

I don't see a reason why it's desirable. If the function succeeds you have the right data, if it fails you don't have to use the data.

Why store unnecessary data? Clogged work. If they can be cleaned immediately.

Mladen Rakic
139663
Mladen Rakic  
linux80s:

Thank you for the answer.

What is the problem with my code?

I got this in the MT5: "cannot load indicator 'Average True Range' [4002]".

4002 is "Wrong parameter in the inner call of the client terminal function". I can't see what is wrong.

Unless you have "chartSymbol" defined somewhere else, then you can not compile that code

Declare the chartSymbol as string and it will work with no problems at all

To add comments, please log in or register