If you’re new to this series and want to check it out from the beginning, you can find that here.
In Part 70 we continued of built-in array functions in our MQL4 tutorial. In this installment, we'll begin our discussion of multi-dimensional arrays.
For the last several installments we've been talking about arrays. More specifically, single-dimension arrays. A single-dimension array has a single subscript or index following the array name contained in square brackets. A two-dimension array has two indices following the array name (for example, Prices[10,10].) A two-dimension array can be thought of like a spreadsheet. You can think of the two indices like the rows and columns of a spreadsheet.
Likewise, a three-dimension array (Prices[10,10,10]) can be thought of like a stack of spreadsheets, rows, columns, and sheet numbers. Four-dimension arrays (Prices[10,10,10,10]) are conceptually a little more difficult. MQL4 allows a maximum of four-dimensional arrays. For the purposes of this article series, we will stick to one- and two-dimensional arrays.
All elements of the array must be of the same declared variable type. In other words, all the elements must be a string, integer, double or some other MQL4-recognized variable type. You can manipulate your way around this by declaring the array as type string and then using the string conversion functions (DoubleToStr(), StrToDouble() and TimeToStr(), StrToTime(), etc.) to convert the stored string elements to whatever variable type you require.
The indices of a multi-dimensional array can be notated in two different ways, according to your preference. You can separate the indices using a comma (Prices[10,10]), or each index can be contained in its own set of square brackets (Prices). I prefer the commas since they are quicker and easier to type, but as I say, use your personal preference.
One very nice feature about arrays is the ability to write the array to and read the array from a file with a single command:
int h; string Pair; double Prices[10,100,6]; int PairElementsWritten, PriceElementsWritten, PairElementsRead, PriceElementsRead; h = FileOpen("TestFile.bin",FILE_BIN|FILE_WRITE); PairElementsWritten = FileWriteArray(h,Pair,0,ArraySize(Pair)); PriceElementsWritten = FileWriteArray(h,Prices,0,ArraySize(Prices)); FileClose(h); h = FileOpen("TestFile.bin",FILE_BIN|FILE_READ); PairElementsRead = FileReadArray(h,Pair,0,ArraySize(Pair)); PriceElementsRead = FileReadArray(h,Pair,0,ArraySize(Prices)); FileClose(h);
This code snippet has written and read hundreds of variables with just a few lines of code. Very efficient.
Once again, this brings us to a convenient stopping point. In the next trader's tech installment, we'll talk about using arrays to store price data from the chart.
Thanks for your attention and please follow me on Twitter and LinkedIn.
Thank you for reading!
Please leave a comment below if you have any questions about Trader's Tech!
Also, please give this topic a 5 star if you enjoyed it!