Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

Double Donchian Trading System – Amibroker AFL code

2 min read

Double Donchian Trading system is a Breakout trading system inspired by Richard J.Dennis. Donchian channels were developed by Richard Donchian, a pioneer of mechanical trend-following systems. Double Donchian trading system is a turtle trading strategy. Curtis Faith in his book Way Of The Turtle describes a variation of the Donchian system used by the legendary Turtle Traders.


Double Donchain Trading System

 
Long Entry Rules
Long Entry is made whenever candlestick breaks the outer upper Donchian Channel for the first time on the upper side.

Short Entry Rule
Short Entry is made whenever candlestick breaks the outer Lower Donchian Channel for the first time on the lower side.

Long Exit Rules
Long Entry is made whenever candlestick breaks the inner lower Donchian Channel for the first time on the lower side.

Short Entry Rule
Short Entry is made whenever candlestick breaks the inner upper Donchian Channel for the first time on the upper side.

The Buy and Sell Rules are represented as

Buy = H>Ref(DonchianUpper1,-1);
Short = L<Ref(DonchianLower1,-1);
Cover = H>Ref(DonchianUpper2,-1);
Sell = L<Ref(DonchianLower2,-1);

Further Exrem is used to eliminate the subsequent signals other than the first breakout signal.

The entry and exit signals on the charts are marked as follows

Long Entry – Green Arrow, Short Entry – Red Arrow, Long Exit – Green Star, Short Exit – Red Star

//Coded by Rajandran R
//Founder - www.marketcalls.in & Co-Founder - www.febinars.com
//Amibroker Algotrading Platform - www.algomojo.com

_SECTION_BEGIN("Double Donchain Trading System");

SetBarsRequired(100000,0);

GraphXSpace = 15;

SetChartOptions(0,chartShowArrows|chartShowDates);

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));

GfxSetBkMode(0); 

GfxSetOverlayMode(1);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));

Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

SetPositionSize(100,spsShares);

pds1 = param("param1",79,30,100,1);      //5min - 79 15min - 38
DonchianUpper1 = HHV( Ref( H, -1 ), pds1 ); // Highest high value of highs in last 40 periods
DonchianLower1 = LLV( Ref( L, -1 ), pds1 ); // Lowest low value of low in last 40 periods

pds2 = param("param2",24,5,50,1);;       //5min - 24 15min - 6
DonchianUpper2 = HHV( Ref( H, -1 ), pds2 ); // Highest high value of highs in last 14 periods
DonchianLower2 = LLV( Ref( L, -1 ), pds2 ); // Lowest low value of low in last 14 periods

Plot( DonchianUpper1, "Donchian U1", ParamColor( "DU Color1", colorBlue ), ParamStyle( "DU Style", styleLine ) );
Plot( DonchianLower1, "Donchian L1", ParamColor( "DL Color1", colorBlue ), ParamStyle( "DL Style", styleLine ) );
Plot( DonchianUpper2, "Donchian U2", ParamColor( "DU Color2", colorRed ), ParamStyle( "DU Style", styleLine ) );
Plot( DonchianLower2, "Donchian L2", ParamColor( "DL Color2", colorRed ), ParamStyle( "DL Style", styleLine ) );

Buy = H>Ref(DonchianUpper1,-1);
Short = L<Ref(DonchianLower1,-1);
Cover = H>Ref(DonchianUpper2,-1);
Sell = L<Ref(DonchianLower2,-1);

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

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

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      

PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(IIf(Cover, shapeStar, shapeNone),colorRed, 0,L, Offset=-45); 

PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGreen, 0, H, Offset=40);





_SECTION_END();

Which is the ideal timeframe to follow?

5min, 10min, 15min timeframes for Stocks/Indices. 10min,15min, and 30min for commodities

What is the Max Winning ratio I can expect?
anywhere between 40-45% across different timeframes.

Can I use the parameter for my studies in other stock/Indices
This parameter is optimized for Nifty and Bank Nifty. Do your own studies and observations with different parameters.

What is the benefit of trading this system?
It is a low-risk trading system with a Max Drawdown% of 13% with 2 lots of Nifty and 2 Lakhs of capital(brokerage included)

Double Donchain Trading System Backtest Results for Nifty Spot Since March 2009 to 10th May 2014

Equity Curve
1. Portfolio Equity
Drawdown Curve
2. Underwater Equity
Profit Table
3. Profit Table

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

17 Replies to “Double Donchian Trading System – Amibroker AFL code”

  1. Sir,

    Thanks for the sharing the system. I am not able o backtest this code, the scan shows me the no of rows with Buy/Sell but, the backtest yields no results. I believe using a leading indicator can increase the win% and a better CAR/MDD. Can you plz help me backtest the code

  2. Hello Rajendran,

    I could not load the Double Donchain AFL code as it throws me the following message

    “Error line 16
    Too many arguments.”

    Request you to verify the error code and upload the workable afl code for donchain.

    Regards
    Rajesh R

  3. After adding following lines, this AFL gives terrible negative profits

    BuyPrice = Open;
    CoverPrice = Open;
    ShortPrice = Open;
    SellPrice = Open;
    SetTradeDelays( 1, 1, 1, 1 );

    1. Hi,

      SetTradeDelays( 1, 1, 1, 1 ); is more than enough condition. BuyPrice = open statement is misleading and the rest of the three variables could give you wrong picture about the system.

  4. Whats the parameter for Nifty and Bank nifty Positional Trading(Daily Timeframe)

  5. Can we have double donchain AFL code for the EOD. we do not have option in our office to check markets.

    Regards
    Rajesh R

  6. Dear Rajendran,
    hav u used money management while backtesting this strategy?.. like u started with 2 lots for 2 lakhs. when ur capital grows to 3lakhs, do u trade with 3 lots or 2 lots for full backtesting period??

      1. K. In ur above back testing result, which money management method u used??. Whether u have increased ur position with increase in trading capital????..

  7. Hi Rajendran, Thank you for sharing this study. In this study, is the signal generated as soon as the channel is breached or the signal is generated only at the close of the candle? I am asking because in the backtest, its taking the closing candle price.

    Also, on a related note, i see plenty of Doichian Channel programs (including the one you’ve given in marketcalls portal) where the signal is generated as soon as the channel is breached. which one of them is a good practice? Thank you

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More