Nadaraya-Watson Envelope Alternative [CHE] Super Envelope
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chervolino

//@version=5
indicator("Nadaraya-Watson Envelope Alternative [CHE] Super Envelope", shorttitle="Super Envelope [CHE]", overlay=true, max_bars_back=1000, max_lines_count=500, max_labels_count=500)
// ———————————————————— Constants and Inputs {

// ————— Constants
string TT_len             = "Length for John Ehlers’ 2-pole Butterworth filter “Super Smoother”"
string TT_mult            = "Multiplier for the bands"
string TT_showMiddle      = "On and off center line"
string TT_showdisclaimer  = "Uncheck to hide the disclaimer"
string  GRP2                    = '═══════════   Settings   ═══════════'
// ————— Inputs
int len         = input(50, "smoothing length"     , tooltip = TT_len           , group = GRP2)
float      mult = input(5.0,"Adjustable multiplier", tooltip = TT_mult          , group = GRP2)
bool showMiddle = input(false, "Show middle band"  , tooltip = TT_showMiddle    , group = GRP2)
bool disclaimer = input(false, 'Hide Disclaimer'   , tooltip = TT_showdisclaimer, group = GRP2)
up_col =    input.color(#39ff14,'Colors',inline='col')
dn_col =    input.color(#ff1100,'',inline='col')
// ———————————————————— Functions {
super(float src, int len) =>
    f = (1.414 * math.pi)/len
    a = math.exp(-f)
    c2 = 2 * a * math.cos(f)
    c3 = -a*a
    c1 = 1-c2-c3
    smooth = 0.0
    smooth := c1*(src+src[1])*0.5+c2*nz(smooth[1])+c3*nz(smooth[2])

// ———————————————————— Calculations and Plots {
// Stop the indicator on charts with no volume.
if barstate.islast and ta.cum(nz(volume)) == 0
    runtime.error("No volume is provided by the data vendor.")

a=super(high,len)
b=super(low,len)
c=(a-b)/2*mult
d=b+(a-b)/2
plotcolor = d>d[1]? up_col : dn_col
bullish_bearish = d>d[1]? true : false
plot(a+c, color=up_col)
plot(b-c, color=dn_col)
plot(showMiddle? d:na, color=plotcolor)
plotchar(ta.crossover(high,a+c),char = "💰", location = location.abovebar, size = size.tiny)
plotchar(ta.crossunder(low,b-c),char = "🛒",location = location.belowbar , size = size.tiny)

// ———————————————————— Alerts
alertcondition(bullish_bearish == true ,title='Bearish Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bearish ▼')
alertcondition(bullish_bearish == false, title='Bullish Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bullish ▲')
alertcondition(ta.crossunder(high,a+c) , title='Price close under lower band', message='Nadaraya-Watson: {{ticker}} ({{interval}}) crossed under lower band 💰')
alertcondition(ta.crossover(low,b-c)   , title='Price close above upper band', message='Nadaraya-Watson: {{ticker}} ({{interval}}) Crossed above upper band 🛒')

//----
var tb = table.new(position.top_right, 1, 1
  , bgcolor = #35202b)

if barstate.isfirst and not disclaimer
    table.cell(tb, 0, 0, 'Super Envelope [CHE] non repaiting'
      , text_size = size.small
      , text_color = #cc2f3c)
    