[MQL5] Script I got the following error message " Array out of range". Why ?

 

Hello,

[MQL5]

I try this script.

With F5, Debugging, I got the following error message " Array out of range".

Why ?

Thanks in advance,

Pierre

//+------------------------------------------------------------------+
//|                                                     TestBars.mq5 |
//|                                                   Pierre Rougier |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pierre Rougier"
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   MqlRates    PriceData[];
   ArraySetAsSeries(PriceData,true);
   Alert("First");
   Alert("Second");
   double dOpen=PriceData[1].open;
   Alert("dOpen :"+dOpen);

  }
//+------------------------------------------------------------------+
 
Pierre Rougier: With F5, Debugging, I got the following error message " Array out of range". Why ?
MqlRates    PriceData[];
   double dOpen=PriceData[1].open;

The array is zero size and you are trying to access the second element.

 
whroeder1:

The array is zero size and you are trying to access the second element.


True.

But why the array is empty ?

With Bars I got 37655.

//+------------------------------------------------------------------+
//|                                                     TestBars.mq5 |
//|                                                   Pierre Rougier |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pierre Rougier"
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   MqlRates    PriceData[];
   ArraySetAsSeries(PriceData,true);
   int iSizePriceData=ArraySize(PriceData);
   Alert("iSizePriceData :"+iSizePriceData);
   Alert("Bars(_Symbol,_Period)"+Bars(_Symbol,_Period));
//double dOpen=PriceData[1].open;
//   Alert("dOpen :"+dOpen);

  }
//+------------------------------------------------------------------+
 
Pierre Rougier:

But why the array is empty ?

You must fill the array before you can use it.

Look to the CopyRates() function.

 
Anthony Garot:

You must fill the array before you can use it.

Look to the CopyRates() function.


Thanks Anthony !

it has nothing to do with the question, but I also wonder what TERMINAL_MAXBARS is for.

//+------------------------------------------------------------------+
//|                                                     TestBars.mq5 |
//|                                                   Pierre Rougier |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Pierre Rougier"
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   MqlRates    PriceData[];
   ArraySetAsSeries(PriceData,true);

   int iSizePriceData=ArraySize(PriceData);
   Alert("iSizePriceData :"+IntegerToString(iSizePriceData));

   Alert("TERMINAL_MAXBARS :"+IntegerToString(TERMINAL_MAXBARS));
   Alert("Bars(_Symbol,_Period)"+IntegerToString(Bars(_Symbol,_Period)));

   int copied=CopyRates(_Symbol,_Period,Bars(_Symbol,_Period)-1,1,PriceData);
   double dOpen=PriceData[0].open;
   Alert("dOpen :"+DoubleToString(dOpen));
   Alert("TERMINAL_MAXBARS :"+IntegerToString(TERMINAL_MAXBARS));
   Alert("-----------------------------");

  }
//+------------------------------------------------------------------+
 
Pierre Rougier: it has nothing to do with the question,
It has everything to do with the question. You declared an array, never gave it a size or filled it, so it is empty.
Pierre Rougier: But why the array is empty ?


With Bars I got 37655.

Bars has nothing to do with your empty array. If you buy a bucket from a hard ware shop, to you think it fills itself?
 

You have to give it it's target size before working with it:

ArrayResize(....
 
Pierre Rougier:
. . . but I also wonder what TERMINAL_MAXBARS is for.

Tools > Options > Charts > "Max bars in chart"

See this post: https://www.mql5.com/en/forum/21019

max bars in chart
max bars in chart
  • 2014.02.25
  • www.mql5.com
How to get number of max bars in chart? Chart Properties?https://www.mql5.com/en/docs/constants/chartconstants/enum_chart_property...
 
Anthony Garot:

Tools > Options > Charts > "Max bars in chart"

See this post: https://www.mql5.com/en/forum/21019


Thanks Anthony

Reason: