Relative Bandwidth Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator("Relative Bandwidth Filter")
import HeWhoMustNotBeNamed/enhanced_ta/14 as eta

bandType = input.string("KC", title="Type", group="Bands", options=["BB", "KC", "DC"])
bmasource = input.source(close, title="Source", group="Bands")
bmatype = input.string("sma", title="Type", group="Bands", options=["sma", "ema", "hma", "rma", "wma", "vwma", "swma", "linreg", "median"])
bmalength = input.int(100, title="Length", group="Bands")
multiplier = input.float(2.0, step=0.5, title="Multiplier", group="Bands")
useTrueRange = input.bool(true, title="Use True Range (KC)", group="Bands")
useAlternateSource = input.bool(false, title="Use Alternate Source (DC)", group="Bands")
bsticky = input.bool(true, title="Sticky", group="Bands/Bandwidth/BandPercent")

atrLength = input.int(20, 'Length', group='ATR')


bbmatype = input.string("sma", title="Type", group="BBands", options=["sma", "ema", "hma", "rma", "wma", "vwma", "linreg", "median"])
bbmalength = input.int(100, title="Length", group="BBands")
mmultiplier = input.float(1.0, step=0.5, title="Multiplier", group="BBands")

desiredCondition = input.string("Higher Bandwidth", "Desired Condition", options=["Higher Bandwidth", "Lower Bandwidth"])
referenceBand = input.string("Middle", options=["Upper", "Lower", "Middle"])

var cloudTransparency = 90
[bbmiddle, bbupper, bblower] = eta.bb(bmasource, bmatype, bmalength, multiplier, sticky=bsticky)
[kcmiddle, kcupper, kclower] = eta.kc(bmasource, bmatype, bmalength, multiplier, useTrueRange, sticky=bsticky)
[dcmiddle, dcupper, dclower] = eta.dc(bmalength, useAlternateSource, bmasource, sticky=bsticky)

upper = bandType == "BB"? bbupper : bandType == "KC"? kcupper : dcupper
lower = bandType == "BB"? bblower : bandType == "KC"? kclower : dclower
middle = bandType == "BB"? bbmiddle : bandType == "KC"? kcmiddle : dcmiddle

atr = ta.atr(atrLength)

relativeBandwidth = (upper-lower)/atr

plot(relativeBandwidth, "Relative Bandwidth", color=color.purple)

[mmiddle, uupper, llower] = eta.bb(relativeBandwidth, bbmatype, bbmalength, mmultiplier, sticky=false)

plot(mmiddle, 'Middle', color=color.blue)
plot(uupper, 'Upper', color=color.green)
plot(llower, 'Lower', color=color.red)

reference = referenceBand == "Middle"? mmiddle : referenceBand == "Upper"? uupper : llower
signal = relativeBandwidth > reference? 2 : 0
signal := desiredCondition == "Lower Bandwidth"? math.abs(signal-2) : signal
plot(signal, "Signal", display=display.data_window)
