Home / Forex & Currency Exchange News / Trader’s Tech – Writing Your Own EA Part 71 – Arrays

Trader’s Tech – Writing Your Own EA Part 71 – Arrays

trader's tech If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

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[10][10]). 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[10]; 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.


Click Here For Original Source Of The Article

About Louie Lewis

Louie Lewis
Successful forex trading starts with you first. Then comes the actual strategies and techniques. I have been involved with forex and forex trading for a few years now. It is a wonderful way to build wealth. The learning never stops and I want to help others along their journey into this wonderful market of opportunity.

Check Also

“Oops, They Did It Again”

“Oops, They Did It Again”

See “a classic capitulation to a rising trend” By Elliott Wave International History often repeats. So, one way to make an educated guess about the future is to look at the past. With that in mind, let me share with you a valuable observation from our market analysts: Overseas investors tend to jump into U.S. […]

Leave a Reply

Your email address will not be published. Required fields are marked *