Donchian Trend Ribbon with Symbol Input

barbaros

Administrator
Staff member
Platform
  1. Thinkorswim
Added symbol input for request by community member @APOT

Code:
# donchian_trend_ribbon_0e

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

# 00f
# added symbol input by Barbaros

# 00e
# can chg secondary colors ( 1 , -1) to yellow or diff shades

# 00d
# add histogram , 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 symbl = "VIX";
input Donchian_Channel_Period = 20;
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;

input secondary_colors = { default yellow , green_red };
input show_full_height_of_histogram = yes;
plot z = if show_full_height_of_histogram and !isnan(close(symbol = symbl)) then 0 else na;
z.setdefaultcolor(color.dark_gray);

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

script dchannelalt {
  input len = 0;
  input maintrend = 0;
  input symbl = "VIX";
  def hh = highest(high(symbol = symbl), len);
  def ll = lowest(low(symbol = symbl), len);
  def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < 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, symbl);

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

# input secondary_colors = { default yellow , green_red };
def seccolor;
switch (secondary_colors) {
case yellow:
  seccolor = 1;
case green_red:
  seccolor = 2;
}

#   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")
DefineGlobalColor("alt1", color.yellow);
# GlobalColor("alt1")
#----------------------------------------

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 = 5;
input square_size = 4;

#plot( 5, color = dchannelalt(dlen - 0, maintrend, symbl), style = plot.style_columns, histbase= 0)
def c01 = dchannelalt(dlen - 0, maintrend, symbl);
plot row01 = if isnan(close(symbol = symbl)) 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")
# up1, dwn1 , if seccolor = 1 then GlobalColor("alt1") else if  seccolor = 2 then ....
else if c01 == 1 and seccolor == 1 then GlobalColor("alt1") else if c01 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c01 == -1 and seccolor == 1 then GlobalColor("alt1") else if c01 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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, symbl);
plot row02 = if isnan(close(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c02 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c02 == -1 and seccolor == 1 then GlobalColor("alt1") else if c02 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c03 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c03 == -1 and seccolor == 1 then GlobalColor("alt1") else if c03 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c04 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c04 == -1 and seccolor == 1 then GlobalColor("alt1") else if c04 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c05 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c05 == -1 and seccolor == 1 then GlobalColor("alt1") else if c05 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c06 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c06 == -1 and seccolor == 1 then GlobalColor("alt1") else if c06 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c07 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c07 == -1 and seccolor == 1 then GlobalColor("alt1") else if c07 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c08 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c08 == -1 and seccolor == 1 then GlobalColor("alt1") else if c08 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c09 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c09 == -1 and seccolor == 1 then GlobalColor("alt1") else if c09 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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(symbol = symbl)) 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 and seccolor == 1 then GlobalColor("alt1") else if c10 == 1 and seccolor == 2 then GlobalColor("up1")
#else if c01 == 1 then GlobalColor("up1")
else if c10 == -1 and seccolor == 1 then GlobalColor("alt1") else if c10 == -1 and seccolor == 2 then GlobalColor("dwn1")
#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.green
#else if colorsum > 0 then color.cyan
#else if colorsum > 0 then color.yellow
else if colorsum > 0 and seccolor == 1 then GlobalColor("alt1") else if colorsum > 0 and seccolor == 2 then
GlobalColor("up1")
else if colorsum == (-2*10) then color.red
# else if colorsum < 0 then color.yellow
else if colorsum < 0 and seccolor == 1 then GlobalColor("alt1") else if colorsum < 0 and seccolor == 2 then
GlobalColor("dwn1")
 else color.gray);
zc.SetHiding(!show_sum_histo);


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

# 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:

barbaros

Administrator
Staff member
Another version...

Code:
# donchian_trend_ribbon_0d

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

# add histogram of sum of color numbers

# added symbol input by Barbaros

# 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 symbl = "VIX";
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;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < ll[1] then -1 else trend[1];
plot z = trend;
}

script dchannelalt {
input len = 0;
input maintrend = 0;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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)
#
 

APOT

New member
Another version...

Code:
# donchian_trend_ribbon_0d

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

# add histogram of sum of color numbers

# added symbol input by Barbaros

# 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 symbl = "VIX";
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;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < ll[1] then -1 else trend[1];
plot z = trend;
}

script dchannelalt {
input len = 0;
input maintrend = 0;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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(symbol = symbl)) 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)
#
thank u so much!!! ur the best!
 

barbaros

Administrator
Staff member
bug fix

Code:
# donchian_trend_ribbon_0f

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

# add histogram of sum of color numbers

# added symbol input by Barbaros

# 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 symbl = "VIX";
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;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < ll[1] then -1 else trend[1];
plot z = trend;
}

script dchannelalt {
input len = 0;
input maintrend = 0;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < 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, symbl);
plot row01 = if isnan(close(symbol = symbl)) 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, symbl);
plot row02 = if isnan(close(symbol = symbl)) 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, symbl);
plot row03 = if isnan(close(symbol = symbl)) 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, symbl);
plot row04 = if isnan(close(symbol = symbl)) 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, symbl);
plot row05 = if isnan(close(symbol = symbl)) 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, symbl);
plot row06 = if isnan(close(symbol = symbl)) 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, symbl);
plot row07 = if isnan(close(symbol = symbl)) 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, symbl);
plot row08 = if isnan(close(symbol = symbl)) 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, symbl);
plot row09 = if isnan(close(symbol = symbl)) 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, symbl);
plot row10 = if isnan(close(symbol = symbl)) 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)
#
 

APOT

New member
bug fix

Code:
# donchian_trend_ribbon_0f

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

# add histogram of sum of color numbers

# added symbol input by Barbaros

# 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 symbl = "VIX";
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;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < ll[1] then -1 else trend[1];
plot z = trend;
}

script dchannelalt {
input len = 0;
input maintrend = 0;
input symbl = "VIX";
def hh = highest(high(symbol = symbl), len);
def ll = lowest(low(symbol = symbl), len);
def trend = if barnumber() == 1 then 0 else if close(symbol = symbl) > hh[1] then 1 else if close(symbol = symbl) < 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, symbl);
plot row01 = if isnan(close(symbol = symbl)) 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, symbl);
plot row02 = if isnan(close(symbol = symbl)) 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, symbl);
plot row03 = if isnan(close(symbol = symbl)) 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, symbl);
plot row04 = if isnan(close(symbol = symbl)) 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, symbl);
plot row05 = if isnan(close(symbol = symbl)) 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, symbl);
plot row06 = if isnan(close(symbol = symbl)) 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, symbl);
plot row07 = if isnan(close(symbol = symbl)) 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, symbl);
plot row08 = if isnan(close(symbol = symbl)) 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, symbl);
plot row09 = if isnan(close(symbol = symbl)) 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, symbl);
plot row10 = if isnan(close(symbol = symbl)) 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)
#
thank you! I had to manual put in the $vold, $add, $tick in the code to get results. Also, would you know why the indicator data is not displayed for 1st hr or half hr when you put all three indicator on one chart??
 
Last edited:

barbaros

Administrator
Staff member
thank you! I had to manual put in the $vold, $add, $tick in the code to get results. Also, would you know why the indicator data is not displayed for 1st hr or half hr when you put all three indicator on one chart??
You can put the symbol in the settings dialog instead of the code. It should work.
 
Top