Basics of Price Extrema Finding in MQL4

A lot of automated strategies filter the entry signals based on market extrema – either High or Low of the specified lookback window. We can use several different ways how to get the value of the H/L.

Finding the H/L of the specified interval is rather very straightforward. Whenever you know the desired interval index, you access the array where all the H/L prices are stored. The indexing of intervals in MQL4 is in reverse order, i.e., the current bar has the index of [0], and the more you travel to history, the higher index will be.


Example 1: High of the previous bar

The code above will be working only on the current TimeFrame and Symbol(). If you need to find H/L with a specific index on a different TimeFrame or a different symbol, you can use another function.

iHigh(str symbol, int timeframe, int shift)
iLow(str symbol, int timeframe, int shift)

Example 2: Low of the current Daily bar on EURUSD
iLow("EURUSD", PERIOD_D1, 0)

This is a useful way how to find the price extremes when you focus on a specific / fixed interval. However, a lot of strategies are focusing on finding these extremes on a custom or dynamic lookback window. You may need to find the highest or lowest price for the last X intervals.

Visual representation of the Highest and Lowest price for the last X=10 intervals

The solution for that will be the following:

int X = 10;					// Number of intervals to find the extrema from
double HighX = High[0];				// Declare High variable and assign current high value
double LowX = Low[0];				// Declare Low variable and assign current low value

// Loop through all the desired intervals
for (int i = 1; i <= X; i++) {

	// New highest value found? Then assign the value to the variable
	if (High[i] > HighX) HighX = High[i];
	// New lowest value found? Then assign the value to the variable
	if (Low[i] < LowX) LowX = Low[i];

// Now you can use the HighX and LowX variables as desired

If you experienced any problems with the price extrema finding or have some practical feedback, let me know in the comments.

Still, have no trading account yet? Open an account at one of my trusted brokers suitable for algorithmic trading completely for free and start testing today!

This post contains affiliate links. If you use these links to register at one of the trusted brokers, I may earn a commission. This helps me to create more free content for you. Thanks!