MQL5 TUTORIAL BASICS – 86 SIMPLE BUY BUTTON OBJECT

video

In this video we are going to create a buy button, this is a chart object and when we click on the button we can see that we have a new open position, so let’s find out how to do that with mql5.
To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file, simple buy button, click on continue, continue and finish.
Now you can delete everything above the ontick function and let’s remove the two comment lines here.
We start with an include statement to include the file trade dot mqh, it contains the class ctrade and we are going to create an instance that is called trade, afterwards, we create two variables for the ask price and the bid price because the event handling for the button will be implemented in a separate function.
Inside of the ontick function, we start by calculating the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we make sure to automatically calculate the right number of digits behind the dot.
Now we want to create a chart object that is done by using object create for the current symbol on the chart, the object name will be buy button, the object type is obj underscore button and if you mark that and press F1 you will see that there are lots of object types that we can draw on the chart.
This is the one that we are going to use, you can place a button in front of any chart. Inside of the documentation, you will find lots of examples for dozens of properties and events but we are going to make it very simple.
The next parameter is the window, we want the button to appear in the main window where the candles are and the last two parameters here for date time and price point are not required, so we set those values to zero.
Let’s define a few properties, we use object set integer for the current symbol on the chart and the object with the name buy button, the property we want to change is the x distance from the border, let’s set the distance to two hundred pixels, I would like to set another property, everything is equal except for the parameter here that’s object property x size and that will change the width of our button. We also want to set the distance from the upper or from the lower border that is done by using object property underscore y distance.
Now let’s set the height of the button to fifty pixels and this time it’s obj prop underscore y size, we can pick one of four borders for our object that is done by using obj prop underscore corner so if you change this number your object will appear in a different corner and finally, I would like to change the text on the button – that’s what we do when we use obj prop underscore text – and I would like to have the text buy and that’s it for the button.
You could compile the code now but the button wouldn’t do anything so what’s missing is the event handling and we are going to create that now.
So let’s create a separate function here, this function is called on chart event, we have four parameters here, the first one is for the chart id and these three parameters here are used to pass some values.
Inside of the function, we first want to check if the first parameter here, id, equals chart event object click, so that would be a mouse click on an object.
And in the next step we want to check the value for the sparam that is a string value and we will call that clicked object name and now we can check if the sparam equals buy button – that’s the name of the button – and if that is true we want to create a comment statement and output the sparam value to show which button was pressed, in our case we only have one button and if it is the buy button we use trade dot buy to buy ten micro lot.
Now let’s close the if loop and the function and that’s about it.
If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in the basic video series or maybe even the premium course on our website might be interesting for you, this was once again a question from a premium course member and if you are already a premium course member and have some ideas for a video like this one just let me know.
For now, we click on view, toolbar and press the compile button or you can also press F7 on your keyboard…
I have messed up something…
And that’s this bracket here, it caused all the following errors here. Now I have removed it and I recompile the code and this time it worked. And if this is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, please open an empty chart, now go to expert advisors and look for the new file, simple buy button, please make sure that you are on a demo account, we need to use a real chart here because it will not work in the strategy tester, so let’s drag it on the chart and click on okay.
Now the buy button should appear on your chart, let’s click on view, toolbox, currently, I have one open position here, so let’s click on the buy button and here is the next one, so our little expert advisor works as designed and in this little video you have learned how to create a button object on the chart and how to do the event handling for it and you have coded it yourself with a few lines of mql5 code.