The previous tutorial said trend following needs a stop that only moves up. This one computes that stop from scratch.
Step 1: what ATR is
ATR stands for Average True Range, and it answers one question: how much does this stock typically move in a day, in dollars?
Start with a single day's true range — the largest of three numbers:
TR = max(today's high - today's low,
|today's high - yesterday's close|,
|today's low - yesterday's close|)
The last two terms handle gaps. If today opens far above yesterday's close, looking only at today's high and low understates how far price really travelled.
Smooth TR (14 days by default) and you have ATR. ATR is denominated in dollars, not percent — which matters: it makes the stop distance adapt automatically to the instrument's volatility, so you never have to widen the stop by hand for a jumpy stock.
Step 2: the formula
stop line = highest price since entry - ATR × multiple
effective stop = the maximum of every stop line computed since entry
The second line is everything. The stop only moves up. When price pulls back, the highest price since entry does not change but ATR may rise, so the formula produces a smaller number — and we discard it, keeping the higher previous value.
Our engine uses two multiples:
entry_mult = 2.0— the initial stop on the entry day, held closer;exit_mult = 3.0— the trailing stop thereafter, held wider to give the trend room.
Step 3: seven days of one trade
Suppose a stock meets the entry conditions on day 1 (close above the 100-day moving average and a new 22-day high) and you buy at the close of 101.00.
| Day | High | Close | ATR | Highest since entry | Formula output | Effective stop |
|---|---|---|---|---|---|---|
| 1 | 102.00 | 101.00 | 4.00 | 102.00 | 102.00 − 2×4.00 = 94.00 | 94.00 |
| 2 | 106.00 | 105.00 | 4.20 | 106.00 | 106.00 − 3×4.20 = 93.40 | 94.00 |
| 3 | 110.00 | 109.00 | 4.50 | 110.00 | 110.00 − 3×4.50 = 96.50 | 96.50 |
| 4 | 109.00 | 104.00 | 4.80 | 110.00 | 110.00 − 3×4.80 = 95.60 | 96.50 |
| 5 | 118.00 | 117.00 | 5.00 | 118.00 | 118.00 − 3×5.00 = 103.00 | 103.00 |
| 6 | 119.00 | 112.00 | 5.40 | 119.00 | 119.00 − 3×5.40 = 102.80 | 103.00 |
| 7 | 114.00 | — | 5.40 | 119.00 | — | low breaks 103.00 → exit |
Row by row:
- Day 1 uses
entry_mult = 2.0, so the stop is 102 − 8 = 94, not 90. The initial stop is tighter because at this moment the trade has no open profit acting as a buffer. - Day 2 the formula produces 93.40, below yesterday's 94.00. We discard it and hold at 94.00. That is the ratchet.
- Day 4 price pulls back (close falls from 109 to 104) and ATR expands, producing 95.60. Again below the prior 96.50, so the stop stays at 96.50.
- Day 5 a large advance lifts the highest-since-entry to 118 and the stop jumps to 103.00. The trade has now locked in a profit: even if stopped tomorrow, an exit at 103 is above the 101 entry.
- Day 7 the intraday low breaks 103.00 and the position is closed.
Step 4: the arithmetic of this trade
- Entry 101.00, exit 103.00 → +1.98%.
- Highest close during the trade was 117.00, an open profit of +15.8% at its peak.
- So roughly 12 percentage points were handed back between the high and the exit.
This has to be said plainly, because it is where beginners are most often disappointed: a trailing stop will always give back part of the profit. Its design goal is not to sell at the high — no rule can do that — but to let you keep most of a trend without ever having to judge where the top is.
Cost and benefit are the same coin: precisely because the stop was wide enough (3 × ATR), the pullbacks on days 4 and 6 did not shake you out, which is why you were still there for day 5. Set exit_mult to 1.5 and you would have exited on day 4, with the rest of the advance happening without you.
Step 5: change exactly one parameter
Open the backtest tool, run the defaults, and note the trade count, win rate and total return. Then change one parameter only:
- **
exit_multfrom 3.0 to 2.0** — tighter stop, more trades, smaller average win. See whether total return improves or degrades. - **
exit_multfrom 3.0 to 5.0** — looser stop, fewer shake-outs, but more given back on every exit.
You will find neither direction improves monotonically: there is a region where both tighter and looser are worse. Locating that region approximately is far more useful than memorising one "optimal" value. For why chasing that optimum is a mistake, see the overfitting section of Three Traps in Backtesting.
Common questions
Is the stop triggered by the close or intraday? Our engine triggers on the intraday low, matching how a resting stop order behaves in live trading, and it is the more conservative choice. Triggering on the close flatters the backtest.
What if a gap opens below the stop? You are filled at the open and the realised loss exceeds the planned loss. A stop is a price level, not an insurance contract; this risk cannot be tuned away, only limited by sizing each position.
Can I add a profit target? You can, but trend-following returns are heavily concentrated in a few large winners, and a fixed target truncates exactly those trades. In practice it usually reduces total return substantially. That is why strategies of this kind carry no profit target by default.
This article is for quantitative research and educational purposes only and does not constitute investment advice. Historical backtest results do not represent future returns; trading risk is borne entirely by the investor.