MQL5 TUTORIAL BASICS – 87 SIMPLE SELL BUTTON OBJECT

video
play-sharp-fill

 

  1. Introduction to Creating a Sell Button Object (00:00 – 00:11)
    • Introduction to the video’s objective: creating a sell button object in MQL5, demonstrated by a new sell position appearing on the chart upon clicking the button.
  2. Starting with MetaEditor and File Creation (00:11 – 00:40)
    • Instructions on opening MetaEditor, creating a new file named “Simple Sell Button”, and preparing the file for coding.
  3. Including the Trade.mqh File and Declaring Variables (00:40 – 01:12)
    • Including the ‘Trade.mqh’ file for simplified trading functions and declaring global variables for the ask and bid prices.
  4. Calculating Ask and Bid Prices (01:12 – 01:41)
    • Using ‘SymbolInfoDouble’ for the current symbol to calculate ask and bid prices, ensuring correct digit formatting.
  5. Creating the Sell Button Object (01:41 – 02:55)
    • Using ‘ObjectCreate’ to create a sell button object on the chart and explaining its parameters.
  6. Setting Button Properties: Distance, Size, and Corner (02:55 – 04:57)
    • Adjusting button properties like distance from the border, width, height, and corner placement using ‘ObjectSetInteger’.
  7. Setting the Button Text and Implementing Event Handling (04:57 – 06:43)
    • Modifying the button text and implementing the ‘OnChartEvent’ function for event handling, specifically for button clicks.
  8. Executing a Sell Trade on Button Click (06:43 – 07:12)
    • Coding the functionality to execute a sell trade when the sell button is clicked and displaying a confirmation message.
  9. Compilation and Testing in MetaTrader (07:12 – 08:19)
    • Compiling the code in MetaEditor, testing the Expert Advisor in MetaTrader on a demo account, and ensuring proper functionality.
  10. Demonstration of the Sell Button in Action (08:19 – 08:51)
  • Demonstrating the sell button on a MetaTrader chart, showing the creation of a new position upon clicking the button.

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, this one is the interesting parameter because we can use it to check the name of the object but before we do that we want to know if the id for the event is equal to chart event underscore object underscore click. That would be a mouse click for an object, if you mark that and press F1 you will see that we have lots of different event types, for example, chart event click would also be a mouse click but this one is not for the button, so we use chart event object click here, so now we know that the object was clicked and we want to get the name for the clicked object and this is what we can get when we check the sparam.
And if the sparam equals sell button, we know that we have clicked on the right button and that’s when we want to use the comment statement to output the name of the button followed by the text, was pressed and then we are going to use trade dot sell to sell ten micro lot.
Let’s move the comment here, close the if statement and the function and that’s about it.
Well, 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 this basic video series or maybe even the premium course on our website might be interesting for you. This was a suggestion from a premium course member and if you are already a premium course member and have an idea for a video like this one, just send me an email, for now, please click on view, toolbar and click on the compile button or press F7, I don’t have any errors here 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 we want to use a demo account and a chart. The reason is that the object clicks are not properly supported in the strategy tester, so let’s open the navigator view and drag the new expert advisor called, simple sell button onto your demo account chart, please allow algo trading, click on okay.
Here is my sell button.
If you see the red sign here you need to click on algo trading and it should become green, so let’s click on view, toolbox or press ctrl and t, now you should see your open positions here, I have one open position and when I click on the sell button, I see the text, sell button was pressed, here is the new position, so our expert advisor works as expected and in this little video you have learned how to create a button object for a sell button and you have coded it yourself with a few lines of mql5 code.