MQL4 TUTORIAL – SIMPLE MOUSE CLICK EXPERT ADVISOR

video
play-sharp-fill

This time we are going to talk about chart events; a chart event will happen when you interact with your chart. For example in this simple Expert Advisor that is called: Simple Mouse Click . You can click anywhere in the chart and you will see the text: ”You pressed the mouse at the following coordinates:” and the x and y coordinates here. That will happen whenever you click on the chart.
For example if you click on the left border here you will see that the x value is very low and the y value is high and if you click on the upper border here it’s the other way around; now the x value is high and y value is low. You will get the highest values here in the lower right corner and now we want to find out how we can create an Expert Advisor that is able to take the mouse coordinates and create the output on the chart.
To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window here and here you want to click on “File/ New/ Expert Advisor (template)” from template, “Continue” – I will call this one: “SimpleMouseClick”, click on “Continue”, “Continue” and “Finish”.
Let’s remove everything that is above the “OnTick” function and the two command lines here.
Usually the “OnTick” function would be triggered whenever the price changes on your chart, but in our case we want to look for mouse clicks, so let’s change “OnTick” to “OnChartEvent”.
Let’s mark that expression here and press F1 and you will see that the “OnChartEvent” is the handler of a group of chart event elements. For example if you press a key, move your mouse, create an object or change an object and whenever something like this happens it will create an event.
In our case the event will be “CHARTEVENT_CLICK” this is the event handling function and it will take a few parameters here; the first parameter is the “ID”, that’s the event ID – in our case that will be “CHARTEVENT_CLICK” –, the second parameter is of the long type, so it’s called: “lparam” for long parameter, the third parameter is a double parameter, so it’s called: “dparam”, 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 and that is the case whenever the id equals the chart event “CHARTEVENT_CLICK” and when that is true we want to use the “Comment” function to output the text: ”You pressed the mouse at the following coordinates:” followed by the x coordinate that is the value of the “lparam” and the y coordinate that is stored in the “dparam”, and that is pretty much it, so when you’re done you can click on the “Compile” button here and that did work without any errors and without any warnings and this code is exactly the same in MQL5 so if you ever wanted to switch from Metatrader 4 to Metatrader 5 the event handling is exactly the same in this case.
Now you can click on the little button here or press F4 on your keyboard to go back to Metatrader.
In Metatrader usually we would click on “View/ Strategy Tester” or press CTRL + R and you could select the “SimpleMouseClick.ex4” file here and start a new test but wherever you click you won’t get anything, so let’s stop the test here, remove the “Strategy Tester”.
Now simply select the Simple Mouse Click Expert Advisor (SimpleMouseClick) here and drag and drop it on your chart.
I will confirm that, click on “OK” and here it is!
When I click on the chart I get the output: “You pressed the mouse at the following coordinates:”, x is: 97 and y is: 113 and when I click anywhere else the values will change because every time I click the chart event is triggered and now you know how to create an Expert Advisor that can get the coordinates of each and every mouse click on your chart and you have created it with a few lines of MQL4 code.

Download “MQL4 TUTORIAL - SIMPLE MOUSE CLICK”

SimpleMouseClick.txt – Downloaded 1788 times – 550.00 B