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



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:



No comments yet.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s



Flag Counter
%d bloggers like this: