Supertrend ANY INDICATOR (RSI, MFI, CCI, etc.) + Range Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wbburgin

import wbburgin/wbburgin_utils/1 as e

//@version=5
indicator("Supertrend ANY INDICATOR (RSI, MFI, CCI, etc.) + Range Filter",shorttitle="ST ANY INDICATOR [wbburgin]",overlay=false)

// This indicator will generate a supertrend of your chosen configuration on any of the following indicators:
    // RSI
    // MFI
    // ACCUMULATION / DISTRIBUTION
    // MOMENTUM
    // ON BALANCE VOLUME
    // CCI
    // Stochastic

// If you find this indicator useful, please boost it and follow! I am open to suggestions for adding new indicators
// to this script, it's very simple to add new ones, just suggest them in the comments.


// INPUTS ______________________________________________________________________________________________________________

sel = input.string("RSI","Indicator for Supertrend",
 options=["RSI","MFI","Accum/Dist","Momentum","OB Volume","CCI","Stochastic"],
  group="Main Settings")
indicator_length = input.int(14, "Indicator Length (if applicable)",group="Main Settings",
 tooltip = "Ignore if indicator does not have length inputs (i.e. Accum/Dist, OBV)")

atr_length = input.int(10, "Supertrend ATR Length",group="Main Settings")
atr_mult = input.float(3.0,"Supertrend ATR Mult",step=0.5,group="Main Settings")

use_range = input.bool(false,"Use Range Filter of Indicator",
 tooltip = "Use a range filter of the indicator instead of the indicator itself for calculations (for smoothing)",
  group="Range Filter")
sampling_period = input.int(50,"Range Filter Sampling Period (if applicable)",group="Range Filter")
range_mult = input.float(3,"Range Filter Multiple (if applicable)",group="Range Filter")

labels = input.bool(true,"Show Labels",group="Display")
highlighting = input.bool(true,"Display Highlighting",group="Display")
oversold = input.int(30,"Oversold Level (if applicable)",group="Display")
overbought = input.int(70,"Overbought Level (if applicable)",group="Display")

// CALCULATIONS ________________________________________________________________________________________________________

filt(source,sampling_period,range_mult) =>
    e.rngfilt(source,e.smoothrng(source,sampling_period,range_mult))

rsi = switch
    use_range == false => ta.rsi(close,indicator_length)
    => filt(ta.rsi(close,indicator_length),sampling_period,range_mult)

mfi = switch
    use_range == false => ta.mfi(close,indicator_length)
    => filt(ta.mfi(close,indicator_length),sampling_period,range_mult)

accdist = switch
    use_range == false => ta.accdist
    => filt(ta.accdist,sampling_period,range_mult)

mom = switch
    use_range == false => ta.mom(close,indicator_length)
    => filt(ta.mom(close,indicator_length),sampling_period,range_mult)

obv = switch
    use_range == false => ta.obv
    => filt(ta.obv,sampling_period,range_mult)

cci = switch
    use_range == false => ta.cci(close,indicator_length)
    => filt(ta.cci(close,indicator_length),sampling_period,range_mult)

stoch = switch
    use_range == false => ta.stoch(close,high,low,indicator_length)
    => filt(ta.stoch(close,high,low,indicator_length),sampling_period,range_mult)

indicator = switch
    sel == "RSI" => rsi
    sel == "MFI" => mfi
    sel == "Accum/Dist" => accdist
    sel == "Momentum" => mom
    sel == "OB Volume" => obv
    sel == "CCI" => cci
    sel == "Stochastic" => stoch

not_bound = indicator==accdist or indicator==obv

[supertrend,dir] = e.supertrend_anysource(indicator,atr_mult,atr_length)
supertrend_up = dir == -1 ? supertrend : na
supertrend_dn = dir == 1 ? supertrend : na
supertrend_up_start = dir == -1 and dir[1] == 1 ? supertrend : na
supertrend_dn_start = dir == 1 and dir[1] == -1 ? supertrend : na

linecolor = dir == -1 ? color.green : color.red
fillcolor = dir == -1 ? color.new(color.green,88) : color.new(color.red,88)

sup = plot(supertrend_up,title="Up Supertrend",color=linecolor,style=plot.style_linebr,linewidth=2)
sdn = plot(supertrend_dn,title="Down Supertrend",color=linecolor,style=plot.style_linebr,linewidth=2)
mid=plot(indicator,color=color.white)

plotshape(supertrend_up_start,title="Supertrend Up Start",style=shape.circle,location=location.absolute,
 color=color.green, size=size.tiny)
plotshape(supertrend_dn_start,title="Supertrend Down Start",style=shape.circle,location=location.absolute,
 color=color.red, size=size.tiny)
plotshape(labels==false?na:supertrend_up_start,title="Supertrend Buy Label",style=shape.labelup,location=location.absolute,
 color=color.green,textcolor=color.white,text = "Buy")
plotshape(labels==false?na:supertrend_dn_start,title="Supertrend Sell Label",style=shape.labeldown,location=location.absolute,
 color=color.red,textcolor=color.white,text = "Sell")

fill(sup,mid,highlighting==true ? fillcolor : na)
fill(sdn,mid,highlighting==true ? fillcolor : na)

hiPlot=plot(not_bound ? na : oversold,title="Oversold Level",color=color.new(color.white,75),style=plot.style_circles)
loPlot=plot(not_bound ? na : overbought,title="Overbought Level",color=color.new(color.white,75),style=plot.style_circles)

fill(hiPlot,loPlot,color=highlighting==true ? color.new(color.purple,95):na)

alertcondition(supertrend_up_start,"Supertrend Any Indicator Buy")
alertcondition(supertrend_dn_start,"Supertrend Any Indicator Sell")