MQL4 TUTORIAL BASICS – 58 SIMPLE DYNAMIC BUY POSITION SIZE

video
play-sharp-fill

In this video we are going to calculate a dynamic position size that is based on the equity, so let’s find out how to do that for buy positions in mql4.
To get started please click on a 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 buy position size, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
For this simple example, we are going to dynamically calculate the buy position size based on the account equity, this function will return the current equity value for the account, basically, that’s what is left over after you have paid for fees and open positions and I would like to have the position size calculated dynamically so I use the account equity and divided by 100,000.
Usually, the result will have a number of digits behind the dot that is too high, so I use normalize double and cut off the calculated value to two digits behind the dot, otherwise, you might see a lot of errors here in the journal and that would be something like wrong position size, so this is a way to prevent that.
I would like to open trades only if the account equity is equal or higher than the account balance and my second condition would be if the return value for orders total equals zero. Orders total will return the number of market and pending orders so if the return value is zero that means that we have no open orders and no open positions and if this is the case we will use the order send command for the current symbol on the chart to open a buy trade with the position size that we have calculated here.
Finally, we use the comment statement to create a chart output that will tell us the current balance, the equity and the current position size and that’s about it.
If this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you, otherwise, click on the compile button now or press F7 on your keyboard. You shouldn’t get any errors and if this is the case you can click on a 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 buy position size dot ex4, mark the option for the visual mode here and start your test.
Here we are! The current position size is one, so let’s speed up the process a little bit and now you should see that the value is changing based on the equity, we see the position size is going down, that’s also what the equity does here, so our little expert advisor is working as expected and in this little video you have learned how to dynamically calculate the position size based on the equity and you have coded it yourself with a few lines of mql4 code.