TPO Letters [Kioseff Trading]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("TPO Letters [Kioseff Trading]", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500, max_bars_back = 500)

lettersOnly                            = input.string(defval = "Letters", title = "Letters | Circles | Boxes", options = ["Letters",  "Circles", "Boxes"])
ti                                     = input.string(defval = "Regular", title = "Use Fixed Range to Calculate?", options = ["Regular", "Fixed Range"])
sess                                   = input.string(defval = "D", title = "Recalculate After How Much Time?", tooltip = "from 1 to 1440 for minutes \nfrom 1D to 365D for days \nfrom 1W to 52W for weeks \nfrom 1M to 12M for months")
auto                                   = input.string(defval = "Custom", options = ["Auto", "Custom"], title = "Auto Calculate Tick Levels? Custom?", inline = "1")
tickzz                                 = input.float(defval = 100 ,title = "Ticks", inline = "1")
textSize                               = input.string(defval = "Small", options = ["Tiny", "Small", "Normal", "Large", "Huge"])
resCount                               = input.string(defval = "No", options = ["Yes", "No"], title = "Reset Characters After Exhuasting Alphabet? (Remove Numbers from TPO Letters)")
fr                                     = input.bool(defval = true, title = "Show Fixed Range Label and Line?")
warn                                   = input.bool(defval = true, title = "Show Warning")
tickLevels                             = input.bool(false, title = "Show Tick Levels?")
showCount                              = input.bool(defval = true, title = "Show Letter Count?")
showCol                                = input.bool(defval = true, title = "Color Letters in Value Area Only?")
ibFill                                 = input.bool(defval = false, title = "Show IB Lines/Fill?")
showIb                                 = input.bool(defval = false, title = "Color Initial Balance Characters?")
chrC                                   = input.bool(defval = true, title = "Show Bottom Right Table?")
st                                     = input.time(defval = timestamp("19 Sep 2022 00:00 +0300"), title = "Fixed Range Start", group = "Fixed Range")
sCol                                   = input.color(title = "Start Color", group = "Gradient Color" ,defval = color.lime, tooltip = "Coloring of TPO Characters Operates on a Gradient. Consequently, the 
                                       Selected Start Color Ascribes the Initial Coloring of Letters. The Selected End Color Dictates the Final Coloring of Characters.")
eCol                                   = input.color(title = "End Color", group = "Gradient Color", defval = color.red)
col                                    = input.color(defval = color.gray, title = "Main Character Color (Gray Default)", group = "Colors")
col1                                   = input.color(defval = color.red  , title = "SP Character Color   (Red Default)")
col2                                   = input.color(defval = color.yellow, title = "POC Character Color (Yellow Default)")
col3                                   = input.color(defval = color.blue, title = "IB Character Color (Blue Default)")
col4                                   = input.color(defval = color.lime, title = "Value Area Color (Lime Default)")
col6                                   = input.color(defval = #a5d6a7, title = "Tick Level Value Area Color")
fnt                                    = input.string(defval = "Default", title = "Font Type", options = ["Default", "Monospace"])

if fr == true and barstate.islast
    line.new(math.round(st), close, math.round(st), close + 0.001, extend = extend.both, color = color.white, width = 4, xloc = xloc.bar_time)
    if ti != "Fixed Range"
        var box frStart = box.new(math.round(st), high + ta.tr, math.round(st), low - ta.tr, 
     bgcolor = color.new(color.white, 100), border_color = na, text_size = size.normal, text_color = color.white, text_wrap = text.wrap_none,  text = "If Selected in Settings, \nFixed Range Begins Here", xloc = xloc.bar_time)


fonT = switch fnt
    
    "Default"   => font.family_default
    "Monospace" => font.family_monospace


finTim = switch ti
    
    "Regular" => timeframe.change(sess)
    "Fixed Range" => time[1] < st and time >= st 
    


sz = switch textSize
    
    "Tiny"             => size.tiny
    "Small"            => size.small
    "Normal"           => size.normal
    "Large"            => size.large
    "Huge"             => size.huge


var int count = 0
var int firs = 0
var int ibB = 0
var float ibTime = 0.0
if session.isfirstbar_regular and count == 0
    count := 1
    firs := bar_index
    ibTime := math.round(timestamp(year, month, dayofmonth, hour + 1, minute, second))
if time == ibTime and count == 1
    ibB := bar_index
if session.isfirstbar_regular and count[1] == 1
    firs := bar_index - firs
    count := 2



var string [] str = switch lettersOnly 
    
    
    "Letters" => array.from(

     "A",
     "B",
     "C",
     "D",
     "E",
     "F",
     "G",
     "H",
     "I",
     "J",
     "K",
     "L",
     "M",
     "N",
     "O",
     "P",
     "Q",
     "R",
     "S",
     "T",
     "U",
     "V",
     "W",
     "X",
     "Y",
     "Z",
     "a",
     "b",
     "c",
     "d",
     "e",
     "f",
     "g",
     "h",
     "i",
     "j",
     "k",
     "l",  
     "m",
     "n",
     "o",
     "p",
     "q",
     "r",
     "s",
     "t",
     "u",
     "v",
     "w",
     "x",
     "y",
     "z"
    
     )

    "Boxes"   => array.from("■")
    "Circles" => array.from("◉")
     

if barstate.isfirst
    if lettersOnly == "Boxes"
        for i = 0 to 50
            array.push(str, "■")
    if lettersOnly == "Circles"
        for i = 0 to 50
            array.push(str, "◉")

    sX = ""
    
    
    if resCount == "No"
        for i = 0 to 51
            sX := array.get(str, i) +  "1 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "2 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "3 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "4 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "5 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "6 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "7 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "8 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "9 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "10 "             // => Loops are run sequentially, not simultaneously, so string characters populate in order
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "11 " 
            array.push(str, sX)
        for i = 0 to 51
            sX := array.get(str, i) +  "12 " 
            array.push(str, sX)
    
    
    else
    
        for i = 0 to array.size(str) - 1
            array.push(str, array.get(str, i))
        for i = 0 to array.size(str) - 1
            array.push(str, array.get(str, i))
        for i = 0 to array.size(str) - 1
            array.push(str, array.get(str, i))
        for i = 0 to array.size(str) - 1
            array.push(str, array.get(str, i))
        
        
import kaigouthro/hsvColor/1 as col // Great Library -> Check it out (CTRL + CLICK)

var color [] tpoLetCol = array.new_color()

if ti == "Regular"
    if count[1] == 1 and count == 2
        for i = 0 to firs
            array.push(tpoLetCol,  col.hsv_gradient(i , 0 , firs , eCol, sCol))




cond(y, x) =>
    
    str.contains(label.get_text (array.get(y, x)),"1" ) or
     str.contains(label.get_text(array.get(y, x)),"2" ) or
     str.contains(label.get_text(array.get(y, x)),"3" ) or
     str.contains(label.get_text(array.get(y, x)),"4" ) or
     str.contains(label.get_text(array.get(y, x)),"5" ) or
     str.contains(label.get_text(array.get(y, x)),"6" ) or
     str.contains(label.get_text(array.get(y, x)),"7" ) or
     str.contains(label.get_text(array.get(y, x)),"8" ) or
     str.contains(label.get_text(array.get(y, x)),"9" ) or
     str.contains(label.get_text(array.get(y, x)),"9" ) 
    
cond2(y, x) =>
     str.contains(label.get_text(array.get(y, x)),"10" ) or
     str.contains(label.get_text(array.get(y, x)),"11" ) or 
     str.contains(label.get_text(array.get(y, x)),"12" ) or 
     str.contains(label.get_text(array.get(y, x)),"13" ) or 
     str.contains(label.get_text(array.get(y, x)),"14" ) or 
     str.contains(label.get_text(array.get(y, x)),"15" ) or 
     str.contains(label.get_text(array.get(y, x)),"16" ) or 
     str.contains(label.get_text(array.get(y, x)),"17" ) or
     str.contains(label.get_text(array.get(y, x)),"18" ) or
     str.contains(label.get_text(array.get(y, x)),"19" ) or
     str.contains(label.get_text(array.get(y, x)),"20" ) or
     str.contains(label.get_text(array.get(y, x)),"21" ) or
     str.contains(label.get_text(array.get(y, x)),"22" ) or
     str.contains(label.get_text(array.get(y, x)),"23" ) or
     str.contains(label.get_text(array.get(y, x)),"24" ) or 
     str.contains(label.get_text(array.get(y, x)),"25" ) or 
     str.contains(label.get_text(array.get(y, x)),"26" ) or 
     str.contains(label.get_text(array.get(y, x)),"27" ) or 
     str.contains(label.get_text(array.get(y, x)),"28" ) or 
     str.contains(label.get_text(array.get(y, x)),"29" ) or 
     str.contains(label.get_text(array.get(y, x)),"30" ) or 
     str.contains(label.get_text(array.get(y, x)),"31" ) or 
     str.contains(label.get_text(array.get(y, x)),"32" ) or 
     str.contains(label.get_text(array.get(y, x)),"33" ) or
     str.contains(label.get_text(array.get(y, x)),"34" ) or 
    
     str.contains(label.get_text(array.get(y, x)),"35" ) or 
     str.contains(label.get_text(array.get(y, x)),"36" ) or 
     str.contains(label.get_text(array.get(y, x)),"37" ) or 
     str.contains(label.get_text(array.get(y, x)),"38" ) or 
     str.contains(label.get_text(array.get(y, x)),"39" ) or 
     str.contains(label.get_text(array.get(y, x)),"40" ) or 
     str.contains(label.get_text(array.get(y, x)),"41" ) or 
     str.contains(label.get_text(array.get(y, x)),"42" ) or 
     str.contains(label.get_text(array.get(y, x)),"43" ) or 
     str.contains(label.get_text(array.get(y, x)),"44" ) or 
     str.contains(label.get_text(array.get(y, x)),"45" ) or 
     str.contains(label.get_text(array.get(y, x)),"46" ) or 
     str.contains(label.get_text(array.get(y, x)),"47" ) or 
     str.contains(label.get_text(array.get(y, x)),"48" ) or 
     str.contains(label.get_text(array.get(y, x)),"49" ) or 
     str.contains(label.get_text(array.get(y, x)),"50" ) or 
     str.contains(label.get_text(array.get(y, x)),"51" ) or 
     str.contains(label.get_text(array.get(y, x)),"52" ) or 
     str.contains(label.get_text(array.get(y, x)),"53" ) or 
     str.contains(label.get_text(array.get(y, x)),"54" ) or 
     str.contains(label.get_text(array.get(y, x)),"55" ) or 
     str.contains(label.get_text(array.get(y, x)),"56" ) or 
     str.contains(label.get_text(array.get(y, x)),"57" ) or 
     str.contains(label.get_text(array.get(y, x)),"58" ) or 
     str.contains(label.get_text(array.get(y, x)),"59" ) or 
     str.contains(label.get_text(array.get(y, x)),"60" ) or 
     str.contains(label.get_text(array.get(y, x)),"61" ) or 
     str.contains(label.get_text(array.get(y, x)),"62" ) or 
     str.contains(label.get_text(array.get(y, x)),"63" ) or 
     str.contains(label.get_text(array.get(y, x)),"64" ) or 
     str.contains(label.get_text(array.get(y, x)),"65" ) or 
     str.contains(label.get_text(array.get(y, x)),"66" ) 



 


atr                                      = ta.atr(14)
var float tickz                          = 0.0
ticks2 = array.new_float()

if ti == "Regular"
    if last_bar_index - bar_index == 1601
        if syminfo.mintick >= 0.01
            tickz := auto == "Custom" ? tickzz : 
             auto == "Auto" and timeframe.period == "1" ? atr * 50 :
             auto == "Auto" and timeframe.period == "5" ? atr * 40 : 
                                                          atr * 30
            
        else
            tickz := auto == "Custom" ? tickzz : atr * 100000
else
    if time < st
        if syminfo.mintick >= 0.01
            tickz := auto == "Custom" ? tickzz : 
             auto == "Auto" and timeframe.period == "1" ? atr * 50 :
             auto == "Auto" and timeframe.period == "5" ? atr * 40 : 
                                                          atr * 30        
        else
            tickz := auto == "Custom" ? tickzz : atr * 100000        


var line [] tpoLines                = array.new_line()
ticks                               = array.new_float()
var float max                       = 0.0
var float min                       = 10000000
var float [] track                  = array.new_float()
var label [] pocL                   = array.new_label()
var float [] finChe                 = array.new_float()

var label [] letters = array.new_label()
var box   [] lettersBox = array.new_box()
index = array.new_int()


var int timRound = 0
var int finB = 0
var float l = 0.0
if session.isfirstbar_regular
    finB := bar_index + ibB
    l := low
if session.isfirstbar_regular[4] and timRound == 0
    timRound := math.round(time - time[4])

timeCond = switch ti
    
    "Regular"     => last_bar_index - bar_index <= 1600
    "Fixed Range" => time >= st



var line [] ib                      = array.new_line()

var label [] SP                     = array.new_label()
var line  [] val                    = array.new_line()
var label [] VA                     = array.new_label()
var int first                       = 0
var int firstBar                    = 0
var linefill fil                    = na
var float ibF                       = 0.0
var label [] tpoCount               = array.new_label()
var line  ibOpen                    = na
var line j                          = na 
var label o                         = na 

if timeCond
    if timeCond[1] == false
        j := line.new(bar_index, high, bar_index, low, color = color.aqua, width = 4, xloc = xloc.bar_index)
        o := label.new(bar_index, open, xloc = xloc.bar_index, size = size.large, text_font_family = fonT, color = color.new(color.white, 100), text = "●", style = label.style_label_right, textcolor = color.blue)    

    if firstBar != 0 
        line.set_x1(j, firstBar - 1)
        line.set_x2(j, firstBar - 1)
        line.set_y1(j, max)
        line.set_y2(j, min)    
        



        if time == ibF
            if ibFill == true
                array.push(ib, line.new(first, max, time, max, color = color.new(col3, 50), xloc = xloc.bar_time))
                array.push(ib, line.new(first, min, time, min, color = color.new(col3, 50), xloc = xloc.bar_time))
            
            if array.size(ib) > 1
                
                linefill.new(array.get(ib, 0), array.get(ib, 1), color.new(col3, 95))
        
            ibOpen := line.new(firstBar - 1, max, firstBar - 1, min, color = color.blue, width = 4)
    
    
    max                                 := math.max(high, max)
    min                                 := math.min(low, min)



    if finTim
        
        line.delete(ibOpen)
        
        if array.size(tpoLetCol) == 0
            for i = 0 to 1200
                array.push(tpoLetCol,  col.hsv_gradient(i , 0 , firs , eCol, sCol))


        if array.size(val) > 0 
            for i = 0 to array.size(val) - 1
                line.delete(array.shift(val))
        
        if array.size(VA) > 0
            for i = 0 to array.size(VA) - 1
                label.delete(array.shift(VA))
        
        if array.size(track) > 0
            array.clear(track)
        
        if array.size(finChe) > 0
            array.clear(finChe)
        
        if array.size(ib) > 0
            for i = 0 to array.size(ib) - 1
                line.delete(array.shift(ib))
        
        if array.size(tpoLines) > 0 
            for i = 0 to array.size(tpoLines) - 1
                line.delete(array.shift(tpoLines))

        if array.size(SP) > 0 
            for i = 0 to array.size(SP) - 1
                label.delete(array.shift(SP))
        
        if array.size(pocL) > 0 
            for i = 0 to array.size(pocL) - 1
                label.delete(array.shift(pocL))
        
        if array.size(lettersBox) > 0 
            for i = 0 to array.size(lettersBox) - 1
                box.delete(array.shift(lettersBox))
        
        max := high
        min := low
        
        first := math.round(time)
        ibF   := math.round(timestamp(year, month, dayofmonth, hour + 1, minute, second))


        label.set_x(o, bar_index)
        label.set_y(o, open)
        firstBar := bar_index
        array.push(ticks, low)
        array.push(track, low)
        for i = 1 to 500
            if array.get(ticks, i - 1) + (tickz * syminfo.mintick) <= high
                array.push(ticks, array.get(ticks, i - 1) + (tickz * syminfo.mintick))
            else
                break

        
        for i = 0 to array.size(ticks) - 1
            array.push(tpoLines, line.new(bar_index, array.get(ticks, i) , 
                                          bar_index + 1,  array.get(ticks, i),
                                          color = tickLevels == true ? color.new(color.lime, 75) : na, xloc = xloc.bar_index))


        
        
        
if barstate.islast

    if array.size(VA) > 0
        for i = 0 to array.size(VA) - 1
            label.delete(array.shift(VA))

    
    if array.size(val) > 0 
        for i = 0 to array.size(val) - 1
            line.delete(array.shift(val))
        
    
    if array.size(tpoLines) > 0 
        for i = 0 to array.size(tpoLines) - 1
            line.delete(array.shift(tpoLines))
    
    if array.size(SP) > 0 
        for i = 0 to array.size(SP) - 1
            label.delete(array.shift(SP))
    
    if array.size(pocL) > 0 
        for i = 0 to array.size(pocL) - 1
            label.delete(array.shift(pocL))
    
    if array.size(finChe) > 0
        array.clear(finChe)

    if array.size(letters) > 0 
        for i = 0 to array.size(letters) - 1
            label.delete(array.shift(letters))
    if array.size(lettersBox) > 0 
        for i = 0 to array.size(lettersBox) - 1
            box.delete(array.shift(lettersBox))
    if array.size(tpoCount) > 0 
        for i = 0 to array.size(tpoCount) - 1
            label.delete(array.shift(tpoCount))
            
    array.push(ticks, array.get(track, array.size(track) - 1))
    for i = 1 to 500
        if array.get(ticks, i - 1) + (tickz * syminfo.mintick) <= max
            array.push(ticks, array.get(ticks, i - 1) + (tickz * syminfo.mintick))
        else
            break
    array.push(ticks2, array.get(track, array.size(track) - 1))
    for i = 1 to 500 
        if array.get(ticks2, i - 1) - (tickz * syminfo.mintick) >= min
            array.push(ticks2, array.get(ticks2, i - 1) - (tickz * syminfo.mintick))
        else 
            break
    for i = array.size(ticks2) - 1 to 0
        
        array.push(tpoLines, line.new( first, array.get(ticks2, i), 
                                             
                                             last_bar_time,  
                                             array.get(ticks2, i), 
                                             color = tickLevels == true ? color.new(color.lime, 75) : na, 
                                             xloc = xloc.bar_time
                                             
                                             ))
        
    for i = 1 to array.size(ticks) - 1
        array.push(tpoLines, line.new( first, array.get(ticks, i), 
                                             
                                             last_bar_time,  
                                             array.get(ticks, i), 
                                             color = tickLevels == true ? color.new(color.lime, 75) : na, 
                                             xloc = xloc.bar_time
                                             
                                             ))
        
    
    if array.size(tpoLines) > 1 and bar_index - firstBar < array.size(str)
        
        levels = array.new_float()
        levels2 = array.new_float()
        che = array.new_float(array.size(tpoLines), 0)

        for i = bar_index - firstBar to 0
            grad = col.hsv_gradient(bar_index[i], firstBar, last_bar_index, sCol, eCol)
            for x = 0 to array.size(tpoLines) - 1
                if line.get_y1(array.get(tpoLines, x)) <= high[i] and line.get_y1(array.get(tpoLines, x)) >= low[i] 
                    if array.size(lettersBox) < 500
                        array.push(lettersBox, box.new(bar_index[i], line.get_y1(array.get(tpoLines,x)), bar_index[i], line.get_y1(array.get(tpoLines,x)), 
                         bgcolor = color.new(color.white, 100), border_color = color.new(color.white, 100), text = array.get(str, bar_index - firstBar - i), text_size = sz,
                         text_color = ti == "Regular" ? array.get(tpoLetCol, i) : grad, text_font_family = fonT))
                        array.push(levels, line.get_y1(array.get(tpoLines,x)))
                    
                    else
                    
                        array.push(letters, label.new(bar_index[i], line.get_y1(array.get(tpoLines, x)), 
                         array.get(str, bar_index - firstBar - i), style = label.style_label_left, color = color.new(color.white, 100), size = sz, 
                         textcolor = ti == "Regular" ? array.get(tpoLetCol, i) : grad, text_font_family = fonT))
                        array.push(levels2, line.get_y1(array.get(tpoLines,x)))
                

        for i = 0 to array.size(tpoLines) - 1
            if array.size(lettersBox) > 0 
                for x = 0 to array.size(levels) - 1
                    if line.get_y1(array.get(tpoLines, i)) == array.get(levels, x) 
                        array.set(che, i, array.get(che, i) + 1)
            
            if array.size(letters) > 0
                for x = 0 to array.size(levels2) - 1
                    if line.get_y1(array.get(tpoLines, i)) == array.get(levels2, x) 
                        array.set(che, i, array.get(che, i) + 1)
            
    
        if showCount == true
            if array.size(tpoCount) > 0
                for i = 0 to array.size(tpoCount) - 1
                    label.delete(array.shift(tpoCount))
            for i = 0 to array.size(che) - 1
                array.push(tpoCount, label.new(bar_index + 5, line.get_y1(array.get(tpoLines, i)), 
                 text = str.tostring(array.get(che, i)) + " (" + str.tostring(line.get_y1(array.get(tpoLines, i)), format.mintick) + ")", 
                 color = color.new(color.white, 100), textcolor = color.white, style = label.style_label_left))

        len = 0.0 
        for x = 0 to array.size(che) - 1
            len := math.max(len, array.get(che, x))
        
        lenTrack = 0
        
        for x = 0 to array.size(tpoLines) - 1
            
            if array.get(che, x) == len
                lenTrack := x
                if bar_index - firstBar >= 4
                    
                    line.set_color(array.get(tpoLines, x), color.new(col2, 75))
                    line.set_width(array.get(tpoLines, x), 2)
                    if showCount == true
                        label.set_textcolor(array.get(tpoCount, x), col2)
                    array.push(finChe, line.get_y1(array.get(tpoLines, x)))
                    if array.size(finChe) == 1 
                        array.push(pocL, label.new(first, line.get_y1(array.get(tpoLines, x)), xloc = xloc.bar_time, 
                         color = color.new(col, 100), textcolor = col2, style = label.style_label_right,  text_font_family = fonT, text = "POC", size = sz))
                        
                        
                        break
                    
            
            
        
        sum                                                                     = array.new_float()
        sum1                                                                    = array.new_float()
        lin                                                                     = array.new_float()
        lin1                                                                    = array.new_float()
        cheX                                                                    = array.new_float()
        cheX1                                                                   = array.new_float()
            
        if lenTrack > 0
            for x = lenTrack - 1 to 0
                array.push(sum , array.size(sum) == 0 ? array.get(che, x) : array.get(sum, array.size(sum) - 1) + array.get(che, x))
                array.push(lin, line.get_y1(array.get(tpoLines, x)))
                array.push(cheX, array.get(che, x))
            for x = lenTrack to array.size(che) - 1
                array.push(sum1, array.size(sum1) == 0 ? array.get(che, x) : array.get(sum1, array.size(sum1) - 1) + array.get(che, x))
                array.push(lin1, line.get_y1(array.get(tpoLines, x)))
                array.push(cheX1, array.get(che, x))
                
                
            miN                                                                 = math.min(array.size(sum), array.size(sum1))
            
            
            for n = 0 to miN - 1
                if array.get(sum, n) + array.get(sum1, n) >= array.sum(che) * .7
                    array.push(val,line.new(first , array.get(lin, n), time, 
                     array.get(lin, n), xloc = xloc.bar_time, color = color.new(col4, 75)))
                     
                    array.push(val,line.new(first, array.get(lin1, n), time, 
                     array.get(lin1, n), xloc = xloc.bar_time, color = color.new(col4, 75)))
                    
                    array.push(VA, label.new(first, line.get_y1(array.get(val, 0)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAH" : "VAL", 
                     textcolor = col4, size = sz, color = color.new(color.white, 100), style = label.style_label_right, text_font_family = fonT, xloc = xloc.bar_time))
                    
                    array.push(VA, label.new(first, line.get_y1(array.get(val, 1)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAL" : "VAH", 
                     textcolor = col4, size = sz, color = color.new(color.white, 100), style = label.style_label_right, text_font_family = fonT, xloc = xloc.bar_time))
                                                
                    
                    break
                         
            if array.size(val) < 2
            
                stop = 0
            
                if miN == array.size(sum1)
                
                
                    for n = 0 to array.size(cheX1) - 1
                        if array.get(cheX1, n) >= math.round(len * .7)
                            stop := n
                    for n = 0 to array.size(sum) - 1    
                    
                        if array.get(sum, n) + array.get(sum1, stop) >= array.sum(che) * .7
                            
                            array.push(val,line.new(first, array.get(lin, n), time, 
                             array.get(lin, n), xloc = xloc.bar_time, color = color.new(col4, 75)))
                            
                            array.push(val,line.new(first, array.get(lin1, stop), time, 
                             array.get(lin1, stop), xloc = xloc.bar_time, color = color.new(col4, 75)))
                                    
                            array.push(VA, label.new(first, line.get_y1(array.get(val, 0)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAH" : "VAL", 
                             textcolor = col4, size = sz, color = color.new(color.white, 100),  text_font_family = fonT, style = label.style_label_right, xloc = xloc.bar_time))
                            
                            array.push(VA, label.new(first, line.get_y1(array.get(val, 1)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAL" : "VAH", 
                             textcolor = col4, size = sz, color = color.new(color.white, 100),  text_font_family = fonT, style = label.style_label_right, xloc = xloc.bar_time))
                                                
                                               
                            
                            break
                
                else
                
                    for n = 0 to array.size(cheX) - 1
                        if array.get(cheX, n) >= math.round(len * .7)
                            stop := n
                    for n = 0 to array.size(sum1) - 1    
                    
                        if array.get(sum, stop) + array.get(sum1, n) >= array.sum(che) * .7
                    
                            array.push(val,line.new(first, array.get(lin1, n), time, 
                             array.get(lin1, n), xloc = xloc.bar_time, color = color.new(col4, 75)))
                        
                            array.push(val,line.new(first, array.get(lin, stop), time, 
                             array.get(lin, stop), xloc = xloc.bar_time, color = color.new(col4, 75)))
                                    
                            array.push(VA, label.new(first, line.get_y1(array.get(val, 0)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAH" : "VAL", 
                             textcolor = col4, size = sz, color = color.new(color.white, 100),  text_font_family = fonT, style = label.style_label_right, xloc = xloc.bar_time))
                            
                            array.push(VA, label.new(first, line.get_y1(array.get(val, 1)), text = line.get_y1(array.get(val, 0)) > line.get_y1(array.get(val, 1)) ? "VAL" : "VAH", 
                             textcolor = col4, size = sz, color = color.new(color.white, 100),  text_font_family = fonT, style = label.style_label_right, xloc = xloc.bar_time))
                                                
                                                                
                                    
                            break                 
         
         
        if array.size(val) == 2 and array.size(pocL) > 0 and array.size(tpoLines) > 0 
            fil := linefill.new(array.get(val, 0), array.get(val, 1), color = color.new(col4, 90))
            
            
            if showCol == true
                if array.size(lettersBox) > 0
                    for i = 0 to array.size(lettersBox) - 1
                        if line.get_y1(array.get(val, 0)) > line.get_y2(array.get(val, 1))
                            if box.get_top(array.get(lettersBox, i)) > line.get_y1(array.get(val, 0))
                              or box.get_top(array.get(lettersBox, i)) < line.get_y1(array.get(val, 1))
                                box.set_text_color(array.get(lettersBox, i), col)
    
                
                        else
                        
                            if box.get_top(array.get(lettersBox, i)) < line.get_y1(array.get(val, 0))
                              or box.get_top(array.get(lettersBox, i)) > line.get_y1(array.get(val, 1))
                                box.set_text_color(array.get(lettersBox, i), col)
                
                if array.size(letters) > 0
                    for i = 0 to array.size(letters) - 1
                        if line.get_y1(array.get(val, 0)) > line.get_y2(array.get(val, 1))
                            if label.get_y(array.get(letters, i)) > line.get_y1(array.get(val, 0))
                              or label.get_y(array.get(letters, i)) < line.get_y1(array.get(val, 1))
                                label.set_textcolor(array.get(letters, i), col)
                                
                        else
                            
                            if label.get_y(array.get(letters, i)) < line.get_y1(array.get(val, 0))
                              or label.get_y(array.get(letters, i)) > line.get_y1(array.get(val, 1))
                                label.set_textcolor(array.get(letters, i), col)

                        
                

            if showCount == true 
                for i = 0 to array.size(tpoLines) - 1
                    
                    if array.get(che, i) == 1
                        label.set_textcolor(array.get(tpoCount, i), col1)
                    
                    if line.get_y1(array.get(tpoLines, i)) == label.get_y(array.get(VA, 0))
                      or line.get_y1(array.get(tpoLines, i)) == label.get_y(array.get(VA, 1))
                        label.set_textcolor(array.get(tpoCount, i), col4)
                    
                    if label.get_y(array.get(VA, 0)) > label.get_y(array.get(VA, 1))
                    
                        if line.get_y1(array.get(tpoLines, i)) < label.get_y(array.get(VA, 0))
                          and line.get_y1(array.get(tpoLines, i)) > label.get_y(array.get(VA, 1))
                          and line.get_y1(array.get(tpoLines, i)) != label.get_y(array.get(pocL, 0))
                            label.set_textcolor(array.get(tpoCount, i), col6)
                    
                        if line.get_y1(array.get(tpoLines, i)) > label.get_y(array.get(VA, 0))
                          or line.get_y1(array.get(tpoLines, i)) < label.get_y(array.get(VA, 1))
                            if array.get(che, i) > 1 and line.get_y1(array.get(tpoLines, i)) != 
                              label.get_y(array.get(pocL, 0)) 
                                label.set_textcolor(array.get(tpoCount, i), col)
                    
                    else if label.get_y(array.get(VA, 0)) < label.get_y(array.get(VA, 1))
                    
                        if line.get_y1(array.get(tpoLines, i)) > label.get_y(array.get(VA, 0))
                          and line.get_y1(array.get(tpoLines, i)) < label.get_y(array.get(VA, 1))
                          and line.get_y1(array.get(tpoLines, i)) != label.get_y(array.get(pocL, 0))
                            label.set_textcolor(array.get(tpoCount, i), col6)
                    
                        if line.get_y1(array.get(tpoLines, i)) > label.get_y(array.get(VA, 1))
                          or line.get_y1(array.get(tpoLines, i)) < label.get_y(array.get(VA, 0))
                            if array.get(che, i) > 1 and line.get_y1(array.get(tpoLines, i)) != 
                              label.get_y(array.get(pocL, 0)) 
                                label.set_textcolor(array.get(tpoCount, i), col)
    
                                
            if showIb == true
                if array.size(lettersBox) > 0
                    for i = 0 to array.size(lettersBox) - 1
                        if box.get_left(array.get(lettersBox, i)) < finB
                            box.set_text_color(array.get(lettersBox, i), col3)
                if array.size(letters) > 0
                    for i = 0 to array.size(letters) - 1
                        if label.get_x(array.get(letters, i)) < finB
                            label.set_textcolor(array.get(letters, i), col3)
         
        if array.size(tpoCount) > 0 
            var label lab = na
            label.delete(lab)
            lab := label.new(bar_index + 5, label.get_y(array.get(tpoCount, 0)) - (tickz * syminfo.mintick), text = "Total TPO: " + str.tostring(array.sum(che)), 
             style = label.style_label_left, color = color.new(color.white, 100), textcolor = color.teal)
         
         


        
        if warn == true
            var table tab = table.new(position.top_right, 2, 2, frame_color = color.white, frame_width = 1)
            table.cell(tab, 0, 0, text_size = size.small, 
         text = "If Letters Aren't Appearing: Decrease the 'Ticks' Setting. \nIf Letters are Cluttered: Increase the 'Ticks' Setting\nFor Your Changes to Take Effect: Change the 'Auto Calculate Tick Levels? Custom?' Setting to 'Custom'", 
         text_color = color.white, bgcolor = color.new(col3, 75))
        if chrC == true
            var table tab1 = table.new(position.bottom_right, 2, 2)
            table.cell(tab1, 0, 0, 
             text_color = color.white, text_size = size.small, text = str.tostring(array.size(lettersBox)) + " Boxes Used (500 Max)\nIf The Number of Labels is Greater Than 500 - Early Session TPO Letters Will Delete." )
            table.cell(tab1, 0, 1, 
             text_color = color.white, text_size = size.small, text = str.tostring(array.size(letters) + array.size(tpoCount) + 1) + " Labels Used (500 Max)\nIf The Number of Labels is Greater Than 500 - Later Session TPO Letters Will Delete." )
        
