Multi-Band Breakout Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Multi-Band Breakout Indicator", overlay=true)

// Calculation Inputs
length = input(14, "ATR Length")
dLength = input(40, 'Donchian Length')
multiplier = input(2.0, "Multiplier")
malength = input(40, 'MA Length')

// Calculate ATR
atr = ta.atr(length)

// Calculate Bands and Basis
upperBand = ta.ema(ta.highest(high, length) + multiplier * atr, length)
lowerBand = ta.ema(ta.lowest(low, length) - multiplier * atr, length)
trend = (upperBand + lowerBand) / 2
trendMA = ta.ema(trend, malength)

// Calculate Donchians
hhigh = ta.highest(upperBand, dLength)
llow = ta.lowest(lowerBand, dLength)

// Breakouts
//breakoutUp = trend > trendMA and close > trend and close > trendMA and high >= hhigh and high >= upperBand
//breakoutDown = trend < trendMA and close < trend and close < trendMA and low <= llow and low <= lowerBand
//breakoutUp2 = high > upperBand and upperBand >= hhigh[1] and close > trendMA and close[1] <= trendMA[1] and trend > trendMA
//breakoutDown2 = low < lowerBand and lowerBand <= llow[1] and close < trendMA and close[1] >= trendMA[1] and trend < trendMA
breakoutUp3 = ta.crossover(trend, trendMA) and upperBand >= hhigh[1] and close > trendMA
breakoutDown3 = ta.crossunder(trend, trendMA) and lowerBand <= llow[1] and close < trendMA

// Color
tColor = trend > close and trendMA > close ? color.fuchsia : trend < close and trendMA < close ? color.lime : color.yellow
maColor = trend > trendMA ? color.new(color.lime, 70) : color.new(color.fuchsia, 70)
barcolor(tColor)

highColor = hhigh > upperBand ? color.new(color.olive, 90) : color.new(color.green,90)
lowColor = llow < lowerBand ? color.new(color.red, 90) : color.new(color.fuchsia, 90)

// Plotting
upperBandplot = plot(upperBand, color=color.lime, title="Upper", linewidth = 2)
lowerBandplot = plot(lowerBand, color=color.fuchsia, title='Lower', linewidth=2)

hhighplot = plot(hhigh, title='Highest Point in Last X Periods', color=color.green, linewidth = 2)
llowplot = plot(llow, title='Lowest Point in X Periods', linewidth = 2, color=color.red)

trendplot = plot(trend, color=color.aqua, title='Basis', linewidth = 4)
trendMAplot = plot(trendMA, color=color.orange, title='Trend MA', linewidth = 4)

fill(plot1=trendplot, plot2=trendMAplot, color=maColor)
fill(plot1=llowplot, plot2=lowerBandplot, color=lowColor)
fill(plot1=hhighplot, plot2=upperBandplot, color=highColor)

plotshape(breakoutUp3, "Long Entry", shape.triangleup, location.belowbar, color=color.green, size=size.large)
plotshape(breakoutDown3, "Short Entry", shape.triangledown, location.abovebar, color=color.red, size=size.large)