ATR with RMA smoothing

 
Hi,

I'm trying to port ATR function with RMA smoothing from mt4. Code I have is below. I'm getting '{' - function definition unexpected ATR-RMA.mq5 108 3 error but can't find out why?
I double checked { } and looks ok. I new to MQL5, have very small experience in MQL4. Could anyone help me solve this error?

//+------------------------------------------------------------------+
//|                                                      ATR-RMA.mq5 |
//|                                                           MrCode |
//|           based on ATR with Smoothing - Vitor Palmeira Abbehusen |
//|                orginal script https://www.mql5.com/en/code/36438 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

#property copyright "MrCode"
#property link      "https://www.mql5.com"
#property version   "1.0"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "ATR RMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_width1  2

//------------------------ Inputs -----------------------------------
input int Length = 14;
//------------------------ Buffers ----------------------------------
double ExtATR[];
//------------------------ Globals ----------------------------------
string Smoothing = "RMA";
bool started = false;
double alpha, lastATR;
int History;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping


   SetIndexBuffer(0,ExtATR, INDICATOR_DATA);
//ArraySetAsSeries(ExtATR, true);
//ArrayInitialize(ExtATR, 0);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Length);
//---
   string short_name="ATR("+IntegerToString(Length)+", "+Smoothing+")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
//--- Indicator initialization successful
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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<=Length)
      return(0);

//--- setting number of bars to all available bars
   History = ArraySize(close)-Length;
//--- smoothing coefficient for RMA
   alpha = 1.0/Length;
   
   int h=Bars(_Symbol,_Period)-prev_calculated-1;
   if(h > History-1)
      {
      h = History-1;
      }

//--- get the first data (SMA of true range data)
   if(!started)
     {
      double sum = 0;
      for(int i=0; i<Length; i++)
        {
         sum += trueRange(i+h);
        }
      ExtATR[h] = sum/Length;
      lastATR = ExtATR[h];
      h--;
     }

   while(h >= 0)
     {
      //--- rma
      ExtATR[h] = alpha*trueRange(h) + (1-alpha)*lastATR;
      lastATR = ExtATR[h];
      h--;
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| calculate true range                                             |
//+------------------------------------------------------------------+
double trueRange(int shift);
  {
   double t1 = high[shift] - low[shift];
   double t2 = MathAbs(high[shift] - close[shift+1]);
   double t3 = MathAbs(low[shift] - close[shift+1]);

   return (MathMax(MathMax(t1, t2), t3));
  }
//+------------------------------------------------------------------+

 

 
WIIW82: I'm trying to port ATR function with RMA smoothing from mt4. Code I have is below. I'm getting '{' - function definition unexpected ATR-RMA.mq5 108 3 error but can't find out why?
I double checked { } and looks ok. I new to MQL5, have very small experience in MQL4. Could anyone help me solve this error?

You have an extra semicolon after the declaration of the trueRange function ...

//+------------------------------------------------------------------+
//| calculate true range                                             |
//+------------------------------------------------------------------+
double trueRange(int shift);
{

However, after removing it, you will then get a whole lot of other compilations errors which you will need to fix.

'ATR-RMA.mq5'   ATR-RMA.mq5     1       1
'high' - undeclared identifier  ATR-RMA.mq5     110     16
'shift' - some operator expected        ATR-RMA.mq5     110     21
']' - semicolon expected        ATR-RMA.mq5     110     26
']' - unexpected token  ATR-RMA.mq5     110     26
'low' - undeclared identifier   ATR-RMA.mq5     110     30
'shift' - some operator expected        ATR-RMA.mq5     110     34
expression has no effect        ATR-RMA.mq5     110     28
'high' - undeclared identifier  ATR-RMA.mq5     111     24
'shift' - some operator expected        ATR-RMA.mq5     111     29
']' - unexpected token  ATR-RMA.mq5     111     34
'low' - undeclared identifier   ATR-RMA.mq5     112     24
'shift' - some operator expected        ATR-RMA.mq5     112     28
']' - unexpected token  ATR-RMA.mq5     112     33
12 errors, 1 warnings           13      2
 
Fernando Carreiro #:

You have an extra semicolon after the declaration of the trueRange function ...

However, after removing it, you will then get a whole lot of other compilations errors which you will need to fix.

Thanks for your quick answer. I'm getting on it now:) MQL4 is much easier... ;)
 
Ok, I fixed trueRange function so now looks like this:

//+------------------------------------------------------------------+
double trueRange(int shift, const double& high[], const double& low[], const double& close[])
  {
   double t1 = high[shift] - low[shift];
   double t2 = MathAbs(high[shift] - close[shift+1]);
   double t3 = MathAbs(low[shift] - close[shift+1]);

   return (MathMax(MathMax(t1, t2), t3));
  }
//+------------------------------------------------------------------+


Compiled with no errors, but when added to chart last 14 bars (default lenght of ATR) is 0, previous bars are calculated ok. In MT4 it works ok but in MT5 something is working different, any ideas what should I change?

EDIT: For live candles also ATR = 0

https://imgur.com/a/tMDQr5C
imgur.com
  • imgur.com
Imgur: The magic of the Internet
 
WIIW82 #:
Ok, I fixed trueRange function so now looks like this:


Compiled with no errors, but when added to chart last 14 bars (default lenght of ATR) is 0, previous bars are calculated ok. In MT4 it works ok but in MT5 something is working different, any ideas what should I change?

EDIT: For live candles also ATR = 0

https://imgur.com/a/tMDQr5C
OK, problems solved. Now works like a charm:)

//+------------------------------------------------------------------+
//|                                                      ATR-RMA.mq5 |
//|                                                           MrCode |
//| based on MT5 ATR and ATR with Smoothing-Vitor Palmeira Abbehusen |
//|                orginal script https://www.mql5.com/en/code/36438 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

#property copyright "MrCode"
#property link      "https://www.mql5.com"
#property version   "1.0"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_label1  "ATR RMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_width1  2
//--- input parameters
input int InpAtrPeriod=14;  // ATR period
//--- indicator buffers
double    ExtATRBuffer[];
double    ExtTRBuffer[];

int       ExtPeriodATR;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input value
   if(InpAtrPeriod<=0)
     {
      ExtPeriodATR=14;
      PrintFormat("Incorrect input parameter InpAtrPeriod = %d. Indicator will use value %d for calculations.",InpAtrPeriod,ExtPeriodATR);
     }
   else
      ExtPeriodATR=InpAtrPeriod;
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtATRBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtTRBuffer,INDICATOR_CALCULATIONS);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpAtrPeriod);
//--- name for DataWindow and indicator subwindow label
   string short_name=StringFormat("ATR(%d)",ExtPeriodATR);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
   //PlotIndexSetString(1,PLOT_LABEL,"TR");
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
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<=ExtPeriodATR)
      return(0);
   
   double alpha = 1.0/ExtPeriodATR;
   int i,start;
   
//--- preliminary calculations
   if(prev_calculated==0)
     {
      ExtTRBuffer[0]=0.0;
      ExtATRBuffer[0]=0.0;
      //--- filling out the array of True Range values for each period
      for(i=1; i<rates_total && !IsStopped(); i++)
         ExtTRBuffer[i]=trueRange(i, high, low, close);
      //--- first AtrPeriod values of the indicator are not calculated
      double firstValue=0.0;
      for(i=1; i<=ExtPeriodATR; i++)
        {
         ExtATRBuffer[i]=0.0;
         firstValue+=ExtTRBuffer[i];
        }
      //--- calculating the first value of the indicator
      firstValue/=ExtPeriodATR;
      ExtATRBuffer[ExtPeriodATR]=firstValue;
      start=ExtPeriodATR+1;
     }
   else
      start=prev_calculated-1;
//--- the main loop of calculations
   for(i=start; i<rates_total && !IsStopped(); i++)
     {
      ExtTRBuffer[i]=trueRange(i, high, low, close);
      ExtATRBuffer[i]=alpha*trueRange(i, high, low, close)+(1-alpha)*ExtATRBuffer[i-1];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| calculate true range                                             |
//+------------------------------------------------------------------+
double trueRange(int shift, const double& high[], const double& low[], const double& close[])
  {
   double t1 = high[shift] - low[shift];
   double t2 = MathAbs(high[shift] - close[shift-1]);
   double t3 = MathAbs(low[shift] - close[shift-1]);

   return MathMax(MathMax(t1, t2), t3);
  }
//+------------------------------------------------------------------+
 
Hi, I have a question. I try to use your last version of code on mql4 but it looks like a problem you have described upper still occurs. For last 14 bars, value of ATR is 0. Can you give me some tip if it works in your case? :)
 
dzeeju #: Hi, I have a question. I try to use your last version of code on mql4 but it looks like a problem you have described upper still occurs. For last 14 bars, value of ATR is 0. Can you give me some tip if it works in your case? :)

The above code is for MQL5, not MQL4. It says so in the file name "ATR-RMA.mq5".

 
Fernando Carreiro #:

The above code is for MQL5, not MQL4. It says so in the file name "ATR-RMA.mq5".

You right, my bad. Thanks!
 
dzeeju #:
You right, my bad. Thanks!


how can i add this code to mt5 please advice me

Reason: