How to Calculate the Position Size in MQL4, Protect your Account!

One of the most important rules when trading forex or any other instrument is Risk Management! Risk Management involves a set of rules to keep your account safe from unexpected events and “unlucky” times, it makes the difference between a good trader and an amateur. In saying that you must consider to learn how to calculate the position size with MQL4 code.

Considering the Position Size

One of the most popular rules of risk management is to invest on each trade only a small percentage of your entire account, this is to prevent your account to be blown up in case of a strike of losing trades. For example, if your position size is 25% of your account each trade with a strike of 4 losing trades you are left with almost nothing, if you had used only the 2.5%, after a strike of 4 losing trades you would still have more than 90% of your balance available. Of course if the trades were winners the profit would have been higher, but we believe Risk Management is more important.

In this article we go straight to an indicator that can do all this for you.

It is beyond the scope of this article to explain what is the optimal percentage of balance to trade, also because it really depends on your strategy and trading style, our focus here is to show you how to calculate the position size with MQL4 code.

First, the theory, how do you calculate the position size?

Assume you want to risk only 1% of your balance on each trade, in this example your account is 10.000$, this means your risk for individual trade is 100$

metatrader4-mql4-calculatepositionsize-1

You also know that for this trade you want a stop loss 20 pips away from the open price and, assuming this trade is on EURUSD, every pip for a standard lot size has a value of 10$, meaning 20 pips are valued 200$ (for more about pip value read What is a PIP (and a PIPETTE) in Forex Trading?)

metatrader4-mql4-calculatepositionsize-2

Since you want to risk 100$ and have a stop loss 20 pips away (200$ for 1 standard lot) then your position size will be 0.5 standard lots

metatrader4-mql4-calculatepositionsize-3

So, to sum up, to calculate the position size you need:

  • Balance of your account
  • Percentage of risk for the single trade
  • Stop loss, in pip in this case
  • Pip value

How to Calculate the Position Size in MQL4

MetaTrader once again comes in our help with some native functions that will make it easy to calculate the position:

  • AccountBalance()  returns the available balance for the account
  • MarketInfo(Symbol(),MODE_TICKVALUE)  returns the value of a tick for the current pair

Now, this may be tricky, as we saw in the article about PIP Normalization, brokers can show the exchange rates with 4 or 5 decimals (2 or 3 for the JPY), if the broker is using 5 decimals (3 for JPY) then MarketInfo(Symbol(),MODE_TICKVALUE)  is the value of a pipette instead of a pip. If this is the case to calculate the correct amount we will need to take into consideration the number of decimals in the exchange rate.

We can see some code to see how to calculate the position size, we will test it on a simple Expert Advisor, the balance of the account is 9.999.53$ and we set 1% risk and 20 pips stop loss.

The result of the script is

metatrader4-mql4-calculatepositionsize-4

As you can see the position size is 0.4999765, this can create some problem when submitting orders as MetaTrader can accept only orders rounded to its minimal incremental lot size, in other words, you can submit an order for 1 lot but an order for 1.00005 lots will not be accepted.

We can find out what is the minimal increment for the orders with the function MarketInfo(Symbol(),MODE_LOTSTEP), usually this value is 0.01 which is equivalent to a micro lot. Using this function and some other maths we can round the number and match the accepted increment. We will add this line to the code

Basically what we do is divide the found position size by the accepted increment and round the number and then multiply for the accepted increment, in the above example:

metatrader4-mql4-calculatepositionsize-5

So if we use the following code we will avoid errors with the position size while ordering

And the result will be

metatrader4-mql4-calculatepositionsize-6

This function is very handy as with only two parameters is able to calculate the position size meeting your risk management strategy, furthermore, in the example above the Stop Loss was set to a static value, however in some strategies the Stop Loss may be variable and in that case this function would be still working as the calculated lot size is function also of the stop loss. Also, the function is suitable for all currency pairs as the tick value is known directly by MetaTrader considering your account currency and the currency you are applying the function to.

In this article we go straight to an indicator that can do all this for you.

Always remember to adopt Risk Management measures, they will make you a better trader!

Please leave a comment if you have any feedback or doubt and remember to like us on Social Media!

4 thoughts on “How to Calculate the Position Size in MQL4, Protect your Account!”

  1. Nice job, just an advice, you’re defining as integer the StopLoss extern variable and using a double argument on the CalculateLotSize(double SL) function, you should change the SL argument type to int; same functionality but without warning alerts!

Leave a Comment