Cubesteak Central

More Array Functions for MT4

August 23, 2006 1:59 am

One of the things that has bugged me, is the inability to search a multi-dimensional array in MT4.

So, I made a function to do so. In this case, we really want the function to give us two things:

  1. If the value exists in the array’s second dimension
  2. If it does exist, the actual coordinates of where

Ideally, you could do this with as little “usage hassle” as possible. So, the function that I’ve written does the following:

  1. It uses a 2 bucket single-dimension array to hold the coordinates of our search value. This array should be declared prior to calling the search function. The search function updates the value of the array because the array is “passed by reference”.
  2. The function itself of a boolean type, so a return of “true” means it was found, and “false” means it wasn’t.

Here is the function spec:

SearchSecondDim (ReturnArray, ArrayToSearch, ValueToSearch, SearchDirection)

Return Values:

True or False
With ReturnArray passed by reference so that after function is run:
ReturnArray [0] = ArrayToSearch first dimension location
ReturnArray [1] = ArrayToSearch second dimension location
if ReturnArray [0] or ReturnArray [1] = -1 value was not found.

NOTE: ReturnArray[2] need to be declared prior to using this function!

Example 1:

double MySearchArray[10][5];
ArrayInitialize(MySearchArray,0);
MySearchArray [7][5] = 7;
double foo[2];
bool FoundIt = SearchSeconDim (foo,MySearchArray,7,MODE_ASCEND);

This will set foo[0]==7 and foo[1]==5. Also, FoundIt will be “true”.

Example 2:

double MySearchArray[10][5];
ArrayInitialize(MySearchArray,0);
MySearchArray [7][5] = 7;
double foo[2];
bool FoundIt = SearchSeconDim (foo,MySearchArray,10,MODE_ASCEND);

This will set foo[0]==-1 and foo[1]==-1 since the value isn’t anywhere in the array’s second dimension. Also, FoundIt will be “false”.

Some Extra Goodies!

Also included in this file, is the single dimension array search function that I posted earlier (see that post for more details) and 2 simple array “printing” functions. These printing functions output to the “Experts” tab of MT4’s terminal window.

Printing a 1 dimensional array:

PrintSingleArray(double ArrayToPrint[])

Return Values:

None / Void.
A single comma separated line is printed to the Experts output window.

Printing a 2 dimensional array:

PrintDblArray(double ArrayToPrint[][])

Return Values:

None / Void.
Each comma separated “row” is printed on a new line in the Experts output window.

ENJOY!

MetaQuotes 4 Language File CSArrayFunctions.mq4
CSArrayFunctions.zip 

No Responses to “More Array Functions for MT4”

Care to comment?