In this video we are going to create an expert advisor that is able to draw an object on the chart like in this case a rectangle, so let’s find out how to do that with mql5.
To get started please click on the little icon 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 chart object, click on continue, continue and finish.
Now you can delete everything above the on tick function and we also remove the two comment lines here.
We start by creating two variables for the highest candle and the lowest candle, we also need two arrays, the first one is called high, the second one low.
We use array set as series to sort the arrays from the current candle downwards and afterwards we use copy high for the current symbol on the chart and the currently selected period to fill the array called high from candle zero up to candle thirty with the high values, that’s why we use copy high and to get the lowest values we use copy low.
You could also use something like copy open or copy close if you would like to get the open and close prices.
To get the highest candle we use array maximum. This is also a function that is included in mql5 and it looks into our high array and finds the highest value from candle zero to candle thirty.
So let’s repeat that for the lowest candle, this time we use array minimum to look inside the array called low and find the lowest value from candle zero to candle thirty.
We also need to have some price information, so we use mql rates, that is a structure that stores information about prices, volumes and spreads.
We call the array price information and once again we use array set as series to sort it from the current candle downwards and now we use copy rates to fill our array for the current symbol on the chart and the currently selected period. By the way, this is the old way to do that, it is equal to this notation, so let’s actually exchange that.
We will start at the current candle zero and this expression here is able to get all the candles on the chart but it consumes a lot of calculation power so if your system is a little weak you could exchange that with a number of candles that is fixed and now we have some price information that is saved in our price array.
The next step is to use object delete to delete the former rectangles for the current symbol on the chart, the object name is rectangle, it will not delete anything when you run the expert advisor for the very first time.
And afterwards, we use object create for the current symbol on the chart to create an object called rectangle, therefore we use the object type obj underscore rectangle and if you mark that and press F1 you will see that there are a lot of other object types. You could, for example, use a button or a label, each of these object types has different settings and values, in our case, the next parameter is zero that is the main window because the rectangle is drawn here where the candles are.
For oscillators, you would see a separate window below.
The object should be drawn from candle thirty to candle zero and we want the upper border to be on the highest high of the highest candle. The lower border will be on the lowest low of the lowest candle within these thirty candles.
Now we use object set integer on the main window. To set the color for the object with the name rectangle we use object property color for this and set the color to blue and to fill the rectangle object we use object property fill to also use blue to fill the rectangle and that’s about it.
Now if this was a too fast for you or if you don’t know what all the code here does, you maybe want to watch one of the other videos in this basic video series first or maybe even the premium course might be interesting for you. You can find that one on our website.
But for now, please click on the compile button or press F7, this worked without any errors and if this is the case you can click on this little symbol 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 chart object dot ex5, mark the visual mode here and start a test.
And here we are! Our object is drawn and in this little video, you have learned how to automatically draw objects on your chart and you have coded it yourself with a few lines of mql5 code.