MQL4 TUTORIAL BASICS – 30 HOW TO CLOSE ALL OPEN BUY POSITIONS

video
play-sharp-fill

In this video, we are going to create an expert advisor that is able to close all the buy positions at once, as soon as we have ten open test positions all the positions will be closed, so let’s find out how to do that with mql4.
To start 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 buy position closer as we are only going to close buy positions in this video.
So let’s click on continue, continue and finish, now we can delete everything above the on tick function and let’s remove the two comment lines here.
We start by checking if orders total is below ten, that would mean that we have less than ten positions, to be exact orders total would also deliver the number of pending orders so we need to check the order type later on and if we have less than ten positions we use order send for the current chart to open buy positions.
These are just test buy positions, you wouldn’t do that on a real account but we need to open a few positions here to have something to close.
And if orders total equals ten that will mean that we have exactly ten positions and that is when we want to close all open buy positions by calling a function called: close buy positions.
So far this function does not exist so we need to create it now.
We don’t use a return type here and the name of the function will be close buy positions.
First, we use a for loop to go through all the open orders and as long as we have found an order we want to use order select to select it.
This is the current counter of the for loop, we use select by position and mode underscore trades to be sure we want to get the current currency pair for the order otherwise, it might be possible to close orders and other currency pairs and you don’t want to do that.
So if the current symbol on the chart and the current order currency pair match and if the current order type equals op underscore buy – that means that we have a buy position – that is when we actually want to close the order by using order close for the current order ticket, we want to close the current position size of the order for the current bid price, the slippage is three so when the price changes during this operation we have a little tolerance.
The last parameter here would be a color but we don’t need that, so finally we want to close the for loop and the function.
If you don’t know what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you.
For now, let’s click on the compile button or press F7, that should work without any errors here and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, you want to click on view, strategy tester or press control and r, please pick the new file, simple buy position closer dot ex4, mark the visual mode here and start a test.
And here we! The expert advisor is opening positions whenever a tick comes in and as soon as we have reached ten positions it is going to close them all at once.
This video was an idea from a premium course member and if you have any idea that should become such a video here just send me an email because in this little video you have learned how to create an automated expert advisor that is able to close all the open buy positions automatically and you have coded it yourself with a few lines of mql4 code.