Home/Blog/Risk Management
Risk Management2024-11-2815 min read

Complete Guide to EA Risk Management: Position Sizing, Stop Loss, and Drawdown Control

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:

  • EA 1: Risks 10% per trade → Blows up after a losing streak
  • EA 2: Risks 1% per trade → Compounds steadily over years
  • The math is brutal. After 10 consecutive losses (which WILL happen eventually):

  • EA 1: Down 65% (needs 186% gain to recover)
  • EA 2: Down 9.6% (needs 10.6% gain to recover)
  • 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:

  • Conservative: 0.5-1% per trade
  • Moderate: 1-2% per trade
  • Aggressive: 2-3% per trade
  • Gambling: >3% per trade (not recommended)
  • Method 2: Fixed Lot Size

    Simple but flawed. Uses the same lot size regardless of stop loss distance or account size.

    Problems:

  • Doesn't scale with account growth
  • Variable risk per trade
  • Can risk too much on wide stops
  • 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:

  • Smaller positions in volatile markets
  • Larger positions in calm markets
  • Natural risk normalization
  • Stop Loss Strategies

    Hard Stop Loss (Required)

    Every trade MUST have a stop loss. No exceptions.

    Types of stop losses:

  • 1. Fixed pips: Simple but doesn't adapt to volatility
  • 2. ATR-based: Adapts to current market conditions
  • 3. Structure-based: Below/above key support/resistance
  • 4. Percentage-based: Fixed % of entry price
  • 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:

  • Scalping: 1.0-1.5x ATR
  • Day trading: 1.5-2.5x ATR
  • Swing trading: 2.0-3.0x ATR
  • 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

  • Win rate: 55% (W = 0.55)
  • Average win: $150
  • Average loss: $100
  • R = 150/100 = 1.5
  • 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:

  • Half Kelly (12.5% in example above): Good balance
  • Quarter Kelly (6.25%): Conservative, smoother equity curve
  • Never exceed Kelly: Mathematically guaranteed to hurt you
  • `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

  • 1. Never risk more than 2% per trade (1% is safer)
  • 2. Always use stop losses - No exceptions, ever
  • 3. Set daily loss limits - 3-5% maximum
  • 4. Set maximum drawdown - Stop at 20-30%
  • 5. Use Kelly/Half-Kelly - Don't guess, calculate
  • Advanced Techniques

  • Volatility-adjusted position sizing
  • Trailing stops for trend-following
  • Scaling in/out of positions
  • Correlation-based exposure limits
  • Monte Carlo stress testing
  • Red Flags Your Risk Management Is Broken

  • Averaging down on losers
  • Removing stop losses
  • Increasing size after losses
  • Single trade can blow 10%+ of account
  • No daily or weekly limits
  • 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.

    🧑‍💻

    TradeMetrics Pro Team

    Expert EA developers with 10+ years of experience in automated trading systems.

    Need Help With Your EA Project?

    Get expert assistance with strategy conversion, EA development, or optimization.