How do I fix the mql4 error "array out of range"?
How do I fix the mql4 error "array out of range"?
Loading saved threads...
mstrong · External communityPost link
External question — Stack Overflow Stack Exchange
Author: mstrong
Original post: https://stackoverflow.com/questions/66981163
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am trying to add an indicator code. While everything compiles just fine, the terminal shows the error, "array out of range".
This is the code that reads the price data from the .csv file and draws it as an indicator on the current chart.
The .csv file contains three months' price data in the form of 1 minute period.
#property indicator_buffers 1
#property indicator_color1 White
double ExtMapBuffer1[];
bool Calculated=false;
extern datetime endDateTime=D'2021.03.31 05:59:00';
int init()
{
SetIndexStyle(0,DRAW_LINE,0,1);
SetIndexBuffer(0,ExtMapBuffer1);
return(0);
}
int start()
{
int i,counted_bars=IndicatorCounted();
if (!Calculated)
{
double PriceArray[24*60*30*1]; **//issue1**
string lastDT = endDateTime;
if (!ReadCSVToArray(PriceArray,lastDT,fileName,"","\n")) Print("Reading file error: ",GetLastError());
int bars=ArraySize(PriceArray);
int firstBar = iBarShift(symbolName,PERIOD_M1,endDateTime);
ExtMapBuffer1[bars+firstBar-1]=PriceArray[0]; **//error1: array out of range**
for (i=2;i<=bars;i++)
{
ExtMapBuffer1[bars+firstBar-i]=PriceArray[i-1];
}
for (i=firstBar-1;i>=0;i--)
{
ExtMapBuffer1[i]=0;
}
Calculated=true;
}
return(0);
}
issue1: If I set the size of PriceArray as a big value for 3 month's data, such as 24
60
30*3, I could meet the compile error: "local variables is larger than 512KB"
error1: If the size of PriceArray is small, the compiling is fine but running error.
Any help is appreciated.
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/66981686
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
To correctly read a csv's of variable or unknown length, you should use the first dimension of the array for your rows and the second dimension for your columns. You would therefore have an array statement of
string data[1][5];
for a csv which contained date/O/H/L/C for example.
You can then resize your array as you loop through reading your csv file as follows:
int handle=FileOpen(FileName,FILE_CSV|FILE_READ,",");
if(handle==INVALID_HANDLE) Print("Error opening file ",GetLastError());
else
{
int row=0;
while(!FileIsEnding(handle))
{
if(row>0) ArrayResize(data,row+1);
//carry out array reading here
data[row][0]=FileReadString(handle); //date
data[row][1]=FileReadString(handle); //open
data[row][2]=FileReadString(handle); //high
data[row][3]=FileReadString(handle); //low
data[row][4]=FileReadString(handle); //close
row++;
}
FileClose(handle);
}
Note, you can not resize the 2nd dimension of a multi dimensional array so this dimension must match the structure of your csv file.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: PaulB Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/66981686 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. To correctly read a csv's of variable or unknown length, you should use the first dimension of the array for your rows and the second dimension for your columns. You would therefore have an array statement of string data[1][5]; for a csv which contained date/O/H/L/C for example. You can then resize your array as you loop through reading your csv file as follows: int handle=FileOpen(FileName,FILE_CSV|FILE_READ,","); if(handle==INVALID_HANDLE) Print("Error opening file ",GetLastError()); else { int row=0; while(!FileIsEnding(handle)) { if(row>0) ArrayResize(data,row+1); //carry out array reading here data[row][0]=FileReadString(handle); //date data[row][1]=FileReadString(handle); //open data[row][2]=FileReadString(handle); //high data[row][3]=FileReadString(handle); //low data[row][4]=FileReadString(handle); //close row++; } FileClose(handle); } Note, you can not resize the 2nd dimension of a multi dimensional array so this dimension must match the structure of your csv file.
Checking account access…