MQL5 TUTORIAL – SIMPLE RECTANGLE OBJECT

In this video we are going to create an object like the blue rectangle you see on the chart, it’s drawn automatically and it shows you the highest and the lowest candles within the last 30 candles, so you could use it for a trading range.
In this video we are going to find out how to do that with MQL5.
To create the Expert Advisor 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/ Expert Advisor (template)” from template. “Continue”. I will call this one: “SimpleRectangle”, click on “Continue”, “Continue” and “Finish” and now I can delete everything above the “OnTick” function and the two comment lines here.
The first thing we need to do is to create a variable for the highest and the lowest candle.
This will hold the number of the candles; for example in this case the highest candle is: candle 0, 1, 2, 3, 4, candle 5.
Let’s also create arrays for the highest and lowest candle prices!
When you place the mouse over any candle here you will see that for each candle we have open, high, low and close prices and these two arrays here will hold 30 prices for our candles.
Let’s use “ArraySetAsSeries” for the high array (High) and the low array (Low) to sort the array downwards from the current candle.
To fill the array with the data we need we use “CopyHigh” or “CopyLow”.
If you mark “CopyHigh” and press F1 you will learn that it gives you the highest bar prices and “CopyLow” is able to give us the minimal bar prices and we want to do that for the current symbol on the chart and the currently selected period starting from the current candle 0 for 30 candles with “CopyHigh”.
We use “CopyHigh” for the highest prices and “CopyLow” for the lowest prices. Everything else is the same and to find the highest candle we use “ArrayMaximum” this one gives you the largest element; meaning the highest of our 30 candles in our high array (High) starting from candle 0 for 30 candles and with “ArrayMinimum” we get the lowest element. In our case the lowest candle in our low array (Low) starting from candle 0 for 30 candles.
One more thing we need is to use “MqlRates” to create a price information array (PriceInformation), “MqlRates” will give us all the prices for each and every candle including the high and the low prices.
With “ArraySetAsSerious” we sort our price information array (PriceInformation) from the current candidate downwards and with “CopyRates” we fill the price information array (PriceInformation) with data for the current symbol on the chart and the currently selected period. We start at candle 0 and this expression here will give us the price information for all the bars on the chart.
Before we create an object we use “ObjectDelete” to delete any rectangles that might have been drawn before. “ObjectDelete” only takes two parameters; the first one is for the chart we are using and the second one is the name of the object.
To create our rectangle we need to use the “ObjectCreate” function. This one needs quite a few parameters here; the first one is for the currently selected chart, the second one is the name of the object – you can use any name you like – the third one is the object type.
I have used “OBJ_RECTANGLE” that will tell MQL5 what kind of object I need. There are several other object types; like “OBJ_TRIANGLE” or “OBJ_ELLIPSE” or things like a button but in our case we want to draw a rectangle.
The third parameter is for the window where it should be drawn, the 0 stands for the current main window – that’s the window with the candles – this expression will help us to draw the left border for candle 30, the upper border should be drawn on the highest price of the highest candle in our price information array (PriceInformation), the right border should be drawn for candle 0 – that’s the current candle here on the right side – and the lower border should be drawn on the lowest price of the lowest candle in our price information array (PriceInformation). In our case this candle has the lowest price.
Now that we have defined everything I would like to change the color. We can do that by using “ObjectSetInteger” on the current chart 0 it will find the rectangle object and with OBJPROP_COCLOR” we changed the color to blue.
There are lots of different colors you can choose. In my case I prefer blue and I would also like to fill the rectangle with blue color.
Basically these two lines are the same; the difference is that I have used “OBJPROP_FILL”, in this case, let’s mark it and press F1 and now you will see that there are lots of object properties you could change but in our case that’s it.
Please click on the compile button here or press F7 on your keyboard and that should work without any errors and if that is the case 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 + R, please select the file: “SimpleRectangular.ex5”, mark the visualization option here and click on “Start”.
This is our little Expert Advisor at work, we see a dynamically drawn rectangle that shows the trading range for the last 30 candles and you have coded it yourself with a few lines of MQL5 code.