MQL5 Tutorial

MQL5 TUTORIAL BASICS – 87 SIMPLE SELL BUTTON OBJECT


Listen Later

In this video we are going to create another object, this is a sell button and when I click the sell button I see that we have a new sell position here, so let’s find out how to code that with mql5. 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. We start with an include statement to include the file trade dot mqh, this one is part of mql5 and it will provide some simplified trading functions. Afterwards, we are going to create an instance of the class ctrade this one is called trade and we also want to declare two global variables for the ask price and the bid price. Inside of the ontick function we start with calculations for the ask price and the bid price that can be done by using symbol info double for the current symbol on the chart, we use 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 let’s continue and create the button object that is done by using object create, if you mark that and press F1 you will see that it will create one of these objects. There are a lot of different object types and depending on the type of the object you need to do different things, we are going to create the object button here that can be placed on the chart. The first parameter is the current symbol or the currency pair on the chart, the second parameter is for the name of the object, we will call it sell button, parameter three is for the object type and this is obj underscore button, this parameter is for the window, we will create the object in the main window – that’s where the candles are – if you use oscillators you could also create objects in the window below the candles but we are not going to do that and the last two parameters are for a time and a price. Depending on the object you want to create you might want to move it to a certain date-time or a certain price point. For example, we have used that to move a line to the highest and to the lowest candle on the chart but we don’t use these values for the button because the button will stay where it is, so that’s how you create the object but so far it will not do anything and we want to change a few properties. The first one is for the distance from the border, we use object set integer for the current symbol on the chart for the object called sell button and we want to modify the property object property x distance and set it to two hundred pixels. Let’s also set the width of the button! Once again object set integer for the current symbol on the chart, the object name is sell button and now the property is object property x size, let’s also set this one to two hundred. Let’s continue and set the property y distance! This time we are going to use two hundred fifty pixels. I have messed up the comments here, so let’s change that. And we use object set integer for the next property object property y size that is the height of the button, we will set it to fifty pixels, the next property is the corner, I have used corner two, you can pick one of the four corners on the chart and we change that by modifying the property object property underscore corner in my case I use corner two. And finally, let’s set the text on the button that is done by modifying object property underscore text and let’s set that to sell, and that’s it for the button creation and the button properties but we need still to implement the event handling. Mql5 comes with a function that is called on chart event, it has four parameters, the first one is for the object id and the other parameters will pass some values, in our case,
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer