MQL5 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, so let’s find out how to do that with mql5.
To get started 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 file, expert advisor from template, continue, I will call this file simple dynamic sell position size, click on continue, continue and finish.
Now you can delete everything above the on tick function and let’s also remove the two comment lines here.
We start with an include statement to include the file trade dot mqh, this one comes with mql5 and it contains some basic simplified trading functions.
Afterwards, we create an instance of the class ctrade, that is called trade and we are going to use it to open a position later on but first, we want to calculate the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid – all in capital letters – and with normalize double and underscore digits we calculate the number of digits behind the dot. Depending on the currency pair this might be three or five digits and this function is going to do that for us.
Now we want to get the equity that is done by using account info double, account underscore equity – all in capital letters – and we repeat the whole thing for the balance. This is the same function but this time we use account underscore balance.
And I would like to have my calculated position size to have two digits behind the dot so I use normalize double here and the size should be equity divided by one hundred thousand.
For the test entry, we want to check a few simple conditions.
The first one is if the equity is at least equal or greater than the balance and the second condition is we want to know if positions total equals zero, if that is the case we have no open positions and that is when we use trade dot sell to open a new sell position and we use the calculated position size.
To see what’s going on we can use the comment statement here that will output the words balance, equity and position size followed by the calculated values and that’s about it.
If you don’t understand what all the code here does or this was too fast for you maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course might be interesting for you, actually, this video was also a request from a premium course member so if you already are a premium course member and have an idea for a video like this one just let me know.
For now, let’s click 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 Metatrader.
And in Metatrader you want to click on view, strategy tester or press control and r, please pick the new file, simple dynamic sell position size dot ex5, mark the option for the visual mode here and start your test.
Here we are! The current equity is one hundred thousand, so the position size is one micro lot, let’s speed that up a little bit and now we should see that the position size is changing so the little expert advisor works as expected and in this little video you have learned how to create an expert advisor for Metatrader 5 that is able to dynamically calculate the position size based on the equity and you have coded it yourself with a few lines of mql5 code.