Algorithmic Risk Management in MT5: ATR Volatility Position Sizing & MQL5 Architecture

Leader 1 2 8
calendar_today agoschedule1 min read
— Originally published at guetaquant.com

Algorithmic Risk Management in MT5: Dynamic Position Sizing & MQL5 Architecture

Most retail trading failures stem from static lot sizing. Trading a fixed 1.0 lot on EUR/USD creates vastly different dollar drawdowns compared to 1.0 lot on Gold (XAU/USD) or NASDAQ (NAS100) due to differing point values, contract multipliers, and underlying volatility regimes.

In professional quantitative finance, position sizing is dynamically governed by the asset Average True Range (ATR) and a fixed risk budget per trade:

\[\\text{Position Size (Lots)} = \\frac{\\text{Account Balance} \\times \\text{Risk \\%}}{\\text{Stop Loss Distance (Points)} \\times \\text{Tick Value}}\]

In this technical guide, we implement an open-source, production-grade MQL5 risk engine that dynamically computes lot sizes and routes orders with slippage protection.

Original interactive tool and web calculator available at GuetaQuant MT5 Position Sizer.


1. MQL5 Dynamic Lot Calculation Class

//+------------------------------------------------------------------+
//|                                             GQ_RiskEngine.mqh    |
//|                             Copyright 2026, Gueta Quant (AGPLv3) |
//|                                       https://guetaquant.com     |
//+------------------------------------------------------------------+
#property copyright "Gueta Quant"
#property link      "https://guetaquant.com"

class CGQRiskManager
{
private:
   string   m_symbol;
   double   m_risk_pct;
   
public:
   CGQRiskManager(string symbol, double risk_pct) : m_symbol(symbol), m_risk_pct(risk_pct) {}
   
   double CalculateLots(double sl_distance_price, double &effective_risk_usd, bool &clamped_min)
   {
      clamped_min = false;
      if (sl_distance_price <= 0) return 0.0;
      
      double balance     = AccountInfoDouble(ACCOUNT_BALANCE);
      double risk_amount = balance * (m_risk_pct / 100.0);
      
      double tick_size   = SymbolInfoDouble(m_symbol, SYMBOL_TRADE_TICK_SIZE);
      double tick_value  = SymbolInfoDouble(m_symbol, SYMBOL_TRADE_TICK_VALUE);
      double min_lot     = SymbolInfoDouble(m_symbol, SYMBOL_VOLUME_MIN);
      double max_lot     = SymbolInfoDouble(m_symbol, SYMBOL_VOLUME_MAX);
      double lot_step    = SymbolInfoDouble(m_symbol, SYMBOL_VOLUME_STEP);
      
      if (tick_size == 0 || tick_value == 0) return 0.0;
      
      double loss_per_lot = (sl_distance_price / tick_size) * tick_value;
      if (loss_per_lot <= 0) return 0.0;
      
      double raw_lots = risk_amount / loss_per_lot;
      
      // Normalize to broker lot step
      double normalized_lots = MathFloor(raw_lots / lot_step) * lot_step;
      
      // Guard against silent risk overshoot on micro accounts
      if (normalized_lots < min_lot) 
      {
         normalized_lots = min_lot;
         clamped_min = true;
      }
      if (normalized_lots > max_lot) normalized_lots = max_lot;
      
      effective_risk_usd = normalized_lots * loss_per_lot;
      return normalized_lots;
   }
};

2. ATR Volatility Trailing Stops

Using historical volatility avoids getting stopped out during normal market noise while protecting capital during structural trend shifts:

double GetATRDistance(string symbol, ENUM_TIMEFRAMES tf, int period, double multiplier)
{
   int handle = iATR(symbol, tf, period);
   if (handle == INVALID_HANDLE) return 0.0;
   
   double atr_val[1];
   if (CopyBuffer(handle, 0, 0, 1, atr_val) <= 0) return 0.0;
   
   return atr_val[0] * multiplier;
}

3. Open Source Tools & Quantitative Ecosystem

All our quantitative research, MQL5 scripts, and risk engines are published under open-source AGPLv3:


About the Author: Mahdi Goodarzi is the founder and quantitative developer at Gueta Quant. Developer profile: g.dev/mahdigoodarzi.

2 Comments

2 votes
2
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Beyond the 98.6°F Myth: Defining Personal Baselines in Health Management

Huifer - Feb 2

Optimizing the Clinical Interface: Data Management for Efficient Medical Outcomes

Huifer - Jan 26

Pine Script v6 in 2026: math.sum, math.tanh, User Defined Types & Risk Management Engine

Guetaquant - Sep 9

The Zero-Net-Loss Fleet & The Mercenary Squad: A Live AI Economy

DEVPlank - Aug 4

SEO-Friendly Web Design Checklist: Architecture Before Aesthetics

stepan-nikonov - Aug 30
chevron_left
686 Points11 Badges
Bogotá, Colombiaguetaquant.com
3Posts
1Comments
4Connections
Founder & Lead Quantitative Engineer at Gueta Quant (https://guetaquant.com).

I design open-source... Show more

Related Jobs

View all jobs →

Commenters (This Week)

8 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!