In this video, we are going to create such a moving text object, it will output the current low price for the current candle. 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 and here you want to click on file, new file, Expert Advisor from Template, continue. I will call this file simple moving text object. Click on Continue, continue and finish.
Now you can delete everything above the ontick function and let’s also remove the two comment lines here, we start by using MQL rates to create an array for the price info. MQLRates is a structure that stores information about the price, the volumes and the spread, and it will also store some time information. Now we are going to use ArraySetAsSeries to sort the array from the current candle downwards. And with CopyRates we can fill the array for the current symbol on the chart and the currently selected period on that chart.
And we want to copy the values for three candles and store them in the price info array. But let’s actually do it this way. Basically, it’s the same. If you mark that and press F1, you will see that you can exchange this function and use underscore period instead, these two values will automatically calculate the right symbol on the chart and the currently selected period on that chart. Now let’s move on and create an object. That is done by using object create for the current symbol on the chart. The object name will be moving text. This is the object type.
If you mark that and press F1, you will see that there are lots of different object types that you can use and we are going to use this one. It is able to output values and text. The last three parameters here are set to zero. Those are for the current subwindow. In case we use window zero, it will be the window where we draw the candles.
And the next parameters are for the date, time and the price. I didn’t provide this information here because we are going to use object_move later on to move our moving text to the point in time and the price that we want to use. But before we do that, we want to change a few properties. In this case, we use ObjectSetInteger for the Chart ID zero and the object that is called moving text. We want to change the object property font size and set it to eight.
Let’s repeat that, this time for the font color. The first two parameters are the same. This time we want to change object property color and set it to CLR red. If you mark that one and press F1, you will see that you can have a lot of different colors for several objects and to shift the form to vertical alignment, we are going to use ObjectSetDouble this time for object property angle and we will set that value to ninety degrees.
And of course we need to set that text value that is done by using object set string for the object property text and the text will be low followed by a colon. And this expression here will give us the low price for the current candle.
Now our object is finished, so let’s move it. And to do that we use object move. The first parameters are the same. The third parameter is for the alignment, or I think they call it the anchor point. We are going to move our object to the point in time where we print candle zero and we want to align it to the low price of candle zero.
That’s the current candle and that’s basically it. If you have no idea what all the code here does or if this was too fast for you, maybe you would like to become a Premium Course member. You can do that on our website. And this was another suggestion from a Premium Course member. So if you are a Premium Course member and have any ideas for a video like this one, please let me know. Let’s click on View Toolbar.
And press the compile button, or you can also use F7, we don’t have any errors, and in that case, we can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, you want to click on View Strategy, Test or Press Control and R. Please pick the new file simple moving text object.ex5, mark the option for the visual mode. You might need to drag the strategy tester panel a little bit to see all the options. Here is the Visual mode, now let’s click on Start to start your test. Here we are. This is our little text object, it is moving and you can see that it changes the values while it’s moving.
And then this little video, you have learned how to create a moving text object and you have coded it yourself with a few lines of MQL5 code.