MQL5 Tutorial – Simple object oriented line
It will be a horizontal line and this horizontal line moves to the highest candle within the last one hundred candles on the chart.
This could for example be a good idea to define a trading range and you could also use it to measure the difference between the highest and the lowest candle, and maybe you will also come up with other ideas.
Now how can we create an Expert Advisor that will draw our little object here and move it to the highest candle?
To do that, please click on the little button here or press F4 on your keyboard and that will open the Meta Editor and here you want to click on File, New, Expert Advisor from Template.
I will call it Simple Object Line, Continue, Continue and Finish, and now we want to remove everything above the OnTick function and the comment lines.
First we create an integer that variable will hold the highest candle number and we need an array because we have several candles and that array will be sorted downwards from the current candle by using array set as series.
We will use Copy High to fill the array with data for the last one hundred candles for the current symbol and the current period from the current candle zero for one hundred candles and this is our target array we have created here.
Let’s remove the one here and our highest candle will be calculated by using Array Maximum for our array from the current candle for one hundred candles.
Now we create another array for our price data, it will be called price information and we create it by using MQL rates that array also needs to be sorted by using array set as series and now we copy price data into the array by using copy rates for the current symbol and the current period.
The starting position will be the current candle zero, and this expression here will give us all the price data that is currently on the chart and all the price data will go into our price information array.
Now we can use Object Create for the current symbol, the object we create will have the name Line One.
It’s horizontal line and that can be created by using OBJ_HLINE.
This zero stands for the current chart window; you could also see it when you use the Tool Tip Function here.
The next parameter would be the DateTime, this is also zero and the third parameter is the price information we need.
And here we want to have the highest candle, we have calculated it here and from the highest candle we need the high.
That’s because we want to draw our horizontal line above the highest candle.
We use ObjectSetInteger OBJ_Prop_COLOR to set the color to red and we use ObjectSetInteger OBJ_Prop_WIDTH to set the width of the line to three, and at the end of the function we move the line that’s the same value here that we have used when we created the object.
Nnormally we only would create the object once, but for this simple example it is okay to use Object Create and Object Move in the same function.
Okay that’s it.
Let’s compile the code and that worked without any errors so let’s click the little button here or press F4 to go back to Metatrader, and in Metatrader you want to click on View, Strategy Tester or press control and R, select the simple objectline.ex5 file, enable the visualisation mode and click on Start.
Here is our Expert Advisor at work, let’s press minus to make it visible, and now you can see the horizontal line object actually move to the highest of the last one hundred candles.
Okay now you know how to create an object for a horizontal line and you have created it with a few lines of MQL5 code.