Trend Gaussian Channels [DeltaAlgo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DeltaAlgo

//@version=5
indicator("Trend Gaussian Channels [DeltaAlgo]", "Trend Gaussian Channels [DeltaAlgo]", true)

// user inputs
length = input.int(25, "Length", 2, 999, 1, inline = "tgc", group = "trend gaussian channel settings")
mult   = input.float(1, "", 1, 5, 0.01, "Top & Bottom Band Multiplier", "tgc", "trend gaussian channel settings")

barcol = input.bool(true, "Bar Colors", "Toggle On/Off Candle Colors", group = "candle color settings")
signal = input.bool(true, "Trend Shift Signals", "Toggle On/Off Trend Signals When Trend Shifts", group = "candle color settings")

// gaussian channel

guassianBasis(float source, int length, float mult) =>
    basis = ta.ema(ta.hma(ta.sma(source, length), length * 2), length /2)
    topBand = basis + (ta.atr(105) * (mult * 1.005))
    lowBand = basis - (ta.atr(105) / (mult / 1.005))
    [basis, topBand, lowBand]

[base, upper, lower] = guassianBasis(close, length, mult)

upward = base < high
dnward = base > low

basePlot  = plot(base, "Base", upward ? color.new(#00cf4b, 15) : color.new(#ff5252, 15), 2)
upperPlot = plot(upper, "Upper", upward ? color.new(#00cf4b, 20) : color.new(#ff5252, 20), 2)
lowerPlot = plot(lower, "Lower", upward ? color.new(#00cf4b, 20) : color.new(#ff5252, 20), 2)

fill(upperPlot, lowerPlot, upward ? color.new(#00cf4b, 80) : color.new(#ff0000, 80), "Channel Fill")

// barcolor 

uptrend = color.from_gradient(ta.rsi(close, 15), 25, 76, #67248f, #00cf4b)
ranging = color.from_gradient(ta.rsi(close, 15), 30, 75, #63726f, #67248f)
dntrend = color.from_gradient(ta.rsi(close, 15), 8, 82, #ff0000, #67248f)

barcolor(barcol ? high > upper ? uptrend : high < upper and high > lower ? ranging : dntrend : na)

// signals

upSignalCondition = high > upper and ta.crossover(low, upper)
dnSignalCondition = low < lower and ta.crossunder(high, lower)

plotshape(signal and upSignalCondition, "Bullish Signal", shape.labelup, location.belowbar, color.new(#00cf4b, 35), size = size.small)
plotshape(signal and dnSignalCondition, "Bearish Signal", shape.labeldown, location.abovebar, color.new(#ff0000, 35), size = size.small)


