Complete MQL5 guide & code

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

Learn the iATR handle and CopyBuffer pattern, then inspect an educational EA that calculates initial ATR-based stop and target distances. The sample is an Expert Advisor, not a plotted custom indicator. Its unconditional buy trigger is only a demonstration; it does not establish a trading edge or implement a trailing stop.

Implementation details & mechanics

Core implementation pattern

On a new chart bar, the sample reads the previous completed bar ATR. If it exceeds the configured threshold and no matching position exists, the demonstration requests a buy with fixed lots. ATR measures price-range magnitude, not direction. Replace the demonstration trigger with a specified and tested entry rule, and check volume, stop and filling-mode constraints for the broker.

Error handling & safety rules

Stop-loss and take-profit distances are set when the order is submitted. This code does not move an existing stop or trail positions. CalcAtrLotSize is a separate illustrative utility and is not called by OpenBuy; a fixed lot setting therefore does not enforce an equity-risk percentage. Minimum-volume constraints can make a proposed trade unsuitable for the requested risk budget.

MQL5 Code Implementation

//+------------------------------------------------------------------+
//|  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) != 3)
     {
      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);
   if(tickSize <= 0 || tickValue <= 0 || riskPercent <= 0) return 0.0;
   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);

   if(lotStep <= 0 || minLot <= 0 || maxLot < minLot) return 0.0;
   lotSize = MathFloor(MathMin(maxLot, lotSize) / lotStep) * lotStep;
   if(lotSize < minLot) return 0.0; // Do not round up beyond the risk budget.
   return lotSize;
  }
//+------------------------------------------------------------------+

Copy this code into MetaEditor, save it in your MQL5 folder (Experts, Scripts, or Include), and compile with F7.

Generate custom MQL5 code with Pineify AI

Describe the trade execution, indicator calculations, risk management, or data processing logic you need. Pineify generates editable MQL5 source code. Review its logic, request a syntax check, and compile and test the final file in MetaEditor.

Pine Script vs MQL5: Same Strategy, Different Platforms

AspectPine Script (TradingView)MQL5 (MetaTrader 5)
ExecutionSeries evaluated on chart updatesEvent handlers with explicit buffers
DeploymentRuns in TradingView with alertsRuns in the MT5 terminal or on a VPS
Broker accessStrategy orders are simulated; live automation needs a separate integrationDirect broker connectivity
BacktestingStrategy Tester for strategy scriptsStrategy Tester for Expert Advisors
Code complexitySimpler, functional syntaxC++-like, more powerful

The calculation can be implemented on either platform, but the runtime model and buffer APIs differ. Read the Pine Script reference guide before porting logic between them.

Frequently Asked Questions

Related MQL5 pages

Technical references

Risk and testing note

These educational examples require independent compilation and testing. A compile pass or an illustrative setting does not establish execution correctness or profitability. Any reported performance must be supported by reproducible data and test settings; past results do not predict future returns. Trading involves significant risk of loss. This content is not financial advice.