Donchian Channel Trend For ThinkOrSwim

Jman831

Member
This is a simple indicator I've created to help determine whether a trend is up or down. It plots the difference between the upper and lower channels of two Donchian Channels. The lengths of the two Donchian Channels are adjustable. The difference between the lower channels is plotted as a positive number, as a green histogram while the difference between the upper channels is plotted as a negative number, as a red histogram. More green and little to no red means uptrend, while more red and little to no green means downtrend. As I like to do with most of my indicators, I've included exponential smoothing which can be toggled on or off. The smoothing factor is also adjustable. Code below:

Update: As requested by another member, I've updated the code here for this indicator to include an option that allows you to see the DC Trend of a different security than the security loaded on the price chart. By default, it is toggled on and shows the DC Trend of the SPY security. If you want to see the DC Trend of the charted security, simply toggle "show different security" to "no".
Code:
declare lower;

input DCFastLength = 9;
input DCSlowLength = 27;
input ExponentialSmoothing = yes;
input SmoothingFactor = 3;
input ShowDifferentSecuirty = yes;
input Security = "SPY";

def dcfasthigh = if ShowDifferentSecuirty == yes then highest(high(Security), DCFastLength) else highest(high, DCFastLength);
def dcfastlow = if ShowDifferentSecuirty == yes then lowest(low(Security), DCFastLength) else lowest(low, DCFastLength);
def dcslowhigh = if ShowDifferentSecuirty == yes then highest(high(Security), DCSlowLength) else highest(high, DCSlowLength);
def dcslowlow = if ShowDifferentSecuirty == yes then lowest(low(Security), DCSlowLength) else lowest(low, DCSlowLength);
def lowdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfastlow - dcslowlow, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfastlow - dcslowlow;
def highdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfasthigh - dcslowhigh, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfasthigh - dcslowhigh;

plot Zero = 0;
plot LowDifference = lowdiff;
plot HighDifference = highdiff;

LowDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
LowDifference.setDefaultColor(color.GREEN);
HighDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
HighDifference.setDefaultColor(color.RED);

A screenshot of the indicator below a chart of the SPY:
Donchian-Channel-Trend.png
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I use every indicator that you have published. You and @mashume are my idols.
I had need of a simple trend indicator to use to filter my potential candidates for trade.
Turns out your invention is very similar to what I use:
Here is your indicator (which is superior because it has more customizing options) compared to what I have been using:
ToS reference MAD() Moving Average Difference
LNBKkA3.png
 
I use every indicator that you have published. You and @mashume are my idols.
I had need of a simple trend indicator to use to filter my potential candidates for trade.
Turns out your invention is very similar to what I use:
Here is your indicator (which is superior because it has more customizing options) compared to what I have been using:
ToS reference MAD() Moving Average Difference
LNBKkA3.png
That means a lot, especially coming from you, thank you! =-)
 
Plotting indicator of a ticker on to a different ticker chart

Hi,
I need help with following. I would appreciate it, if someone can guide me on following. I'm not a coder so I'm not sure how to go about it. I have a lower study indicator ( donchian_trend_ribbon with supertrend) which I would like it to plotted under chart ( for example spy, /es,,) but I want it to show the result of $vold, $add and $tick. So if I'm viewing chart of spy, /es Or any other......I would like lower study indicator to show results of $ vold, $tick, and $add.
Is there a special code I can add on to the indicator to make this happen? Please help! Thank u in advance @halcyonguy

Here is the code to the donchian_trend_ribbon:
https://usethinkscript.com/threads/donchian-trend-ribbon-for-thinkorswim.10861/
Code:
# donchian_trend_ribbon_0d

# convert - donchian trend ribbon
# halcyonguy
# 22-04-07

# add histogram of sum of color numbers

# https://usethinkscript.com/threads/convert-tradingview-donchian-trend-ribbon.10822/
# Convert Tradingview Donchian Trend Ribbon
# chewie76 4/4
# Can someone convert the Donchian Trend Ribbon into TOS?
# --------------------------------
# https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/
#I think you all know Donchian Channels . so I am not going to write about it.
#https://www.tradingview.com/scripts/donchianchannels/
#With this indicator I tried to create Donchian Trend Ribbon by using Donchian Channels .

# How it works ?
# - it calculates main trend direction by using the length that is user-defined. so you can change it as you wish
# - then it calculates trend direction for each 9 lower lengths. if you set the length = 20 then the lengths are 19, 18,...11
# - and it checks if the trend directions that came from lower lengths is same or not with main trend direction.
# - it changes the trend color of the ribbon.
# -----------------------------

declare lower;

input Donchian_Channel_Period = 10;
def dlen = if Donchian_Channel_Period < 10 then 10 else Donchian_Channel_Period;
input show_data_rows = yes;
input show_sum_histo = yes;
def na = double.nan;


script dchannel {
input len = 0;
def hh = highest(high, len);
def ll = lowest(low, len);
def trend = if barnumber() == 1 then 0 else if close > hh[1] then 1 else if close < ll[1] then -1 else trend[1];
plot z = trend;
}

script dchannelalt {
input len = 0;
input maintrend = 0;
def hh = highest(high, len);
def ll = lowest(low, len);
def trend = if barnumber() == 1 then 0 else if close > hh[1] then 1 else if close < ll[1] then -1 else trend[1];

def maincolor =
if maintrend == 1 then if trend == 1 then 2 else 1
else if maintrend == -1 then if trend == -1 then -2 else -1
else 0;

plot color = maincolor;
# plot tr = trend;
}

def maintrend = dchannel(dlen);

# colors
# 2 - up trend - green
# 1 - up trend - dark green
# 0 no trend
# -1 - down trend - dark red
# -2 - down trend - red

DefineGlobalColor("up2", color.green);
DefineGlobalColor("up1", color.dark_green);
DefineGlobalColor("none", color.dark_gray);
DefineGlobalColor("dwn1", color.dark_red);
DefineGlobalColor("dwn2", color.red);
# GlobalColor("up1")
#----------------------------------------

input bottom_row = 0;
def rowstart = if bottom_row <> 0 then bottom_row
else if ( show_data_rows and show_sum_histo ) then 25
else 0;

input rowskip = 3;
input square_size = 4;

#plot( 5, color = dchannelalt(dlen - 0, maintrend), style = plot.style_columns, histbase= 0)
def c01 = dchannelalt(dlen - 0, maintrend);
plot row01 = if isnan(close) then double.nan else (rowstart + ( 1 * rowskip));
row01.SetPaintingStrategy(PaintingStrategy.SQUARES);
row01.setlineweight(square_size);
row01.hidebubble();
row01.AssignValueColor(
if c01 == 2 then GlobalColor("up2")
else if c01 == 1 then GlobalColor("up1")
else if c01 == -1 then GlobalColor("dwn1")
else if c01 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row01.SetHiding(!show_data_rows);

def c02 = dchannelalt(dlen - 1, maintrend);
plot row02 = if isnan(close) then double.nan else (rowstart + ( 2 * rowskip));
row02.SetPaintingStrategy(PaintingStrategy.SQUARES);
row02.setlineweight(square_size);
row02.hidebubble();
row02.AssignValueColor(
if c02 == 2 then GlobalColor("up2")
else if c02 == 1 then GlobalColor("up1")
else if c02 == -1 then GlobalColor("dwn1")
else if c02 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row02.SetHiding(!show_data_rows);

def c03 = dchannelalt(dlen - 2, maintrend);
plot row03 = if isnan(close) then double.nan else (rowstart + ( 3 * rowskip));
row03.SetPaintingStrategy(PaintingStrategy.SQUARES);
row03.setlineweight(square_size);
row03.hidebubble();
row03.AssignValueColor(
if c03 == 2 then GlobalColor("up2")
else if c03 == 1 then GlobalColor("up1")
else if c03 == -1 then GlobalColor("dwn1")
else if c03 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row03.SetHiding(!show_data_rows);

def c04 = dchannelalt(dlen - 3, maintrend);
plot row04 = if isnan(close) then double.nan else (rowstart + ( 4 * rowskip));
row04.SetPaintingStrategy(PaintingStrategy.SQUARES);
row04.setlineweight(square_size);
row04.hidebubble();
row04.AssignValueColor(
if c04 == 2 then GlobalColor("up2")
else if c04 == 1 then GlobalColor("up1")
else if c04 == -1 then GlobalColor("dwn1")
else if c04 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row04.SetHiding(!show_data_rows);

def c05 = dchannelalt(dlen - 4, maintrend);
plot row05 = if isnan(close) then double.nan else (rowstart + ( 5 * rowskip));
row05.SetPaintingStrategy(PaintingStrategy.SQUARES);
row05.setlineweight(square_size);
row05.hidebubble();
row05.AssignValueColor(
if c05 == 2 then GlobalColor("up2")
else if c05 == 1 then GlobalColor("up1")
else if c05 == -1 then GlobalColor("dwn1")
else if c05 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row05.SetHiding(!show_data_rows);

def c06 = dchannelalt(dlen - 5, maintrend);
plot row06 = if isnan(close) then double.nan else (rowstart + ( 6 * rowskip));
row06.SetPaintingStrategy(PaintingStrategy.SQUARES);
row06.setlineweight(square_size);
row06.hidebubble();
row06.AssignValueColor(
if c06 == 2 then GlobalColor("up2")
else if c06 == 1 then GlobalColor("up1")
else if c06 == -1 then GlobalColor("dwn1")
else if c06 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row06.SetHiding(!show_data_rows);

def c07 = dchannelalt(dlen - 6, maintrend);
plot row07 = if isnan(close) then double.nan else (rowstart + ( 7 * rowskip));
row07.SetPaintingStrategy(PaintingStrategy.SQUARES);
row07.setlineweight(square_size);
row07.hidebubble();
row07.AssignValueColor(
if c07 == 2 then GlobalColor("up2")
else if c07 == 1 then GlobalColor("up1")
else if c07 == -1 then GlobalColor("dwn1")
else if c07 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row07.SetHiding(!show_data_rows);

def c08 = dchannelalt(dlen - 7, maintrend);
plot row08 = if isnan(close) then double.nan else (rowstart + ( 8 * rowskip));
row08.SetPaintingStrategy(PaintingStrategy.SQUARES);
row08.setlineweight(square_size);
row08.hidebubble();
row08.AssignValueColor(
if c08 == 2 then GlobalColor("up2")
else if c08 == 1 then GlobalColor("up1")
else if c08 == -1 then GlobalColor("dwn1")
else if c08 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row08.SetHiding(!show_data_rows);

def c09 = dchannelalt(dlen - 8, maintrend);
plot row09 = if isnan(close) then double.nan else (rowstart + ( 9 * rowskip));
row09.SetPaintingStrategy(PaintingStrategy.SQUARES);
row09.setlineweight(square_size);
row09.hidebubble();
row09.AssignValueColor(
if c09 == 2 then GlobalColor("up2")
else if c09 == 1 then GlobalColor("up1")
else if c09 == -1 then GlobalColor("dwn1")
else if c09 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row09.SetHiding(!show_data_rows);

def c10 = dchannelalt(dlen - 9, maintrend);
plot row10 = if isnan(close) then double.nan else (rowstart + ( 10 * rowskip));
row10.SetPaintingStrategy(PaintingStrategy.SQUARES);
row10.setlineweight(square_size);
row10.hidebubble();
row10.AssignValueColor(
if c10 == 2 then GlobalColor("up2")
else if c10 == 1 then GlobalColor("up1")
else if c10 == -1 then GlobalColor("dwn1")
else if c10 == -2 then GlobalColor("dwn2")
else GlobalColor("none"));
row10.SetHiding(!show_data_rows);

# -----------------------------

# add up color numbers, to come up with a 'trend' number , 20 to -20
def colorsum = ( c01 + c02 + c03 + c04 + c05 + c06 + c07 + c08 + c09 + c10 );

# plot a histogram of colorsum.
# it uses absvalue( ), so it is positive numbers, 0 to 20
#input vert1 = 0; + vert1
plot zc = if show_sum_histo then (absvalue(colorsum)) else na;
zc.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
zc.AssignValueColor( if colorsum == (2*10) then color.cyan
else if colorsum > 0 then color.dark_green
else if colorsum == (-2*10) then color.magenta
else if colorsum < 0 then color.dark_red
else color.gray);
zc.SetHiding(!show_sum_histo);

plot buy = if colorsum == (-2*10) then 1 else double.nan;
plot sell = if colorsum == (2*10) then 1 else double.nan;
plot weakbuy = if colorsum > 0 then 1 else double.nan;
plot weaksell = if colorsum <0 then 1 else double.nan;


# -----------------------------

# pine code

#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © LonesomeTheBlue

#//@version=4
#study("Donchian Trend Ribbon", precision = 0)
#dlen = input(defval = 20, title = "Donchian Channel Period", minval = 10)

#dchannel(len)=>
# float hh = highest(len)
# float ll = lowest(len)

# int trend = 0
# trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])

#dchannelalt(len, maintrend)=>
# float hh = highest(len)
# float ll = lowest(len)

# int trend = 0
# trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])

# last 2 digits are opacity of the color. if a trend, then dark, else pale color ( i think)
# maintrend == 1 ? trend == 1 ? #00FF00ff : #00FF009f :
# maintrend == -1 ? trend == -1 ? #FF0000ff : #FF00009f :
# na

#maintrend = dchannel(dlen)

#plot( 5, color = dchannelalt(dlen - 0, maintrend), style = plot.style_columns, histbase= 0)
#plot(10, color = dchannelalt(dlen - 1, maintrend), style = plot.style_columns, histbase= 5)
#plot(15, color = dchannelalt(dlen - 2, maintrend), style = plot.style_columns, histbase=10)
#plot(20, color = dchannelalt(dlen - 3, maintrend), style = plot.style_columns, histbase=15)
#plot(25, color = dchannelalt(dlen - 4, maintrend), style = plot.style_columns, histbase=20)
#plot(30, color = dchannelalt(dlen - 5, maintrend), style = plot.style_columns, histbase=25)
#plot(35, color = dchannelalt(dlen - 6, maintrend), style = plot.style_columns, histbase=30)
#plot(40, color = dchannelalt(dlen - 7, maintrend), style = plot.style_columns, histbase=35)
#plot(45, color = dchannelalt(dlen - 8, maintrend), style = plot.style_columns, histbase=40)
#plot(50, color = dchannelalt(dlen - 9, maintrend), style = plot.style_columns, histbase=45)
#
 
Last edited by a moderator:
I want to create a stratgy using the code below. Indicator- Donchian Trend Ribbon
http://tos.mx/uuXb4mo

im trying to use multiple market internals (confluence) to get better buy and sell during day trading ..if there was a code that would tell me when Cyan color is lined up
Dark Green-lined up
Magenta- lined up
Dark Green-lined up
using arrows plotted on the chart.

I can use those information along with other things to buy or sell stocks during day time.
declare lower;

Input 1: $add
Input 1: On/OFF

Input 2 : $Vold
Input2: On/Off

Input 3: $ADspd
Input 3; On/Off

Input 4: $Volspd
INput 4: on/off

Input 5: $adqd
input 5: on/off

Input 6: volqd
input 6: on/off

Paint bar: On/off
Arrows by candle on/Off

Cyan color- Bullish (Green Arrow)
Dark Green-Bull Trend slowing (Blue Arrow)
Magenta- Bearish (red arrow)
Dark Green-Bear Trend Slowingg (white arrow)
Arrows by the candle

What am I trying to do? When all the colors line up->it will tell me bullish/bearish based on those Market Internals. Hopefully, I can put in any Ticker in the INPUTS (besides what I have put on top).

Can someone help? Thank you
 
Last edited:
https://usethinkscript.com/threads/donchian-channel-trend-for-thinkorswim.11848/
This is a simple indicator I've created to help determine whether a trend is up or down. It plots the difference between the upper and lower channels of two Donchian Channels. The lengths of the two Donchian Channels are adjustable. The difference between the lower channels is plotted as a positive number, as a green histogram while the difference between the upper channels is plotted as a negative number, as a red histogram. More green and little to no red means uptrend, while more red and little to no green means downtrend. As I like to do with most of my indicators, I've included exponential smoothing which can be toggled on or off. The smoothing factor is also adjustable. Code below:
Code:
declare lower;

input DCFastLength = 9;
input DCSlowLength = 27;
input ExponentialSmoothing = yes;
input SmoothingFactor = 3;

def dcfasthigh = highest(high, DCFastLength);
def dcfastlow = lowest(low, DCFastLength);
def dcslowhigh = highest(high, DCSlowLength);
def dcslowlow = lowest(low, DCSlowLength);
def lowdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfastlow - dcslowlow, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfastlow - dcslowlow;
def highdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfasthigh - dcslowhigh, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfasthigh - dcslowhigh;

plot Zero = 0;
plot LowDifference = lowdiff;
plot HighDifference = highdiff;

LowDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
LowDifference.setDefaultColor(color.GREEN);
HighDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
HighDifference.setDefaultColor(color.RED);
Can this code be converted to be able add any symbol?
For example, if I'm viewing AApl chart, and I want spy data in the indicator.
 
Last edited by a moderator:
This is a simple indicator I've created to help determine whether a trend is up or down. It plots the difference between the upper and lower channels of two Donchian Channels. The lengths of the two Donchian Channels are adjustable. The difference between the lower channels is plotted as a positive number, as a green histogram while the difference between the upper channels is plotted as a negative number, as a red histogram. More green and little to no red means uptrend, while more red and little to no green means downtrend. As I like to do with most of my indicators, I've included exponential smoothing which can be toggled on or off. The smoothing factor is also adjustable. Code below:
Code:
declare lower;

input DCFastLength = 9;
input DCSlowLength = 27;
input ExponentialSmoothing = yes;
input SmoothingFactor = 3;

def dcfasthigh = highest(high, DCFastLength);
def dcfastlow = lowest(low, DCFastLength);
def dcslowhigh = highest(high, DCSlowLength);
def dcslowlow = lowest(low, DCSlowLength);
def lowdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfastlow - dcslowlow, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfastlow - dcslowlow;
def highdiff = if ExponentialSmoothing == yes then ExpAverage(ExpAverage(ExpAverage(dcfasthigh - dcslowhigh, SmoothingFactor), SmoothingFactor), SmoothingFactor) else dcfasthigh - dcslowhigh;

plot Zero = 0;
plot LowDifference = lowdiff;
plot HighDifference = highdiff;

LowDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
LowDifference.setDefaultColor(color.GREEN);
HighDifference.setPaintingStrategy(paintingStrategy.HISTOGRAM);
HighDifference.setDefaultColor(color.RED);

A screenshot of the indicator below a chart of the SPY:
Donchian-Channel-Trend.png
can this be customized so there are four colors as follow: Positive trend green, green to negative- yellow, Bearish trend- red and negative to positive trend-White color.


Also, what are the typical fib averages? you can share the code? thank you
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
152 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top