MQL4 TUTORIAL BASICS – 60 SIMPLE DYNAMIC SELL POSITION SIZE

video

In this video we are going to create an expert advisor that is able to dynamically calculate the position size for sell positions based on the equity, so let’s find out how to do that with mql4.
To get started 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 dynamic sell position size, click on continue, continue and finish.
Now we can delete everything above the on tick function and let’s also remove the two comment lines here.
Inside of the on tick function we are going to calculate the position size, the variable type is double so it can have floating type values. Basically, I would like to have my position size to be calculated based on the account equity, the equity is basically what is left over after you have paid for all commissions, costs and open positions, the calculation should be the account equity divided by one hundred thousand and I use normalize double and the value two to calculate a position size with two digits behind the dot, that’s what we see here because otherwise, you might see a lot of errors here, saying something like the position size is wrong and this is a way to avoid requotes from the broker.
Afterwards, we want to find out if the account equity is bigger or at least equal to the account balance and if this is the case we want to also check if orders total returns a value of zero, if this is true we have no open positions (and if this is true) and we are going to use the order send statement for the current symbol, we want to open a sell trade for the position size that we have calculated here.
Finally, we want to use the comment statement to output the text balance, equity and position size followed by the calculated values on our chart.
This is pretty much it.
If you don’t understand what all the code here does or if this was too fast for you maybe you want to watch one of the other videos of this basic video series first or maybe even the premium course might be interesting for you, actually this video was an idea of a premium course member so if you are already a premium course member and have an idea for such a video just send me an email.
For now, we click on the compile button, we don’t see any errors here and if this 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 select the new file, simple dynamic sell position size dot ex4, mark the option for the visual mode here and start a test.
Here we are! The expert advisor is running, the current position size is one, that might change in a few seconds when the equity goes below one hundred thousand dollars, so let’s speed that up a little bit!
Actually, it’s now one point zero one, so let’s fast forward and now it’s zero dot ninety-nine because the equity is ninety-nine thousand four hundred dollars so our little expert advisor works as expected and in this little video you have learned how to create an expert advisor that is able to automatically calculate position sizes for sell trades based on the equity and you have coded it yourself with a few lines of mql4 code.