In this video we are going to create a simple buy button object directly on the chart, so let’s find out how to code that with mql4.
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 the two comment lines here.
Inside of the ontick function, we are going to use object create, this will create an object of a defined type.
The first parameter is for the current symbol on the chart, the second parameter is the name, the third parameter is obj underscore button if you mark that and press F1 you will see that we have lots of different object types and this is the one we are going to use, it will place a button on your chart.
The next parameter here is the window, we would like to have the button in front of the candles that’s window zero.
If you would use an oscillator like the MACD that would create a separate window below window zero, that would be window one but let’s place the button on the main chart, we have no values for the date time and for the price because we are going to set the placement for our button by setting a few properties now.
To do that we use object set integer for the current symbol on the chart, the second parameter is the name of the object, in our case, it’s the buy button, the property that we want to change is object property x distance and we will set it to two hundred pixels.
Afterwards, I would like to change the width, so let’s repeat that object set integer for the current symbol on the chart, the name of the object is the same, this time we are going to change a different property, this is called object property x size, let’s set that also to two hundred pixels.
To set the distance from the upper or the lower border we repeat the whole thing that we have done here but this time we change the property object property y distance, let’s set that also to two hundred pixels.
Of course, we also need to define the button height that’s done by using obj properties y size, and I have used fifty pixels here.
We can pick one of four corners for our button that is done by changing obj property underscore corner in my case I have chosen corner three and to make the button useful we need to set some object property text. Let’s remove the bracket here because this is for the end of the function.
Now you could actually compile the code but the button wouldn’t do anything and to change that we need to add some event handling, therefore, we need to use a predefined function that is called on chart event. It uses several parameters.
The first one is for the event id and the other parameters are used to pass values depending on the object type.
Let’s find out what kind of event happened and to do that we check if the id equals chart event underscore object underscore click. If you mark that one and press F1 you will see that we have lots of different events, for example, key down for the keyboard, this would be a click on the chart but we need this one because we want to find out if the object was clicked. That’s where we can use the sparam value because it will pass the name of the object, in our case it’s the buy button, so let’s create a comment statement that will output the value for the s parameter, so it should say buy button was pressed and afterwards we use order send for the current symbol on the chart to open a buy position for ten micro lot, so please don’t do that on your real account, use a demo account. You can create one from right inside of Metatrader and one more thing, the on chart event will not work in the strategy tester, so you actually need to drag the expert advisor onto a real chart.
Let’s close the if statement 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 actually was another suggestion from a premium course member and if you are already a premium course member and have ideas for videos like this one just send me an email but for now please click on the compile button, I don’t have any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader please make sure that you are on the demo account, please open an empty chart, look for the new file simple buy button in the navigator window and drag it on the chart.
You need to click on common and make sure that allow live trading is enabled, now let’s click on okay and here is our buy button, currently we have no open positions here, so let’s click on the button.
Here is our buy position, this is the text buy button was pressed and in this little video you have learned how to create a buy button object and how to do the event handling for the button and you have coded it yourself with a few lines of mql4 code.