Why Risk Management Matters
Here's a truth that separates profitable traders from broke ones: Your edge is worthless without risk management.
Consider two EAs with identical 55% win rates and 1:1.5 risk-reward:
The math is brutal. After 10 consecutive losses (which WILL happen eventually):
Risk management isn't optional. It's survival.
Position Sizing Methods
Method 1: Fixed Percentage Risk
The most common and recommended approach. Risk a fixed percentage of your account on each trade.
``mql4
double CalculateLotSize(double stopLossPips, double riskPercent) {
double accountBalance = AccountBalance();
double riskAmount = accountBalance * (riskPercent / 100);
double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
double pipValue = tickValue * (0.0001 / tickSize);
double lotSize = riskAmount / (stopLossPips * pipValue);
// Normalize to broker's lot step double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); lotSize = MathFloor(lotSize / lotStep) * lotStep;
// Apply min/max limits double minLot = MarketInfo(Symbol(), MODE_MINLOT); double maxLot = MarketInfo(Symbol(), MODE_MAXLOT); lotSize = MathMax(minLot, MathMin(maxLot, lotSize));
return lotSize;
}
`
Recommended risk levels:
Method 2: Fixed Lot Size
Simple but flawed. Uses the same lot size regardless of stop loss distance or account size.
Problems:
Only use for: Initial testing or very small accounts
Method 3: Volatility-Adjusted Sizing
Adjusts position size based on current market volatility (ATR).
`mql4
double CalculateVolatilityAdjustedLots(double riskPercent) {
double atr = iATR(Symbol(), Period(), 14, 0);
double stopLossPips = atr * 2 / Point / 10; // 2x ATR stop
return CalculateLotSize(stopLossPips, riskPercent);
}
``
Benefits:
Stop Loss Strategies
Hard Stop Loss (Required)
Every trade MUST have a stop loss. No exceptions.
Types of stop losses:
ATR-Based Stop Loss
``mql4
double CalculateStopLoss(int orderType, double atrMultiplier) {
double atr = iATR(Symbol(), Period(), 14, 0);
double stopDistance = atr * atrMultiplier;
if(orderType == OP_BUY) {
return Bid - stopDistance;
} else {
return Ask + stopDistance;
}
}
`
Recommended ATR multipliers:
Trailing Stop Loss
Locks in profits as price moves in your favor.
`mql4
void ManageTrailingStop(int ticket, double trailPips) {
if(OrderSelect(ticket, SELECT_BY_TICKET)) {
double trailDistance = trailPips * Point * 10;
if(OrderType() == OP_BUY) { double newStop = Bid - trailDistance; if(newStop > OrderStopLoss() && newStop > OrderOpenPrice()) { OrderModify(ticket, OrderOpenPrice(), newStop, OrderTakeProfit(), 0); } } // Similar for SELL orders... } } ``
Drawdown Control
Maximum Drawdown Limits
Set hard limits on how much your EA can lose before stopping.
``mql4
// Global variables
double startingBalance;
double maxDrawdownPercent = 20.0; // Stop at 20% drawdown
int OnInit() { startingBalance = AccountBalance(); return INIT_SUCCEEDED; }
void OnTick() { // Check drawdown before any trading double currentDrawdown = (startingBalance - AccountBalance()) / startingBalance * 100;
if(currentDrawdown >= maxDrawdownPercent) { CloseAllTrades(); Alert("Maximum drawdown reached. EA stopped."); ExpertRemove(); return; }
// Continue with normal trading logic...
}
`
Daily Loss Limits
Prevents revenge trading and emotional spirals.
`mql4
double dailyStartBalance;
double maxDailyLossPercent = 3.0;
void CheckDailyLimit() { if(dailyStartBalance == 0) { dailyStartBalance = AccountBalance(); }
double dailyLoss = (dailyStartBalance - AccountBalance()) / dailyStartBalance * 100;
if(dailyLoss >= maxDailyLossPercent) {
tradingAllowed = false;
Alert("Daily loss limit reached. Trading paused until tomorrow.");
}
}
`
Consecutive Loss Limits
Stop trading after X losses in a row.
`mql4
int consecutiveLosses = 0;
int maxConsecutiveLosses = 5;
void OnTradeClose(bool isWin) { if(isWin) { consecutiveLosses = 0; } else { consecutiveLosses++; if(consecutiveLosses >= maxConsecutiveLosses) { tradingAllowed = false; Alert("Max consecutive losses reached. Review and reset required."); } } } ``
Kelly Criterion
The Kelly Criterion tells you the mathematically optimal percentage of your bankroll to risk.
The Formula
``
Kelly % = W - [(1-W) / R]
Where:
W = Win rate (as decimal)
R = Win/Loss ratio (average win / average loss)
`
Example Calculation
Kelly % = 0.55 - [(1-0.55) / 1.5] = 0.55 - 0.30 = 0.25 = 25%
Why You Should Use Half-Kelly
Full Kelly is mathematically optimal but emotionally brutal. The variance is extreme.
Recommendations:
`mql4
double CalculateKellyRisk(double winRate, double avgWin, double avgLoss) {
double R = avgWin / avgLoss;
double kelly = winRate - ((1 - winRate) / R);
// Use Half Kelly for safety double halfKelly = kelly / 2;
// Cap at reasonable maximum return MathMin(halfKelly * 100, 5.0); // Max 5% } ``
Implementing in MQL
Complete Risk Management Class
``mql4
class CRiskManager {
private:
double m_riskPercent;
double m_maxDrawdown;
double m_dailyLossLimit;
double m_startingBalance;
double m_dailyStartBalance;
public: CRiskManager(double risk, double maxDD, double dailyLimit) { m_riskPercent = risk; m_maxDrawdown = maxDD; m_dailyLossLimit = dailyLimit; m_startingBalance = AccountBalance(); m_dailyStartBalance = AccountBalance(); }
bool CanTrade() { // Check max drawdown double dd = (m_startingBalance - AccountBalance()) / m_startingBalance * 100; if(dd >= m_maxDrawdown) return false;
// Check daily limit double dailyDD = (m_dailyStartBalance - AccountBalance()) / m_dailyStartBalance * 100; if(dailyDD >= m_dailyLossLimit) return false;
return true; }
double GetLotSize(double stopLossPips) { double riskAmount = AccountBalance() * (m_riskPercent / 100); double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE); double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE); double pipValue = tickValue * (0.0001 / tickSize);
double lots = riskAmount / (stopLossPips * pipValue);
// Normalize double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); lots = MathFloor(lots / lotStep) * lotStep;
return MathMax(MarketInfo(Symbol(), MODE_MINLOT), MathMin(MarketInfo(Symbol(), MODE_MAXLOT), lots)); }
void ResetDaily() { m_dailyStartBalance = AccountBalance(); } }; ``
Conclusion
Risk management is the foundation of profitable automated trading. Here's your checklist:
Essential Rules
Advanced Techniques
Red Flags Your Risk Management Is Broken
Remember: The goal isn't to maximize returns. It's to maximize returns while surviving.
Test your strategy's risk parameters with our Monte Carlo Calculator or contact us for a professional risk assessment.