In this video, we are going to find out how to calculate the highest of the last one hundred candles and we are also going to draw a horizontal line above the highest price, so let’s find out how to do that for Metatrader 4.
To get started please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple highest candle, click on continue, continue and finish, now you can delete everything above the on tick function and let’s also remove the two comment lines here.
To calculate the highest of the last one hundred candles we use the function i highest, it will return the maximum value over a specific number of bars and it takes a few parameters, we want to do it for the current symbol, so we use underscore symbol, the second parameter is underscore period, that will automatically calculate the current period for the selected chart, we use mode underscore high – all in capital letters – the count parameter is for the number of bars that we are interested in.
In our case we want to calculate the highest of one hundred candles, started from candle number zero –that’s the current candle –, we start with object delete, that’s a little bit strange here because we have not created the object so far but once we have done that the old instance will be deleted each time the on tick function is called, please remember on tick is the function that will be called whenever the price changes, so let’s actually create the object that is done by using object create, the name of the object will be line, I use obg hline, let’s mark that and press F1 and you will see all the different types of objects that can be created, we have lots of them.
After the object type we need to define the window where the object should be drawn, window zero stands for the main chart, this is the first anchor point, we are using candle zero and the line should be drawn on the high of the highest candle that we have calculated here.
Finally, let’s create a chart output by using the comment function, this will output the text: “The highest of the last one hundred candles”, “is candle number” followed by the number of the highest candle and in a new line we want to see: “The highest price is” and the calculated price.
That’s almost it, please click on the compile button or press F7, we don’t have any errors and we don’t have any warnings, so in my case everything is working, if this was too fast for you or if you don’t know what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course could be interesting for you, you can find that on the website but if the compilation worked for you, you now want to click on the little symbol here or press F4 to go back to Metatrader.
And in Metatrader we want to click on view, strategy tester or press control and r, please pick the new file simple highest candle dot ex4, please enable the visual mode here and start a test.
Here we are, the expert advisor is counting, it has drawn the line above the highest high, let’s zoom in and as the price is rising we see that the candle number is changing here, now it says the highest candle is candle number one, that’s correct, this is candle zero and this would be candle one, so our little expert advisor is working and in this little video you have learned how to calculate the highest of one hundred candles and how to draw a line for the highest price and you have coded it yourself with a few lines of mql4 code.