Multi Period Donchian Channel Trend Following and Divergence Trading Strategy
This strategy builds a trend following system based on multi-period Donchian Channels. By analyzing Donchian Channel breakouts across different timeframes and combining main and local trend relationships, it forms an…

Overview
This strategy builds a trend following system based on multi-period Donchian Channels. By analyzing Donchian Channel breakouts across different timeframes and combining main and local trend relationships, it forms an visually intuitive trend ribbon. The strategy uses varying color depths to display trend strength, with green representing uptrends and red representing downtrends, where deeper colors indicate more pronounced trends.
Strategy Principles
The core of the strategy is trend determination based on the Donchian Channel indicator. The Donchian Channel consists of upper and lower bands, determining trends by comparing current price position relative to the channel. Key components include:
- Main trend determination: Using 20-period Donchian Channel, uptrend forms when price breaks above upper band, downtrend forms when breaking below lower band
- Local trend determination: Under the main trend framework, shorter period Donchian Channels determine local trend direction
- Trend ribbon: Combination of 10 different period Donchian Channels forms a trend ribbon, with color depth reflecting trend strength
- Trading signals: Long entries on uptrend, short entries on downtrend, positions closed on trend reversal
Strategy Advantages
- Objective trend determination: Based on price breakouts, avoiding subjective judgment bias
- Multi-period confirmation: Improves trend determination accuracy through multiple timeframe trend overlay
- Intuitive visualization: Trend ribbon color changes clearly display market conditions
- Clear trading rules: Entry and exit signals are well-defined for programmatic implementation
- High adaptability: Parameters can be adjusted to optimize strategy performance for different instruments
Strategy Risks
- Trend reversal risk: Significant drawdowns possible at trend turning points
- Unsuitable for ranging markets: False signals likely during sideways consolidation
- Parameter sensitivity: Different parameter settings significantly affect strategy performance
- Slippage impact: Frequent trading may be affected by slippage
- Systematic risk: Market events may cause major losses
Strategy Optimization Directions
- Add trend strength filtering: Incorporate ADX and other trend strength indicators to filter weak trend signals
- Optimize stop loss settings: Dynamically adjust stop loss positions based on ATR for better risk control
- Add volume confirmation: Incorporate volume analysis to verify trend validity
- Introduce volatility adaptation: Dynamically adjust parameters based on market volatility
- Improve position management: Dynamically adjust position size based on trend strength
Summary
This strategy creates a visually striking and logically clear trend following trading system through innovative application of multi-period Donchian Channels. The core advantage lies in visualizing complex trend analysis processes, making it easier for traders to intuitively grasp market trends. Through proper parameter optimization and risk control measures, this strategy has good practical application value. Traders are advised to consider market conditions carefully when implementing live trading and manage positions according to their risk tolerance.
Source Pine Script
/*backtest
start: 2024-06-12 00:00:00
end: 2025-02-19 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy("Donchian Trend Ribbon Strategy", shorttitle="DonchianTrendRibbonStrat", overlay=true, precision=0)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Parameters
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dlen = input.int(defval=20, title="Donchian Channel Period", minval=10)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Helper function to determine color
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f_color(mainTrend, localTrend) =>
// mainTrend = 1 => uptrend, -1 => downtrend
// localTrend = 1 => local uptrend, -1 => local downtrend
// Return color based on whether local trend aligns with the main trend
color c = na
if mainTrend == 1
c := localTrend == 1 ? color.new(color.lime, 0) : color.new(color.lime, 60)
else if mainTrend == -1
c := localTrend == -1 ? color.new(color.red, 0) : color.new(color.red, 60)
else
c := na
c
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Function dchannel - determines main trend (1 or -1)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dchannel(len) =>
float hh = ta.highest(len)
float ll = ta.lowest(len)
var int tr = 0
tr := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(tr[1])
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Function dchannelalt - determines local trend and returns color
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dchannelalt(len, maintrend) =>
float hh = ta.highest(len)
float ll = ta.lowest(len)
var int tr = 0
tr := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(tr[1])
f_color(maintrend, tr)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Calculate main trend
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
maintrend = dchannel(dlen)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Plotting the Donchian Trend Ribbon
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plot( 5, color=dchannelalt(dlen - 0, maintrend), style=plot.style_columns, histbase= 0)
plot(10, color=dchannelalt(dlen - 1, maintrend), style=plot.style_columns, histbase= 5)
plot(15, color=dchannelalt(dlen - 2, maintrend), style=plot.style_columns, histbase=10)
plot(20, color=dchannelalt(dlen - 3, maintrend), style=plot.style_columns, histbase=15)
plot(25, color=dchannelalt(dlen - 4, maintrend), style=plot.style_columns, histbase=20)
plot(30, color=dchannelalt(dlen - 5, maintrend), style=plot.style_columns, histbase=25)
plot(35, color=dchannelalt(dlen - 6, maintrend), style=plot.style_columns, histbase=30)
plot(40, color=dchannelalt(dlen - 7, maintrend), style=plot.style_columns, histbase=35)
plot(45, color=dchannelalt(dlen - 8, maintrend), style=plot.style_columns, histbase=40)
plot(50, color=dchannelalt(dlen - 9, maintrend), style=plot.style_columns, histbase=45)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Trading Logic (STRATEGY)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool goLong = (maintrend == 1)
bool goShort = (maintrend == -1)
// Entry signals
if goLong
strategy.entry("Long", strategy.long)
if goShort
strategy.entry("Short", strategy.short)
// Close positions when trend changes
if strategy.position_size > 0 and goShort
strategy.close("Long")
if strategy.position_size < 0 and goLong
strategy.close("Short")
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-21. 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/Multi-Period-Donchian-Channel-Trend-Following-and-Divergence-Trading-Strategy.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
- Momentum Breakout Trading Strategy 435896Pine Script · Breakout · 1d
- Multi Filter Trend Breakthrough Smart Moving Average Trading StrategyPine Script · Breakout · 1h
- Multi Indicator Trend Breakout Strategy with Dynamic Stop LossPine Script · Breakout · 1d
- Multi Technical Indicator Dynamic Wave Trading StrategyPine Script · Breakout · 1h
- Multi Technical Indicator Trend Following Channel Breakout Trading Strategy with Candlestick Pattern Filtering SystemPine Script · Breakout · 1h
- Pivot Based Volume Weighted Breakout Reversal StrategyPine Script · Breakout · 1d