Donchian Channel Scan


Category:
0
0

Hello,

I took the custom dong chian channel by Chris Ball and tried to create a scan with it. I’m using the candle close value instead of highest or lowest value. I’m sharing it with anyone interested in it. Feel free to try it and  make improvements and post back your results. Thanks all.

 

# Dongchian channel Scan

#Name: DonchianChannel
#Programmed By: Chris Ball ([email protected]) 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 = 21;
input TermPeriod = {“1 min”, “3 min”, “5 min”, “15 min”, “30 min”, “60 min”, “120 min”, “240 min”,default “Daily”, “Weekly”, “Monthly”};

def nowAggregation;

switch (TermPeriod) {
case “1 min”:
nowAggregation = AggregationPeriod.MIN;
case “3 min”:
nowAggregation = AggregationPeriod.THREE_MIN;
case “5 min”:
nowAggregation = AggregationPeriod.FIVE_MIN;
case “15 min”:
nowAggregation = AggregationPeriod.FIFTEEN_MIN;
case “30 min”:
nowAggregation = AggregationPeriod.THIRTY_MIN;
case “60 min”:
nowAggregation = AggregationPeriod.HOUR;
case “120 min”:
nowAggregation = AggregationPeriod.TWO_HOURS;
case “240 min”:
nowAggregation = AggregationPeriod.FOUR_HOURS;

case “Daily”:
nowAggregation = AggregationPeriod.DAY;
case “Weekly”:
nowAggregation = AggregationPeriod.WEEK;
case “Monthly”:
nowAggregation = AggregationPeriod.MONTH;
}

# This section is for calculating dongchian values for the period resolution chosen
# def upperBand = Highest(close(period= nowAggregation), length);
# def upperBand = Lowest(close(period= nowAggregation), length);

plot scan;

# plot upperBand = Highest(close[1], length);
# scan = Highest(close(period= nowAggregation), length);
scan = Lowest(close(period= nowAggregation), length);

#plot lowerBand = Lowest(close[1], length);
#plot middleBand = (upperBand + lowerBand) / 2;

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

Marked as spam
Posted by (Questions: 2, Answers: 6)
Asked on January 28, 2017 12:02 pm
1346 views
0

Moderator’s Note: The title of this post has been edited to reflect the correct spelling, ”Donchian”. This is to ensure folks are able to locate this post when searching the topics list. This code contains a statement ”plot scan;”, but it is not assigned to any signals and is therefore non-functional.

( at January 28, 2017 2:49 pm)
0

I wanted to add that the scan isn’t working properly. It’s returning stocks which are hitting the upper donchian channel although I programmed it to return the stocks which are hitting the lower donchian channel. Can someone look at the code and see if they can find what’s wrong. Thanks.

( at January 28, 2017 3:29 pm)
0
Private answer

Ok, thanks for clarifying that bit. Well I can tell you these two lines of code are not going to work.
# scan = Highest(close(period= nowAggregation), length);
#scan = Lowest(close(period= nowAggregation), length);

I noticed you commented them out so I assume you are aware.

In it’s original form, the study should have an upperBand and a lowerBand. However you have commented out both of them. And for the upperBand variable there seems to be a few extra. Guess you were experimenting with those? From your clarification, you are wanting the scan to pick up when a stock is “hitting the lower Donchian channel”.

This solution is as simple as this statement:

scan = low < lowerBand;

And it must be placed BELOW the lowerBand plot, which you have also commented out. You need to uncomment that lowerBand plot or you have no chance whatsoever of getting this to work correctly. To run as a scan, the “plot” will need to be changed to “def”, and you will have to comment out, or delete the style statement for the lowerBand.

Hope this explains why it wasn’t working.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on January 28, 2017 4:24 pm
0
Pete, thanks a lot for the prompt reply. I was able to get it to work and here is the final code. Note that the indicator allows you to change the length in the settings, but in the scan you have to change it inside the code. I’ve set the default as 21. I’ve also set the scan to find all stocks that are hitting the lower band. You can switch it inside the code to scan for stocks that are hitting the upper band instead. # Dongchian channel #Name: DonchianChannel #Programmed By: Chris Ball ([email protected]) 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 = 21; input TermPeriod = {”1 min”, ”3 min”, ”5 min”, ”15 min”, ”30 min”, ”60 min”, ”120 min”, “240 min”,default ”Daily”, ”Weekly”, ”Monthly”}; def nowAggregation; switch (TermPeriod) { case ”1 min”: nowAggregation = AggregationPeriod.MIN; case ”3 min”: nowAggregation = AggregationPeriod.THREE_MIN; case ”5 min”: nowAggregation = AggregationPeriod.FIVE_MIN; case ”15 min”: nowAggregation = AggregationPeriod.FIFTEEN_MIN; case ”30 min”: nowAggregation = AggregationPeriod.THIRTY_MIN; case ”60 min”: nowAggregation = AggregationPeriod.HOUR; case ”120 min”: nowAggregation = AggregationPeriod.TWO_HOURS; case ”240 min”: nowAggregation = AggregationPeriod.FOUR_HOURS; case ”Daily”: nowAggregation = AggregationPeriod.DAY; case ”Weekly”: nowAggregation = AggregationPeriod.WEEK; case ”Monthly”: nowAggregation = AggregationPeriod.MONTH; } # This section is for calculating dongchian values for the period resolution chosen # def upperBand = Highest(close(period= nowAggregation), length); # def upperBand = Lowest(close(period= nowAggregation), length); # plot upperBand = Highest(close[1], length); # scan = Highest(close(period= nowAggregation), length); # scan = Lowest(close(period= nowAggregation), length); def lowerBand = Lowest(close(period= nowAggregation), length); def upperBand = Highest(close(period= nowAggregation), length); plot scan; scan = low = upperBand;
( at January 28, 2017 7:57 pm)
0

There is a potential problem with ’scan = low = upperBand;’ as this code will require the low of the candle to be equal to the upper band. Not higher than, and not greater than but equal to. That is going to be an extremely rare case indeed. And I would think you would want to test if the low has fallen through and below the lower band, yes? And likewise test if the high has rallied through and above the upperBand? That’s just my two cents worth, but I can’t trade my way out of a box so….

( at January 28, 2017 8:06 pm)
0
Private answer

Pete, I’m having some issues with the formatting of my latest posting. Can u give me a hint what I might be doing wrong. It posted well the first time but I’ve already tried several times to ‘correct’ the formatting of the last posting without success. Thanks.

Marked as spam
Posted by (Questions: 2, Answers: 6)
Answered on January 29, 2017 1:55 pm
0

I see what you mean. Unfortunately we don’t really have much control over the way comments are formatted. I suggest you take the content of that comment and submit it as a new answer to the topic. As you can see, the section where you post a new answer has all sorts of handy text formatting tools. Once you get it posted a new answer you can delete that comment to avoid any confusion.

( at January 29, 2017 2:22 pm)
0
Private answer

My previous posting was a little messed up and hard to read. Hopefully this one will come out right and easier to read.

# Dongchian channel

#Name: DonchianChannel
#Programmed By: Chris Ball ([email protected]) 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 = 21;
input TermPeriod = {“1 min”, “3 min”, “5 min”, “15 min”, “30 min”, “60 min”, “120 min”, “240 min”,default “Daily”, “Weekly”, “Monthly”};

def nowAggregation;

switch (TermPeriod) {
case “1 min”:
nowAggregation = AggregationPeriod.MIN;
case “3 min”:
nowAggregation = AggregationPeriod.THREE_MIN;
case “5 min”:
nowAggregation = AggregationPeriod.FIVE_MIN;
case “15 min”:
nowAggregation = AggregationPeriod.FIFTEEN_MIN;
case “30 min”:
nowAggregation = AggregationPeriod.THIRTY_MIN;
case “60 min”:
nowAggregation = AggregationPeriod.HOUR;
case “120 min”:
nowAggregation = AggregationPeriod.TWO_HOURS;
case “240 min”:
nowAggregation = AggregationPeriod.FOUR_HOURS;

case “Daily”:
nowAggregation = AggregationPeriod.DAY;
case “Weekly”:
nowAggregation = AggregationPeriod.WEEK;
case “Monthly”:
nowAggregation = AggregationPeriod.MONTH;
}

# This section is for calculating dongchian values for the period resolution chosen
# def upperBand = Highest(close(period= nowAggregation), length);
# def upperBand = Lowest(close(period= nowAggregation), length);

# plot upperBand = Highest(close[1], length);
# scan = Highest(close(period= nowAggregation), length);
# scan = Lowest(close(period= nowAggregation), length);
def lowerBand = Lowest(close(period= nowAggregation), length);

def upperBand = Highest(close(period= nowAggregation), length);
plot scan;

scan = low <= lowerBand;
# scan = high >= upperBand;

Marked as spam
Posted by (Questions: 2, Answers: 6)
Answered on January 30, 2017 11:29 am
0
Private answer

Obelix, do you have the code for the Donchian channels?

Marked as spam
Posted by (Questions: 34, Answers: 56)
Answered on February 17, 2017 4:56 am
0

The code for the Donchian channel was included in the original question. Otherwise you can find the code to plot the Donchian channels under the Strategies section of Thinkorswim studies. You can copy and paste that into a new user defined study and delete the AddOrder() statements.

( at February 17, 2017 8:29 am)