In this video we are going to create an automated rectangle at a certain time.
In our case this time is 9.30; so in a few minutes it’s the end of the opening session of the market. The rectangle should appear right now and there it is. This thing was automatically drawn at 9.30 and it encloses the highest and the lowest prices of the opening period.
Now we want to found out how to code it in MQL4.
To do that please click on the little button here or press F4 in your Metatrader. Now you should see the Metaeditor 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”.
Now everything above the “OnTick” function can be deleted and that’s also true for the two comments line here.
The First thing we need is to define is the end of the opening period (EndOfOpeningPeriod), in my case that’s at 9.30 in the morning. We use a string variable because we are going to search for this text here and we look for it in another string. This string is called: “CurrentTime” and this formula here will give you the current time in this format.
We use “TimeLocal” and “TIME_SECONDS” to get the time and with “TimeToStr” we convert the result into the string we need.
Now we want to find out if the defined time period is found or not. When it is not found we will deliver a -1, we use “StringFind” to search for the end of the opening period – that’s this string here – in the current time (CurrentTime) we have calculated here and the result will be an integer we call it: “EndOfOpeningPeriodFound” and when the search result is negative it will deliver a -1, in our case that’s equal to no.
To get the highest and the lowest price for the rectangle – we also need to calculate that – the highest candle can be found by using the iHighest” function for the current symbol on the chart and the currently selected period. We use “MODE_HIGH” here that will give us the highest price for the last 30 candles, starting at candle zero – that’s the current candle.
This line is basically is the same, but this time we use “iLowest” and “MODE_LOW” and with “Comment” we output the text: “Current Time:” followed by the time (CurrentTime) we have calculated here on our chart.
If our desired value is found in the current time (CurrentTime); so if “EndOfOpeningPeriodFound” is not -1, we first use “ObjectDelete” for the rectangle; that will delete the rectangle if it already exists maybe from the day before.
Afterwards we use “ObjectCreate” to draw a new rectangle, we use “OBJ_RECTANGLE” to tell MQL4 what type of object we need.
There are a lot of other options. For example this would create a button but we use this kind of object. It needs a few parameters: the first one is the current chart – we use a zero to draw it near on the candle chart, this stands for the time of the current candle, the rectangle should be drawn for 30 candles, the upper boarder should be drawn above highest candle and lower boarder will be drawn below the lowest candle.
Okay. That’s it.
Now you can click on the “Compile” button or press F7 on your keyboard and that worked without any errors so now we 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, select the SimpleRectangle.ex4 file, mark the visualization mode here and start you test.
Let’s speed that up. Now you can see that each day a new rectangle is drawn, you only have to wait until the current time (CurrentTime) contains the time you have defined.
Now you know how to create an object automatically on your chart – like in our case a simple rectangle that pops up – and you have coded it yourself with a few lines of MQL4 code.

Download “MQL4 TUTORIAL - SIMPLE RECTANGLE OBJECT” SimpleRectangle.txt – Downloaded 7 times – 1 KB