In Part 70 we continued our discussion of MQL4’s built-in array functions. 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 spread sheet. 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 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 it’s 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. Next time 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.
Latest posts by admin (see all)
- The Secret of Taking Profit and Why it is Important - September 20, 2017
- Fading the Momentum in Forex Trading - September 14, 2017
- Trend Line Drawing With Fractals - September 8, 2017
Winner’s Edge Trading, as seen on: