ATR Indicator MQL5: Custom Code, Stop-Loss Sizing & EA Integration

This page covers how to implement the Average True Range (ATR) indicator in MQL5 using the iATR() handle pattern introduced in MetaTrader 5. It walks through custom ATR indicator code, dynamic stop-loss sizing based on ATR multiples, and integrating ATR-based risk management into Expert Advisors across multiple currency pairs and asset classes.

atr indicator mql5mql5 atr stop lossaverage true range mql5 eamql5 atr position sizingiATR mql5 handle

Strategy Logic

Entry Conditions

N/A — this is a tutorial/indicator reference page. ATR itself is not a directional entry signal but is used to size stops and filter low-volatility environments. Example EAs in the code section demonstrate how to gate entries when ATR exceeds a minimum threshold, ensuring trades are only placed during sufficiently volatile market conditions.

Exit Conditions

N/A — this is a tutorial/indicator reference page. The code examples show how to calculate a trailing stop distance as a multiple of the current ATR value (e.g., 1.5x ATR), allowing exits to widen during high-volatility sessions and tighten during consolidation.

MQL5 Expert Advisor Code

//+------------------------------------------------------------------+
//|  ATR Indicator MQL5 — Stop-Loss Sizing & EA Integration Demo     |
//|  For educational purposes only. Not financial advice.            |
//+------------------------------------------------------------------+
#property copyright "Pineify — pineify.app"
#property link      "https://pineify.app"
#property version   "1.00"
#property strict

//--- Input parameters
input int    InpAtrPeriod      = 14;      // ATR Period
input double InpAtrMultiplySL  = 1.5;    // ATR multiplier for stop-loss
input double InpAtrMultiplyTP  = 3.0;    // ATR multiplier for take-profit
input double InpMinAtrFilter   = 0.0005; // Minimum ATR to allow entries (0 = disabled)
input double InpLotSize        = 0.1;    // Fixed lot size
input int    InpMagicNumber    = 202406; // Magic number
input int    InpSlippage       = 10;     // Max slippage in points

//--- Global handles and buffers
int    g_atrHandle   = INVALID_HANDLE;
double g_atrBuffer[];
bool   g_tradeOpen   = false;
ulong  g_ticket      = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- Create ATR indicator handle
   g_atrHandle = iATR(_Symbol, _Period, InpAtrPeriod);
   if(g_atrHandle == INVALID_HANDLE)
     {
      Print("ERROR: Failed to create ATR handle. Error code: ", GetLastError());
      return INIT_FAILED;
     }

   //--- Set ATR buffer as a series (newest value at index 0)
   ArraySetAsSeries(g_atrBuffer, true);

   Print("ATR Indicator EA initialized on ", _Symbol,
         " | Period: ", InpAtrPeriod,
         " | SL Multiplier: ", InpAtrMultiplySL,
         " | TP Multiplier: ", InpAtrMultiplyTP);

   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- Release the ATR indicator handle to free memory
   if(g_atrHandle != INVALID_HANDLE)
     {
      IndicatorRelease(g_atrHandle);
      g_atrHandle = INVALID_HANDLE;
     }

   Print("ATR Indicator EA deinitialized. Reason code: ", reason);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //--- Only act on new bar open to avoid multiple signals per bar
   static datetime s_lastBarTime = 0;
   datetime currentBarTime = iTime(_Symbol, _Period, 0);
   if(currentBarTime == s_lastBarTime)
      return;
   s_lastBarTime = currentBarTime;

   //--- Copy ATR values into buffer (need at least 2 values)
   if(CopyBuffer(g_atrHandle, 0, 0, 3, g_atrBuffer) < 2)
     {
      Print("WARNING: Not enough ATR data yet.");
      return;
     }

   //--- Read the completed bar's ATR (index 1 = previous closed bar)
   double atrValue = g_atrBuffer[1];

   if(atrValue <= 0)
      return;

   //--- Apply minimum ATR filter — skip if market is too quiet
   if(InpMinAtrFilter > 0 && atrValue < InpMinAtrFilter)
     {
      Print("ATR filter active — current ATR ", DoubleToString(atrValue, _Digits),
            " below minimum ", DoubleToString(InpMinAtrFilter, _Digits), ". No trade.");
      return;
     }

   //--- Calculate dynamic SL and TP distances in price
   double slDistance = atrValue * InpAtrMultiplySL;
   double tpDistance = atrValue * InpAtrMultiplyTP;

   //--- Get current market prices
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
   int    digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);

   //--- Log current ATR metrics for analysis
   PrintFormat("Bar[%s] ATR=%.5f | SL dist=%.5f | TP dist=%.5f | Ask=%.5f | Bid=%.5f",
               TimeToString(currentBarTime, TIME_DATE | TIME_MINUTES),
               atrValue, slDistance, tpDistance, ask, bid);

   //--- Demo: open a buy if no position is open
   //    In a real EA, replace this with your directional signal logic
   if(!IsPositionOpen())
     {
      double sl = NormalizeDouble(bid - slDistance, digits);
      double tp = NormalizeDouble(ask + tpDistance, digits);

      OpenBuy(ask, sl, tp);
     }
  }

//+------------------------------------------------------------------+
//| Check whether a position with our magic is already open         |
//+------------------------------------------------------------------+
bool IsPositionOpen()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i);
      if(ticket == 0) continue;
      if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
         PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
         return true;
     }
   return false;
  }

//+------------------------------------------------------------------+
//| Open a market Buy order using MqlTradeRequest                   |
//+------------------------------------------------------------------+
bool OpenBuy(double price, double sl, double tp)
  {
   MqlTradeRequest request = {};
   MqlTradeResult  result  = {};

   request.action    = TRADE_ACTION_DEAL;
   request.symbol    = _Symbol;
   request.volume    = InpLotSize;
   request.type      = ORDER_TYPE_BUY;
   request.price     = price;
   request.sl        = sl;
   request.tp        = tp;
   request.deviation = InpSlippage;
   request.magic     = InpMagicNumber;
   request.comment   = "ATR-EA Buy";
   request.type_filling = ORDER_FILLING_FOK;

   bool sent = OrderSend(request, result);

   if(sent && result.retcode == TRADE_RETCODE_DONE)
     {
      PrintFormat("BUY opened | Ticket=%d | Price=%.5f | SL=%.5f | TP=%.5f",
                  result.order, result.price, sl, tp);
      return true;
     }
   else
     {
      PrintFormat("ERROR opening BUY | retcode=%d | comment=%s",
                  result.retcode, result.comment);
      return false;
     }
  }

//+------------------------------------------------------------------+
//| Utility: compute ATR-based lot size for fixed-risk position     |
//| riskPercent — percentage of account equity to risk per trade    |
//| atr         — current ATR value                                 |
//| multiplier  — how many ATRs to use for the stop distance        |
//+------------------------------------------------------------------+
double CalcAtrLotSize(double riskPercent, double atr, double multiplier)
  {
   double equity      = AccountInfoDouble(ACCOUNT_EQUITY);
   double riskAmount  = equity * (riskPercent / 100.0);
   double stopPoints  = (atr * multiplier) / SymbolInfoDouble(_Symbol, SYMBOL_POINT);
   double tickValue   = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   double tickSize    = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   double pointValue  = tickValue / (tickSize / SymbolInfoDouble(_Symbol, SYMBOL_POINT));

   if(stopPoints <= 0 || pointValue <= 0)
      return 0.0;

   double lotSize = riskAmount / (stopPoints * pointValue);

   double minLot  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double maxLot  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);

   lotSize = MathFloor(lotSize / lotStep) * lotStep;
   lotSize = MathMax(minLot, MathMin(maxLot, lotSize));

   return NormalizeDouble(lotSize, 2);
  }
//+------------------------------------------------------------------+

Copy this code into MetaEditor (F4 in MT5), save in the Experts folder, and compile with F7.

Generate a Custom Multi-pair Volatility EA →

Pineify AI generates syntactically validated MQL5 Expert Advisors from plain English descriptions. Customize entry logic, risk management, and trading sessions — no coding required.

Pine Script vs MQL5: Same Strategy, Different Platforms

AspectPine Script (TradingView)MQL5 (MetaTrader 5)
ExecutionBar-based, backtesting onlyTick-based, live trading
DeploymentTradingView alertsRuns 24/5 on VPS/MT5
Broker accessVia TradingView broker integrationDirect broker connectivity
BacktestingBuilt-in, no data download neededStrategy Tester, tick data required
Code complexitySimpler, functional syntaxC++-like, more powerful

Pineify supports both platforms. Prototype your strategy visually in TradingView Pine Script, then generate the equivalent MQL5 EA for live MT5 trading.

Frequently Asked Questions

Related MQL5 Expert Advisors