MQL4 Lot Size Parameters

You may have already heard this, but choosing appropriate lot sizing for your trading system is a key ingredient for developing a good system. Learn in this MQL4 Lot Size Parameters article how to program your Expert Advisor with risk, lot size, money management and other variables.

You can specify a lot size as simply as declaring one in an internal variable as a fixed lot size for every order, but we will explore a simple method that calculates the lot size based on a percentage of your free margin.  

There is a little math behind the scenes, but basically, if you choose a custom risk setting of 1, you will trade 0.01 micro lot for every 1K in equity size. Thus, with a custom risk setting of 2 and a 10K account size, you will be starting with 0.2 lots, and it will automatically add/subtract 0.01 lot for every $100 in profit/loss. This auto lot sizing technique is as simple as it gets, but very effective for automatically compounding your profits, de-compounding your losses, or automatically adjusting for deposits and withdrawals in the account.  

Parameters

MM

Bool: Whether or not you will use money management. 

Risk

Double: Your predefined risk setting. 

Lots

Double: If MM is turned off, this is the manual lot size you will use. 

LotDigits

Double: This is the number of decimal places for the lots provided by your broker.  Most have two decimal places, but some have one. 

MT4 Code Snippets

extern bool MM = TRUE;
extern double Risk = 2;
extern double Lots = 0.1;
extern double LotDigits =2;

 

double GetLots()
{
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double leverage = AccountLeverage();
double lotsize = MarketInfo(Symbol(), MODE_LOTSIZE);
double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

double MinLots = 0.01;
double MaximalLots = 50.0;

if(MM)
{
double lots = Lots;

double lots = NormalizeDouble(AccountFreeMargin() * Risk/100 / 1000.0, LotDigits);
if(lots < minlot) lots = minlot;
if (lots > MaximalLots) lots = MaximalLots;
if (AccountFreeMargin() < Ask * lots * lotsize / leverage) {
Print(“We have no money. Lots = “, lots, ” , Free Margin = “, AccountFreeMargin());
Comment(“We have no money. Lots = “, lots, ” , Free Margin = “, AccountFreeMargin());
}}
else lots=NormalizeDouble(Lots,Digits);
return(lots);
}

You will see that we first had to declare a number of extern variables to dermine if we should have turn the management on (true) or off (false), what is going to be our custom risk setting if on, and if not, what is going to be the default lot size.

LotDigits
is how many decimal places your broker allows for (for instance, if it allows for micro lots, such as 0.01, it would have 2 digits or decimal places). 

GetLots() is the name we have given to our custom function So all of the o(it could have been any name), and all that is subsumed between its brackets is a calculation of this function. You will simply place GetLots() in the third parameter of the OrderSend() function in order to call upon it, replacing the fixed lots variable that was there before. 

We create a variable minlot to reference the MarketInfo() function. The MarketInfo() function is the function we need to retrieve various market data of the given currency, such as the Bid or Ask price, the Swap value, the number of digits, and for our purposes, it can also tell us the minimum lot size for that currency. We want to make sure that whatever lot calculation is conducted, it is greater than the min lot size of the broker, else it is less than minlot, it will be the minlot.

The main calculation of the automatic MM lot happens in one line:

double lots = NormalizeDouble(AccountEquity() * Risk/100 / 1000.0, LotDigits);

AccountEquity() is one of many account information functions that returns the equity value of the current account. We want to return the equity value of the account, as opposed to the AccountBalance(), because equity represents a more valid picture of the state of the account (aka, the net account value). We want the equity value to conduct our math on appropriate lot sizing. We are going to multiply this equity value with our risk value, then divide by 100, and then further divide by 1000, in order to determine the appropriate lot size.

The effect is proportional lot sizing, based on the risk setting chosen: it makes a risk setting of 1 trade 0.01 lots per 1K in equity, a risk setting of 2 trade 0.02 lots per 1K in equity, etc. There is a host of possibilities, depending on the risk setting chosen. Lots are added or subtracted to the account as it grows or diminishes in size. For instance, a risk setting of 2 would trade 0.2 lots on a 10K account , and add / subtract by 0.01 lot for every $100 gain or loss in equity. The user can easily adjust a risk setting that is suitable to his or her risk tolerance, EA trading style, and account size. 

If MM is set to true, we will calculate the lot size based on the equity, and assign that value to the lots variable. If MM is false, we simply assign the value of lots to the fixed lot size of Lots

You can see that the above code is relatively simple, but it can make a world of difference in auto lot sizing based on a changing equity size. There are more complicated ways in determining lot sizing, but sometimes the simplest methods work best. 

Note:

The EA developer or end-user must determine the appropriate risk setting for the EA based on a rigorous back test, paying close attention to risk related stats (average losing trade, consecutive losing trades,  and max draw down). Usually this risk analysis is first done with a fixed lot size, such as 0.1 for a 5K account. Once all the risk-related stats are compiled for a fixed lot size, one can better determine the risk setting one can be comfortable trading with.