Donchian Trend Ribbon
someone asked for this to be converted from tradingview.
the source tradingview code was someone elses' attempt to create the ribbon in tradingview, so i am unsure if it correct.
https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/
i converted his code to thinkscript.
each row uses a different length to determine an up or down trend.
this is compared to a trend using the length , dlen ( default 20).
instead of using clouds for shading, i decided to go a simple way and use PaintingStrategy.SQUARES.
i set the size to 4 and it seems to look good. (on a scale of 1 to 5)
i defined the 5 colors, so they can be changed in 1 place not 10.
can change the row spacing and square size.
TTWO 15min
10 rows,
.top row has the shortest length(len - 9)
.bottom row has longest length(len)
someone asked for this to be converted from tradingview.
the source tradingview code was someone elses' attempt to create the ribbon in tradingview, so i am unsure if it correct.
https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/
i converted his code to thinkscript.
each row uses a different length to determine an up or down trend.
this is compared to a trend using the length , dlen ( default 20).
instead of using clouds for shading, i decided to go a simple way and use PaintingStrategy.SQUARES.
i set the size to 4 and it seems to look good. (on a scale of 1 to 5)
i defined the 5 colors, so they can be changed in 1 place not 10.
can change the row spacing and square size.
Ruby:
# donchian_trend_ribbon_0c
# convert - donchian trend ribbon
# halcyonguy
# 22-04-07
# 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 = 20;
def dlen = if Donchian_Channel_Period < 10 then 10 else Donchian_Channel_Period;
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 rowstart = 0;
input rowskip = 5;
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
# -----------------------------
# 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)
#
TTWO 15min
10 rows,
.top row has the shortest length(len - 9)
.bottom row has longest length(len)