Loading…

Donchian Channel Breakout Strategy Thinkscript

What you’ll find below:

I’ve had a few emails asking me about the Donchian Channel Breakout Thinkscript strategy I use in Thinkorswim.  Specifically, people have been asking for the code so I thought I’d pass it along with a little caveat.  I wrote the Thinkscript Strategies below for the entry and exit, but I found the original Donchian Channel study code on this site.  I will note that getting the entry and exit strategies to work was a little tricky for me because I’m not a programmer, but it does work.

It’s a good idea to understand what’s going on in the Donchian Channel Thinkscript so you might spend some time looking through the code.  The specific code below goes to a default order size of 100 shares.  I have a Donchian Thinkscript Strategy that incorporates money management, but it’s not working quite right yet (sometimes it doesn’t take trades and I haven’t figure out why).  Once I get the money management version working, I’ll be sure to share that as well.

If you have any questions about the code please let me know.  Enjoy and feel free to share link with anyone who would benefit from it.

Here’s what the Donchian Channel Strategy and Study look like:

Apple Donchian Channel Breakout
Donchian Channel Breakout Strategy and Study Thinkscript on Apple (AAPL)

Here are the Donchian Channel Long Entry and Short Entry ThinkScript Strategies

Donchian Channel Thinkscript Strategy Long Entry:

#Donchian Channel Long Entry Thinkscript Strategy
#Written by ThetaTrend.com - Actionable Trend Following Trading with Options
#Feel free to share the code, but please include a link back to ThetaTrend.com

input length = 50;

def upperBand = Highest(high[1], length);
def lowerBand = Lowest(low[1], length);

AddOrder(OrderType.BUY_AUTO, high > Highest(high[1], length), Highest(high[1], length));

Donchian Channel Thinkscript Strategy Short Entry:

#Donchian Channel Short Entry Thinkscript Strategy
#Written by ThetaTrend.com - Actionable Trend Following Trading with Options
#Feel free to share the code, but please include a link back to ThetaTrend.com

input length = 50;

def upperBand = Highest(high[1], length);
def lowerBand = Lowest(low[1], length);

AddOrder(OrderType.SELL_AUTO, low < Lowest(low[1], length), Lowest(low[1], length));

Here are the Donchian Channel Long Exit and Short Exit Thinkscript Strategies

Donchian Channel Long Exit Thinkscript Strategy:

#Donchian Channel Long Exit Thinkscript Strategy
#Written by ThetaTrend.com - Actionable Trend Following Trading with Options
#Feel free to share the code, but please include a link back to ThetaTrend.com

#define inputs
input length = 25; #Number of days for Trailing Donchian Breakdown
input ATRlength = 20; #Number of days for ATR Calculation
input ATRMeasure = 3; #ATR stop value, 3 is for 3 times the daily ATR

def entryP = EntryPrice();
def stopValue = ATRMeasure * (AverageTrueRange(ATRlength));
def stopOut = entryP - stopValue;

#Determine if the stop was hit
def peaceout = if ((low <= stopOut) or (low < Lowest(low[1], length))) then 1 else 0;

#Determine the exit price
def chickendinner = if (low <= stopOut) then (stopOut) else (Lowest(low[1], length));

AddOrder(OrderType.SELL_TO_CLOSE, peaceout, chickendinner, tickcolor = GetColor(7), arrowcolor = GetColor(7));

Donchian Channel Short Exit Strategy Thinkscript:

#Donchian Channel Long Exit Thinkscript Strategy
#Written by ThetaTrend.com - Actionable Trend Following Trading with Options
#Feel free to share the code, but please include a link back to ThetaTrend.com

#define inputs
input length = 25; #Number of days for Trailing Donchian Breakdown
input ATRlength = 20; #Number of days for ATR Calculation
input ATRMeasure = 3; #ATR stop value, 3 is for 3 times the daily ATR

def entry = entryPrice();
def stopValue = ATRMeasure * (AverageTrueRange(ATRlength));
def stopOut = entry + stopValue;

#Determine if stop was hit
def peaceout = if (high >= stopOut) or (high > Highest(high[1], length)) then 1 else 0;

#Determine the exit price
def chickendinner = if (high >= stopOut) then (stopOut) else (Highest(high[1], length));

AddOrder(OrderType.BUY_TO_CLOSE, peaceout, chickendinner, tickColor = GetColor(7), arrowColor = GetColor(7));

Donchian Channel Study Thinkscript (from Chris Ball):

#Name:             DonchianChannel
#Programmed By:    Chris Ball (chris.m.ball@gmail.com) on 10/23/08
#Posted At:        http://chartingwithchris.blogspot.com
#Description:      This is a channel system that is used frequently for trend trading.  Google the term "turtle trader" for more information.

input length = 20;

plot upperBand = Highest(high[1], length);
plot lowerBand = Lowest(low[1], length);
plot middleBand = (upperBand + lowerBand) / 2;

upperBand.SetDefaultColor(Color.Cyan);
lowerBand.SetDefaultColor(Color.Cyan);
middleBand.SetDefaultColor(Color.Cyan);

Video Explanation:

If you need any help adding the Donchian Channel Strategies and Studies to Thinkorswim, the following video should help.  Additionally, the Donchian Channel strategies I wrote include my default breakout and stop settings. You may want to modify those to fit your needs.

If you enjoyed this post, please share it using your favorite social media platform above. Thanks.

  • KD

    Hi Dan,

    Came across your scripts for Donchian channels in ToS. was wondering, do these ‘autoorder’ commands actually place order? I dont want them to. I like the signals being generated just for information.

    Thanks!

    • Dan

      Hey KD. Nope, the “auto order” won’t place a real order. They just create an order in the backtest report. Thanks for reading the blog.

      -Dan

    • Hey KD. Nope, the “auto order” won’t place a real order. They just create an order in the backtest report. Thanks for reading the blog.

      -Dan

  • Zachary Hamm

    Did you ever get the money management part of the strategy working?

    • Dan

      Hi Zachary,

      Sorry, I never got the money management part to work. I’ve put it on the side for now, but I may revisit it in the future.

      Dan

  • Michael Choffy

    Curious on the addorder code, why is the

    Highest(high[1], length) statement used twice?

  • Tony

    Hi Dan; I read your Donchain Breakout and enjoyed it. You mentioned that you were in the process of a money management system. Have you completed it? Can I have its code to use it with ToS? Thanks Tony

  • v4vasilis

    Hello,

    Can you write this code for pine editor for Trading view? If so, please contact me with the time and cost required.

  • Cindi

    the short exit script indicated by the verbage on the webpage here, includes comments which indicate that it is the long exit strategy, although the code seems correct to me, I hope, only the comments in line one, need to be corrected. Can you confirm?

    Thanks so much for this GREAT strategy code!