Highest/Lowest value since X time ago, various indicators
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dean_Crypto
//@version=5
// User input
indicator(title="v2 Highest/Lowest value since X time ago", shorttitle = "H/L-V X-Ago", overlay=false, max_labels_count = 500)


grp1 = "Sources | Length, (length only applies to (MA)"
grp2 = "Display Filter"
il1  = " 1"
il2  = " 2"
il3  = " 3"
il4  = " 4"
il5  = " 5"
il6  = " 6"
il7  = " 7"
il8  = " 8"
il9  = " 9"
il10 = "10"

maxbb = input.int( title = "count bars back limit", defval = 5000, minval = 1, maxval = 100000, tooltip = "also affects how long ago since 'x' occurrence happened. 24 bars on 1hr chart at most can show max 1D ago")
indicatorNameDistance = input.int(defval = 2, minval = 1, title = "indicator name distance")
labelFilterCount = input.int(defval = 1, minval = 0,  title = "label higher than:", tooltip = "only show labels higher than x bars ago", group = grp2, inline = il1)
labelFilterTFOpt = input.string(defval = "hr",     title = "", options =  ["sec","min","hr","day","week","month","year"], group = grp2, inline = il1)

// displays the amount of sources you'd like to use. Note adjusting this will also align the hlines correctly
sourceChoices = input.int(defval = 5, minval = 1, maxval = 10,   title = "dispaly sources", tooltip = "Adjusting this also corrects the hline display issue. Resize the indicator so the sources are aligned with the table")

hlSourceOpt1 = input.string(defval = "high",        title = "source 1", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il1)
hlSourceOpt2 = input.string(defval = "low",         title = "source 2", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il2)
hlSourceOpt3 = input.string(defval = "close",       title = "source 3", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il3)
hlSourceOpt4 = input.string(defval = "volume",      title = "source 4", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il4)
hlSourceOpt5 = input.string(defval = "volume (MA)", title = "source 5", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il5)
hlSourceOpt6 = input.string(defval = "range (MA)",  title = "source 6", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il6)
hlSourceOpt7 = input.string(defval =  "eom",        title = "source 7", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il7)
hlSourceOpt8 = input.string(defval =  "mfi (MA)",   title = "source 8", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il8)
hlSourceOpt9 = input.string(defval =  "obv",        title = "source 9", options =  ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il9)
hlSourceOpt10 = input.string(defval = "rsi (MA)",   title = "source 10", options = ["atr (MA)", "cci (MA)", "cog (MA)", "close", "close percent", "dollar value", "eom", "gaps", "high", "low", "mfi (MA)", "obv", "open", "range (MA)", "rsi (MA)", "rvi (MA)", "timeClose (MA)", "volume", "volume (MA)"], group = grp1, inline = il10)

// color red for the lowest value since x time ago
// color green for the highest value since x time ago
colorLowOpt = input.color(defval = color.rgb(255, 150, 150), title = "lowest since x time ago color")
colorHighOpt =input.color(defval =  color.rgb(150, 255, 150), title = "highest since x time ago color")


// len only applies if a source with (MA) has been selected
sDataLen1 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il1)
sDataLen2 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il2)
sDataLen3 = input.int(defval  = 10 , minval = 1, title = "",  group = grp1, inline = il3)
sDataLen4 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il4)
sDataLen5 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il5)
sDataLen6 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il6)
sDataLen7 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il7)
sDataLen8 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il8)
sDataLen9 = input.int(defval  = 20 , minval = 1, title = "",  group = grp1, inline = il9)
sDataLen10 = input.int(defval = 20,  minval = 1, title = "",  group = grp1, inline = il10)

textSizeOption = input.string(title = "text size", defval = "large", options = ["auto", "huge",  "large", "normal",  "small",  "tiny"])

aget(id, i)     => array.get(id, i)
aget0(id)       => array.get(id, 0)
aset(id, i, v)  => array.set(id, i, v)
asize(id)       => array.size(id)
apush(id, v)    => array.push(id, v)
apop(id)        => array.pop(id)
ashift(id)      => array.shift(id)
unshift(id, v)  => array.unshift(id, v)
bi = bar_index
lbi = last_bar_index

// without this line nothing works. Only begin loading data once the bar index reaches the amount of bars you're 
plottable = bi + 1 > lbi - maxbb

//if data exists in array
canGet(id1, id2) => asize(id1) > 0 and asize(id2) > 0


addArrowsOpt = input.bool(defval = true, title = "add highest/lowest arrows")

onOffAlt(onOffOpt, value, valueAlt) => onOffOpt ? value : valueAlt

// retrieve the highest or lowest of the indicator's current value with the most time ago
labelHLCondition(sourceL, sourceH) =>
    if canGet(sourceL, sourceH)
        aget0(sourceL) > aget0(sourceH) ? aget0(sourceL) : aget0(sourceH)

// color red for the lowest value since, color green for the highest value since
labelColorCondition(sourceL, sourceH) =>
    if canGet(sourceL, sourceH)
        aget0(sourceL) > aget0(sourceH) ? color.rgb(255, 150, 150) : color.rgb(150, 255, 150)

// function for text sizes
size(sizeName) =>
    switch sizeName
        "auto" => size.auto
        "huge" => size.huge
        "large" => size.large
        "normal" => size.normal
        "small" => size.small
        "tiny" => size.tiny


// user defined type to store the time values as it is repeated in a few functions
type timeKey
    int s
    int m
    int h
    int d
    int w
    int M
    int Y

timeKey timeTrack = timeKey.new(1, 60, 3600, 86400, 604800, 2628000, 31540000)

// used to set the time type when using the filter option
labelFilterTF = switch labelFilterTFOpt
    "sec"   => timeTrack.s
    "min"   => timeTrack.m
    "hr"    => timeTrack.h
    "day"   => timeTrack.d
    "week"  => timeTrack.w
    "month" => timeTrack.M
    "year"  => timeTrack.Y


//returns a string of the time taken since the last time the occurence happened



barTF(barsBack) => 

    string forammtted = na
    
    // convertToSec 
    min = timeTrack.m
    hr  = timeTrack.h
    d   = timeTrack.d
    w   = timeTrack.w
    m   = timeTrack.M
    y   = timeTrack.Y

    totalSeconds = timeframe.in_seconds() * barsBack

    string timeAgoText = na

    if totalSeconds > 0 and totalSeconds < min
        timeAgoText      := str.format("{0, number, #s}", totalSeconds)

    else if totalSeconds >= min and totalSeconds < hr
        totalSeconds     := math.round(totalSeconds, 1) / min
        timeAgoText      := str.format("{0, number, #.#m}", totalSeconds)

    else if totalSeconds >= hr and totalSeconds < d
        totalSeconds     := math.round(totalSeconds, 1) / hr
        timeAgoText      := str.format("{0, number, #.#h}", totalSeconds)

    else if totalSeconds >= d and totalSeconds < w
        totalSeconds     := math.round(totalSeconds, 1) / d
        timeAgoText      := str.format("{0, number, #.#d}", totalSeconds)

    else if totalSeconds >= w and totalSeconds < m
        totalSeconds     := math.round(totalSeconds, 1) / w
        timeAgoText      := str.format("{0, number, #.#w}", totalSeconds)

    else if totalSeconds >= m and totalSeconds < y
        totalSeconds     := math.round(totalSeconds, 1) / m
        timeAgoText      := str.format("{0, number, #.#M}", totalSeconds)
    
    else if totalSeconds >= y
        totalSeconds     := math.round(totalSeconds, 1) / y
        timeAgoText      := str.format("{0, number, #.#Y}", totalSeconds)

    timeAgoText

// display filter that will return true if the amount of bars is higher than the user input of time
displayFilter(barsBack) => (timeframe.in_seconds() * barsBack) > (labelFilterCount * labelFilterTF)

labelTextSize = size(textSizeOption)

// begin adding bars once the bar index reaches the first bar of maxbb (max bars back)
addData(id, source) =>
    if plottable
        if asize(id) > maxbb
            ashift(id)

        unshift(id, source)
    
// takes in an array and stores the count number of bars back until it reaches a higher value from the current value of the indicator
addHighCount(data, highs) =>
    if plottable
        sSize = asize(data)
        cbar = aget(data, 0)
        count = 1
        hCount = 0

        if sSize > 1
            while cbar > aget(data, count) and count < sSize - 1
                count := count + 1
                hCount := hCount + 1
                if count > sSize or cbar < aget(data, count)
                    break
        
        if asize(highs) > maxbb
            ashift(highs)
        unshift(highs, hCount)

// takes in an array and stores the count number of bars back until it reaches a lower value from the current value of the indicator
addLowCount(data, lows) =>
    if plottable
        sSize = asize(data)
        cbar = aget(data, 0)
        count = 1
        lCount = 0

        if sSize > 1
            while cbar < aget(data, count) and count < sSize - 1
                count := count + 1
                lCount := lCount + 1
                if count > sSize or cbar > aget(data, count)
                    break
        
        if asize(lows) > maxbb
            ashift(lows)
        unshift(lows, lCount)


tablePosition(OptionsName) =>
    switch OptionsName
        "middle right" => position.middle_right
        "top left" => position.top_left
        "top center" => position.top_center
        "top right" => position.top_right
        "middle left" => position.middle_left
        "middle center" => position.middle_center
        "middle right" => position.middle_right
        "bottom left" => position.bottom_left
        "bottom center" => position.bottom_center
        "bottom right" => position.bottom_right


closePercentChange() => (nz(close[1], low) < close ? 1 : -1) * (high - low) / low * 100

closePercent = closePercentChange()



var float [] sourceData1 = array.new_float()
var float [] sourceData2 = array.new_float()
var float [] sourceData3 = array.new_float()
var float [] sourceData4 = array.new_float()
var float [] sourceData5 = array.new_float()
var float [] sourceData6 = array.new_float()
var float [] sourceData7 = array.new_float()
var float [] sourceData8 = array.new_float()
var float [] sourceData9 = array.new_float()
var float [] sourceData10 = array.new_float()

var float [] sourceLow1 = array.new_float()
var float [] sourceHigh1 = array.new_float()
var float [] sourceLow2 = array.new_float()
var float [] sourceHigh2 = array.new_float()
var float [] sourceLow3 = array.new_float()
var float [] sourceHigh3 = array.new_float()
var float [] sourceLow4 = array.new_float()
var float [] sourceHigh4 = array.new_float()
var float [] sourceLow5 = array.new_float()
var float [] sourceHigh5 = array.new_float()
var float [] sourceLow6 = array.new_float()
var float [] sourceHigh6 = array.new_float()
var float [] sourceLow7 = array.new_float()
var float [] sourceHigh7 = array.new_float()
var float [] sourceLow8 = array.new_float()
var float [] sourceHigh8 = array.new_float()
var float [] sourceLow9 = array.new_float()
var float [] sourceHigh9 = array.new_float()
var float [] sourceLow10 = array.new_float()
var float [] sourceHigh10 = array.new_float()

label [] sourceLBL1 = array.new_label()
label [] sourceLBL2 = array.new_label()
label [] sourceLBL3 = array.new_label()
label [] sourceLBL4 = array.new_label()
label [] sourceLBL5 = array.new_label()
label [] sourceLBL6 = array.new_label()
label [] sourceLBL7 = array.new_label()
label [] sourceLBL8 = array.new_label()
label [] sourceLBL9 = array.new_label()
label [] sourceLBL10 = array.new_label()


// switch function for source choices
sources(optionName, len) =>
    switch optionName
        "atr (MA)"           => ta.atr(len)
        "cci (MA)"           => ta.cci(close, len)
        "close percent"      => closePercent
        "close"              => close
        "cog (MA)"           => ta.cog(close, len)
        "dollar value"       => volume * hlc3
        "eom"                => volume / closePercent
        "gaps"               => open - close[1]
        "high"               => high
        "low"                => low
        "mfi (MA)"           => ta.mfi(hlc3, len)
        "obv"                => ta.obv
        "open"               => open
        "range (MA)"         => math.round(((ta.highest(high, len) - ta.lowest(low, len)) / ta.highest(high, len) * 100), 1)
        "rsi (MA)"           => ta.rsi(close, len)   
        "rvi (MA)"           => ta.ema((close - open) / (high - low) * volume, len)
        "timeClose (MA)"     => ta.sma(time_close, 10)
        "volume (MA)"        => ta.ema(volume, len)
        "volume"             => volume


//assigning each source to a switch option
source1 = sources(hlSourceOpt1, sDataLen1)
source2 = sources(hlSourceOpt2, sDataLen2)
source3 = sources(hlSourceOpt3, sDataLen3)
source4 = sources(hlSourceOpt4, sDataLen4)
source5 = sources(hlSourceOpt5, sDataLen5)
source6 = sources(hlSourceOpt6, sDataLen6)
source7 = sources(hlSourceOpt7, sDataLen7)
source8 = sources(hlSourceOpt8, sDataLen8)
source9 = sources(hlSourceOpt9, sDataLen9)
source10 = sources(hlSourceOpt10, sDataLen10)

//store each bar in sourceData from the selected source
addData(sourceData1, source1)
addData(sourceData2, source2)
addData(sourceData3, source3)
addData(sourceData4, source4)
addData(sourceData5, source5)
addData(sourceData6, source6)
addData(sourceData7, source7)
addData(sourceData8, source8)
addData(sourceData9, source9)
addData(sourceData10, source10)

// add the lowest value since bar count and the same for highest value since bar count
addLowCount( sourceData1  ,   sourceLow1 )
addHighCount(sourceData1  ,  sourceHigh1 )
addLowCount( sourceData2  ,   sourceLow2 )
addHighCount(sourceData2  ,  sourceHigh2 )
addLowCount( sourceData3  ,   sourceLow3 )
addHighCount(sourceData3  ,  sourceHigh3 )
addLowCount( sourceData4  ,   sourceLow4 )
addHighCount(sourceData4  ,  sourceHigh4 )
addLowCount( sourceData5  ,   sourceLow5 )
addHighCount(sourceData5  ,  sourceHigh5 )
addLowCount( sourceData6  ,   sourceLow6 )
addHighCount(sourceData6  ,  sourceHigh6 )
addLowCount( sourceData7  ,   sourceLow7 )
addHighCount(sourceData7  ,  sourceHigh7 )
addLowCount( sourceData8  ,   sourceLow8 )
addHighCount(sourceData8  ,  sourceHigh8 )
addLowCount( sourceData9  ,   sourceLow9 )
addHighCount(sourceData9  ,  sourceHigh9 )
addLowCount( sourceData10 ,  sourceLow10 )
addHighCount(sourceData10 , sourceHigh10 )


line1 =  9.5
line2 =  8.5
line3 =  7.5
line4 =  6.5
line5 =  5.5
line6 =  4.5
line7 =  3.5
line8 =  2.5
line9 =  1.5
line10 = 0.5



lineWidth = 10

// fill array with names of the source choices
string [] sourceChoiceNames = array.from(hlSourceOpt1, hlSourceOpt2, hlSourceOpt3, hlSourceOpt4, hlSourceOpt5, hlSourceOpt6, hlSourceOpt7, hlSourceOpt8, hlSourceOpt9, hlSourceOpt10)

//outline color for labels
outlineColor = color.rgb(0, 0, 0)

sourceLCC1          = labelColorCondition(sourceLow1, sourceHigh1) 
sourceLCC2          = labelColorCondition(sourceLow2, sourceHigh2) 
sourceLCC3          = labelColorCondition(sourceLow3, sourceHigh3) 
sourceLCC4          = labelColorCondition(sourceLow4, sourceHigh4) 
sourceLCC5          = labelColorCondition(sourceLow5, sourceHigh5) 
sourceLCC6          = labelColorCondition(sourceLow6, sourceHigh6) 
sourceLCC7          = labelColorCondition(sourceLow7, sourceHigh7) 
sourceLCC8          = labelColorCondition(sourceLow8, sourceHigh8) 
sourceLCC9          = labelColorCondition(sourceLow9, sourceHigh9) 
sourceLCC10         = labelColorCondition(sourceLow10, sourceHigh10)

sourceLC1           = labelHLCondition(sourceLow1, sourceHigh1) 
sourceLC2           = labelHLCondition(sourceLow2, sourceHigh2) 
sourceLC3           = labelHLCondition(sourceLow3, sourceHigh3) 
sourceLC4           = labelHLCondition(sourceLow4, sourceHigh4) 
sourceLC5           = labelHLCondition(sourceLow5, sourceHigh5) 
sourceLC6           = labelHLCondition(sourceLow6, sourceHigh6) 
sourceLC7           = labelHLCondition(sourceLow7, sourceHigh7) 
sourceLC8           = labelHLCondition(sourceLow8, sourceHigh8) 
sourceLC9           = labelHLCondition(sourceLow9, sourceHigh9) 
sourceLC10          = labelHLCondition(sourceLow10, sourceHigh10) 

tfToStr1  =  barTF(sourceLC1)
tfToStr2  =  barTF(sourceLC2)
tfToStr3  =  barTF(sourceLC3)
tfToStr4  =  barTF(sourceLC4)
tfToStr5  =  barTF(sourceLC5)
tfToStr6  =  barTF(sourceLC6)
tfToStr7  =  barTF(sourceLC7)
tfToStr8  =  barTF(sourceLC8)
tfToStr9  =  barTF(sourceLC9)
tfToStr10 =  barTF(sourceLC10)

// only plot hlines based on amount of source choices selected
hline(10, linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 1  ? 9 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 2  ? 8 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 3  ? 7 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 4  ? 6 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 5  ? 5 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 6  ? 4 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 7  ? 3 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 8  ? 2 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 9  ? 1 : na   , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))
hline(sourceChoices >= 10 ? 0 : na  , linestyle = hline.style_solid, color = color.rgb(120, 123, 134, 50))

color  [] sourceColorAr = array.from(sourceLCC1 ,sourceLCC2 ,sourceLCC3 ,sourceLCC4 ,sourceLCC5 ,sourceLCC6 , sourceLCC7 , sourceLCC8 , sourceLCC9 , sourceLCC10)
label  [] sourceCount = array.new_label(sourceChoices)
label  [] sourceCountLabelNames = array.new_label(sourceChoices)
string [] countedTime = array.from(barTF(sourceLC1), barTF(sourceLC2), barTF(sourceLC3), barTF(sourceLC4), barTF(sourceLC5), barTF(sourceLC6), barTF(sourceLC7), barTF(sourceLC8), barTF(sourceLC9), barTF(sourceLC10))
float  [] sourceLC = array.from(sourceLC1, sourceLC2, sourceLC3, sourceLC4, sourceLC5, sourceLC6, sourceLC7, sourceLC8, sourceLC9, sourceLC10 )

// if value above the display filter, then add label
for i = 0 to sourceChoices - 1
    if  displayFilter(aget(sourceLC, i))
        array.push( sourceCount       , label.new(x = bi, y = 10 - (i + 0.5),   text = aget(countedTime, i)         ,  color = outlineColor, textcolor =  aget(sourceColorAr, i)      , style = label.style_text_outline, size = labelTextSize))

var label [] nameLB1 = array.new_label()
var label [] nameLB2 = array.new_label()
var label [] nameLB3 = array.new_label()
var label [] nameLB4 = array.new_label()
var label [] nameLB5 = array.new_label()
var label [] nameLB6 = array.new_label()
var label [] nameLB7 = array.new_label()
var label [] nameLB8 = array.new_label()
var label [] nameLB9 = array.new_label()
var label [] nameLB10 = array.new_label()

if asize(nameLB1) > 0
    label.delete(ashift(nameLB1))
if asize(nameLB2) > 0
    label.delete(ashift(nameLB2))
if asize(nameLB3) > 0
    label.delete(ashift(nameLB3))
if asize(nameLB4) > 0
    label.delete(ashift(nameLB4))
if asize(nameLB5) > 0
    label.delete(ashift(nameLB5))
if asize(nameLB6) > 0
    label.delete(ashift(nameLB6))
if asize(nameLB7) > 0
    label.delete(ashift(nameLB7))
if asize(nameLB8) > 0
    label.delete(ashift(nameLB8))
if asize(nameLB9) > 0
    label.delete(ashift(nameLB9))
if asize(nameLB10) > 0
    label.delete(ashift(nameLB10))


if sourceChoices >= 1
    array.unshift(nameLB1, label.new(bi + indicatorNameDistance, y = line1, text = hlSourceOpt1, color = outlineColor, textcolor = color.white, style = label.style_text_outline, textalign = text.align_left, size = labelTextSize))

if sourceChoices >= 2
    array.unshift(nameLB2, label.new(bi + indicatorNameDistance, y = line2, text = hlSourceOpt2, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 3
    array.unshift(nameLB3, label.new(bi + indicatorNameDistance, y = line3, text = hlSourceOpt3, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 4
    array.unshift(nameLB4, label.new(bi + indicatorNameDistance, y = line4, text = hlSourceOpt4, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 5
    array.unshift(nameLB5, label.new(bi + indicatorNameDistance, y = line5, text = hlSourceOpt5, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 6
    array.unshift(nameLB6, label.new(bi + indicatorNameDistance, y = line6, text = hlSourceOpt6, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 7
    array.unshift(nameLB7, label.new(bi + indicatorNameDistance, y = line7, text = hlSourceOpt7, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 8
    array.unshift(nameLB8, label.new(bi + indicatorNameDistance, y = line8, text = hlSourceOpt8, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices >= 9
    array.unshift(nameLB9, label.new(bi + indicatorNameDistance, y = line9, text = hlSourceOpt9, color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))

if sourceChoices == 10
    array.unshift(nameLB10, label.new(bi + indicatorNameDistance, y = line10, text = hlSourceOpt10 , color = outlineColor, textcolor = color.white, style = label.style_text_outline, size = labelTextSize))