
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.