In this video we want to to find out how to close all the sell positions at the same time, this little expert advisor is going to open ten sell positions and as soon as we have exactly ten positions it is going to close them, so let’s find out how to code that in mql5.
To do that please click on the 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, expert advisor from template, continue, I will call this file: simple sell position closer, click on continue, continue and finish, now you can delete everything above the on tick function and let’s also remove the two comment lines.
We start by using the include statement here to include the file trade dot mqh, it comes with mql5 and it makes it possible to create an instance of ctrade called trade in this case and we are going to use this instance to open our positions later on.
First, we need to get the bid price that is done by using symbol info double for the current symbol on the chart and we are using symbol underscore bid, all in capital letters. I also like to use normalize double and underscore digits to automatically calculate the right number of digits behind the dot, that depends on the currency pair…
If we have less than ten positions, so if positions total returns a value below ten we use trade dot sell to open a test position, the lot size will be ten micro lot and we continue to do that until positions total will deliver a return value of exactly ten, so now we have ten open positions and in that case we want to close all open sell positions with a function called: close all sell positions that doesn’t exist right now so we need to code it.
The return type of the function will be void, the name is close all sell positions, we will use a for loop to go through all the open positions until there are no positions left.
First, we need to use the function position get ticket to get the ticket number for the current position number, so i in this case is just the counter for this for loop and the ticket number is a longer number that will identify the position.
We also need to find out the position direction so we use position get integer, the parameter here is position underscore type – all in capital letters – and now we can check if the position direction equals position type sell – all in capital letters –, if this is the case we have a sell position and now we use trade dot position close for the current position ticket that we have calculated here to close the position. Finally, we need to close the for loop and the function and that’s about it.
If you don’t understand what all the code here does or if this was too fast for you, you might want to watch the other videos in this basic video series or maybe even the premium course is interesting, for now, please click on the compile button or press F7, you shouldn’t get any errors here and in that case you can click on the little button here or press F4 to go back to Metatrader.
Since the last update there are some strange settings here, first I need to re-enable the standard toolbar, now we can click on view, strategy tester or press control and r, and here we now have a list of choices that has been implemented by Metaquotes, I want to select “Run single test of an Expert Advisor”, please pick the new file: simple sell position closer dot ex5, enable the visualization option here and start a test.
Here we are! The expert advisor is working, it is able to open positions here and as soon as we have ten positions on the chart it will call the new function to close them all at the same time, so in this little video you have learned how to code that with mql5 and you have done it yourself with a few lines of code.