Donchian Range, RSI, and Levels System
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator('Donchian RSI and Support & Resistance Levels Ribbon System', 
     shorttitle='Donchian Ribbon', 
     overlay=true, 
     timeframe="", 
     timeframe_gaps=true)

//DONCHIAN MA
donchian(_length) =>
    math.avg(ta.lowest(_length), ta.highest(_length))

//input
donchianon = input(true, 'Donchian Channel on?', inline="0", group='Donchian Channel')
dclength = input.int(20, minval=2, inline="0", title="", group='Donchian Channel')
donchianrainbow2 = input(false, 'RSI Rainbow?', inline="0", group='Donchian Channel')
color regularfill = input.color(color.rgb(33, 150, 243, 0), '', group='Donchian Channel', inline='00')
color uplim = input.color(color.new(color.red, 0), '', group='Donchian Channel', inline='00')
color dnlim = input.color(color.new(color.green, 0), '', group='Donchian Channel', inline='00')
transparencyvalue2 = input.int(95, minval=0, maxval=100, title="Channel Fill Transparency", group='Donchian Channel', inline='00')
ribbonon = input(true, 'Donchian Ribbon on?', inline="0", group='Donchian Moving Average Ribbon')
donchianrainbow = input(true, 'RSI Rainbow?', inline="0", group='Donchian Moving Average Ribbon')
color bullma = input.color(color.new(#26a69a, 50), '', inline='00', group='Donchian Moving Average Ribbon')
color bearma = input.color(color.new(#ff5252, 50), '', inline='00', group='Donchian Moving Average Ribbon')
transparencyvaluema = input.int(50, minval=0, maxval=100, title="                    Ribbon Transparency", group='Donchian Moving Average Ribbon', inline='00')

transparencyvalue = if ribbonon == true
    transparencyvaluema
else
    100

len2=input(2, inline='00')
len3=input(3, inline='00')
len4=input(4, inline='00')
len5=input(5, inline='01')
len6=input(6, inline='01')
len7=input(7, inline='01')
len8=input(8, inline='02')
len9=input(9, inline='02')
len10=input(10, inline='02')
len11=input(11, inline='03')
len12=input(12, inline='03')
len13=input(13, inline='03')
len14=input(14, inline='04')
len15=input(15, inline='04')
len16=input(16, inline='04')
len17=input(17, inline='05')
len18=input(18, inline='05')
len19=input(19, inline='05')
len20=input(20, inline='06')
len21=input(21, inline='06')
len22=input(22, inline='06')

don2=donchian(len2)
don3=donchian(len3)
don4=donchian(len4)
don5=donchian(len5)
don6=donchian(len6)
don7=donchian(len7)
don8=donchian(len8)
don9=donchian(len9)
don10=donchian(len10)
don11=donchian(len11)
don12=donchian(len12)
don13=donchian(len13)
don14=donchian(len14)
don15=donchian(len15)
don16=donchian(len16)
don17=donchian(len17)
don18=donchian(len18)
don19=donchian(len19)
don20=donchian(len20)
don21=donchian(len21)
don22=donchian(len22)

//grad
var grad = array.new_color(na)
if barstate.isfirst
    array.push(grad, color.new(color.gray, transparencyvalue))
    array.push(grad, color.new(#800080, transparencyvalue))
    array.push(grad, color.new(#890089, transparencyvalue))
    array.push(grad, color.new(#910091, transparencyvalue))
    array.push(grad, color.new(#9a009a, transparencyvalue))
    array.push(grad, color.new(#a300a3, transparencyvalue))
    array.push(grad, color.new(#ab00ab, transparencyvalue))
    array.push(grad, color.new(#b400b4, transparencyvalue))
    array.push(grad, color.new(#bd00bd, transparencyvalue))
    array.push(grad, color.new(#c700c7, transparencyvalue))
    array.push(grad, color.new(#d000d0, transparencyvalue))
    array.push(grad, color.new(#d900d9, transparencyvalue))
    array.push(grad, color.new(#e200e2, transparencyvalue))
    array.push(grad, color.new(#ec00ec, transparencyvalue))
    array.push(grad, color.new(#f500f5, transparencyvalue))
    array.push(grad, color.new(#ff00ff, transparencyvalue))
    array.push(grad, color.new(#ff00f0, transparencyvalue))
    array.push(grad, color.new(#ff00e1, transparencyvalue))
    array.push(grad, color.new(#ff00d2, transparencyvalue))
    array.push(grad, color.new(#ff00c4, transparencyvalue))
    array.push(grad, color.new(#ff00b7, transparencyvalue))
    array.push(grad, color.new(#ff00aa, transparencyvalue))
    array.push(grad, color.new(#ff009d, transparencyvalue))
    array.push(grad, color.new(#ff0091, transparencyvalue))
    array.push(grad, color.new(#ff0086, transparencyvalue))
    array.push(grad, color.new(#ff187c, transparencyvalue))
    array.push(grad, color.new(#ff2972, transparencyvalue))
    array.push(grad, color.new(#ff3669, transparencyvalue))
    array.push(grad, color.new(#ff4160, transparencyvalue))
    array.push(grad, color.new(#ff4a59, transparencyvalue))
    array.push(grad, color.new(#ff5252, transparencyvalue))
    array.push(grad, color.new(#ff5a4e, transparencyvalue))
    array.push(grad, color.new(#ff624a, transparencyvalue))
    array.push(grad, color.new(#ff6a45, transparencyvalue))
    array.push(grad, color.new(#ff7240, transparencyvalue))
    array.push(grad, color.new(#ff7b3b, transparencyvalue))
    array.push(grad, color.new(#ff8336, transparencyvalue))
    array.push(grad, color.new(#ff8c30, transparencyvalue))
    array.push(grad, color.new(#ff942a, transparencyvalue))
    array.push(grad, color.new(#ff9d23, transparencyvalue))
    array.push(grad, color.new(#ffa61c, transparencyvalue))
    array.push(grad, color.new(#ffaf12, transparencyvalue))
    array.push(grad, color.new(#ffb805, transparencyvalue))
    array.push(grad, color.new(#ffc100, transparencyvalue))
    array.push(grad, color.new(#ffca00, transparencyvalue))
    array.push(grad, color.new(#ffd300, transparencyvalue))
    array.push(grad, color.new(#ffdc00, transparencyvalue))
    array.push(grad, color.new(#ffe500, transparencyvalue))
    array.push(grad, color.new(#ffed00, transparencyvalue))
    array.push(grad, color.new(#fff600, transparencyvalue))
    array.push(grad, color.new(#ffff00, transparencyvalue))
    array.push(grad, color.new(#eefd1d, transparencyvalue))
    array.push(grad, color.new(#ddfb2d, transparencyvalue))
    array.push(grad, color.new(#ccf83a, transparencyvalue))
    array.push(grad, color.new(#bcf546, transparencyvalue))
    array.push(grad, color.new(#adf150, transparencyvalue))
    array.push(grad, color.new(#9eee59, transparencyvalue))
    array.push(grad, color.new(#8fea62, transparencyvalue))
    array.push(grad, color.new(#81e66a, transparencyvalue))
    array.push(grad, color.new(#74e172, transparencyvalue))
    array.push(grad, color.new(#66dc79, transparencyvalue))
    array.push(grad, color.new(#5ad87f, transparencyvalue))
    array.push(grad, color.new(#4dd385, transparencyvalue))
    array.push(grad, color.new(#41cd8a, transparencyvalue))
    array.push(grad, color.new(#36c88f, transparencyvalue))
    array.push(grad, color.new(#2cc393, transparencyvalue))
    array.push(grad, color.new(#24bd96, transparencyvalue))
    array.push(grad, color.new(#1fb798, transparencyvalue))
    array.push(grad, color.new(#1eb299, transparencyvalue))
    array.push(grad, color.new(#21ac9a, transparencyvalue))
    array.push(grad, color.new(#26a69a, transparencyvalue))
    array.push(grad, color.new(#26aca0, transparencyvalue))
    array.push(grad, color.new(#26b1a6, transparencyvalue))
    array.push(grad, color.new(#25b7ad, transparencyvalue))
    array.push(grad, color.new(#24bdb3, transparencyvalue))
    array.push(grad, color.new(#24c3ba, transparencyvalue))
    array.push(grad, color.new(#23c9c0, transparencyvalue))
    array.push(grad, color.new(#21cfc7, transparencyvalue))
    array.push(grad, color.new(#20d5ce, transparencyvalue))
    array.push(grad, color.new(#1edbd4, transparencyvalue))
    array.push(grad, color.new(#1be1db, transparencyvalue))
    array.push(grad, color.new(#18e7e2, transparencyvalue))
    array.push(grad, color.new(#15ede9, transparencyvalue))
    array.push(grad, color.new(#10f3f0, transparencyvalue))
    array.push(grad, color.new(#09f9f8, transparencyvalue))
    array.push(grad, color.new(#00ffff, transparencyvalue))
    array.push(grad, color.new(#00f5ff, transparencyvalue))
    array.push(grad, color.new(#00ebff, transparencyvalue))
    array.push(grad, color.new(#00e1ff, transparencyvalue))
    array.push(grad, color.new(#00d6ff, transparencyvalue))
    array.push(grad, color.new(#00cbff, transparencyvalue))
    array.push(grad, color.new(#00bfff, transparencyvalue))
    array.push(grad, color.new(#00b3ff, transparencyvalue))
    array.push(grad, color.new(#00a6ff, transparencyvalue))
    array.push(grad, color.new(#0099ff, transparencyvalue))
    array.push(grad, color.new(#008aff, transparencyvalue))
    array.push(grad, color.new(#007bff, transparencyvalue))
    array.push(grad, color.new(#0069ff, transparencyvalue))
    array.push(grad, color.new(#0055ff, transparencyvalue))
    array.push(grad, color.new(#003bff, transparencyvalue))
    array.push(grad, color.new(#0000ff, transparencyvalue))

lower = ta.lowest(dclength)
upper = ta.highest(dclength)
dma = math.round(math.avg(lower, upper))
rsival = math.round(ta.rsi(dma, dclength))
gradcolor = array.get(grad, rsival)
basecolor = donchianrainbow2 ? color.new(gradcolor, 0) : regularfill

//envelopes plots
dc1 = plot(donchianon ? upper : na, title='Upper Channel Limit', color=donchianrainbow2 ? color.new(basecolor, 0) : uplim, linewidth=1, style=plot.style_stepline)
dc2 = plot(donchianon ? lower : na, title='Lower Channel Limit', color=donchianrainbow2 ? color.new(basecolor, 0) : dnlim, linewidth=1, style=plot.style_stepline)
dcfill = color.new(gradcolor, transparencyvalue2)
dccolor = donchianrainbow2 ? color.new(dcfill, 95) : regularfill
fill(dc1, dc2, color=color.new(dccolor, transparencyvalue2), title='Fill')

p2 = plot(don2, title='Donchian 2', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don2, len2) / 1)) : don2 >= don2[1] ? bullma : bearma)
p3 = plot(don3, title='Donchian 3', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don3, len3) / 1)) : don3 >= don3[1] ? bullma : bearma)
p4 = plot(don4, title='Donchian 4', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don4, len4) / 1)) : don4 >= don4[1] ? bullma : bearma)
p5 = plot(don5, title='Donchian 5', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don5, len5) / 1)) : don5 >= don5[1] ? bullma : bearma)
p6 = plot(don6, title='Donchian 6', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don6, len6) / 1)) : don6 >= don6[1] ? bullma : bearma)
p7 = plot(don7, title='Donchian 7', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don7, len7) / 1)) : don7 >= don7[1] ? bullma : bearma)
p8 = plot(don8, title='Donchian 8', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don8, len8) / 1)) : don8 >= don8[1] ? bullma : bearma)
p9 = plot(don9, title='Donchian 9', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don9, len9) / 1)) : don9 >= don9[1] ? bullma : bearma)
p10 = plot(don10, title='Donchian 10', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don10, len10) / 1)) : don10 >= don10[1] ? bullma : bearma)
p11 = plot(don11, title='Donchian 11', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don11, len11) / 1)) : don11 >= don11[1] ? bullma : bearma)
p12 = plot(don12, title='Donchian 12', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don12, len12) / 1)) : don12 >= don12[1] ? bullma : bearma)
p13 = plot(don13, title='Donchian 13', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don13, len13) / 1)) : don13 >= don13[1] ? bullma : bearma)
p14 = plot(don14, title='Donchian 14', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don14, len14) / 1)) : don14 >= don14[1] ? bullma : bearma)
p15 = plot(don15, title='Donchian 15', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don15, len15) / 1)) : don15 >= don15[1] ? bullma : bearma)
p16 = plot(don16, title='Donchian 16', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don16, len16) / 1)) : don16 >= don16[1] ? bullma : bearma)
p17 = plot(don17, title='Donchian 17', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don17, len17) / 1)) : don17 >= don17[1] ? bullma : bearma)
p18 = plot(don18, title='Donchian 18', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don18, len18) / 1)) : don18 >= don18[1] ? bullma : bearma)
p19 = plot(don19, title='Donchian 19', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don19, len19) / 1)) : don19 >= don19[1] ? bullma : bearma)
p20 = plot(don20, title='Donchian 20', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don20, len20) / 1)) : don20 >= don20[1] ? bullma : bearma)
p21 = plot(don21, title='Donchian 21', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don21, len21) / 1)) : don21 >= don21[1] ? bullma : bearma)
p22 = plot(don22, title='Donchian 22', linewidth=1, color = donchianrainbow ? array.get(grad, math.round(ta.rsi(don22, len22) / 1)) : don22 >= don22[1] ? bullma : bearma)