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

ParameterValue
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.
Margin10%.

Donchian Channel AFL Code

				
					//------------------------------------------------------
//
//  Formula Name:    Donchian Channel trading System
//  Website:         https://zerobrokerageclub.com/
//------------------------------------------------------

_SECTION_BEGIN("Donchian Channel trading System");

SetChartOptions(0,chartShowArrows|chartShowDates);
_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("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );

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

pds=Param("DonchianPeriods",5,5,100,5);
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 );  

Plot(DonchianUpper,"DU",colorBlue,styleLine);
Plot(DonchianMiddle,"DM",colorGreen,styleLine);
Plot(DonchianLower,"DL",colorRed,styleLine);

ATRMultiplier=Param("ATRMultiplier",5,1,5,1);
ATRPeriods=Param("ATRPeriods",25,5,25,1);

Buy=Cross(High,DonchianUpper);
Short=Cross(DonchianLower,Low);
Sell=Cross(DonchianMiddle,Low);
Cover=Cross(High,DonchianMiddle);

BuyPrice=DonchianUpper;
SellPrice=DonchianMiddle;
ShortPrice=DonchianLower;
CoverPrice=DonchianMiddle;

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

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);


_SECTION_END();
				
			

Screenshot of the AFL

Donchian Channel

ParameterValue
  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:

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: