Problem with a simple indicator, please help

 

Hi all, i'm learning MQL5 and i have a problem with a simple indicator.

Indicator take the highest value of last 5 (you can change it by input window but it's default value) close candles for each period.

Debugging the program show that all variables are correctly calculated but when i add the indicator to the graphic, it shows wrong values.

//+------------------------------------------------------------------+
//|                                            indicadorejemplo.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Avg
#property indicator_label1  "Avg"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int Periodo=5;

//--- indicator buffers
double AvgBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,AvgBuffer,INDICATOR_DATA);
//---

   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[])
  {
  
int limit = rates_total - prev_calculated;   

for(int i=0; i<limit; i++)      
{
   double Values_to_compare[];
   ArrayResize(Values_to_compare,Periodo);  
   
   for(int j=0; j<Periodo; j++)
   {
      Values_to_compare[j] = iClose(NULL,PERIOD_CURRENT,i+j);
   }
   
   int k = ArrayMaximum(Values_to_compare,0,WHOLE_ARRAY);  
   AvgBuffer[i]=Values_to_compare[k]; 
}

   return(rates_total);
  }
//+------------------------------------------------------------------+


anybody can say me whta's wrong with my indicator?

thanks you very much and sorry about my english, it's not my native language.

 

Hi,

OK,So complete code is below by a little modification:

//+------------------------------------------------------------------+
//|                                            indicadorejemplo.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Avg
#property indicator_label1  "Avg"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int Periodo=5;

//--- indicator buffers
double AvgBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,AvgBuffer,INDICATOR_DATA);
//---
   ArraySetAsSeries(AvgBuffer,true);

   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[])
  {

   int limit=rates_total-prev_calculated;

   for(int i=0; i<limit; i++)
     {
      double Values_to_compare[];
      ArrayResize(Values_to_compare,Periodo);

      for(int j=0; j<Periodo; j++)
        {
         Values_to_compare[j]=iClose(NULL,PERIOD_CURRENT,i+j);
        }

      int k=ArrayMaximum(Values_to_compare,0,WHOLE_ARRAY);
      AvgBuffer[i]=Values_to_compare[k];
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Wow thanks you so much, i didn't know this function. Indicator works fine in mt4 but in mt5 you need to add ArraySetAsSeries.

thanks :)

 
roger686:

Wow thanks you so much, i didn't know this function. Indicator works fine in mt4 but in mt5 you need to add  ArraySetAsSeries.

thanks :)

You're welcome,

Fixed:So in MT4 the buffer arrays are in series by default,but in MT5 it should be set as series.

 
Mehrdad Jeddi: So in MT4 the arrays are in series by default,but in MT5 it should be set as series.

In MT4, only buffers are as-series by default. All other arrays including those in OnCalculate, have no default direction.

Reason: