MQL5 TUTORIAL – SIMPLE MOUSE CLICK EVENT

video
play-sharp-fill

In this video we are going to talk about simple event handling.
Event handling is something that happens when you try to interact with your chart; in our case we are going to find out how to get a simple mouse click.
Let’s left-click on the chart here and we get the text: “You pressed the mouse at the following coordinates:”, we have an “x” value and the “y” value here and when I press the mouse on the left side of the chart here you will see that we have a low “x” value because the “x” axis goes from the left to the right and the “y” axis goes from the upper to the lower border, so when I click here at the middle of the upper border we have a high “x” value and a low “y” value and you will get the highest values in the lower right corner here.
In this video we are going to find out how to code and MQL5 Expert Advisor that is able to capture the coordinates of the mouse click wherever you click on the chart.
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 select “File/ New/ Expert Advisor (template )” from template click on “Continue”, I will call this Expert Advisor “SimpleMouseClick”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function here and the two comment lines and usually we would use the “OnTick” function here because this one is triggered whenever the price changes on the chart, but in our special case we are more interested in mouse clicks so let’s change the “OnTick” to an “OnChartEvent” function. The “OnChartEvent” occurs whenever you do something on the chart, for example you could press a key, move your mouse, create or change an object and whenever that happens the “OnChartEvent” will be fired. It has a few parameters; the first one is an ID – that’s for the event ID –, the second one is of the type long; it’s called “lparam” for long, the third parameter is the “dparam” for double and the last parameter is called “sparam” for string because it’s of the string type.
Now we want to find out if the mouse button was clicked. MQL5 has a special kind of event type that is called “chartevent_click”, let’s mark that and trust the F1 key and we learn that we have “chartevent_KEYDOWN”, “chartevent_MOUSE_MOVE” or in our case “chartevent_click” for clicking on the chart and if our first parameter for the ID equals “chartevent_click” we want to use the built in comment function to output the text: “You pressed the mouse at the following coordinates:” followed by the “x” and the “y” value.
The x value will be delivered by the “lparam” and the y value will be delivered by the “dparam”.
Please don’t forget the closing brackets here and when you’re done you can click on compile or press F7 on your keyboard; that worked without any errors or warnings so now we can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we would usually click on “View/ Strategy Tester” or press CTRL and R and we are still able to pick the “SimpleMouseEvent.ex5” file here and start a test but when we click on the chart we don’t get any output, so let’s stop the test here and simply drag the Expert Advisor with the name: “SimpleMouseEvent” on your chart, let’s confirm the question if we really want to attach the “SimpleMouseEvent” on to our chart, so now you know how to create an Expert Advisor to get the coordinates for the “x” and the “y” values for any point on the chart and you have created it yourself with a few lines of MQL5 code.