MQL5 TUTORIAL BASICS – 34 HOW TO CALCULATE THE HIGHEST CANDLE PRICE

video
play-sharp-fill

 

  1. Introduction to Finding the Highest Candle in MQL5 (00:00 – 00:10)
    • Introduction to the tutorial on finding the highest of the last one hundred candles using MQL5.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:10 – 00:35)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “Simple Highest Candle”.
  3. Setting Up the Code Structure and Creating Price Information Array (00:35 – 01:00)
    • Removing unnecessary code, creating an array for price data, and sorting the array from the current candle downwards.
  4. Filling the Price Information Array with Price Data (01:00 – 01:12)
    • Using ‘CopyRates’ to fill the price information array with price data from candle zero to candle one hundred.
  5. Creating Variables for the Highest Candle and High Prices Array (01:12 – 01:39)
    • Creating a variable for the highest candle and an array for the highest prices, and sorting the high prices array.
  6. Copying the Highest Prices and Finding the Highest Candle (01:39 – 02:09)
    • Using ‘CopyHigh’ to copy the highest prices and ‘ArrayMaximum’ to find the number of the highest candle.
  7. Creating and Setting Up a Horizontal Line Object (02:09 – 02:58)
    • Using ‘ObjectCreate’ to create a horizontal line object and setting its properties.
  8. Defining Object Properties and Moving the Line Object (02:58 – 03:54)
    • Setting the color and width of the line object and moving it to the new high using ‘ObjectMove’.
  9. Outputting the Highest Candle Number and Price (03:54 – 04:04)
    • Using ‘Comment’ to display the number and price of the highest candle on the chart.
  10. Compiling and Testing the Expert Advisor in MetaTrader (04:04 – 05:04)
  • Compiling the code and instructions on testing the Expert Advisor in MetaTrader using the Strategy Tester.
  1. Observing the Expert Advisor in Action (05:04 – 05:11)
  • Running the Expert Advisor in MetaTrader and observing it drawing a line above the current high of the last one hundred candles.

 

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.