Donchian RSI Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DDMyke

//@version=5
indicator(title="Donchian RSI Bands", shorttitle="dR$Ib", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

//Inputs
src = input(ohlc4, "Source")
rbdBC = input.bool(false, 'RSI Bollinger Band Barcolor', group='Barcolor')
rcBC = input.bool(false, 'RSI Cloud Barcolor', group='Barcolor')
dcBC = input.bool(false, 'Donchian Basis Barcolor', group='Barcolor')

showFrsi = input.bool(true, 'RSI', group='RSI')
len = input.int(7, minval=1, title="Length", group='RSI')
rsiUc = input(color.new(#00bcd4,0), title='Bullish Color', inline='rsi', group='RSI')
rsiNc = input(color.new(#0c3299,0), title='Neutral Color', inline='rsi', group='RSI')
rsiDc = input(color.new(#d930ff,0), title='Bearish Color', inline='rsi', group='RSI')
rsiWidth = input.int(1, 'Line Width', minval=1, maxval=4, group='RSI')
showDrsi = input.bool(false, 'RSI (Default Length)', inline='rsi2', group='RSI')
rsi2C = input(color.new(#ffffff,50), title='Color', inline='rsi2', group='RSI' )
showCloud = input(false, 'RSI Cloud', group='RSI')
upCloud = input(color.new(#00bcd4,85), title='Bullish Cloud', inline='cloud', group='RSI')
downCloud = input(color.new(#d930ff,85), title='Bearish Cloud', inline='cloud', group='RSI')

showBB = input.bool(true, 'Bollinger Bands', group='Double Bollinger Bands')
length = input.int(35, 'Length', group='Double Bollinger Bands')
mult2 = input.float(0.5, minval=0.5, step=0.25, maxval=5, title='Outer Band Deviation', group='Double Bollinger Bands')
mult = input.float(0.25, minval=0.25, step=0.25, maxval=5, title='Inner Band Deviation', group='Double Bollinger Bands')
offset = input.int(0, "Offset", minval = -500, maxval = 500, group='Double Bollinger Bands')
bbandColor = input(color.new(#131722,20), title='Color', group='Double Bollinger Bands')
showBBbasis = input.bool(true, 'Basis', group = 'Double Bollinger Bands')
disp = input.float(0.1, 'Multiplier', minval=0.025, step=0.025, maxval=2, group='Double Bollinger Bands')

showDC = input.bool(true,'Donchian Channel', group='Donchian Channel')
DClength = input.int(70, 'Length', group='Donchian Channel')
DCwidth = input.int(5, 'Band Width', minval=1, maxval=20, group='Donchian Channel')
dcUb = input(color.new(#00bcd4,85), title='Upper Band', inline='dcb', group='Donchian Channel')
dcDb = input(color.new(#d930ff,85), title='Lower Band', inline='dcb', group='Donchian Channel')
showDCB = input.bool(false,'Donchian Channel Basis', group='Donchian Channel')
dcUc = input(color.new(#4caf50,0), title='Bullish Color', inline='dc', group='Donchian Channel')
dcDc = input(color.new(#e91e63,0), title='Bearish Color', inline='dc', group='Donchian Channel')

//Calculations
//RSI
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
crsi = rsi

//RSI (Default Length)
up2 = ta.rma(math.max(ta.change(src), 0), 14)
down2 = ta.rma(-math.min(ta.change(src), 0), 14)
rsi2 = down2 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up2 / down2))
crsi2 = rsi2

//Double Bollinger Bands
basis = ta.sma(rsi, length)
dev = mult * ta.stdev(rsi, length)
dev2 = mult2 * ta.stdev(rsi, length)
upper = basis + dev
upper2 = basis + dev2
lower = basis - dev
lower2 = basis - dev2


//Basis Dispersion
UpDisp = basis + (upper - lower) * disp
DownDisp = basis - (upper - lower) * disp
d1 = plot(showBBbasis and UpDisp ? UpDisp : na, color=color.new(#131722, 100), title='BB Basis Upper', linewidth=1, editable=false)
d2 = plot(showBBbasis and DownDisp ? DownDisp : na, color=color.new(#131722, 100), title='BB Basis Lower', linewidth=1, editable=false)

//Donchian Channel
upperdc = ta.highest(rsi,DClength)
lowerdc = ta.lowest(rsi,DClength)
basisdc = math.avg(upperdc, lowerdc)

//Plots
rsiColor = rsi > upper2  ? rsiUc : rsi < lower2 ? rsiDc : rsiNc
dcColor = rsi > basisdc ? dcUc : rsi < basisdc ? dcDc : na
cloudFill = rsi > rsi2 ? upCloud : downCloud
cloudColor = rsi > rsi2 ? rsiUc : rsiDc

barcolor(rbdBC ? rsiColor : na, editable=false)
barcolor(dcBC ? dcColor : na, editable=false)
barcolor(rcBC ? cloudColor : na, editable=false)

plot(showFrsi and rsi ? rsi : na, 'RSI', color=rsiColor, linewidth=rsiWidth, editable=false)
c1 = plot(crsi, 'CRSI', color=color.new(#000000,100), editable=false)
plot(showDrsi and rsi2 ? rsi2 : na, 'RSI (Default Length)', color=rsi2C, linewidth=1, editable=false)
c2 = plot(crsi2, 'CRSI2', color=color.new(#000000,100), editable=false)
p1 = plot(showBB and upper ? upper : na, 'Inner Upper Band', color=color.new(#2a2e39,100), offset=offset, editable=false)
p2 = plot(showBB and upper2 ? upper2 : na, 'Outer Upper Band', color=color.new(#2a2e39,100), offset=offset, editable=false)
p3 = plot(showBB and lower ? lower : na, 'Inner Lower Band', color=color.new(#2a2e39,100), offset=offset, editable=false)
p4 = plot(showBB and lower2 ? lower2 : na, 'Outer Lower Band', color=color.new(#2a2e39,100), offset=offset, editable=false)

plot(showDC and upperdc ? upperdc : na, color=dcUb, title='Donchian Channel Upper Band', linewidth=DCwidth, editable=false)
plot(showDC and lowerdc ? lowerdc : na, color=dcDb, title='Donchian Channel Lower Band', linewidth=DCwidth, editable=false)
plot(showDCB and basisdc ? basisdc : na, 'Donchian Channel Basis', color=dcColor, linewidth=1, editable=false)

fill(p1,p2, color=bbandColor, title='Upper Band Fill', editable=false)
fill(p3,p4, color=bbandColor, title='Upper Band Fill', editable=false)
fill(d1, d2, color=showBBbasis ? bbandColor : na, title='BB Basis Fill', editable=false)
fill(c1, c2, color=showCloud ? cloudFill : na, title='RSI Cloud Fill', editable=false)

hline(50, color=color.new(#ff8160,85), linewidth=1, linestyle=hline.style_dashed)




