In this video we are going to calculate the highest the lowest and the average price for a particular number of candles.
If we pause the Expert Advisor we can see that the lowest price was 1.20083, the highest price was 1.20089 and the average price is 1.20086.
Now how can we create an Expert Advisor that is able to calculate those prices?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on “File/ New/ Expert Advisor” from template (template), “Continue” (Next). I will call this file: Simple Highest Lowest and Average Price (SimpleHighestLowestAndAveragePrice.mq5). – let’s correct that – and click on “Continue” (Next), “Continue” (Next) and “Finish”.
Now you can remove everything above the “OnTick” function and the two comment lines here.
First we create a variable for the highest and the lowest candle. Both variables will be of the type: “int” because it’s the number of the candle and we will call them highest candle (HighestCandle) and lowest candle (lowestCandle).
We also need two arrays for the highest and lowest candles; one array will be called: high (High[]) and the other one: low (Low[]).
Now we need to sort both arrays downwards from the current candle by using “ArraySetAsSeries” for the high and the low array. The built-in function: “CopyHigh” gets the history data of the highest bar prices for the selected symbol and period, it takes the current currency pair and the currently selected period on the chart, and we need the data from candle 0 for 10 candles and copy the result in our high array that we have created here.
Let’s do the same for the lowest price of 10 candles but this time the result will be stored in the array with a name: “Low”. To calculate the highest candle of 10 candles we use the function: “ArrayMaximum”, it searches the largest element in our array and for the highest candle we look into the high array (High), we start to look for the highest value from candle zero and we want to find it within the first 10 values. For the lowest value we do exactly the same but this time we use the function: “ArrayMinimum”, it does the same as “ArrayMaximum” but in this case it finds the lowest element in our numeric array.
Let’s create another array for the price information (PriceInformation[]) of the candles, that can be done by using “MqlRates” because this one stores the information about prices volumes and spread, let’s also sort this one from the current to the oldest candle by using “ArraySetAsSeries” and with “CopyRates” we fill our price information array (PriceInformation[]) for the current symbol and the currently selected period from the current bar zero for all the bars that are available, and the result will be stored in the price information array (PriceInformation[]).
It would be enough to calculate it for 10 bars here but maybe you want to write down this expression because it will give you all the bars that are on your chart.
Now the highest price is the highest point of the highest candle in our price information array (PriceInformation[]) and the lowest price is the lowest price from the lowest candle in our price information array (PriceInformation[]).
To get the average price we add the highest price and the lowest price divided by 2 and with “NormalizedDouble” and a 6 we make sure that we get one digit more otherwise the average price would be rounded. With 6 instead of 5 digits it’s easier for us to see if it is calculated correctly and last but not least we use the “Comment” function to output the highest price, the average price, and the lowest price each value in a new line.
Now if you’re done you can press F7 to compile your code or if you have enabled the toolbar you could click on the compile button here, and if it works without any errors and warnings you can click here or press F4 to go back to Metatrader.
In Metatrader you want to click on “View/ Strategy Tester” or press CTRL and R; let’s select the new ex5 file: SimpleHighestLowestAndAveragePrice, mark the option for the visualization here and start your test.
Now you should see the highest, the average and the lowest price appearing on the left corner of your chart. You can use these values to make intelligent trading decisions and you have coded the calculation part yourself with a few lines of MQL5 code.

Download “MQL5 Tutorial - Simple Highest Lowest and Average Price”

SimpleHighestLowestAndAveragePrice.txt – Downloaded 500 times – 1.34 KB