Donchian Channel Breakout Strategy with ATRSL Trailing Stop
The Donchian Channel Breakout Strategy is a trend-following quantitative trading strategy. It utilizes Donchian Channels to capture market trends while using an ATRSL trailing stop to manage risk. When the price…

Strategy Overview
The Donchian Channel Breakout Strategy is a trend-following quantitative trading strategy. It utilizes Donchian Channels to capture market trends while using an ATRSL trailing stop to manage risk. When the price breaks above the upper band of the Donchian Channel, the strategy enters a long position; when the price falls below the ATRSL trailing stop line, the strategy closes the position.
Strategy Principle
- Calculate Donchian Channel: Based on the user-defined
donLengthparameter, calculate the highest high and lowest low of the pastdonLengthperiods as the upper banddonUpperand lower banddonLowerof the Donchian Channel, respectively. The midlinedonBasisis the average of the upper and lower bands. - Calculate ATRSL Trailing Stop: Based on the user-defined
AP2andAF2parameters, calculate the ATR valueSL2. Then, dynamically adjust the trailing stop priceTrail2according to the relationship between the current close priceSCand the previous trailing stop priceTrail2[1]. - Entry Condition: When the current close price crosses above the upper band of the Donchian Channel, enter a long position.
- Exit Condition: When the current close price crosses below the ATRSL trailing stop line, close the position.
Strategy Advantages
- Trend Following: By using Donchian Channels to determine trend direction, the strategy can effectively capture market trends.
- Dynamic Stop Loss: The ATRSL trailing stop allows for dynamic adjustment of the stop loss level based on market volatility, helping to manage risk.
- Parameter Flexibility: Users can adjust parameters such as
donLength,AP2, andAF2according to their needs to optimize strategy performance.
Strategy Risks
- Parameter Risk: Different parameter settings can lead to significant differences in strategy performance, requiring thorough backtesting and parameter optimization.
- Market Risk: During choppy markets or trend reversals, the strategy may experience significant drawdowns.
- Slippage and Trading Costs: Frequent trading may result in high slippage and trading costs, impacting strategy profitability.
Optimization Directions
- Add Trend Filters: In the entry condition, indicators such as ADX can be added to assess trend strength and only enter positions when the trend is strong, improving entry quality.
- Optimize Stop Loss: Experiment with other stop loss methods, such as percentage-based stop loss or ATR stop loss, or combine multiple stop loss approaches to increase stop loss flexibility.
- Incorporate Position Sizing: Dynamically adjust position size based on market volatility and account risk to manage risk exposure.
Summary
The Donchian Channel Breakout Strategy is a classic trend-following strategy that captures trends using Donchian Channels and manages risk with an ATRSL trailing stop. The strategy's advantages include its simple and clear logic, ease of implementation, and potential for optimization. However, its drawbacks include poor performance during choppy markets and trend reversals, and significant impact of parameter settings on strategy performance. In practical application, the strategy can be enhanced by adding trend filters, optimizing stop loss, and incorporating position sizing modules to improve stability and profitability. At the same time, it is important to control trading frequency and costs, and flexibly adjust strategy parameters based on market characteristics and personal risk preferences.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 100 | donLength |
| v_input_2 | 10 | Slow ATR period |
| v_input_3 | 3 | Slow ATR multiplier |
Source Pine Script
/*backtest
start: 2023-03-16 00:00:00
end: 2024-03-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Stock Trend USE THIS", overlay = true)
donLength = input(100, minval=1)
//Donchian Long
donLower = lowest(donLength)
donUpper = highest(donLength)
donBasis = avg(donUpper,donLower)
// ATRSL
SC = close
// Slow Trail //
AP2 = input(10, title="Slow ATR period") // ATR Period
AF2 = input(3, title="Slow ATR multiplier") // ATR Factor
SL2 = AF2 * atr(AP2) // Stop Loss
Trail2 = 0.0
iff_3 = SC > nz(Trail2[1], 0) ? SC - SL2 : SC + SL2
iff_4 = SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0) ? min(nz(Trail2[1], 0), SC + SL2) : iff_3
Trail2 := SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0) ? max(nz(Trail2[1], 0), SC - SL2) : iff_4
// Long and Short Conditions
longCondition = (crossover(close,donUpper[1]))
// Close Conditions
closeLongCondition = crossunder(close,Trail2)
// Strategy logic
if (longCondition)
strategy.entry("Long", strategy.long)
alert("Open Long position")
if (closeLongCondition)
strategy.close("Long")
alert("Close Long position")
// Plot Donchian
l = plot(donLower, color=color.blue)
u = plot(donUpper, color=color.blue)
plot(donBasis, color=color.orange)
fill(u, l, color=color.blue)
plot(Trail2, color=color.blue, title="ATRSL Trail")Published by ChaoZhang on the FMZ strategy square · last modified 2024-03-22. 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/Donchian-Channel-Breakout-Strategy-with-ATRSL-Trailing-Stop.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 Donchian Channel Trend Following and Divergence Trading StrategyPine Script · Breakout · 1d
- Breakout Strategy Based on Turtle TradingPine Script · Breakout · 1d
- Donchian Channel Breakout StrategyPine Script · Breakout · 4h
- Donchian Channel Trend Following StrategyPine Script · Breakout · 1h
- Donchian Channel and Larry Williams Large Trade Index StrategyPine Script · Breakout · 1m
- Dual Breakout Volatility Channel StrategyPine Script · Breakout · 1h