Strategy vault / Trend following
ATR Trailing Stop Strategy with Fibonacci Retracement Targets
This strategy combines Average True Range (ATR) trailing stop and Fibonacci retracement lines to design a trend following strategy with stop loss protection. When price breaks through the ATR trailing stop line, the…

Overview
This strategy combines Average True Range (ATR) trailing stop and Fibonacci retracement lines to design a trend following strategy with stop loss protection. When price breaks through the ATR trailing stop line, the strategy starts to follow the trend. At the same time, Fibonacci retracement lines are used to set price targets, achieving an organic combination of trend following, stop loss and take profit.
Strategy Logic
- Calculate ATR value and ATR trailing stop line. The ATR trailing stop line is calculated by multiplying ATR value by a factor (e.g. 3.5).
- Calculate three Fibonacci retracement lines as profit targets. Fibonacci retracement lines are positioned between the ATR trailing stop line and the new high/low point according to Fibonacci ratios (e.g. 61.8%, 78.6%, 88.6%).
- Generate buy/sell signals when price breaks through the ATR trailing stop line to follow the trend.
- Set take profit targets at the three Fibonacci retracement lines.
Advantages
- ATR stop loss can effectively control risks and prevent losses from expanding.
- Fibonacci targets allow decent profits during trends while avoiding chasing tops and bottoms.
- The strategy logic is simple and easy to implement.
- Flexibility to adjust ATR factor and Fibonacci settings to adapt to different markets.
Risks
- Frequent ATR stop loss triggers in ranging markets, leading to excessive trading.
- Possibilities of missing pullbacks and adjustments.
- Parameter optimization needed for ATR period etc.
Enhancement
- Incorporate trend filter to avoid trading in ranging markets.
- Add re-entry mechanism to reduce missing pullbacks.
- Test and optimize ATR period, ATR multiplier, Fibonacci parameters etc.
Summary
The strategy integrates two important technical analysis methods – ATR trailing stop and Fibonacci retracement for trend following, risk control and profit targeting. With further optimizations, it can become a very practical trend trading strategy that is more robust and adaptive to market conditions.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 5 | ATR Period |
| v_input_2 | 3.5 | ATR Factor |
| v_input_3 | 61.8 | Fib1 Level |
| v_input_4 | 78.6 | Fib2 Level |
| v_input_5 | 88.6 | Fib3 Level |
Source Pine Script
/*backtest
start: 2023-02-21 00:00:00
end: 2024-02-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("ATR TrailStop with Fib Targets", overlay=true)
// Input parameters
atrPeriod = input(5, title="ATR Period")
ATRFactor = input(3.5, title="ATR Factor")
Fib1Level = input(61.8, title="Fib1 Level")
Fib2Level = input(78.6, title="Fib2 Level")
Fib3Level = input(88.6, title="Fib3 Level")
// ATR Calculation
atrValue = ta.atr(atrPeriod)
// ATR TrailStop Calculation
loss = ATRFactor * atrValue
trendUp = close[1] > close[2] ? (close - loss > close[1] ? close - loss : close[1]) : close - loss
trendDown = close[1] < close[2] ? (close + loss < close[1] ? close + loss : close[1]) : close + loss
trend = close > close[2] ? 1 : close < close[2] ? -1 : 0
trailStop = trend == 1 ? trendUp : trendDown
// Fibonacci Levels Calculation
ex = trend > trend[1] ? high : trend < trend[1] ? low : na
fib1 = ex + (trailStop - ex) * Fib1Level / 100
fib2 = ex + (trailStop - ex) * Fib2Level / 100
fib3 = ex + (trailStop - ex) * Fib3Level / 100
// Plotting
plot(trailStop, title="TrailStop", color=color.red)
plot(fib1, title="Fib1", color=color.white)
plot(fib2, title="Fib2", color=color.white)
plot(fib3, title="Fib3", color=color.white)
// Buy and Sell Signals
longCondition = close > trailStop and close[1] <= trailStop
shortCondition = close < trailStop and close[1] >= trailStop
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-28. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/ATR-Trailing-Stop-Strategy-with-Fibonacci-Retracement-Targets.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Multi Period Trend Confirmation Dynamic Risk Control Quantitative Trading StrategyPine Script · Trend following · 1h
- Multi Indicator Crossover Trend Following Strategy with Fibonacci Retracement and Stop Loss Take Profit Optimization SystemPine Script · Trend following · 1h
- ATR Fusion Trend Optimization Model StrategyPine Script · Trend following · 1d
- Dynamic WaveTrend and Fibonacci Integrated Quantitative Trading StrategyPine Script · Trend following · 1h
- Fibonacci Average Candle with Moving Average Strategy for Quantitative TradingPine Script · Trend following · 1h
- Multi Condition Trend Following Quantitative Trading Strategy Based on Fibonacci Retracement LevelsPine Script · Trend following · 1h