The Average True Range (ATR) is a widely used volatility indicator that helps traders measure market volatility. Unlike other indicators focused on price direction, ATR is purely about assessing price range, making it especially useful for managing risk. In this article, we’ll explain how ATR is calculated, its applications in risk management, and how to use ATR to set dynamic stop-loss levels in an algorithmic trading strategy. We’ll also provide an MQL4 example of an ATR-based stop-loss.
What is the Average True Range (ATR)?
Developed by J. Welles Wilder, the ATR is a volatility indicator that measures the average range of price movements over a specified period. It helps traders understand how much an asset’s price typically moves, which is essential for setting realistic stop-loss and take-profit levels that account for volatility.
The True Range (TR) is the largest of:
- The difference between today’s high and low,
- The difference between today’s high and yesterday’s close, or
- The difference between today’s low and yesterday’s close.
The ATR is the average of the True Range over a specified number of periods, usually 14.
Calculating the ATR
1. Calculate the True Range (TR) for each period:
2. Calculate the ATR:
The ATR is the moving average of the True Range values over a given period, typically 14.
Applications of ATR in Risk Management
1. Setting Dynamic Stop-Losses:
- Using ATR to set stop-losses helps traders adjust stops based on market volatility. In a highly volatile market, a wider stop-loss (e.g., 2 * ATR) prevents premature exit, while in a low-volatility environment, a tighter stop-loss (e.g., 1 * ATR) preserves gains.
2. Position Sizing:
- ATR can guide position sizing based on risk tolerance. For example, traders can set position sizes to risk a consistent percentage of their capital, calculated relative to the ATR.
3. Trailing Stops:
- ATR-based trailing stops allow for dynamic adjustment as the trade moves in a profitable direction, locking in profits while giving the trade room to breathe during volatility fluctuations.
ATR-Based Stop-Loss Strategy Example in MQL4
The following MQL4 example demonstrates how to set a dynamic stop-loss using the ATR indicator. In this strategy, we’ll use ATR to adjust the stop-loss based on current market volatility, with a stop distance set as a multiple of the ATR value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // ATR-Based Stop-Loss Strategy in MQL4 input int ATRPeriod = 14; // Period for ATR input double ATRMultiplier = 2.0; // Multiplier for stop-loss distance input double LotSize = 0.1; // Lot size for orders // Global variables to track trade status bool buyTradeOpen = false; bool sellTradeOpen = false; void OnTick() { double atrValue = iATR(NULL, 0, ATRPeriod, 0); double stopLossDistance = ATRMultiplier * atrValue; double buyPrice = Ask; double sellPrice = Bid; // Check if there are no open trades before placing a new trade if (!buyTradeOpen && !sellTradeOpen) { // Example buy condition: Price above a moving average double ma = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0); if (buyPrice > ma) { OpenBuy(stopLossDistance); } // Example sell condition: Price below a moving average else if (sellPrice < ma) { OpenSell(stopLossDistance); } } } // Function to open a buy position with ATR-based stop-loss void OpenBuy(double stopLossDistance) { double stopLoss = Ask - stopLossDistance; int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, stopLoss, 0, "ATR Buy", 0, 0, clrGreen); if (ticket > 0) { buyTradeOpen = true; sellTradeOpen = false; Print("Buy order opened with ATR-based stop-loss."); } else { Print("Error opening buy order: ", GetLastError()); } } // Function to open a sell position with ATR-based stop-loss void OpenSell(double stopLossDistance) { double stopLoss = Bid + stopLossDistance; int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, stopLoss, 0, "ATR Sell", 0, 0, clrRed); if (ticket > 0) { sellTradeOpen = true; buyTradeOpen = false; Print("Sell order opened with ATR-based stop-loss."); } else { Print("Error opening sell order: ", GetLastError()); } } |
Explanation of the ATR Strategy
1. Parameters:
ATRPeriod
sets the period for calculating ATR, with 14 as the typical default.ATRMultiplier
determines how far the stop-loss is from the entry price based on ATR.LotSize
specifies the trade size.
2. Trade Execution:
- A buy order is opened if the price is above the 50-period moving average, with the stop-loss set at
Ask - (ATR * ATRMultiplier)
. - A sell order is opened if the price is below the moving average, with the stop-loss set at
Bid + (ATR * ATRMultiplier)
.
3. Functions:
OpenBuy()
andOpenSell()
open trades with ATR-based stop-losses.
This strategy provides adaptive risk management by adjusting stop-losses based on current market volatility, helping to avoid early exits in volatile conditions.
Tips for Optimising ATR-Based Stop-Loss Strategies
- Experiment with ATR Multipliers: Test different ATR multipliers based on asset volatility. For example, use a larger multiplier (e.g., 3 * ATR) in high-volatility assets to allow trades more room.
- Combine with Volume Indicators: Use volume indicators to confirm trades, entering only when volatility is confirmed by volume to avoid false signals.
- Adjust ATR Period: Shorter ATR periods make the stop-loss more responsive, while longer periods smooth out volatility.
Conclusion
The Average True Range (ATR) is a valuable tool for managing risk in algorithmic trading, offering a way to set dynamic stop-loss levels that adapt to market volatility. This MQL4 example demonstrates how to use ATR for more flexible risk management, helping traders stay in trades during high volatility while protecting capital in calmer markets. In the next article, we’ll discuss Volume Indicators in Algo Trading, focusing on the On-Balance Volume (OBV) and VWAP indicators and their roles in confirming trends and assessing trade strength.