https://mql4tutorial.com/?s=event
With MQL4 for Metatrader4 we create an Expert Advisor to handle mouse click events. Whenever we click on a chart, there is a chart event that we will use to output the X and Y coordinates.
In this video we are going to find out how to use a mouse click to bring up a message box, when I click on the chart nothing happens, let's drag the expert advisor on to a live chart, click on ok, and when I now click into the chart I will get a message box telling me the chart was clicked, we also see the coordinates here, so let's find out how to code that in mql4.
To do that please click on a 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 call this file simple mouse click, click on continue, continue and finish.
Now you can delete everything above the on tick function and let's also remove the two comment lines here. Well usually we like to use the on tick event, that's also an event but this one is automatically caused whenever the price changes, but this time we are going to use the on chart event because when I click on the chart the event is fired.
This one takes a few parameters.
The first one is for the event id, you see that we use the term const because it's a constant value – in this case, an integer value – and we are going to find out the kind of event id that is fired and check if it is a mouse click.
The second parameter is a long parameter so it's called lparam for long and this is not a mistake, it's the way that you can pass values by reference in mql4, the third parameter is called dparam because it's a double value, and the last one is called sparam because it's a string value, and that's basically it.
So what can we do?
We want to check if the id equals chart event click – all in capital letters – and if that is the case we will use a message box that will contain the text: “the chart was clicked”, it will output the x value that is what is inside of the l parameter, we also want to know the y value that's inside of the d parameter, this message here is just their heading and our message box will only have one button, that's the okay button and that's about it.
When you are done you can click on the compile button here or press F7 on your keyboard… Oh, I did something wrong because this one needs to be the event id, so let's recompile the code and this time it worked.
If this is also the case for you, you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader right now when I click on a live chart nothing is happening so let's drag the expert advisor right on to the live chart, click on ok, now you should see the little symbol here and when you click on to the chart the message box comes up right away.
By the way that wouldn't work in the strategy tester, if you would like to do something like this and use the strategy tester you would need to use the comment statement because the message box is not supported but it works on our live chart and in this little video you have learned how to get the coordinates for a mouse click on the chart and show a message box with the values and you have coded it yourself with a few lines of mql4 code.