[MQL5] Script 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.
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); } //+------------------------------------------------------------------+
But why the array is empty ?
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("-----------------------------"); } //+------------------------------------------------------------------+
With Bars I got 37655.
You have to give it it's target size before working with it:
ArrayResize(....
. . . 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
- 2014.02.25
- www.mql5.com
Hello,
[MQL5]
I try this script.
With F5, Debugging, I got the following error message " Array out of range".
Why ?
Thanks in advance,
Pierre