You're reading...
EA development, MQL4, MQL5

MQL: Get maximum value in an array

Simple script illustrating getting the maximum value in an array. Code identical for MQL4 and MQL5.

void OnStart(){

   double testarray[5] = {0.1, 0.2, 15, 1.5, 0.01}; //sample array with 5 elements

   Print(maxValueOfArray(testarray));

}

double maxValueOfArray(double &arrayToSearch[]){ //function data type should be the same as the data type we are expecting to return

   int indexMaxValueOfArray = ArrayMaximum(arrayToSearch, 0, WHOLE_ARRAY); //ArrayMaximum returns the index of the maximum value and not the maximum value of itself

   return(arrayToSearch[indexMaxValueOfArray]); //return the value at the index determined to hold the maximum value, as obtained by ArrayMaximum()

}

MT4 output:

MT5 output:

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Archives

Visitors

Flag Counter
Advertisements
%d bloggers like this: