Skip to main content

The Donchian Channel is a powerful tool for creating trend-following systems, making it well-suited for both intraday and daily timeframes, especially in bullish markets. This indicator is derived by calculating the highest high and lowest low over a predefined period. Any breach of the upper or lower band of the Donchian Channel signifies the initiation of a new trend. Additionally, the Donchian channel serves as a valuable resource for assessing price volatility. In stable price conditions, the Donchian channel remains relatively narrow, while frequent price fluctuations widen it.

It was originally conceptualized by the esteemed Richard Dennis, and serves as the foundation of the renowned Turtle Trading System. Our strategy here revolves around the breakout of the 5-Day Donchian Channel. The upper band is established using the highest high of the past 5 days, while the lower band is determined by the lowest low within the same timeframe. An additional middle band is computed as the mean between the upper and lower bands. This middle band guides entry and exit points for long and short positions. Furthermore, to manage risk effectively, we’ve integrated a trailing stop loss based on a 25-period Average True Range (ATR).

Explore here to dive into AFL coding and create your personalized trading systems.

Overview of the Donchian Channel Strategy

Preferred TimeframeDaily
Indicators Used5-Period Donchian Channel
Buy ConditionIf the Current Candle High surpasses the upper band of the Donchian Channel.
Short ConditionIf the Current Candle Low drops below the lower band of the Donchian Channel.
Sell ConditionIf the Current Candle Low is lower than the middle band of the Donchian Channel.
Cover ConditionIf the Current Candle High exceeds the middle band of the Donchian Channel.
Stop LossUtilizes a Trailing Stop Loss based on a 25-period ATR.
TargetsNo fixed targets.
Position SizeFixed at 150.
Initial EquitySet at 200,000.
Brokerage100 per order.

Donchian Channel AFL Code

//  Formula Name:    Donchian Channel trading System
//  Website:

_SECTION_BEGIN("Donchian Channel trading System");

_N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//Initial Parameters
SetTradeDelays( 0,0,0, 0 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption( "AllowPositionShrinking", True );

Plot( Close, "Price", colorWhite, styleCandle );

DonchianUpper =HHV(Ref(H,-1),pds);
DonchianLower = LLV(Ref(L,-1),pds);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;
printf("\nDonchianUpper : " + DonchianUpper );  
printf("\nDonchianLower : " + DonchianLower );  
printf("\nDonchianMiddle : " + DonchianMiddle );  





Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

ApplyStop(stopTypeTrailing, stopModePoint, ATRMultiplier*ATR(ATRPeriods), True, True );

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover );  

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);


Screenshot of the AFL

Donchian Channel

  NiftyBank Nifty
Initial Capital 200,000 200,000
Final Capital2,079,916.005,926,967.00
Backtest Period01-Jan-2004 to 20-Jun-201601-Jan-2004 to 20-Jun-2016
Net Profit %939.96% 2863.48%
Annual Return %20.66%31.23%
Number of Trades470456
Winning Trade %46.17%48.03%
Average Holding Period5.40 periods5.48 periods
Max Consecutive Losses77
Max System % Drawdown-17.15%-40.04%
Max Trade % Drawdown-18.70%-14.52%

Access the detailed backtest report here.


Nifty equity curve

Bank Nifty:

BankNifty equity curve

Additionally, explore the profit table for BankNifty below. This strategy consistently delivers robust performance each year:

strategy performance

Visit Symbol–>Information to specify the lot size and margin requirements. The screenshot below illustrates a lot size of 30 and a 10% margin requirement for Bank Nifty: