In this video, we are going to find out how to find the lowest of the last one hundred candles and we are also going to draw a horizontal line on the chart, 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 lowest candle, click on continue, continue and finish, now you can delete everything above the on tick function and the two comment lines here.
We start by creating an array for the price data, the array will be called price information and we use mqlrates to create it, afterwards we sort the array using array set as series that will be done from the current candle downwards for our price information array and now we can use copy rates to fill our price information array with data for the current symbol on the chart and the currently selected period on that chart. We will start at candle zero – that’s the current candle – and we need the price information for one hundred candles, let’s create a variable for the lowest candle, that will be also called lowest candle, it’s an integer value because it would just give us the number of the lowest candle.
We also need an array for the price data for the lowest prices, that will be called low, let’s also sort that one by using array set as series and to fill this array with price data we use copy low.
Copy low will give us the minimal bar prices for the selected period for the current symbol on the chart and the period that is selected on that chart, once again we start with the current candle zero, we want the price data for one hundred candles and we want to store the values in our array called: low, and now we can use another function called array minimum, it searches for the lowest element in our array, the array is the first parameter, the second parameter is the start index, and the third parameter is the number of checked elements. We are going to start at the current candle zero and we will look into one hundred candles to find out which candle is the lowest one.
Now we can use that information to create an object, that is done by using object create, object create requires a lot of parameters, the first one is for the current symbol on the chart, the second one is the name of the object, in our case it’s named line one, we use obj hline, that’s for a horizontal line, you could also draw vertical lines or a lot of other objects, depending on what you are going to draw you will need different parameters, in our case we need to define in which window we want the horizontal line to appear, the value zero will draw the horizontal line on our chart, the next one is the anchor point, the first anchor point is candle zero and the line should be drawn on the lowest price of the lowest candle in our price information array. We can also use object set integer to set properties, in our case it’s the color, the first parameter is for the chart id, in our case once again this is zero because zero stands for the main chart, the name of the object is line one, the property we want to change is object property color – all in capital letters – and we want to use the color clr magenta, this is just one of several predefined colors that we can use, let’s also set the object width, this is very similar, we use objects set integer on the main chart for the object line one, this time the property we want to change is object property width and it should be three pixels.
Finally, we use object move to move the created object line, for the current symbol on a chart, for the object with the name line one, on the main chart starting at anchor point candle zero, to the low of the lowest candle in our price information array.
Object create and object move are very similar, you could also use object create and object delete and create another object for each cycle but for this simple example it should be okay, let’s add a comment statement here, I would like to see the output “The lowest candle” followed by the number of the lowest candle and I would like to see the lowest price that is calculated by looking at the low of the lowest candle in our price information array.
If you don’t know what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series first or maybe the premium course is interesting for you, you can find that one on the website, for now, if you are done, please click on the compile button, you shouldn’t get any errors here and if this is the case you can click on the little button here or press 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 lowest candle dot ex5, mark the option for the visualization here and start a test.
And this is how it should look like! We zoom into the chart and see that the line is drawn below the lowest of the last one hundred candles, so now you know how to calculate the lowest candle with mql5 and you have also learned how to draw an object below that candle, and you did it yourself with a few lines of mql5 code.