ATR Stop & Target in Pine Script
A stop and a profit target placed a multiple of the average true range from the entry, so the trade's risk and reward scale with the market's current volatility.
Definition
An ATR stop is a stop loss placed a multiple of the average true range away from the entry price. An ATR target is a profit target placed another multiple away in the direction of the trade. Because ATR measures how much the market has been moving per bar, the distances stretch in volatile conditions and tighten in quiet ones without the trader changing a number. The pair belongs to the volatility stops family with the chandelier exit and the trailing stop. Unlike those, it is fixed at entry: the levels are set once from that bar's ATR and do not move.
The reason it works is that a stop has to sit outside the noise. A fixed percentage or a round number ignores whether the market is moving 0.5 percent a day or 3 percent. It is then either too tight in the volatile case or too loose in the quiet one. An ATR multiple asks how many typical bars of movement the trade can survive, which is the question that matters. The target does the same in reverse: 3 ATR is a distance the market has shown it can travel, whereas a fixed price target may be unreachable in a quiet regime.
Wilder introduced ATR in 1978 together with RSI, and measuring stops in ATR units followed in the trend-following systems of the 1980s, where a 2 ATR stop and position sizing by ATR were standard. The common multiples today are 1.5 to 3 ATR for the stop and 2 to 4 for the target, with a 14-period ATR. The ratio between the two is the trade's R multiple. A 2 ATR stop with a 3 ATR target risks 1 R to make 1.5 R, and the win rate needed to break even follows from that ratio alone.
In the library's strategies the block is the same everywhere. On the entry bar the script reads a 14-period ATR. For a long it places the stop at the close minus the stop multiple times ATR, and the target at the close plus the target multiple times ATR; a short mirrors both. The levels stay fixed until the trade closes on the stop, the target or the opposite signal. Setting a multiple to 0 removes that level. The EMA cross and MACD strategies carry both. The RSI, Bollinger and Donchian strategies carry the stop only and exit by their own rule, and the SuperTrend strategy carries the target only and exits on the flip.
Reading the chart
Placing the levels is a sequence: read the ATR, choose the multiples from the setup, then size the position from the stop.
- Read the current 14-period ATR on the chart's timeframe and write it in price units. On a stock at 100 with an ATR of 2, one ATR is 2 percent; on the same stock in a quiet month it may be 1.
- Choose the stop multiple from the setup. A trend-following entry needs 2 to 3 ATR to survive a normal pullback; a reversion entry at an extreme can work with 1.5, because the trade is wrong quickly if it is wrong.
- Choose the target multiple from the expected move, or leave it off. Trend entries that exit on the opposite signal often do better without a target; reversion entries exit at the mean and rarely need one.
- Compute the R multiple as target divided by stop and check the win rate the system needs. A 1.5 R trade breaks even at a 40 percent win rate before costs; a 0.5 R trade needs 67 percent.
- Size the position from the stop distance, not the other way round. Divide the money you will risk by the stop distance in price units to get the number of shares or contracts.
- Leave the levels alone once set, unless the plan is a trailing stop. Moving an ATR stop closer after entry recreates the noise problem it was placed to avoid.
The maths
The stop and the target are multiples of ATR measured from the entry price, and their ratio is the trade's R multiple.
The library's strategies use the entry bar's close as E when placing the levels, and the order fills on the next bar's open, so the realised distances differ slightly from the nominal multiples.
ATR is in the instrument's price units; the same 2 ATR stop is a different percentage on every chart and on every day, which is the point.
A stop multiple of 0 removes the stop and a target multiple of 0 removes the target; the position then closes only on the strategy's own exit rule or the opposite signal.
The break-even win rate for an R multiple is 1 / (1 + R) before costs; commission and slippage raise it.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Stop (ATR multiples) | 2 | Wider stops survive more noise and lose more per trade when hit; tighter stops cut the loss per trade and raise the number of trades stopped before the move. |
| Target (ATR multiples, 0 = off) | 3 (0 in the SuperTrend strategy) | A closer target raises the win rate and caps the winners; a further one, or none, lets trend trades run and lowers the win rate. |
| ATR length | 14 (fixed in the templates) | Shorter lengths make the levels react to the last few bars; longer ones set the distances from the month's typical bar. |
In practice
- As the risk block of a trend system: an EMA cross or a MACD cross with a 2 ATR stop and a 3 ATR target on every trade. That is exactly what the library's EMA Cross and MACD Cross strategies run.
- As the stop of a reversion or breakout system: the RSI, Bollinger and Donchian strategies in the library carry an ATR stop only. Each exits by its own rule, at RSI 50, at the basis or at the opposite shorter channel.
- As the target of an always-in system: the SuperTrend strategy uses the flip as its stop and takes an optional ATR target. A trade can then bank a defined move before the trend line catches up.
- As the position-sizing input: dividing the money risked per trade by the stop distance in price gives a size that keeps every trade's risk equal regardless of the instrument's volatility.
- As a fixed alternative to trailing: where a trailing stop gives back open profit in a reversal, a fixed ATR target takes it. The choice depends on whether the system's results come from the long tail or from the average move.
Where it goes wrong
- Using the same multiple on every timeframe and setup. A 2 ATR stop is right for a trend entry and loose for a reversion entry; choose the multiple from the setup.
- Sizing first and placing the stop where the size allows. The stop belongs outside the noise; the size follows from it.
- Putting a target on a trend system by habit. A 3 ATR target on an EMA cross caps the trades the system depends on; test the strategy with the target set to 0.
- Forgetting that ATR changes. A stop set from a volatile week may be far too wide once the market calms; the multiples are fixed per trade, but the reading is refreshed on every new entry.
Compared with
Trailing Stop. A trailing stop follows price as the trade moves into profit and never moves back; an ATR stop is fixed at entry. The trail protects open profit in a trend and gives some back at the end; the fixed pair defines the outcome in advance. The library's EMA Cross with Trailing Stop swaps one block for the other.
Chandelier Exit. The chandelier exit hangs a stop a multiple of ATR below the highest high since entry, so it is a trailing ATR stop that ratchets. An ATR stop measured from the entry stays where it was placed. Both use the same volatility unit; the chandelier follows the trend and the fixed stop does not.
Average True Range. ATR is the measure; the stop and target are the use. ATR on its own says how much the market moves per bar in price units. The stop and target turn that into distances from the entry and, with the money risked, into a position size.
Support & Resistance. A structural stop sits beyond a level, a swing low or a prior day's low, so its distance is set by the chart. An ATR stop sits at a distance set by volatility, wherever that lands. Traders combine them: place the stop beyond the level and check that the distance is at least one ATR.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
What ATR multiple should I use for a stop?
2 is the usual starting point on daily bars, and the library's default. Trend entries that need to survive a pullback use 2 to 3; reversion entries at an extreme use 1.5 to 2, because they are wrong quickly if they are wrong. Below 1 the stop sits inside the normal bar and is hit by noise. Test a range of multiples on your market and pick the one whose losses are noise-sized and whose winners are not cut short.
What is an R multiple?
The outcome of a trade measured in units of its initial risk. If the stop is 2 ATR below the entry, 1 R is 2 ATR. A target 3 ATR away is 1.5 R, and a trade that closes on the opposite signal after running 6 ATR made 3 R. Recording results in R makes trades on different instruments comparable and makes the required win rate for any target explicit.
Why an ATR stop instead of a percentage?
Because a percentage ignores what the market is doing. A 2 percent stop on a stock that moves 3 percent a day is inside the noise and gets hit constantly. The same stop on a stock that moves 0.5 percent a day is far wider than it needs to be. ATR measures the actual bar size, so a 2 ATR stop is always about two typical bars away, on any chart and in any regime.
Should a trend strategy have a target?
Often not. Trend systems depend on the few trades that run a long way, and a 3 ATR target exits them early. The library's EMA Cross, MACD and SuperTrend strategies take the target as an input with 0 meaning off, so the comparison is one backtest each way. Reversion systems, which exit at the mean, rarely need a target at all.
How do the library's strategies place the stop and target?
On the entry bar they read a 14-period ATR. The stop goes at the close minus the stop multiple times ATR for a long, or plus for a short, and the target at the close plus or minus the target multiple times ATR. The levels stay fixed until the trade closes on the stop, the target or the opposite signal. A multiple of 0 removes that level. The order fills on the next bar.
How do I size a position with an ATR stop?
Decide the money you will risk on the trade, then divide by the stop distance in price. With 500 at risk and a stop 2 ATR away on a stock with an ATR of 1.25, the stop distance is 2.5 and the size is 200 shares. The size falls when volatility rises and grows when it falls, which keeps every trade's risk the same in money terms.