- Introduction to Closing All Buy Positions in MQL5 (00:00 – 00:14)
- Introduction to creating an Expert Advisor in MQL5 that opens ten test positions and closes all buy positions.
- Opening MetaEditor and Creating a New Expert Advisor File (00:14 – 00:35)
- Instructions on opening MetaEditor and creating a new Expert Advisor file named “Simple Buy Position Closer”.
- Setting Up the Code Structure and Including Trade.mqh (00:35 – 00:56)
- Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
- Creating an Instance of CTrade and Getting the Ask Price (00:56 – 01:09)
- Creating an instance of the CTrade class and getting the Ask price using ‘SymbolInfoDouble’.
- Opening Test Buy Positions (01:09 – 01:49)
- Opening test buy positions using ‘trade.Buy’ until there are ten open positions.
- Creating the Close All Buy Positions Function (01:49 – 02:51)
- Implementing the ‘Close All Buy Positions’ function to close all open buy positions using a for loop and ‘trade.PositionClose’.
- Compiling and Testing the Expert Advisor in MetaTrader (02:51 – 03:14)
- Compiling the code and instructions on testing the Expert Advisor in MetaTrader.
- Running the Strategy Tester in MetaTrader (03:14 – 03:32)
- Running the Expert Advisor in the Strategy Tester with visualization to observe the closing of buy positions.
- Observing the Expert Advisor in Action (03:32 – 04:06)
- Watching the Expert Advisor open and then close all buy positions once ten positions are open.
This test expert advisor is going to open ten test positions and as soon as we have ten open positions we call a function called close all buy positions, so let’s find out how to do that with mql5.
To get started please press F4 or click on the little symbol here, 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 position closer as we are going only to close buy positions.
Now let’s click on continue, continue and finish, and now we can delete everything above the on tick function and the two comment lines here.
First, we need to include the file trade dot mqh, this will help us to create an instance of the class ctrade that will be called trade and we are using it to open our test positions.
Inside of the on tick function we start by getting the ask price, that is done with symbol info double for the current symbol and we use symbol underscore ask – all in capital letters – I also use normalize double and underscore digits to automatically calculate the number of digits behind the dot.
First we want to know if the number of positions total is below ten, that is the case if we have less than open positions and if that is the case we use trade dot buy to buy ten micro lot for the ask price that we have calculated here and as soon as we have exactly ten positions we want to call a function called close all buy positions to close all the open buy positions at the same time.
This function does not exist so far, so let’s create it, it has no return type and it will be called close all buy positions, we use a for loop to go through all the open positions and for each position we use position ticket to get the ticket number, we also want to calculate the position direction that is done by using position get integer, position underscore type – all in capital letters – and if the position direction equals position underscore type underscore buy, this position is a buy position and now we use trade dot position close for the current ticket number to close the current position.
Finally, we need to close the for loop and the function, that’s about it, and if you don’t know what all the code here does maybe you want to start with the other videos in this basic series or maybe even the premium course is interesting for you.
For now please click on the compile button here or press F7, that should work without any errors and if that is the case you can click on the button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file, simple buy position closer dot ex5, enable the visualization mode here and start a test.
And here we! The expert advisor is opening test positions and as soon as we have ten open positions it calls our custom function to close all open buy positions.
This video was an idea of a premium course member and if you have any ideas that should be explained in such a video here just send me an email because in this little video you have learned how to create an expert advisor to close all the open buy positions at once and you have coded it yourself with a few lines of mql5 code.