In this video we are going to find out how to reduce the position size for an existing sell position, here we have nine micro lot and now the position size is reduced 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 file, expert advisor from template, continue, I will call this file simple sell position modifier, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here, we start by including the file trade dot mqh, this one comes with mql5 and we use it to open trades, to do that we create an instance of ctrade that will be called trade.
As we are only going to close sell positions we need to calculate the bid price, this is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid as the second parameter and I also like to use normalize double and underscore digits because this will automatically calculate the right number of digits behind the dot.
Afterwards, we want to check if we have no open positions, that would be the case when positions total delivers a return value of zero and in that case we use trade dot sell to open a test position for ten micro lot. Actually, this is a sell position not a buy position and now we want to reduce the position size, so we call the function change position size with the bid price but before we can do that we need to code that function here. We use the return type void, the name of the new function is change position size and here we will use the double value for the bid price that we have passed here.
First we want to calculate the balance that is done by using account info double, the parameter we use is account underscore balance – all in capital letters –, let’s repeat that for the equity, this is almost similar, once again we use account info double but this time the parameter is account underscore equity.
Afterwards, we use a for loop to go through all the open positions, in our case it’s only one test position, you wouldn’t do that on a real account, on a real account you would use the for loop to go through all the open positions.
Afterwards, we need to get the order symbol for the current position that is done by using position get symbol for the current counter value in the for loop, so now we can check if the current symbol on the chart equals the order symbol and if that is the case we need to find out the position ticket because we will need the ticket number to close a part of the existing position. We do it by using position get integer, the parameter is position underscore ticket and as we are only going to close sell positions we also want to get the position direction that’s done by using position get integer for the position type, so let’s find out if this is a sell position, that would be the case if the position direction has the value position type sell, and finally we want to check if the equity is ten points or more above the balance, that’s the case when we make a profit and in that case we are going to use trade dot position close partial for the current position ticket, we want to reduce the current position size by one micro lot, this would be a tolerance value, we use minus one in this case, let’s close all the open loops here and that’s about it.
Now if this was too fast for you or if you don’t know what all the code here does 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 please click on the compile button here or press F7 you shouldn’t get any errors and if that is the case you can click on the little button here or press F4 to go back to Met trader.
And in Metatrader you want to click on view, strategy tester or you could press control and r on your keyboard, this is the new strategy tester, here we have to select “Run single tests of an Expert Advisor”, please pick the new file simple sell position modifier dot ex5, mark the visualization option here and start a test.
And here our little expert advisor is running, we have ten, now nine micro lot and as soon as the price goes down we should see that the position size is changing here – that’s the case now – so our little expert advisor works and in this little video you have learned how to code an expert advisor that is able to automatically reduce the position size of an existing sell position and you have coded it yourself with a few lines of mql5 code.