All of the EAs that we program generally use one of three types of money management. I don’t really like that term, though. I believe that position sizing formula is generally more accurate.
The options are:
- Fixed Lot Size
- Use a percentage of available margin
- Lose a certain percentage of the account balance whenever the stop loss is hit.
Most MetaTrader users are accustomed to using fixed lots in their forex money management. It’s far and away the most common method found in commercial EAs. If you’ve watched any of the videos on this site or spoken with me, you know that I generally have a low opinion of most commercial EAs. Just because everyone does it does not mean that it’s a good idea!
Whenever a customer order mentions the idea of using a Risk input to control the lot size for their money management, it typically means to use a selected percentage of the available margin. Say, for example, that you trade a $10,000 account on 1% margin (100:1 leverage) and want to use 2% of available margin on any given trade. If you have no open positions, then the margin to use is $10,000 * 2% = $200.
The lot size is the outcome of the following formula:
Lots = Margin to Use / Margin Required per standard lot
Going back to our example, you simply plug in the numbers:
$200 / $1000 per standard lot (100:1 margin) = 0.2 lots, which is 2 mini lots.
The MQL4 code for this is
double lots = (AccountFreeMargin() * Risk) / MarketInfo( Symbol(), MODE_MARGINREQUIRED);
The advantage to this method is that the lot size remains consistent barring a dramatic change in available margin. I don’t actually see that as an advantage, but many traders like seeing the same lot size on most trades.
The disadvantages are numerous. If you like to trade on high leverage and trade many different instruments, you can easily get yourself into a margin call. If your strategy calls for placing a stop based on price action, the amount lost will vary depending on where the stop is placed. Some trades might lose $20 while others lose $100.
double lots = Risk * AccountEquity() / MarketInfo(Symbol(), MODE_TICKVALUE) / Stop;
My favorite forex money management method is to select my lot size based on the equity loss if my stop is hit. If I risk 0.5% on a $10,000 account and my stop loss is 20 pips away, then desired lot size is
lots = 0.5% * $10,000 / $10 per tick per standard lot / 20 pips = 0.25 lots, or 2.5 mini lots
The lot size decreases whenever the stop loss distance increases, and vice versa. A 60 pip stop loss would require a lot size of
lots = 0.5% * $10,000 / $10 per tick per standard lot / 60 pips = 0.083 lots, or 0.8 micro lots after accounting for rounding.
The varying lot size drives most traders crazy. I believe such rationale ignores the logic of trading. Trading is a statistical outcome, a distribution of events that theoretically returns a net result greater than zero. We call this profit in daily life.
If your trading system is x% accurate and you know the profit factor is greater than 1, why on earth a trader would haphazardly bet different dollars amount on every trade is beyond me. The effect is no different than randomly choosing to bet $10 on this trade and $100 on the next. The random, system-less money management of choosing position sizes for your trading system would overwhelm the nice, even distribution that you’re expecting from the signals.
Hello,
The problem I have is that I need to calculate lot size based on a set percentage of USED MARGIN. The reason being that I trade 12 currency pairs with a recovery system. I’m having a difficult time working backwards, for some reason.With the following inputs how is Lot Size calculated based on a set percentage of USED MARGIN:
Standard lot = 100,000
Leverage = 50
Account Equity = $10,000
Used Margin Risk max per trade = 5% = $500
AUDUSD = $10.00
Stop Loss = 20 pips = $200
Lot Size = ???
Thanks
Thank you for the question. The lot size is
used margin * leverage
$500 * 50 = 25,000 units, or 2.5 mini lots (0.25 lots in MetaTrader’s standard notation).
hi,
can you write MM like this:
Risk=5 means stop loss = 5% of account balance
thank you
Hi swiz,
Yes, this is possible to do. Are you asking me something specific? I get the feeling that maybe I didn’t understand you correctly.
do you recall John Burch from forex-tools-cafe.com? He created trademan v4 which looked for the past highest high and low and set those as sl and trade position size to give the amount of lots for the trade. I think he might of passed on but his website still exists and the software sells for a cool $18 dollars.
BTW your enthusiasm for what you do rather infectious.
Milt
Thanks, Milton!
Talking about this line: “My favorite forex money management method is to select my lot size based on the equity loss if my stop is hit.” How would you describe it in MQL4 though?
Thanks Shaun….hope you don’t mind to share the code
PS you got a good EA there but it is very unfortunate you only open a MAM or PAMM account
Thanks, Thomas. The code is already in the middle of the article in a different font. Please look again.
hi Shaun,
I’m trying to figure out how to calculate below figures with a ratio of $500:0.01micro lot
Account Balance = $500
Leverage = 200
Risk % = 10
can you please give me a formula with every $500 increase, lot size increase by 0.01 micro lot?
Hi David,
Thanks for asking. You’re making it more complicated than it needs to be. Leverage and risk are extraneous variables for the equation that you’ve proposed.
The formula is:
$500 (account equity) * 0.01 lots / $500 = 0.01 lots.
If your account equity grew to $1,200, the formula gives you:
$1,200 * 0.01 lots / $500 = 0.02 lots (I rounded down).
I hope that helps.
Well is educative
I’m glad you learned something.
Your formula (and weakness in MT4) is that one alwats needs to calculate LOTS – which doesn’t help when trading a multi-instrument portfolio. For example 1 lot of EURUSD isn’t the same as 1 lot of GBPJPY – that is, they expose different quantities of USD$ to the market. So if you are looking to create a system that ensures you expose USD$x then the formula becomes a lot more complex
Actually, that’s not the true. The formulae provided use MarketInfo, which returns the values in terms of the account currency. It already compensates for multiple instruments and currencies.
thankq .. more use ful infomormation.. use full to me thankqs
I appreciate you letting me know. It’s feedback like that that keeps me going.
i try your formula
lots = 0.5% * $10,000 / $10 per tick per standard lot / 20 pips = 0.25 lots, or 2.5 mini lots
but if i calculate it
0.5*10.000/10/20 = 25 not 0.25
it’s wrong, how i can correct?
0.5% needs to be 0.005.
Hallo Herr Shaun Overton,
ich habe auch ein Problem. Vielleicht könnten sie mir beim Code helfen:
Gehandelt wird auf der Dax.
Long Trade: 3% Risiko pro Trade – Position sollte auf 0,10lot auf / abgerundet werden. SL ist -15Pips
Short Trade: 4% Risiko pro Trade – Position sollte auf 0,10lot auf / abgerundet werden. SL ist -20 Pips.
Danke für Ihre Hilfe!
Make sure that your stop loss is a positive number. You wrote a negative number in your post.
The formulas should work on the DAX or any instrument. It’s not FX specific.
It is often easier to specify Stop Loss as a Rate difference, i.e. from the current Ask or Bid Rate to a Support or Resistance Level respectively, rather than in Pips. This is typically how an EA would form the Stop Loss Value, or alternatively as some (sub one) multiple of Average True Range.
Here is a minimal EA, to demonstrate Lot size Calculation. It includes normalization of the raw Lot size formed into Lot Step sizes the Server will accept, within minimum and maximum Lot sizes also.
Example Balance Risk and Stop Loss are provided as input Statements, for ease:
#property strict
input double StopLossRate = 0.00500; // Stop Loss with 50 Pips (Non JPY)
input double Balance_Risk = 0.012; // 1.2% of Account Balance risked
//+——————————————————————+
//| Expert initialization function |
//+——————————————————————+
void OnInit()
{
double tickVal;
double powTerm;
double rawLots;
double stpLots;
double lotSize;
double maxLots;
tickVal = MarketInfo (NULL, MODE_TICKVALUE);
Print (“TICKVALUE = “, tickVal);
rawLots = (Balance_Risk * AccountEquity() * Point) / (tickVal * StopLossRate);
Print (“rawLots = “, rawLots);
stpLots = MarketInfo (NULL, MODE_LOTSTEP);
lotSize = stpLots * NormalizeDouble (rawLots / stpLots, 0);
Print (“Normalized lotSize = “, lotSize);
if (lotSize maxLots)
{
lotSize = maxLots;
}
}
Print (“Trade lotSize = “, lotSize);
}
//+——————————————————————+
//| Expert deinitialization function |
//+——————————————————————+
void OnDeinit(const int reason)
{
//—
}
//+——————————————————————+
//| Expert tick function |
//+——————————————————————+
void OnTick()
{
//—
Thanks for adding this!
[Private Note]
It seems as though the .LT. sign on the minimum Lot Size stripped out some of the submitted code.
It might be easier to send you the MQ4 File, if you think it might be helpful to post, or expand your already very helpful Page.
The “powTerm” Variable can be removed. This was just a Test-bed for an EA written, that runs on M15 Time Frame. Very challenging to get into profit. I wanted a lot of Trades, for rapid Equity growth.
I had considerable problems getting the MT4 Back-tester to work. Eventually achieved a Profit Factor of 1.29 over a 7 Month Back-test with a few combinations of Parameters.
I could not get a Break Even Stop Loss to work in the Back-test EA Version. The operational EA is radically differently coded (written for real-time, with fault tolerance).
Expect to go Live shortly, if forward Demo Test is consistent with the Back-test Results. Currently migrating the Entry Filters proven in the Back-test EA, into the Operational EA. The latter has already run (with minor Bugs), at a small loss on Demo.
Please do share your code snippet. Thanks!
I added a post in my recent blog discussing some useful MQl4 functions when verifying and calculating lot size based on currency and risk parameters – http://www.ilantree.com/useful-mql4-lot-size-functions/, happy to exchange ideas with you.