MQL5 TUTORIAL BASICS – 36 SIMPLE MOUSE CLICK EVENT

video

In this video, we want to talk about chart events, in this case about a mouse click. When I click on the chart nothing happens, now let’s drag the little expert advisor, a simple mouse click on that chart, click on ok and when I click into the chart now I will get this little message box here, it says that the chart was clicked, the x value is two hundred and sixty three, and the y value is three hundred and eleven, let’s close that box and click again in the lower right corner and now you see that the values are much bigger, these numbers here are actually the mouse coordinates, so let’s find out how to fire a chart event with mql5.
To do that, please click on the little button here or press F4, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple mouse click, click on continue, continue, and finish, now you can delete everything above the on tick function and the two comment lines here and most of the time we would do anything inside of the on tick function, this one is also an event, it’s called whenever the price changes on the chart and that’s the most interesting event, but we can also use other events like in our case a mouse click. Actually a mouse click belongs to what is called a chart event because it happens when we click anywhere on the chart and this chart event takes a few parameters.
We use the expression const here for constant or not changeable, the first parameter is an int parameter, it is called id, and it belongs to the event id, the second one is a long parameter – this is not a typo here, it’s actually the way to pass a reference instead of a value – the parameter is called lparam for long parameter, the next one is a reference and this one is called dparam for double parameter, because it’s a double value and the last one is called sparam because it’s a string, that’s basically it.
Now what can we do with the on short event function?
We can check if the id equals something, in our case we want to use chart event click and if that is the case we want to create an output.
This is a message box, it will show the words: chart was clicked, the x value, the y value, this is the title of the message box, and this is the button that we are going to use so let’s compile the code.
If you don’t have any errors here you can now click on the little button here or press F4 to go back to Met trader, and in Metatrader we simply drag the expert advisor on a live chart, click on ok and now when you click you will see the little message box here, so now you know how to get the current position of the mouse click and you have coded it yourself with a few lines of mql5 code.