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
//+------------------------------------------------------------------+ 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
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
//+------------------------------------------------------------------+ //| 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); } //+------------------------------------------------------------------+
The above code is for MQL5, not MQL4. It says so in the file name "ATR-RMA.mq5".
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?