MQL4 TUTORIAL BASICS – 87 SIMPLE SELL BUTTON OBJECT

video

In this video we are going to create a button object, this is a sell button it’s actually working and we want to 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 file, expert advisor from template, continue I will call this file, simple sell button, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
Inside of the ontick function, we start by using object create, object create can create different kinds of objects, so let’s mark that and press F1 and you will see it takes a lot of parameters depending on the object type and when you click on this entry you will see that we have a lot of different objects that can be created, in our case we want to create a button, so we use obj underscore button and that button will appear on the main chart with the candles.
The first parameter is for the current currency pair on the chart, the second one is the name of the object, we use the name sell button, parameter three is for the object type, as I said we are going to use obj underscore button, parameter four is for the window that we are going to use, the one with the candles is the main window. We want our button to appear on the main window if you like to use any oscillators you might know that Metatrader is able to show oscillators in a separate window below the candles, we don’t need a value for the date time and for the price because we are not going to move the button, we did that in the video where we have created the horizontal line, for the highest price we needed to move the object, in that case, you would need to use the parameters but for the button that’s it. So now we have an object and we want to change a few properties, so we use object set integer for the current symbol on the chart and the object with the name sell button, the property I want to change is object property x distance and set that to two hundred pixels.
To change the width of the button this is very similar we use object set integer for the current symbol on the chart and the object that is called sell button, we want to change the property object property underscore x size and set it to two hundred pixels.
Here comes the next one, this time we want to change the y distance – that’s the vertical distance – and we will also use two hundred pixels here and to set the height of the button we change the property object property y size and we will use fifty pixels.
And now we want to decide in which corner we want the object to show up, so we change the property object property corner, I have used corner three in this case and of course we want to change the text for the button, this time we use object set string and change the property object property underscore text and change the text to sell.
Here I made a mistake, this one belongs to the end of the function, our object is ready but we don’t have any event handling so far, so we need to code that now.
And to do that we use the function on chart event, this one comes with mql4, the first parameter will give us the event id, the other parameters will pass some values, interesting for us is this value sparam because it will tell us the name of the object but first, we want to check if the id for the event equals chart event underscore object underscore click, if you mark that and press F1 you will see that we have different event types here and it’s easy to confuse them.
For example chart event click wouldn’t work because we need to find out if the click was for an object, so chart event object click is what we want and if that is true we also want to check if the value for the sparam that we have passed here equals sell button because that is our object name and if that is true we will use the comment statement to output the value for the sparam followed by the text, was pressed, so it should say sell button was pressed and afterwards we use order send for the current symbol on the chart to open a sell trade for ten micro lot.
Finally, let’s close the if loop and let’s close 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 once again was an idea that was emailed from a premium course member and if you are already a premium course member and have ideas for a video like this one just let me know. For now please click on the compile button here or press F7 on your keyboard, we have no errors here and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we need to open a new chart on a demo account because the strategy tester will not really work with button events and as we need to open a position we need to open a chart.
Now I use the navigator to find the new file, simple sell button dot ex4 and I will drag the expert advisor right onto the chart, here it is, please make sure that allow live trading is true and click on okay.
Now you should see the sell button object here, currently, I have no open positions, so let’s click and here is the text the sell button was pressed, we actually have a sell trade here, so the expert advisor is working as designed and in this little video you have learned how to create a button object and how to do the event handling and you have coded it yourself with a few lines of mql4 code.