"array out of range" error #zohair

 

My code is the following ... and I'm getting an error saying that "array out of range in '1.mq5' (23,13)" , when use it.

How can I correct this error?

Thanks a lot.

#21/06/2020 2300

//+------------------------------------------------------------------+
//|                                                extermumPoint.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
double H1SortP[], H2SortP[], HSaveP[], HExtermumP[], HExtermumRsi[];
datetime HExtermumD[],HSaveD[];
int HLP1,HLP2;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   for(int i=0; i<=200; i++)
     {
      HSaveP[i]=iHigh(Symbol(),Period(),0);
      HSaveD[i]=iTime(Symbol(),Period(),0);
     }
   for(int j=4; j<=196; j++)
     {
      if(HSaveP[j]>HSaveP[j-1] && HSaveP[j]>HSaveP[j-2] && HSaveP[j]>HSaveP[j-3] && HSaveP[j]>HSaveP[j-4] &&
         HSaveP[j]>HSaveP[j+1] && HSaveP[j]>HSaveP[j+2] && HSaveP[j]>HSaveP[j+3] && HSaveP[j]>HSaveP[j+4])
        {
         for(int q=0; q<4; q++)
           {
            H1SortP[q]=HSaveP[j+q];
            H2SortP[q]=HSaveP[j-q];
           }
         HLP1=ArrayMinimum(H1SortP,0,WHOLE_ARRAY);
         HLP2=ArrayMinimum(H2SortP,0,WHOLE_ARRAY);
        }
      if(fabs(HSaveP[j]-H1SortP[HLP1])>fabs((H1SortP[HLP1]/100)*8) &&
         fabs(HSaveP[j]-H2SortP[HLP2])>fabs((H2SortP[HLP2]/100)*8))
        {
         HExtermumP[j]=HSaveP[j];
         HExtermumD[j]=HSaveD[j];
         HExtermumRsi[j]=iRSI(Symbol(),Period(),14,j);
        }
      ObjectCreate(0,"myEllipse1",OBJ_ELLIPSE,0,HExtermumD[j],HExtermumP[j],HExtermumD[j-1],HExtermumP[j-1],HExtermumD[j+1],HExtermumP[j+1]);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 
mousa2727lotfi64: How can I correct this error?
double … HSaveP[], … HSaveD[];
int OnInit(){
   for(int i=0; i<=200; i++){
      HSaveP[i]=iHigh(Symbol(),Period(),0);
      HSaveD[i]=iTime(Symbol(),Period(),0);
  1. Stop exceeding the size of your array(s). Those two arrays have no size, yet you try to store 200 identical elements into them.
  2. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
Your error message "array out of range in '1.mq5' (23,13)" means that the error occurs on line 23, at position 13. Place your cursor at that position and you'll see what is wrong.
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 

Am having  same error kindly help fixing it

***

 
Joseph Kachimba # :

Am having  same error kindly help fixing it

***

1. Please insert code   correctly: when editing a post, click  Code  and paste your code in the popup window

2. You should not use the 'CopyLowXXXX' functions when working with the current timeframe - you should use arrays from OnCalculate:

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

Here is the code from which all garbage is thrown out:

//+------------------------------------------------------------------+
//|                                               Temp Indicator.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Label2
#property indicator_label2  "Label2"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input double InpExponenta  = 2;     // Exponenta
//--- indicator buffers
double   ExtMapBuffer1[];
double   ExtMapBuffer2[];
//---
double   m_offset=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtMapBuffer2,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,234);
   PlotIndexSetInteger(1,PLOT_ARROW,233);
//--- Set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,5);
//--- Set as an empty value 0
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---
   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<3)
      return(0);
//---
   int limit=prev_calculated-1;
   if(prev_calculated==0)
     {
      ExtMapBuffer1[0]=0.0;
      ExtMapBuffer2[0]=0.0;
      limit=1;
     }
   for(int i=limit; i<rates_total; i++)
     {
      ExtMapBuffer1[i]=0.0;
      ExtMapBuffer2[i]=0.0;
      //---
      if(open[i]<close[i] && open[i-1]<close[i-1] && open[i-2]<close[i-2] &&
         close[i-1]-open[i-1]>(close[i-2]-open[i-2])*InpExponenta)
        {
         ExtMapBuffer1[i]=high[i];
        }
      if(open[i]>close[i] && open[i-1]>close[i-1] && open[i-2]>close[i-2] &&
         open[i-1]-close[i-1]>(open[i-2]-close[i-2])*InpExponenta)
        {
         ExtMapBuffer2[i]=low[i];
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Result:

Files:
 
Vladimir Karputov #:

Here is the code from which all garbage is thrown out:

Result:

Hi Vlad thank you very much but the code still giving same error if i change time frames?

 
Joseph Kachimba # :

Hi Vlad 

I have no idea who you are referring to. Who is 'Vlad'???

 
Vladimir Karputov #:

I have no idea who you are referring to. Who is 'Vlad'???

I apologies i meant Vladimir

 
Joseph Kachimba # :

I apologies i meant Vladimir

In the code I gave - only 65 lines:


Your error message refers to some line #82.

 
Vladimir Karputov #:

In the code I gave - only 65 lines:


Your error message refers to some line #82.

The download above has 96 lines see below

***

Files:
Reason: