4.1 Position size based on risk
In the course on trading plans you learned that position size is the most important decision on every trade. A robot with a fixed size risks more when the stop loss is far away, and less when it is close. We want to avoid that.
The calculation
Size = risk in money ÷ loss per lot at the stop loss
The platforms state what one tick (the smallest price change) is worth for one lot – in the account currency. The loss per lot is then:
Loss per lot = distance to stop loss ÷ tick size × tick value
MQL5
input double InpRiskPercent = 1.0; // Risk per trade in %
double LotSize(double slDistance) // slDistance in price, for example 30 * pip
{
double riskMoney = AccountInfoDouble(ACCOUNT_EQUITY) * InpRiskPercent / 100.0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if(slDistance <= 0 || tickValue <= 0 || tickSize <= 0) return(0);
double lots = riskMoney / (slDistance / tickSize * tickValue);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lots = MathFloor(lots / step) * step; // round down to a valid size
if(lots < SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN)) return(0);
return(MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX)));
}MQL4
The same, but with AccountEquity() and MarketInfo(Symbol(), MODE_TICKVALUE), MODE_TICKSIZE, MODE_LOTSTEP, MODE_MINLOT and MODE_MAXLOT.
cTrader
cTrader makes it even simpler. Symbol.PipValue is the value of one pip for one unit:
double riskMoney = Account.Equity * RiskPercent / 100.0;
double volume = riskMoney / (StopLossPips * Symbol.PipValue);
volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);
if (volume < Symbol.VolumeInUnitsMin) return; // too small: no tradeAlways round down
Notice that we round down. If you round up, you risk a little more than planned on every trade. If the calculated size is smaller than the minimum allowed size, it is safest not to trade – not to round up to the minimum.
The calculation in practice
Equity €10,000, risk 1% = €100. Stop loss 25 pips on EUR/USD. Tick size 0.00001 and tick value about €0.91 per lot. The distance of 0.0025 is 250 ticks, which gives a loss of about €227 per lot. 100 ÷ 227 = 0.4405 – rounded down to 0.44 lot.
Try it yourself
Run the robot with 0.5% and 2% risk. How much does the maximum drawdown change?
Key takeaways
- Calculate the size so that every trade risks a fixed percentage of the account.
- Loss per lot = distance to stop loss ÷ tick size × tick value.
- Always round down, and do not trade if the size becomes too small.
65% of retail investor accounts lose money when trading CFDs with this provider.