Head-on-Correlation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CubanEmissary


//@version=5 
indicator("HoC", format=format.price, precision=2, overlay=false) 

//In case it times out
rsec = input.bool(title="Tick/Untick Box to Reload Indicator", defval=true)

//Manually configure incoming data source, calculation, and select series output 
src0 = input.source(close, title="Line-In Source")
src1 = input.string(title="Source Calculation", defval="Close", options=["Close", "Line-In", "TSI", "RSI", "MFI", "SMA", "HMA", "H-L", "BBW", "WAD"])
src2 = input.string(title="Line-Out", defval="None", options=["None","1-ShortestTF","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40-LongestTF"])

//Set starting length size and incremental increase
step0 = input.int(2, title="Start Length", minval=2)
step1 = input.int(1, title="Step Size")

//Display Line Levels and customize colors
haw = input.bool(title="Show Line Levels", defval=false)
color1 = input.color(color.new(color.red, 30), title="Color1")
color2 = input.color(color.new(color.orange, 30), title="Color2")
color3 = input.color(color.new(color.yellow, 30), title="Color3")
color4 = input.color(color.new(color.green, 30), title="Color4")
color5 = input.color(color.new(color.blue, 30), title="Color5")
color6 = input.color(color.new(color.purple, 30), title="Color6")
color7 = input.color(color.new(color.fuchsia, 30), title="Color7")
color8 = input.color(color.new(color.white, 30), title="Color8")

//Switch assignment for Calculation dropdown
float org = switch src1
    "Close" => close
    "Line-In" => src0
    "TSI" => ta.tsi(src0, step0, step1)
    "RSI" => ta.rsi(src0, step0)
    "MFI" => ta.mfi(src0, step0)
    "SMA" => ta.sma(src0, step0)
    "HMA" => ta.hma(src0, step0)
    "H-L" => (ta.barssince(ta.highest(src0,step0)))-(ta.barssince(ta.lowest(src0,step0)))
    "BBW" => ta.bbw(src0, step0, step1) 
    "WAD" => ta.wad
    => 
        float(na)

//Simple reference function and assignment calls left for you to customize      
somefuncybusiness(_org, _step0, _step1) => 

    ta.sma(_org, (_step0+_step1))

ss1 = somefuncybusiness(org, step0, 0*step1)
ss2 = somefuncybusiness(org, step0, 1*step1)
ss3 = somefuncybusiness(org, step0, 2*step1)
ss4 = somefuncybusiness(org, step0, 3*step1)
ss5 = somefuncybusiness(org, step0, 4*step1)
ss6 = somefuncybusiness(org, step0, 5*step1)
ss7 = somefuncybusiness(org, step0, 6*step1)
ss8 = somefuncybusiness(org, step0, 7*step1)
ss9 = somefuncybusiness(org, step0, 8*step1)
ss10 = somefuncybusiness(org, step0, 9*step1)
ss11 = somefuncybusiness(org, step0, 10*step1)
ss12 = somefuncybusiness(org, step0, 11*step1)
ss13 = somefuncybusiness(org, step0, 12*step1)
ss14 = somefuncybusiness(org, step0, 13*step1)
ss15 = somefuncybusiness(org, step0, 14*step1)
ss16 = somefuncybusiness(org, step0, 15*step1)
ss17 = somefuncybusiness(org, step0, 16*step1)
ss18 = somefuncybusiness(org, step0, 17*step1)
ss19 = somefuncybusiness(org, step0, 18*step1)
ss20 = somefuncybusiness(org, step0, 19*step1)
ss21 = somefuncybusiness(org, step0, 20*step1)
ss22 = somefuncybusiness(org, step0, 21*step1)
ss23 = somefuncybusiness(org, step0, 22*step1)
ss24 = somefuncybusiness(org, step0, 23*step1)
ss25 = somefuncybusiness(org, step0, 24*step1)
ss26 = somefuncybusiness(org, step0, 25*step1)
ss27 = somefuncybusiness(org, step0, 26*step1)
ss28 = somefuncybusiness(org, step0, 27*step1)
ss29 = somefuncybusiness(org, step0, 28*step1)
ss30 = somefuncybusiness(org, step0, 29*step1)
ss31 = somefuncybusiness(org, step0, 30*step1)
ss32 = somefuncybusiness(org, step0, 31*step1)
ss33 = somefuncybusiness(org, step0, 32*step1)
ss34 = somefuncybusiness(org, step0, 33*step1)
ss35 = somefuncybusiness(org, step0, 34*step1)
ss36 = somefuncybusiness(org, step0, 35*step1)
ss37 = somefuncybusiness(org, step0, 36*step1)
ss38 = somefuncybusiness(org, step0, 37*step1)
ss39 = somefuncybusiness(org, step0, 38*step1)
ss40 = somefuncybusiness(org, step0, 39*step1)

//Horizontal line displays for chart anchoring and value approximation
h2 = hline((haw == false) ? na : 75, title="Higher", color=color.new(color.silver, 95)) 
h3 = hline((haw == false) ? na : 50, title="Middle", color=color.new(color.silver, 95))
h4 = hline((haw == false) ? na : 25, title="Lower", color=color.new(color.silver, 95))

//Line displays with deletes on every new bar

l40 = line.new(bar_index-39, ss40[0], bar_index-40, ss40[0], color=color8, width=4)
l39 = line.new(bar_index-38, ss39[0], bar_index-39, ss39[0], color=color8, width=4)
l38 = line.new(bar_index-37, ss38[0], bar_index-38, ss38[0], color=color8, width=4)
l37 = line.new(bar_index-36, ss37[0], bar_index-37, ss37[0], color=color8, width=4)
l36 = line.new(bar_index-35, ss36[0], bar_index-36, ss36[0], color=color8, width=4)
line.delete(l40[1])
line.delete(l39[1])
line.delete(l38[1])
line.delete(l37[1])
line.delete(l36[1])

l35 = line.new(bar_index-34, ss35[0], bar_index-35, ss35[0], color=color7, width=4)
l34 = line.new(bar_index-33, ss34[0], bar_index-34, ss34[0], color=color7, width=4)
l33 = line.new(bar_index-32, ss33[0], bar_index-33, ss33[0], color=color7, width=4)
l32 = line.new(bar_index-31, ss32[0], bar_index-32, ss32[0], color=color7, width=4)
l31 = line.new(bar_index-30, ss31[0], bar_index-31, ss31[0], color=color7, width=4)
line.delete(l35[1])
line.delete(l34[1])
line.delete(l33[1])
line.delete(l32[1])
line.delete(l31[1])

l30 = line.new(bar_index-29, ss30[0], bar_index-30, ss30[0], color=color6, width=4)
l29 = line.new(bar_index-28, ss29[0], bar_index-29, ss29[0], color=color6, width=4)
l28 = line.new(bar_index-27, ss28[0], bar_index-28, ss28[0], color=color6, width=4)
l27 = line.new(bar_index-26, ss27[0], bar_index-27, ss27[0], color=color6, width=4)
l26 = line.new(bar_index-25, ss26[0], bar_index-26, ss26[0], color=color6, width=4)
line.delete(l30[1])
line.delete(l29[1])
line.delete(l28[1])
line.delete(l27[1])
line.delete(l26[1])

l25 = line.new(bar_index-24, ss25[0], bar_index-25, ss25[0], color=color5, width=4)
l24 = line.new(bar_index-23, ss24[0], bar_index-24, ss24[0], color=color5, width=4)
l23 = line.new(bar_index-22, ss23[0], bar_index-23, ss23[0], color=color5, width=4)
l22 = line.new(bar_index-21, ss22[0], bar_index-22, ss22[0], color=color5, width=4)
l21 = line.new(bar_index-20, ss21[0], bar_index-21, ss21[0], color=color5, width=4)
line.delete(l25[1])
line.delete(l24[1])
line.delete(l23[1])
line.delete(l22[1])
line.delete(l21[1])

l20 = line.new(bar_index-19, ss20[0], bar_index-20, ss20[0], color=color4, width=4)
l19 = line.new(bar_index-18, ss19[0], bar_index-19, ss19[0], color=color4, width=4)
l18 = line.new(bar_index-17, ss18[0], bar_index-18, ss18[0], color=color4, width=4)
l17 = line.new(bar_index-16, ss17[0], bar_index-17, ss17[0], color=color4, width=4)
l16 = line.new(bar_index-15, ss16[0], bar_index-16, ss16[0], color=color4, width=4)
line.delete(l20[1])
line.delete(l19[1])
line.delete(l18[1])
line.delete(l17[1])
line.delete(l16[1])

l15 = line.new(bar_index-14, ss15[0], bar_index-15, ss15[0], color=color3, width=4)
l14 = line.new(bar_index-13, ss14[0], bar_index-14, ss14[0], color=color3, width=4)
l13 = line.new(bar_index-12, ss13[0], bar_index-13, ss13[0], color=color3, width=4)
l12 = line.new(bar_index-11, ss12[0], bar_index-12, ss12[0], color=color3, width=4)
l11 = line.new(bar_index-10, ss11[0], bar_index-11, ss11[0], color=color3, width=4)
line.delete(l15[1])
line.delete(l14[1])
line.delete(l13[1])
line.delete(l12[1])
line.delete(l11[1])

l10 = line.new(bar_index-9, ss10[0], bar_index-10, ss10[0], color=color2, width=4)
l9 = line.new(bar_index-8, ss9[0], bar_index-9, ss9[0], color=color2, width=4)
l8 = line.new(bar_index-7, ss8[0], bar_index-8, ss8[0], color=color2, width=4)
l7 = line.new(bar_index-6, ss7[0], bar_index-7, ss7[0], color=color2, width=4)
l6 = line.new(bar_index-5, ss6[0], bar_index-6, ss6[0], color=color2, width=4)
line.delete(l10[1])
line.delete(l9[1])
line.delete(l8[1])
line.delete(l7[1])
line.delete(l6[1])

l5 = line.new(bar_index-4, ss5[0], bar_index-5, ss5[0], color=color1, width=4)
l4 = line.new(bar_index-3, ss4[0], bar_index-4, ss4[0], color=color1, width=4)
l3 = line.new(bar_index-2, ss3[0], bar_index-3, ss3[0], color=color1, width=4)
l2 = line.new(bar_index-1, ss2[0], bar_index-2, ss2[0], color=color1, width=4)
l1 = line.new(bar_index, ss1[0], bar_index-1, ss1[0], color=color1, width=4)
line.delete(l5[1])
line.delete(l4[1])
line.delete(l3[1])
line.delete(l2[1])
line.delete(l1[1]) 


//Line-Out switch and plot; display suppressed
float lineout = switch src2
    "1-ShortestTF" => ss1
    "2" => ss2
    "3" => ss3
    "4" => ss4
    "5" => ss5
    "6" => ss6
    "7" => ss7
    "8" => ss8
    "9" => ss9
    "10" => ss10
    "11" => ss11
    "12" => ss12
    "13" => ss13
    "14" => ss14
    "15" => ss15
    "16" => ss16
    "17" => ss17
    "18" => ss18
    "19" => ss19
    "20" => ss20
    "21" => ss21
    "22" => ss22
    "23" => ss23
    "24" => ss24
    "25" => ss25
    "26" => ss26
    "27" => ss27
    "28" => ss28
    "29" => ss29
    "30" => ss30
    "31" => ss31
    "32" => ss32
    "33" => ss33
    "34" => ss34
    "35" => ss35
    "36" => ss36
    "37" => ss37
    "38" => ss38
    "39" => ss39
    "40-LongestTF" => ss40
    => 
        float(na)
p1 = plot(lineout, "Line-Out", color=color.new(color.black, 100), display=display.none)