In this video, we are going to find the highest of the last one hundred candles so let’s find out how to do that with mql5.
To start please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple highest candle, click on continue, continue and finish, and now you can delete everything that is above the on tick function and let’s also delete the two comment lines here.
We start by creating an array for the price data that will be called price information and we create it using mql rates, afterwards we sort the array from the current candle downwards, that is done by using array set as series for the price information array, and now we use copy rates for the current symbol and the currently selected period on the chart to fill the price information array with price data from candle zero up to candle one hundred, so that’s it for the price data.
Let’s create a variable for the highest candle, that will also be called highest candle, it’s an integer because we want the number of the highest candle, we also want to create an array for the price data for the highest prices that will be called high, let’s also sort that one with array set as series and now we can use copy high for the current symbol on the chart and the currently selected period to copy the highest prices for each candle starting from candle zero up to candle one hundred, and to get the highest candle we use the function array maximum, this one searches for the largest element in the first dimension of a multi-dimensional numeric array and this one will simply give us the number of the highest candle and now that we know all that, we can use object create for the current symbol on the chart, I want to create an object and name it line one, I use obg underscore hline for our horizontal line, that’s the object type here, the next parameter is for the window, I use zero for the current chart, we want to start with candle zero and the line should be drawn on the high of the highest candle inside of our price information array.
Let’s select a color, in my case I will use magenta, object properties can be defined using object set, in our case object set integer for the current chart, the second parameter is the name of the object, in our case it’s line one, I want to set the color for the object and this is the value, clr magenta is one of the countless colors that are predefined, I can also set the property for the object width, that’s almost the same but now we simply use a value of three pixels and for each time the price changes we use object move to move our object to the new high.
Finally, we use comment to create an output that would show us the number of the highest candle and the price of the highest candle directly on our chart.
That’s about it, please click on the compile button or press F7 on your keyboard, we don’t have any errors here just two warnings for typecasting from string to number, if you don’t understand all the code here or if this is all too fast for you maybe you want to watch the other videos in this basic series or maybe even the premium course might be interesting for you, for now please click on the compile button and if you don’t have any errors you can click the little icon here or press a F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file simple highest candle dot ex5, mark the visualization mode here and start a test.
And here we are! The expert advisor is already working, it draws a line above the current high of the last one hundred candles and now you know how to code that with mql5 and you have coded it yourself with a few lines of code in the Metaeditor.