MQL5 TUTORIAL BASICS – 32 SIMPLE BUY POSITION MODIFIER

video

In this video we want to find out how to modify an existing position, in our case we have a ten micro lot position here, it’s a buys position and as soon as the equity is higher than the balance the position size will be reduced.
Let’s speed up this process a little bit, and now you should see that this volume is going to decrease, so let’s find out how to code that in mql5.
To do that please click on the 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, expert advisor from template, continue, I will call this file simple buy position modifier, click on continue, continue and finish.
Now we can delete everything above the on tick function and the two comment lines here.
First, we need to use the include statement for the file trade dot mqh that comes with mql5, we use it to create an instance of the class ctrade that will be called trade, first, we need to get the ask price because we are going to use test buy positions.
To calculate the ask price we use symbol info double for the current symbol on the chart, we use symbol underscore ask and with normalize double and underscore digits we want to automatically calculate the right number of digits behind the dot.
Now we use positions total to calculate if we have no open positions, that would be the case if the return value is zero and if that is the case we use trade dot buy to open a test buy position, the position size will be ten micro lot and afterwards we want to use a new function called change position size and we pass the ask price as a parameter here, we use void as we don’t need to return any values, the name is change position size, and this is the parameter that we have passed here.
First we calculate the balance that is done by using account info double, the parameter is account underscore balance – all in capital letters – we also want to calculate the equity, this is done with account info double and the parameter account equity – also in capital letters – now we use a for loop to go through all the open positions, we want to get the symbol of the position that is done by using position get symbol for the current position in the loop and now we compare it to the symbol on the chart and if the symbol on the chart and the symbol of the currency pair are equal we need to get a few things; the first one is the position ticket, we get the ticket by using position get integer and the parameter here is position underscore ticket, we want to get the position direction, the position direction can be calculated by using position get integer, position type – this one is also in capital letters – and if the position direction equals position type buy that means we have a buy position, so let’s check if the equity is ten points above the current balance and when all those conditions are true we use trade dot position close partial for the current position ticket, we want to close one micro lot each time we call this function.
For the tolerance you could define a few points here, we use minus one and finally we need to close the if loop, the for loop and the function, and that’s about it.
If you don’t know what the code here does or if it was too fast for you, you might want to watch the other videos of this basic series first or maybe even the premium course is interesting for you, for now we click on the compile button or press F7, we don’t have any errors, so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on view, strategy tester or press control and r, here comes up the new strategy tester wizard, we select “Run single test on an Expert Advisor”, let’s select the new file: simple buy position modifier dot ex5, we also need to enable the visual mode here and start a test.
Here is our test position, we have a ten micro lot test position and as soon as the price crosses the green line here we should see that the position size is going to be reduced, here we are, the position size is changing now so the little expert adviser works as designed.
In this little video, you have learned how to automatically reduce position sizes and close a part of an open position and you have coded it yourself with a few lines of mql5 code.