Download MetaTrader 5

Find Last 2 iHighest

To add comments, please log in or register
Chihming Tsao
166
Chihming Tsao  

Hi Guys,

When the new candle generates, I would like to get the last one and last two iHighest.

My code is

double lasthi1=iHighest(Symbol(),0,MODE_HIGH,50,1);

double lasthi2=iHighest(Symbol(),0,MODE_HIGH,50,2);


if(Close[2]>lasthi2 && Close[1] > lasthi1)

{

return(true);

}

But it seems I can get last one iHighest but cannot get the last 2 iHighest.

Is there anybody could give me a hand ?

Thanks a lot.

Keith Watford
Moderator
12322
Keith Watford  

Maybe create an array of the last 50 Highs

Find the highest in the array and assign its value to your first variable.

Then set the value of that call in the array to 0

Scan the array for the highest again, and this time you will find the second highest.

You can use

int ArrayMaximum( double array[], int count=WHOLE_ARRAY, int start=0)
Searches for the element with maximum value. The function returns position of this maximum element in the array.
Simon Gniadkowski
Moderator
18075
Simon Gniadkowski  
chihmingtsao:

Hi Guys,

When the new candle generates, I would like to get the last one and last two iHighest.

You can do this but you need to think a little, you find the highest in the last 50 bars, then the next highest can be before this highest in time, or after this highest in time . . . so you then need to do two more iHighest() calls, one either side of the Highest and then compare the Highs of these 2 Highest bars to see which is higher . . . then you will have your 2nd highest bar . . .
To add comments, please log in or register