In this video we are going to create a simple dynamic label that will be placed on the chart, it shows the current bid price and as soon as the price changes here, the label will also change, so let’s find out how to create something like this in mql4.
To do that 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 from template, continue, I will use the name simple chart label, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here, we start by creating an object that is done by using object create, this is the name of the object, I will call it bid because it will show the bid price, this is the type of the object, in our case it’s a label and the last three parameters here are used to define the window, we use the main chart, we don’t have to set a time and we don’t need to set a price, so that’s it for the object create statement.
We can use object set to set the label for the bid price, it should appear in one of the four corners and we use corner one, let’s mark that and press F1, these are different types of objects, it uses this enum base corner, you could use something like corner left upper, let’s actually use corner right upper, that should be equal to one, that’s the return value for this corner here, okay you see that the bid price label is not right in the corner and that’s because we can set a distance, this is once again the name of the object, this is the setting for the x distance, and I would like to have a distance of thirty pixels to the right side of the window, and let’s also use object set to set a distance for the height, so this is sixty pixels from the top and that’s about it.
This is very short but if you don’t understand what the code does or if this was too fast for you, maybe you want to watch the other videos in this basic series or maybe the premium course that you can find on the website might be interesting for you, for now, we can click on the compile button, you shouldn’t get any errors or warnings here and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we pick an empty chart, here is the new expert advisor that we have created, if you don’t see it you can right-click anywhere and choose refresh, now let’s drag that expert advisor on the chart, click on ok and you see that something is appearing here, it’s called label, it actually shows the bid price but I would like to have a real output and to do that we need to set the text for the label, that is done by using object set text for the object with the name bid, the text will be bid price and we use this expression double to string, to convert the bid price into a string value with the right number of digits behind the dot. I use twenty for the font size, this is the font type and this is the color, so let’s re-compile the code and actually our label has changed, I can use green instead, re-compile the code and now we have a green label, let’s try another font type here, I like impact and when our re-compile and switch to Metatrader now we have a different font type and that’s about it.
So in this little video, you have learned how to create a dynamic object label right on the chart and you have coded it yourself with a few lines of mql4 code.