Donchian System with ThinkOrSwim Strategies

i ran across the “4 week rule” system developed by Richard Donchian when i first read John J. Murphy’s book “Technical Analysis of the Financial Markets”. personally, i have a sweet spot for simple rules that produce surprising results and this system is as simple as it gets

the original system had two rules:

  1. enter long positions and cover short positions whenever price exceeds the high of the past 4 weeks
  2. enter short positions and cover long positions whenever price falls below the low of the past 4 weeks

though simple and does work, the system as it is defined above is in the market, be it long or short, at all times. the result of this is that during periods that the market is moving sideways, the system tends to get “whipsawed” pretty badly.

the system below is a modification of the “4 week rule” which i picked up from Ed Seykota’s site.

for the sake of this writing, the system is briefly described here in the following way:

  • The system has two states – it can either be long or short. when it is long, take signals to the upside. when it is short, take signals to the down
  • A long term price channel, the slow channel, is used to determine the overall trend – either up or down. If the price high goes above this channel, the system is long and stays long until price drops below the channel at which time the system is short
  • A short term price channel, the fast channel, is used to generate trading signals – both enter and exit. If the system is long and price goes above the short term upper channel a long trade signal is issued. At the same time a stop is place at the short term lower channel (the stop trails as the lower channel moves up)

as mentioned, this “modified” approach was taken from Ed Seykota – definitely worth a visit to have a look at what rigorous system development looks like

below is the code in thinkScript for the long side (both entry and exit scripts)

Long_Entry

declare LONG_ENTRY;
input SLOW_LENGTH = 126;
input FAST_LENGTH = 30;
# define system as long or short

# N resistance - the highest high over the last N bars
# N support - the lowest low over the last N bars

def longTermResistance = Highest(high, SLOW_LENGTH);
def longTermSupport = Lowest(low, SLOW_LENGTH);

def shortTermResistance = Highest(high, FAST_LENGTH);
def shortTermSupport = Lowest(low, FAST_LENGTH);

# system changes from short to long when price moves above long term resistance
# system changes from long to short when price moves below long term support

rec state = compoundValue(1, if state[1] < 1 and high > longTermResistance[1] then 1
else if state[1] > -1 and low < longTermSupport[1] then -1
else state[1], 1);

def trigger = if state == 1 and high > shortTermResistance[1] then 1 else 0;
def orderprice = open;

addOrder(trigger, orderprice);

SetColor(color.green);

Long Exit

declare LONG_EXIT;
input SLOW_LENGTH=126;
input FAST_LENGTH = 30;

# define system as long or short

# N resistance - the highest high over the last N bars
# N support - the lowest low over the last N bars

def shortTermResistance = Highest(high, FAST_LENGTH);
def shortTermSupport = Lowest(low, FAST_LENGTH);

addorder(low < shortTermSupport[1]);
setcolor(color.gray);

in order to run the code with ToS, do the following:

  • In ToS, create a new strategy – call it myLongEntry
  • Copy/paste the Long_Entry code above and save
  • Create a second strategy – call it myLongExit
  • Copy/paste the Long_Exit code and save
  • Add both these strategies to any chart
  • Play around with the input values (fastlength, slowlength) and open the report to see results

the short side is left as an exercise for the reader 😉
feel free to leave me a message if you have comments or need help with any of this

6 responses to “Donchian System with ThinkOrSwim Strategies

  1. Hi, Thank you for sharing !

    Is it possible that there are some errors in the rules ?

    Thank you for your kind answer.

    Best regards

  2. possible is the most probable outcome 😉

    not sure what exactly you are referring to. the definition of the rules or the implementation?

  3. aha – thanks freedombanker. i naively copied and pasted the code directly from ToS and into WordPress and a lot of it got lost on the way (mostly due to XML escape sequences)…i updated the code and now at least it is in sync with what i intended to post. don’t hesitate to let me know if you still see something wrong…

    thanks,
    Onn

  4. Do you have the script for shorts? How would you adjust it?

  5. thanks, this is another way to backup my delicious bookmarks just in case it go down again. Click https://twitter.com/moooker1

Leave a comment