Hurst Spectral Analysis SwamiChart
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BarefootJoey

// ██████████████████████████████████████████████████████████████████████
// █▄─▄─▀██▀▄─██▄─▄▄▀█▄─▄▄─█▄─▄▄─█─▄▄─█─▄▄─█─▄─▄─███▄─▄█─▄▄─█▄─▄▄─█▄─█─▄█
// ██─▄─▀██─▀─███─▄─▄██─▄█▀██─▄███─██─█─██─███─███─▄█─██─██─██─▄█▀██▄─▄██
// █▄▄▄▄██▄▄█▄▄█▄▄█▄▄█▄▄▄▄▄█▄▄▄███▄▄▄▄█▄▄▄▄██▄▄▄██▄▄▄███▄▄▄▄█▄▄▄▄▄██▄▄▄██

//@version=5
indicator("Hurst Spectral Analysis SwamiChart", overlay=false)

var color brightgreen = #00ff0a
var color brightred = #ff1100

grbps = "Bandpass Settings"
grscs = "SwamiChart Settings"

//-------------------------------Inputs-------------------------------//
source = input(close, 'Bandpass Source', group=grbps)
bandWidth = input.float(0.025, 'Bandwidth', minval=0.0, maxval=1.0, group=grbps)
periodBP = input.int(40, 'Base Period', minval=2, group=grbps)
periodStep = input.int(1, 'Step', minval=1, group=grbps)
hm_type = "Amplitude"
bull_col = input.color(brightgreen, group=grscs)
bear_col = input.color(brightred, group=grscs)
width = input.int(2, 'Linewidth', minval=1, maxval=4, group=grscs)
text_size   = input.session('Normal', "Text Size", options=["Tiny","Small","Normal","Large"], group=grscs)

//-------------------------------Functions & Calculations-------------------------------//
// @TMPascoe found & offered this bandpass by @HPotter found here https://www.tradingview.com/script/A1jhw5fG-Bandpass-Filter/
//      @BarefootJoey turned the code into a function and made it accept float Period
bpf(Series, float Period, Delta) =>
    float tmpbpf = na 
    var beta = math.cos(3.14 * (360 / Period) / 180)
    var gamma = 1 / math.cos(3.14 * (720 * Delta / Period) / 180)
    var alpha = gamma - math.sqrt(gamma * gamma - 1)
    tmpbpf := 0.5 * (1 - alpha) * (Series - Series[2]) + beta * (1 + alpha) * nz(tmpbpf[1]) - alpha * nz(tmpbpf[2])

// Individual Bandpass Filters
bpf1 = bpf(source, periodBP + (periodStep*0), bandWidth)
bpf2 = bpf(source, periodBP + (periodStep*1), bandWidth)
bpf3 = bpf(source, periodBP + (periodStep*2), bandWidth)
bpf4 = bpf(source, periodBP + (periodStep*3), bandWidth)
bpf5 = bpf(source, periodBP + (periodStep*4), bandWidth)
bpf6 = bpf(source, periodBP + (periodStep*5), bandWidth)
bpf7 = bpf(source, periodBP + (periodStep*6), bandWidth)
bpf8 = bpf(source, periodBP + (periodStep*7), bandWidth)
bpf9 = bpf(source, periodBP + (periodStep*8), bandWidth)
bpf10 = bpf(source, periodBP + (periodStep*9), bandWidth)
bpf11 = bpf(source, periodBP + (periodStep*10), bandWidth)
bpf12 = bpf(source, periodBP + (periodStep*11), bandWidth)
bpf13 = bpf(source, periodBP + (periodStep*12), bandWidth)
bpf14 = bpf(source, periodBP + (periodStep*13), bandWidth)
bpf15 = bpf(source, periodBP + (periodStep*14), bandWidth)
bpf16 = bpf(source, periodBP + (periodStep*15), bandWidth)
bpf17 = bpf(source, periodBP + (periodStep*16), bandWidth)
bpf18 = bpf(source, periodBP + (periodStep*17), bandWidth)
bpf19 = bpf(source, periodBP + (periodStep*18), bandWidth)
bpf20 = bpf(source, periodBP + (periodStep*19), bandWidth)
bpf21 = bpf(source, periodBP + (periodStep*20), bandWidth)
bpf22 = bpf(source, periodBP + (periodStep*21), bandWidth)
bpf23 = bpf(source, periodBP + (periodStep*22), bandWidth)
bpf24 = bpf(source, periodBP + (periodStep*23), bandWidth)
bpf25 = bpf(source, periodBP + (periodStep*24), bandWidth)
bpf26 = bpf(source, periodBP + (periodStep*25), bandWidth)
bpf27 = bpf(source, periodBP + (periodStep*26), bandWidth)
bpf28 = bpf(source, periodBP + (periodStep*27), bandWidth)
bpf29 = bpf(source, periodBP + (periodStep*28), bandWidth)
bpf30 = bpf(source, periodBP + (periodStep*29), bandWidth)
bpf31 = bpf(source, periodBP + (periodStep*30), bandWidth)
bpf32 = bpf(source, periodBP + (periodStep*31), bandWidth)

// Hi/Lo (Amplitude) Gradient
grad1 = color.from_gradient(bpf1, ta.lowest(bpf1,periodBP +(periodStep*0)), ta.highest(bpf1,periodBP +(periodStep*0)), bull_col, bear_col)
grad2 = color.from_gradient(bpf2, ta.lowest(bpf2,periodBP +(periodStep*1)), ta.highest(bpf2,periodBP +(periodStep*1)), bull_col, bear_col)
grad3 = color.from_gradient(bpf3, ta.lowest(bpf3,periodBP +(periodStep*2)), ta.highest(bpf3,periodBP +(periodStep*2)), bull_col, bear_col)
grad4 = color.from_gradient(bpf4, ta.lowest(bpf4,periodBP +(periodStep*3)), ta.highest(bpf4,periodBP +(periodStep*3)), bull_col, bear_col)
grad5 = color.from_gradient(bpf5, ta.lowest(bpf5,periodBP +(periodStep*4)), ta.highest(bpf5,periodBP +(periodStep*4)), bull_col, bear_col)
grad6 = color.from_gradient(bpf6, ta.lowest(bpf6,periodBP +(periodStep*5)), ta.highest(bpf6,periodBP +(periodStep*5)), bull_col, bear_col)
grad7 = color.from_gradient(bpf7, ta.lowest(bpf7,periodBP +(periodStep*6)), ta.highest(bpf7,periodBP +(periodStep*6)), bull_col, bear_col)
grad8 = color.from_gradient(bpf8, ta.lowest(bpf8,periodBP +(periodStep*7)), ta.highest(bpf8,periodBP +(periodStep*7)), bull_col, bear_col)
grad9 = color.from_gradient(bpf9, ta.lowest(bpf9,periodBP +(periodStep*8)), ta.highest(bpf9,periodBP +(periodStep*8)), bull_col, bear_col)
grad10 = color.from_gradient(bpf10, ta.lowest(bpf10,periodBP +(periodStep*9)), ta.highest(bpf10,periodBP +(periodStep*9)), bull_col, bear_col)
grad11 = color.from_gradient(bpf11, ta.lowest(bpf11,periodBP +(periodStep*10)), ta.highest(bpf11,periodBP +(periodStep*10)), bull_col, bear_col)
grad12 = color.from_gradient(bpf12, ta.lowest(bpf12,periodBP +(periodStep*11)), ta.highest(bpf12,periodBP +(periodStep*11)), bull_col, bear_col)
grad13 = color.from_gradient(bpf13, ta.lowest(bpf13,periodBP +(periodStep*12)), ta.highest(bpf13,periodBP +(periodStep*12)), bull_col, bear_col)
grad14 = color.from_gradient(bpf14, ta.lowest(bpf14,periodBP +(periodStep*13)), ta.highest(bpf14,periodBP +(periodStep*13)), bull_col, bear_col)
grad15 = color.from_gradient(bpf15, ta.lowest(bpf15,periodBP +(periodStep*14)), ta.highest(bpf15,periodBP +(periodStep*14)), bull_col, bear_col)
grad16 = color.from_gradient(bpf16, ta.lowest(bpf16,periodBP +(periodStep*15)), ta.highest(bpf16,periodBP +(periodStep*15)), bull_col, bear_col)
grad17 = color.from_gradient(bpf17, ta.lowest(bpf17,periodBP +(periodStep*16)), ta.highest(bpf17,periodBP +(periodStep*16)), bull_col, bear_col)
grad18 = color.from_gradient(bpf18, ta.lowest(bpf18,periodBP +(periodStep*17)), ta.highest(bpf18,periodBP +(periodStep*17)), bull_col, bear_col)
grad19 = color.from_gradient(bpf19, ta.lowest(bpf19,periodBP +(periodStep*18)), ta.highest(bpf19,periodBP +(periodStep*18)), bull_col, bear_col)
grad20 = color.from_gradient(bpf20, ta.lowest(bpf20,periodBP +(periodStep*19)), ta.highest(bpf20,periodBP +(periodStep*19)), bull_col, bear_col)
grad21 = color.from_gradient(bpf21, ta.lowest(bpf21,periodBP +(periodStep*20)), ta.highest(bpf21,periodBP +(periodStep*20)), bull_col, bear_col)
grad22 = color.from_gradient(bpf22, ta.lowest(bpf22,periodBP +(periodStep*21)), ta.highest(bpf22,periodBP +(periodStep*21)), bull_col, bear_col)
grad23 = color.from_gradient(bpf23, ta.lowest(bpf23,periodBP +(periodStep*22)), ta.highest(bpf23,periodBP +(periodStep*22)), bull_col, bear_col)
grad24 = color.from_gradient(bpf24, ta.lowest(bpf24,periodBP +(periodStep*23)), ta.highest(bpf24,periodBP +(periodStep*23)), bull_col, bear_col)
grad25 = color.from_gradient(bpf25, ta.lowest(bpf25,periodBP +(periodStep*24)), ta.highest(bpf25,periodBP +(periodStep*24)), bull_col, bear_col)
grad26 = color.from_gradient(bpf26, ta.lowest(bpf26,periodBP +(periodStep*25)), ta.highest(bpf26,periodBP +(periodStep*25)), bull_col, bear_col)
grad27 = color.from_gradient(bpf27, ta.lowest(bpf27,periodBP +(periodStep*26)), ta.highest(bpf27,periodBP +(periodStep*26)), bull_col, bear_col)
grad28 = color.from_gradient(bpf28, ta.lowest(bpf28,periodBP +(periodStep*27)), ta.highest(bpf28,periodBP +(periodStep*27)), bull_col, bear_col)
grad29 = color.from_gradient(bpf29, ta.lowest(bpf29,periodBP +(periodStep*28)), ta.highest(bpf29,periodBP +(periodStep*28)), bull_col, bear_col)
grad30 = color.from_gradient(bpf30, ta.lowest(bpf30,periodBP +(periodStep*29)), ta.highest(bpf30,periodBP +(periodStep*29)), bull_col, bear_col)
grad31 = color.from_gradient(bpf31, ta.lowest(bpf31,periodBP +(periodStep*30)), ta.highest(bpf31,periodBP +(periodStep*30)), bull_col, bear_col)
grad32 = color.from_gradient(bpf32, ta.lowest(bpf32,periodBP +(periodStep*31)), ta.highest(bpf32,periodBP +(periodStep*31)), bull_col, bear_col)

plot(0*width, color=grad1, linewidth=width, editable=false)
plot(1*width, color=grad2, linewidth=width, editable=false)
plot(2*width, color=grad3, linewidth=width, editable=false)
plot(3*width, color=grad4, linewidth=width, editable=false)
plot(4*width, color=grad5, linewidth=width, editable=false)
plot(5*width, color=grad6, linewidth=width, editable=false)
plot(6*width, color=grad7, linewidth=width, editable=false)
plot(7*width, color=grad8, linewidth=width, editable=false)
plot(8*width, color=grad9, linewidth=width, editable=false)
plot(9*width, color=grad10, linewidth=width, editable=false)
plot(10*width, color=grad11, linewidth=width, editable=false)
plot(11*width, color=grad12, linewidth=width, editable=false)
plot(12*width, color=grad13, linewidth=width, editable=false)
plot(13*width, color=grad14, linewidth=width, editable=false)
plot(14*width, color=grad15, linewidth=width, editable=false)
plot(15*width, color=grad16, linewidth=width, editable=false)
plot(16*width, color=grad17, linewidth=width, editable=false)
plot(17*width, color=grad18, linewidth=width, editable=false)
plot(18*width, color=grad19, linewidth=width, editable=false)
plot(19*width, color=grad20, linewidth=width, editable=false)
plot(20*width, color=grad21, linewidth=width, editable=false)
plot(21*width, color=grad22, linewidth=width, editable=false)
plot(22*width, color=grad23, linewidth=width, editable=false)
plot(23*width, color=grad24, linewidth=width, editable=false)
plot(24*width, color=grad25, linewidth=width, editable=false)
plot(25*width, color=grad26, linewidth=width, editable=false)
plot(26*width, color=grad27, linewidth=width, editable=false)
plot(27*width, color=grad28, linewidth=width, editable=false)
plot(28*width, color=grad29, linewidth=width, editable=false)
plot(29*width, color=grad30, linewidth=width, editable=false)
plot(30*width, color=grad31, linewidth=width, editable=false)
plot(31*width, color=grad32, linewidth=width, editable=false)

var label_text_size = text_size == 'Tiny' ? size.tiny :
  text_size == 'Small' ? size.small :
  text_size == 'Normal' ? size.normal : size.large
var label l1  = na
var label l2  = na
var label l3  = na
if  barstate.islast
    l1 := label.new(bar_index, y=0*width, text=str.tostring(periodBP, '#'), size=label_text_size, style=label.style_label_left, color=color.new(color.white,100), textcolor=color.new(grad1,0)) 
    l2 := label.new(bar_index, y=15*width, text=str.tostring(periodBP + (periodStep*15), '#'), size=label_text_size, style=label.style_label_left, color=color.new(color.white,100), textcolor=color.new(grad16,0))
    l3 := label.new(bar_index, y=31*width, text=str.tostring(periodBP + (periodStep*39), '#'), size=label_text_size, style=label.style_label_left, color=color.new(color.white,100), textcolor=color.new(grad32,0)) 
    label.delete(l1[1])
    label.delete(l2[1])
    label.delete(l3[1])

// EoS made w/ ❤ by @BarefootJoey ✌💗📈