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.
How the Supertrend signal works
Bullish flip
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.
Bearish flip
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 Custom Indicator 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, save it in the MQL5/Indicators folder, and compile with F7.
Change the settings or add your own signal filter
Describe the ATR inputs, alert rules, visual style, or confirmation filter you want. Pineify generates editable MQL5 source code that you can inspect and compile in MetaEditor.
Pine Script vs MQL5: Same Strategy, Different Platforms
| Aspect | Pine Script (TradingView) | MQL5 (MetaTrader 5) |
|---|---|---|
| Execution | Series evaluated on chart updates | Event handlers with explicit buffers |
| Deployment | Runs in TradingView with alerts | Runs in the MT5 terminal or on a VPS |
| Broker access | Via TradingView broker integration | Direct broker connectivity |
| Backtesting | Strategy Tester for strategy scripts | Strategy Tester for Expert Advisors |
| Code complexity | Simpler, functional syntax | C++-like, more powerful |
The calculation can be implemented on either platform, but the runtime model and buffer APIs differ. Read the Pine Script Supertrend reference before porting logic between them.
Frequently Asked Questions
Related MQL5 pages
Risk and testing note
Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.