﻿//@version=6
indicator('<[Дельта объема - Дельта открытого интереса]>','<[DV/DOI]>', precision = 12, format = format.volume, explicit_plot_zorder = true, max_labels_count = 500, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 5000, overlay = true)

data = input.string(defval = 'Vol. Delta', title = 'Данные', options = ['Volume', 'Vol. Delta', 'OI Delta (BINANCE PERP FUTURES ONLY)'])

[m, m1, m2, m3, m4, m5, m6, m7] = request.security_lower_tf(syminfo.tickerid, '1', [close, close[1], volume, high, low, hlc3, open, volume[1]])

Recalculation = input.string(defval = 'No Recalculation', title = 'Без перерасчета? Или пересчитывать?', options = ['No Recalculation', 'Recalculate'], group = 'Перерасчет | Без перерасчета')
sess = input.string(defval = '1', title = 'Если выбран \'Recalculate\' выше: Пересчитывать через какое время?', group = 'Перерасчет | Без перерасчета', tooltip = 'от 1 до 1440 для минут \nот 1D до 365D для дней \nот 1W до 52W для недель \nот 1M до 12M для месяцев')

calcBy = input.string(defval = 'Percentages (Recommended)', title = 'Рассчитывать уровни по тикам или процентам?', options = ['Ticks', 'Percentages (Recommended)'], group = 'Выбор способа построения уровней')
perc = input.float(defval = 0.1, step = 0.1, title = 'Расстояние между уровнями в % (если выбрано)', inline = '1', group = 'Если выбраны "Проценты"') / 100
tickzz = input.float(defval = 12, title = 'Тики (если выбрано)', inline = '1', group = 'Если выбраны "Тики"')

classic = input.bool(defval = true, title = 'Классический вид?', group = 'Классический')
barDiv = input.int(defval = 5, minval = 4, title = 'На сколько строк делить бары?', group = 'Классический')
assume = input.bool(defval = false, title = 'Использовать предположение по объему?', group = 'Предположение по объему', tooltip = 'Когда эта настройка включена, скрипт не будет записывать "0" объема покупок или "0" объема продаж на уровне, где сформировались только зеленые бары или только красные бары. Если уровень был записан только с "Объемом покупок" - скрипт предположит, что 40% объема покупок было объемом продаж. И наоборот для уровня, где был записан только "Объем продаж" - скрипт предположит, что 40% "Объема продаж" было "Объемом покупок". Это предположение может помочь индикатору лучше воспроизводить Volume Delta индикаторы на других платформах. Если вы не хотите использовать это предположение - просто отключите эту настройку')
lastBar = input.bool(defval = false, title = 'Расчет только для последнего бара?', group = 'Расчет только для последнего бара')
hideAll = input.bool(defval = true, title = 'Показывать конечные линии и метки?', group = 'Показывать суммарные значения в конце графика (отключите для лучшего масштабирования цены)')
outline = input.bool(defval = false, title = 'Показывать контур цвета метки?', group = 'Показывать "Неоновый эффект"')
extend = input.bool(defval = false, title = 'Продлевать конечные линии влево?', group = 'Продлевать линии суммарных значений влево через данные цены (отключите для лучшего масштабирования цены)')
eX = input.bool(defval = true, title = 'Продлевать линию максимального дельта/объема ', group = 'Продлевать линию \'POC\' влево через данные цены? (отключите для лучшего масштабирования цены)')
heat = input.bool(defval = true, title = 'Показывать тепловую карту?', group = 'Показывать тепловую карту (отключите для лучшего масштабирования цены)')
heatOnly = input.bool(defval = false, title = 'Только тепловая карта', group = 'Показывать только тепловую карту (отключите для лучшего масштабирования цены)')
textSize = input.string(defval = 'Small', options = ['Tiny', 'Small', 'Normal', 'Large', 'Huge'], group = 'Эстетика (отключите все для лучшего масштабирования цены)')
fnt = input.string(defval = 'Default', title = 'Тип шрифта', options = ['Default', 'Monospace'], group = 'Эстетика (отключите все для лучшего масштабирования цены)')

c = input.color(defval = color.lime, title = 'Общий цвет "Вверх/Покупки"', group = 'Цвет Вверх/Покупки')
c1 = input.color(defval = color.red, title = 'Общий цвет "Вниз/Продажи"', group = 'Цвет Вниз/Продажи')
c2 = input.color(defval = color.rgb(148, 139, 57), title = 'Общий цвет "POC"', group = 'Цвет "POC"')

fonT = switch fnt

    'Default' => font.family_default
    'Monospace' => font.family_monospace

finTim = switch Recalculation
    'No Recalculation' => heatOnly == false ? bar_index == last_bar_index - 500 : array.size(m)[1] == 0 and array.size(m) > 0
    'Recalculate' => timeframe.change(sess) and last_bar_index - bar_index <= 500

sz = switch textSize

    'Tiny' => size.tiny
    'Small' => size.small
    'Normal' => size.normal
    'Large' => size.large
    'Huge' => size.huge

var float tickz = 0.0
ticks2 = array.new_float()

if Recalculation == 'No Recalculation'

    tickz := tickzz
    tickz

ticks = array.new_float()
var float max = 0.0
var float min = 10000000
var array<float> track = array.new_float()
var array<float> heatLevels = array.new_float()

letters = array.new_label()
lettersBox = array.new_label()
lettersBoxClassic = array.new_box()
boxCo = array.new_float()

req() =>
    switch data
        'OI Delta (BINANCE PERP FUTURES ONLY)' => syminfo.ticker + '_OI'
        => string(na)

colorChange(x, y) =>
    switch outline
        false => label.set_textcolor(x, y)
        => label.set_color(x, y)

colorChange2(x, y) =>
    box.set_bgcolor(x, y)

[clo, clo1, HIGH, LOW] = request.security_lower_tf(req(), '1', [close, close[1], high, low])

var int startHeat = 0
var float startHeat1 = 0

if array.size(m)[1] == 0 and array.size(m) > 0
    startHeat := math.round(time)
    startHeat1 := bar_index
    startHeat1
timeCond = heatOnly == false ? last_bar_index - bar_index <= 500 : array.size(m) > 0
var array<float> volCountUpFin = array.new_float(3000, initial_value = 0.0)
var array<float> volCountDnFin = array.new_float(3000, initial_value = 0.0)

var int first = 0
var int firstBar = math.round(1e8)
levelsCount = array.new_float()

var string calcSince = ''

if last_bar_index - bar_index == 500 and Recalculation == 'No Recalculation'
    calcSince := 'Расчет с \n' + str.tostring(month(time)) + '/' + str.tostring(dayofmonth(time)) + ' - ' + str.tostring(hour(time)) + ':' + str.tostring(minute(time)) + ':' + str.tostring(second(time))
    calcSince

if timeCond

    max := math.max(high, max)
    min := math.min(low, min)

    if finTim

        if array.size(label.all) > 0
            for i = 0 to array.size(label.all) - 1 by 1
                label.delete(array.shift(label.all))

        for i = 0 to array.size(volCountUpFin) - 1 by 1
            array.set(volCountUpFin, i, 0)
            array.set(volCountDnFin, i, 0)
        if array.size(ticks) > 0
            array.clear(ticks)
            array.clear(ticks2)
        if array.size(track) > 0
            array.clear(track)

        max := high
        min := low

        first := math.round(time)

        firstBar := bar_index
        array.push(track, low)

if lastBar == false ? bar_index >= firstBar : barstate.islast

    if array.size(track) > 0
        array.push(ticks, array.get(track, array.size(track) - 1))
        if calcBy == 'Ticks'
            for i = 1 to 500 by 1
                array.push(ticks, array.get(ticks, i - 1) + tickz * syminfo.mintick)

            array.push(ticks2, array.get(track, array.size(track) - 1) - tickz * syminfo.mintick)
            for i = 1 to 500 by 1
                array.push(ticks2, array.get(ticks2, i - 1) - tickz * syminfo.mintick)
        else
            for i = 1 to 500 by 1
                array.push(ticks, array.get(ticks, i - 1) * (1 + perc))

            array.push(ticks2, array.get(track, array.size(track) - 1) * (1 - perc))
            for i = 1 to 500 by 1
                array.push(ticks2, array.get(ticks2, i - 1) * (1 - perc))

ticks3 = array.concat(ticks, ticks2)
array.sort(ticks3, order.ascending)

if array.size(ticks3) > 1
    if classic == false
        if data != 'OI Delta (BINANCE PERP FUTURES ONLY)'
            if array.size(m2) > 0
                for x = 0 to array.size(ticks3) - 1 by 1
                    if array.get(ticks3, x) <= high and array.get(ticks3, x) >= low and array.includes(levelsCount, array.get(ticks3, x)) == false
                        array.push(levelsCount, array.get(ticks3, x))
                        array.push(boxCo, 1)
                        array.push(heatLevels, array.get(ticks3, x))
        else
            if array.size(clo) > 0
                for x = 0 to array.size(ticks3) - 1 by 1
                    if array.get(ticks3, x) <= high and array.get(ticks3, x) >= low and array.includes(levelsCount, array.get(ticks3, x)) == false
                        array.push(levelsCount, array.get(ticks3, x))
                        array.push(boxCo, 1)
                        array.push(heatLevels, array.get(ticks3, x))

    else
        div = (high - low) / barDiv
        if data != 'OI Delta (BINANCE PERP FUTURES ONLY)'
            if array.size(m2) > 0
                for x = 0 to barDiv - 1 by 1
                    array.push(levelsCount, low + x * div)
                    array.push(boxCo, 1)
                    array.push(heatLevels, low + x * div)
        else
            if array.size(clo) > 0
                for x = 0 to barDiv - 1 by 1
                    array.push(levelsCount, low + x * div)
                    array.push(boxCo, 1)
                    array.push(heatLevels, low + x * div)

if array.size(levelsCount) > 0
    array.sort(levelsCount, order.ascending)
    if array.size(levelsCount) > 1
        if classic == false
            if calcBy == 'Ticks'
                array.push(levelsCount, array.get(levelsCount, array.size(levelsCount) - 1) + tickz * syminfo.mintick)
            else
                array.push(levelsCount, array.get(levelsCount, array.size(levelsCount) - 1) * (1 + perc))
        else
            array.push(levelsCount, array.get(levelsCount, array.size(levelsCount) - 1) + (high - low) / barDiv)

        array.push(boxCo, 1)
    array.sort(levelsCount, order.ascending)
    if array.size(levelsCount) > 0
        if classic == false
            for i = 0 to array.size(levelsCount) - 1 by 1
                array.push(lettersBox, label.new(bar_index, array.get(levelsCount, i), color = color.new(color.white, 100), text = '', size = sz, textcolor = color.white, text_font_family = fonT, style = outline == true ? label.style_text_outline : label.style_none))

        else
            array.pop(levelsCount)
            array.pop(boxCo)
            div = (high - low) / barDiv
            for i = 0 to array.size(levelsCount) - 1 by 1
                array.push(lettersBoxClassic, box.new(bar_index, array.get(levelsCount, i), bar_index + 1, array.get(levelsCount, i) + div, bgcolor = color.new(color.white, 100), text = '', text_size = sz, text_color = color.white, text_font_family = fonT, border_color = na))

volCountUp = array.new_float(array.size(levelsCount), 0)
volCountDn = array.new_float(array.size(levelsCount), 0)

if data != 'OI Delta (BINANCE PERP FUTURES ONLY)'

    if array.size(boxCo) > 1 and array.sum(boxCo) == array.size(levelsCount)
        boxTrack2 = array.new_float(array.size(m2), 0.0)
        if array.size(m2) > 0
            for nx = 0 to array.size(m2) - 1 by 1
                for x = 0 to array.size(levelsCount) - 1 by 1
                    if array.get(levelsCount, x) <= array.get(m3, nx) and array.get(levelsCount, x) >= array.get(m4, nx)
                        array.set(boxTrack2, nx, array.get(boxTrack2, nx) + 1)
            if array.sum(boxTrack2) > 0
                for x = 0 to array.size(levelsCount) - 1 by 1
                    for nx = 0 to array.size(m2) - 1 by 1
                        if array.get(boxTrack2, nx) != 0 and array.get(levelsCount, x) <= array.get(m3, nx) and array.get(levelsCount, x) >= array.get(m4, nx)
                            if array.get(m, nx) >= array.get(m6, nx)
                                array.set(volCountUp, x, array.get(volCountUp, x) + array.get(m2, nx) / array.get(boxTrack2, nx))
                                SW = switch data
                                    'Volume' => str.tostring(array.get(volCountUp, x), format.volume) + ' | ' + str.tostring(array.get(volCountDn, x), format.volume)
                                    'Vol. Delta' => math.sign(array.get(volCountUp, x) - array.get(volCountDn, x)) == 1 ? '+' + str.tostring(array.get(volCountUp, x) - array.get(volCountDn, x), '###,###,###.00') : str.tostring(array.get(volCountUp, x) - array.get(volCountDn, x), '###,###,###.00')
                                if classic == false and array.size(lettersBox) > 0
                                    label.set_text(array.get(lettersBox, x), SW)
                                else if classic == true and array.size(lettersBoxClassic) > 0
                                    box.set_text(array.get(lettersBoxClassic, x), SW)
                            else
                                array.set(volCountDn, x, array.get(volCountDn, x) + array.get(m2, nx) / array.get(boxTrack2, nx))
                                SW = switch data
                                    'Volume' => str.tostring(array.get(volCountUp, x), format.volume) + ' | ' + str.tostring(array.get(volCountDn, x), format.volume)
                                    'Vol. Delta' => math.sign(array.get(volCountUp, x) - array.get(volCountDn, x)) == 1 ? '+' + str.tostring(array.get(volCountUp, x) - array.get(volCountDn, x), '###,###,###.00') : str.tostring(array.get(volCountUp, x) - array.get(volCountDn, x), '###,###,###.00')
                                if array.size(lettersBox) > 0 and classic == false
                                    label.set_text(array.get(lettersBox, x), SW)
                                else if classic == true and array.size(lettersBoxClassic) > 0
                                    box.set_text(array.get(lettersBoxClassic, x), SW)

            for i = 1 to array.size(levelsCount) - 1 by 1
                for x = 0 to array.size(m2) - 1 by 1
                    if array.get(m3, x) < array.get(levelsCount, i) and array.get(m4, x) > array.get(levelsCount, i - 1)
                        if array.get(m, x) >= array.get(m6, x)
                            array.set(volCountUp, i - 1, array.get(volCountUp, i - 1) + array.get(m2, x))
                            if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
                                SW2 = switch data
                                    'Volume' => str.tostring(array.get(volCountUp, i - 1), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i - 1), format.volume)
                                    'Vol. Delta' => math.sign(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1), '###,###,###.00')
                                if classic == false
                                    label.set_text(array.get(lettersBox, i - 1), SW2)
                                else if classic == true
                                    box.set_text(array.get(lettersBoxClassic, i - 1), SW2)
                        else 
                            array.set(volCountDn, i - 1, array.get(volCountDn, i - 1) + array.get(m2, x))
                            if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
                                SW2 = switch data
                                    'Volume' => str.tostring(array.get(volCountUp, i - 1), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i - 1), format.volume)
                                    'Vol. Delta' => math.sign(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, i - 1) - array.get(volCountDn, i - 1), '###,###,###.00')
                                if classic == false
                                    label.set_text(array.get(lettersBox, i - 1), SW2)
                                else if classic == true
                                    box.set_text(array.get(lettersBoxClassic, i - 1), SW2)

            array.unshift(volCountUp, 0)
            array.unshift(volCountDn, 0)
            for x = 0 to array.size(m2) - 1 by 1
                if array.get(m3, x) < array.get(levelsCount, 0)
                    if array.get(m, x) >= array.get(m6, x)
                        array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(m2, x))
                    else
                        array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(m2, x))
            volCountExUp = array.new_float(1)
            array.set(volCountExUp, 0, array.get(volCountUp, 0) + array.get(volCountUp, 1))
            volCountExDn = array.new_float(1)
            array.set(volCountExDn, 0, array.get(volCountDn, 0) + array.get(volCountDn, 1))
            array.shift(volCountUp)
            array.set(volCountUp, 0, array.sum(volCountExUp))
            array.shift(volCountDn)
            array.set(volCountDn, 0, array.sum(volCountExDn))
            SW3 = switch data
                'Volume' => str.tostring(array.sum(volCountExUp), format.volume) + ' | ' + str.tostring(array.sum(volCountExDn), format.volume)
                'Vol. Delta' => math.sign(array.sum(volCountExUp) - array.sum(volCountExDn)) == 1 ? '+' + str.tostring(array.sum(volCountExUp) - array.sum(volCountExDn), '###,###,###.00') : str.tostring(array.sum(volCountExUp) - array.sum(volCountExDn), '###,###,###.00')
            if classic == false
                label.set_text(array.get(lettersBox, 0), SW3)
            else
                box.set_text(array.get(lettersBoxClassic, 0), SW3)

    if array.size(boxCo) == 1
        for i = 0 to array.size(m2) - 1 by 1
            if array.get(m, i) >= array.get(m6, i)
                array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(m2, i))
            else
                array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(m2, i))

        if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
            SW4 = switch data
                'Volume' => str.tostring(array.get(volCountUp, 0), format.volume) + ' | ' + str.tostring(array.get(volCountDn, 0), format.volume)
                'Vol. Delta' => math.sign(array.get(volCountUp, 0) - array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00')
            if classic == false
                label.set_text(array.get(lettersBox, 0), SW4 + '*')
                colorChange(array.get(lettersBox, 0), array.sum(volCountUp) > array.sum(volCountDn) ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))

            else
                box.set_text(array.get(lettersBoxClassic, 0), SW4 + '*')
                colorChange2(array.get(lettersBoxClassic, 0), array.sum(volCountUp) > array.sum(volCountDn) ? color.new(c, 90) : color.new(c1, 90))

    if array.size(boxCo) == 0 and array.size(m2) > 0 and bar_index >= firstBar and array.size(ticks3) > 1
        for i = 1 to array.size(ticks3) - 1 by 1
            if array.get(ticks3, i) > low and array.get(ticks3, i - 1) <= low
                array.push(levelsCount, array.get(ticks3, i - 1))
                break
        if classic == false
            array.push(lettersBox, label.new(bar_index, array.get(levelsCount, 0), '', textcolor = color.white, style = outline == true ? label.style_text_outline : label.style_none, size = sz))
        else
            calculation = switch calcBy
                'Ticks' => array.get(levelsCount, 0) + tickz * syminfo.mintick
                => array.get(levelsCount, 0) * (1 + perc)
            array.push(lettersBoxClassic, box.new(bar_index, calculation, bar_index + 1, array.get(levelsCount, 0), text = '', text_color = color.white, text_size = sz))

        array.unshift(volCountUp, 0)
        array.unshift(volCountDn, 0)
        for i = 0 to array.size(m2) - 1 by 1
            if array.get(m, i) >= array.get(m6, i)
                array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(m2, i))
                SW5 = switch data
                    'Volume' => str.tostring(array.sum(volCountUp), format.volume) + ' | ' + str.tostring(array.sum(volCountDn), format.volume)
                    'Vol. Delta' => math.sign(array.sum(volCountUp) - array.sum(volCountDn)) == 1 ? '+' + str.tostring(array.sum(volCountUp) - array.sum(volCountDn), '###,###,###.00') : str.tostring(array.sum(volCountUp) - array.sum(volCountDn), '###,###,###.00')
                if classic == false
                    label.set_text(array.get(lettersBox, 0), SW5 + '*')
                else
                    box.set_text(array.get(lettersBoxClassic, 0), SW5 + '*')

            else
                array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(m2, i))
                SW5 = switch data
                    'Volume' => str.tostring(array.sum(volCountUp), format.volume) + ' | ' + str.tostring(array.sum(volCountDn), format.volume)
                    'Vol. Delta' => math.sign(array.sum(volCountUp) - array.sum(volCountDn)) == 1 ? '+' + str.tostring(array.sum(volCountUp) - array.sum(volCountDn), '###,###,###.00') : str.tostring(array.sum(volCountUp) - array.sum(volCountDn), '###,###,###.00')
                if classic == false
                    label.set_text(array.get(lettersBox, 0), SW5 + '*')
                    colorChange(array.get(lettersBox, 0), array.sum(volCountUp) >= array.sum(volCountDn) ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))
                else
                    box.set_text(array.get(lettersBoxClassic, 0), SW5 + '*')
                    colorChange2(array.get(lettersBoxClassic, 0), array.sum(volCountUp) >= array.sum(volCountDn) ? color.new(c, 90) : color.new(c1, 90))

    if array.size(lettersBox) > 1 and classic == false or array.size(lettersBoxClassic) > 1 and classic == true
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            if classic == false
                switch data
                    'Volume' => colorChange(array.get(lettersBox, i), array.get(volCountUp, i) < array.get(volCountDn, i) ? color.new(c1, outline == true ? 50 : 0) : color.new(c, outline == true ? 50 : 0))
                    'Vol. Delta' => colorChange(array.get(lettersBox, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))
            else
                switch data
                    'Volume' => colorChange2(array.get(lettersBoxClassic, i), array.get(volCountUp, i) < array.get(volCountDn, i) ? color.new(c1, 90) : color.new(c, 90))
                    'Vol. Delta' => colorChange2(array.get(lettersBoxClassic, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? color.new(c, 90) : color.new(c1, 90))

            if classic == false
                if label.get_text(array.get(lettersBox, i)) == ''
                    label.set_color(array.get(lettersBox, i), na)
            else
                if box.get_bottom(array.get(lettersBoxClassic, i)) > high
                    box.set_bgcolor(array.get(lettersBoxClassic, i), na)

        maxUp = 0.0
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            maxUp := data == 'Volume' ? math.max(maxUp, array.get(volCountUp, i), array.get(volCountDn, i)) : math.max(maxUp, math.abs(array.get(volCountUp, i) - array.get(volCountDn, i)))
            maxUp
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            if data == 'Volume'
                if array.get(volCountUp, i) == maxUp or array.get(volCountDn, i) == maxUp
                    if classic == false
                        colorChange(array.get(lettersBox, i), c2)
                    else
                        box.set_bgcolor(array.get(lettersBoxClassic, i), color.new(c2, 90))
                        box.set_border_color(array.get(lettersBoxClassic, i), c2)

            else
                if math.abs(array.get(volCountUp, i) - array.get(volCountDn, i)) == maxUp
                    if classic == false
                        colorChange(array.get(lettersBox, i), c2)
                    else
                        box.set_bgcolor(array.get(lettersBoxClassic, i), color.new(c2, 90))
                        box.set_border_color(array.get(lettersBoxClassic, i), c2)
                    break
    if classic
        if array.size(levelsCount) > 1
            if array.size(m2) > 0
                for x = 0 to array.size(m2) - 1 by 1
                    if array.get(m4, x) > array.get(levelsCount, array.size(levelsCount) - 1)
                        if array.get(m, x) >= array.get(m6, x)
                            array.set(volCountUp, array.size(volCountUp) - 1, array.get(volCountUp, array.size(volCountUp) - 1) + array.get(m2, x))
                            SWF = switch data

                                'Volume' => str.tostring(array.get(volCountUp, array.size(volCountUp) - 1), format.volume) + ' | ' + str.tostring(array.get(volCountDn, array.size(volCountDn) - 1), format.volume)
                                'Vol. Delta' => math.sign(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00')

                            box.set_text(array.get(lettersBoxClassic, array.size(lettersBoxClassic) - 1), SWF)

                        else 
                            array.set(volCountDn, array.size(volCountDn) - 1, array.get(volCountDn, array.size(volCountDn) - 1) + array.get(m2, x))

                            SWF = switch data

                                'Volume' => str.tostring(array.get(volCountUp, array.size(volCountUp) - 1), format.volume) + ' | ' + str.tostring(array.get(volCountDn, array.size(volCountDn) - 1), format.volume)
                                'Vol. Delta' => math.sign(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) - array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00')

                            box.set_text(array.get(lettersBoxClassic, array.size(lettersBoxClassic) - 1), SWF)

        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            if assume == true
                if data == 'Volume' or data == 'Vol. Delta'
                    if array.get(volCountUp, i) == 0 and array.get(volCountDn, i) > 0
                        array.set(volCountUp, i, array.get(volCountDn, i) * .40)
                        array.set(volCountDn, i, array.get(volCountDn, i) * .60)
                        if data == 'Volume'
                            if classic == false
                                label.set_text(array.get(lettersBox, i), str.tostring(array.get(volCountUp, i), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i), format.volume))
                            else 
                                box.set_text(array.get(lettersBoxClassic, i), str.tostring(array.get(volCountUp, i), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i), format.volume))
                        else 
                            if classic == false
                                label.set_text(array.get(lettersBox, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? '+' + str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00') : str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00'))
                            else
                                box.set_text(array.get(lettersBoxClassic, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? '+' + str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00') : str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00'))

                    else if array.get(volCountUp, i) > 0 and array.get(volCountDn, i) == 0
                        array.set(volCountDn, i, array.get(volCountUp, i) * .40)
                        array.set(volCountUp, i, array.get(volCountUp, i) * .60)
                        if classic == false
                            if data == 'Volume'
                                label.set_text(array.get(lettersBox, i), str.tostring(array.get(volCountUp, i), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i), format.volume))
                            else
                                label.set_text(array.get(lettersBox, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? '+' + str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00') : str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00'))
                        else
                            if data == 'Volume'
                                box.set_text(array.get(lettersBoxClassic, i), str.tostring(array.get(volCountUp, i), format.volume) + ' | ' + str.tostring(array.get(volCountDn, i), format.volume))
                            else
                                box.set_text(array.get(lettersBoxClassic, i), math.sign(array.get(volCountUp, i) - array.get(volCountDn, i)) == 1 ? '+' + str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00') : str.tostring(array.get(volCountUp, i) - array.get(volCountDn, i), '###,###,###.00'))

    else if array.size(lettersBox) == 1 or array.size(lettersBoxClassic) == 1
        if assume == true
            if data == 'Volume' or data == 'Vol. Delta'
                if array.get(volCountUp, 0) == 0 and array.get(volCountDn, 0) != 0
                    array.set(volCountUp, 0, array.get(volCountDn, 0) * .40)
                    array.set(volCountDn, 0, array.get(volCountDn, 0) * .60)
                    if classic == false
                        if data == 'Volume'
                            label.set_text(array.get(lettersBox, 0), str.tostring(array.get(volCountUp, 0), format.volume) + ' | ' + str.tostring(array.get(volCountDn, 0), format.volume))
                        else 
                            label.set_text(array.get(lettersBox, 0), math.sign(array.get(volCountUp, 0) - array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00'))
                    else
                        if data == 'Volume'
                            box.set_text(array.get(lettersBoxClassic, 0), str.tostring(array.get(volCountUp, 0), format.volume) + ' | ' + str.tostring(array.get(volCountDn, 0), format.volume))
                        else 
                            box.set_text(array.get(lettersBoxClassic, 0), math.sign(array.get(volCountUp, 0) - array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00'))

                if array.get(volCountDn, 0) == 0 and array.get(volCountUp, 0) != 0
                    array.set(volCountDn, 0, array.get(volCountUp, 0) * .40)
                    array.set(volCountUp, 0, array.get(volCountUp, 0) * .60)
                    if classic == false
                        if data == 'Volume'
                            label.set_text(array.get(lettersBox, 0), str.tostring(array.get(volCountUp, 0), format.volume) + ' | ' + str.tostring(array.get(volCountDn, 0), format.volume))

                        else 
                            label.set_text(array.get(lettersBox, 0), math.sign(array.get(volCountUp, 0) - array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00'))
                    else
                        if data == 'Volume'
                            box.set_text(array.get(lettersBoxClassic, 0), str.tostring(array.get(volCountUp, 0), format.volume) + ' | ' + str.tostring(array.get(volCountDn, 0), format.volume))
                        else
                            box.set_text(array.get(lettersBoxClassic, 0), math.sign(array.get(volCountUp, 0) - array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) - array.get(volCountDn, 0), '###,###,###.00'))

    if array.size(levelsCount) > 0
        var array<label> finalBox = array.new_label()
        SA = switch data
            'Volume' => str.tostring(array.sum(volCountUp), '+###,###,###.00') + ' | ' + str.tostring(-array.sum(volCountDn), '###,###,###.00')
            'Vol. Delta' => str.tostring(array.sum(volCountUp) - array.sum(volCountDn), array.sum(volCountUp) - array.sum(volCountDn) >= 0 ? '+###,###,###.00' : '###,###,###.00')

        array.push(finalBox, label.new(bar_index, low, yloc = classic == false ? yloc.belowbar : yloc.abovebar, text = classic == false ? SA + '\nTotal' : '                    ' + SA + '\n                    Total', size = sz, style = outline == true ? label.style_text_outline : label.style_none, textcolor = color.white, text_font_family = fonT))
        colorChange(array.get(finalBox, array.size(finalBox) - 1), array.sum(volCountUp) - array.sum(volCountDn) >= 0 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))

        if array.size(levelsCount) > 0
            for i = 0 to array.size(levelsCount) - 1 by 1
                for x = 0 to array.size(ticks3) - 1 by 1
                    if array.get(levelsCount, i) == array.get(ticks3, x)
                        array.set(volCountUpFin, x, array.get(volCountUpFin, x) + array.get(volCountUp, i))
                        array.set(volCountDnFin, x, array.get(volCountDnFin, x) + array.get(volCountDn, i))

if data == 'OI Delta (BINANCE PERP FUTURES ONLY)'

    if classic
        if array.size(levelsCount) > 0
            if array.size(clo) > 0
                for x = 0 to array.size(clo) - 1 by 1
                    if array.get(m4, x) > array.get(levelsCount, array.size(levelsCount) - 1)
                        if array.get(clo, x) >= array.get(clo1, x)
                            array.set(volCountUp, array.size(volCountUp) - 1, array.get(volCountUp, array.size(volCountUp) - 1) + array.get(clo, x) - array.get(clo1, x))
                            cache = math.sign(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountUp) - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountUp) - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00')

                            box.set_text(array.get(lettersBoxClassic, array.size(lettersBoxClassic) - 1), cache)
                        else 
                            array.set(volCountDn, array.size(volCountDn) - 1, array.get(volCountDn, array.size(volCountDn) - 1) + array.get(clo, x) - array.get(clo1, x))

                            cache = math.sign(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountUp) - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountUp) - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, array.size(volCountUp) - 1) + array.get(volCountDn, array.size(volCountDn) - 1), '###,###,###.00')

                            box.set_text(array.get(lettersBoxClassic, array.size(lettersBoxClassic) - 1), cache)

    if array.size(boxCo) > 1 and array.sum(boxCo) == array.size(levelsCount)
        boxTrack2 = array.new_float(array.size(clo), 0.0)

        if array.size(clo) > 0 and array.size(clo) == array.size(m2)
            for nx = 0 to array.size(clo) - 1 by 1
                for x = 0 to array.size(levelsCount) - 1 by 1
                    if array.get(levelsCount, x) <= array.get(m3, nx) and array.get(levelsCount, x) >= array.get(m4, nx)
                        array.set(boxTrack2, nx, array.get(boxTrack2, nx) + 1)

            if array.sum(boxTrack2) > 0
                for x = 0 to array.size(levelsCount) - 1 by 1
                    for nx = 0 to array.size(clo) - 1 by 1

                        if array.get(boxTrack2, nx) != 0 and array.get(levelsCount, x) <= array.get(m3, nx) and array.get(levelsCount, x) >= array.get(m4, nx)

                            if array.get(clo, nx) >= array.get(clo1, nx)

                                array.set(volCountUp, x, array.get(volCountUp, x) + (array.get(clo, nx) - array.get(clo1, nx)) / array.get(boxTrack2, nx))

                                cache = math.sign(array.get(volCountUp, x) + array.get(volCountDn, x)) == 1 ? '+' + str.tostring(array.get(volCountUp, x) + array.get(volCountDn, x), '###,###,###.00') : str.tostring(array.get(volCountUp, x) + array.get(volCountDn, x), '###,###,###.00')

                                if array.size(lettersBox) > 0 and classic == false
                                    label.set_text(array.get(lettersBox, x), cache)
                                else if array.size(lettersBoxClassic) > 0 and classic == true
                                    box.set_text(array.get(lettersBoxClassic, x), cache)

                            else 
                                array.set(volCountDn, x, array.get(volCountDn, x) + (array.get(clo, nx) - array.get(clo1, nx)) / array.get(boxTrack2, nx))

                                cache = math.sign(array.get(volCountUp, x) + array.get(volCountDn, x)) == 1 ? '+' + str.tostring(array.get(volCountUp, x) + array.get(volCountDn, x), '###,###,###.00') : str.tostring(array.get(volCountUp, x) + array.get(volCountDn, x), '###,###,###.00')

                                if array.size(lettersBox) > 0 and classic == false
                                    label.set_text(array.get(lettersBox, x), cache)
                                else if array.size(lettersBoxClassic) > 0 and classic == true
                                    box.set_text(array.get(lettersBoxClassic, x), cache)

            array.unshift(volCountUp, 0)
            array.unshift(volCountDn, 0)

            for x = 0 to array.size(m2) - 1 by 1
                if array.get(m3, x) < array.get(levelsCount, 0)

                    if array.get(clo, x) >= array.get(clo1, x)
                        array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(clo, x) - array.get(clo1, x))
                    else
                        array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(clo, x) - array.get(clo1, x))

            volCountExUp = array.new_float(1)
            array.set(volCountExUp, 0, array.get(volCountUp, 0) + array.get(volCountUp, 1))
            volCountExDn = array.new_float(1)
            array.set(volCountExDn, 0, array.get(volCountDn, 0) + array.get(volCountDn, 1))

            array.shift(volCountUp)
            array.set(volCountUp, 0, array.sum(volCountExUp))
            array.shift(volCountDn)
            array.set(volCountDn, 0, array.sum(volCountExDn))

            cache = math.sign(array.sum(volCountExUp) + array.sum(volCountExDn)) == 1 ? '+' + str.tostring(array.sum(volCountExUp) + array.sum(volCountExDn), '###,###,###.00') : str.tostring(array.sum(volCountExUp) + array.sum(volCountExDn), '###,###,###.00')

            if classic == false
                label.set_text(array.get(lettersBox, 0), cache)
            else
                box.set_text(array.get(lettersBoxClassic, 0), cache)

            if array.sum(boxTrack2) > 1
                for i = 1 to array.size(levelsCount) - 1 by 1
                    for x = 0 to array.size(clo) - 1 by 1
                        if array.get(m3, x) < array.get(levelsCount, i) and array.get(m4, x) > array.get(levelsCount, i - 1)
                            if array.get(clo, x) >= array.get(clo1, x)
                                array.set(volCountUp, i - 1, array.get(volCountUp, i - 1) + array.get(clo, x) - array.get(clo1, x))
                                if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
                                    cache2 = math.sign(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1), '###,###,###.00')
                                    if classic == false
                                        label.set_text(array.get(lettersBox, i - 1), cache2)
                                    else
                                        box.set_text(array.get(lettersBoxClassic, i - 1), cache2)
                            else
                                array.set(volCountDn, i - 1, array.get(volCountDn, i - 1) + array.get(clo, x) - array.get(clo1, x))
                                if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
                                    cache2 = math.sign(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1)) == 1 ? '+' + str.tostring(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1), '###,###,###.00') : str.tostring(array.get(volCountUp, i - 1) + array.get(volCountDn, i - 1), '###,###,###.00')
                                    if classic == false
                                        label.set_text(array.get(lettersBox, i - 1), cache2)
                                    else if classic == true
                                        box.set_text(array.get(lettersBoxClassic, i - 1), cache2)

    if array.size(boxCo) == 1 and array.size(m2) == array.size(clo)
        for i = 0 to array.size(clo) - 1 by 1
            if array.get(clo, i) >= array.get(clo1, i)
                array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(clo, i) - array.get(clo1, i))
            else
                array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(clo, i) - array.get(clo1, i))
        if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0

            cache = math.sign(array.get(volCountUp, 0) + array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00')

            if classic == false
                label.set_text(array.get(lettersBox, 0), cache)
                colorChange(array.get(lettersBox, 0), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))
            else
                box.set_text(array.get(lettersBoxClassic, 0), cache)
                colorChange2(array.get(lettersBoxClassic, 0), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? color.new(c, 90) : color.new(c1, 90))

    if array.size(boxCo) == 0 and array.size(m2) > 0 and array.size(m2) == array.size(clo) and bar_index >= firstBar and array.size(ticks3) > 1
        for i = 1 to array.size(ticks3) - 1 by 1
            if array.get(ticks3, i) > low and array.get(ticks3, i - 1) <= low
                array.push(levelsCount, array.get(ticks3, i - 1))
                break
        if classic == false
            array.push(lettersBox, label.new(bar_index, array.get(levelsCount, 0), '', textcolor = color.white, style = outline == true ? label.style_text_outline : label.style_none, size = sz))
        else
            calculation = switch calcBy
                'Ticks' => array.get(levelsCount, 0) + tickz * syminfo.mintick
                => array.get(levelsCount, 0) * (1 + perc)
            array.push(lettersBoxClassic, box.new(bar_index, low, bar_index + 1, high, text = '', text_color = color.white, text_size = sz, bgcolor = color.new(color.white, 100)))

        array.unshift(volCountUp, 0)
        array.unshift(volCountDn, 0)
        for i = 0 to array.size(clo) - 1 by 1
            if array.get(clo, i) >= array.get(clo1, i)
                array.set(volCountUp, 0, array.get(volCountUp, 0) + array.get(clo, i) - array.get(clo1, i))

                cache = math.sign(array.get(volCountUp, 0) + array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00')

                if classic == false
                    label.set_text(array.get(lettersBox, 0), cache)
                else
                    box.set_text(array.get(lettersBoxClassic, 0), cache)

            else
                array.set(volCountDn, 0, array.get(volCountDn, 0) + array.get(clo, i) - array.get(clo1, i))

                cache = math.sign(array.get(volCountUp, 0) + array.get(volCountDn, 0)) == 1 ? '+' + str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00') : str.tostring(array.get(volCountUp, 0) + array.get(volCountDn, 0), '###,###,###.00')

                if classic == false
                    label.set_text(array.get(lettersBox, 0), cache)
                    colorChange(array.get(lettersBox, 0), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))
                else
                    box.set_text(array.get(lettersBoxClassic, 0), cache)
                    colorChange2(array.get(lettersBoxClassic, 0), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? color.new(c, 90) : color.new(c1, 90))

    if array.size(lettersBox) > 0 or array.size(lettersBoxClassic) > 0
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            if classic == false
                colorChange(array.get(lettersBox, i), math.sign(array.get(volCountUp, i) + array.get(volCountDn, i)) == 1 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))
                if label.get_text(array.get(lettersBox, i)) == ''
                    label.set_color(array.get(lettersBox, i), na)
            else
                colorChange2(array.get(lettersBoxClassic, i), math.sign(array.get(volCountUp, i) + array.get(volCountDn, i)) == 1 ? color.new(c, 90) : color.new(c1, 90))
                if box.get_bottom(array.get(lettersBoxClassic, i)) > high
                    box.set_bgcolor(array.get(lettersBoxClassic, i), na)

        maxUp = 0.0
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            maxUp := math.max(maxUp, math.abs(array.get(volCountUp, i) + array.get(volCountDn, i)))
            maxUp
        for i = 0 to classic == false ? array.size(lettersBox) - 1 : array.size(lettersBoxClassic) - 1 by 1
            if math.abs(array.get(volCountUp, i) + array.get(volCountDn, i)) == maxUp
                if classic == false
                    label.set_color(array.get(lettersBox, i), c2)
                else
                    box.set_bgcolor(array.get(lettersBoxClassic, i), color.new(c2, 90))
                    box.set_border_color(array.get(lettersBoxClassic, i), c2)
                break
        for i = 0 to array.size(levelsCount) - 1 by 1
            for x = 0 to array.size(ticks3) - 1 by 1
                if array.get(levelsCount, i) == array.get(ticks3, x)
                    array.set(volCountUpFin, x, array.get(volCountUpFin, x) + array.get(volCountUp, i))
                    array.set(volCountDnFin, x, array.get(volCountDnFin, x) + array.get(volCountDn, i))
        var array<label> finalBox = array.new_label()
        SA = str.tostring(array.sum(volCountUp) + array.sum(volCountDn), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? '+###,###,###.00' : '###,###,###.00')

        array.push(finalBox, label.new(bar_index, low, yloc = classic == false ? yloc.belowbar : yloc.abovebar, text = classic == false ? SA + '\nTotal' : '                    ' + SA + '\n                    Total', size = sz, style = outline == true ? label.style_text_outline : label.style_none, textcolor = color.white))
        colorChange(array.get(finalBox, array.size(finalBox) - 1), array.sum(volCountUp) + array.sum(volCountDn) >= 0 ? color.new(c, outline == true ? 50 : 0) : color.new(c1, outline == true ? 50 : 0))

if barstate.islast and array.size(label.all) > 0 and classic == false and heatOnly == false or barstate.islast and array.size(volCountUpFin) > 0 and heatOnly == true
    mX = 0.0
    mX1 = 1e8
    if heatOnly == false
        for i = 0 to array.size(label.all) - 1 by 1
            mX := math.max(label.get_y(array.get(label.all, i)), mX)
            mX1 := math.min(label.get_y(array.get(label.all, i)), mX1)
            mX1
    else
        for i = 0 to array.size(heatLevels) - 1 by 1
            mX := math.max(array.get(heatLevels, i), mX)
            mX1 := math.min(array.get(heatLevels, i), mX1)
            mX1
    var line endLine = na
    var label cs = na
    if na(endLine) and heatOnly == false
        endLine := line.new(bar_index + 7, mX, bar_index + 7, mX1, color = c, width = 3)
        if Recalculation == 'No Recalculation' and hideAll == true
            cs := label.new(bar_index + 4, mX, text = calcSince, color = color.new(color.white, 100), textcolor = color.white, size = sz)
            cs

    var array<float> set = array.new_float()
    var array<line> finBox = array.new_line()
    var array<box> finBox2 = array.new_box()

    if heatOnly == false
        if array.size(finBox) == 0 and array.size(finBox2) == 0
            for i = 0 to array.size(label.all) - 1 by 1
                if array.includes(set, label.get_y(array.get(label.all, i))) == false and label.get_text(array.get(label.all, i)) != ''
                    array.push(set, label.get_y(array.get(label.all, i)))
                    array.push(finBox, line.new(extend ? bar_index - 100 : bar_index + 2, label.get_y(array.get(label.all, i)), bar_index + 3, label.get_y(array.get(label.all, i)), color = na))
                    array.push(finBox2, box.new(bar_index + 3, label.get_y(array.get(label.all, i)) + syminfo.mintick, bar_index + 5, label.get_y(array.get(label.all, i)), bgcolor = na, border_color = color.new(color.white, 100), text = '', text_color = color.white, text_size = sz, text_font_family = fonT))
        else 
            for i = 0 to array.size(finBox) - 1 by 1
                line.set_x1(array.get(finBox, i), extend ? bar_index - 100 : bar_index + 2)
                line.set_x2(array.get(finBox, i), bar_index + 3)
                box.set_left(array.get(finBox2, i), bar_index + 3)
                box.set_right(array.get(finBox2, i), bar_index + 5)
    else 
        if array.size(finBox) == 0

            for i = 0 to array.size(heatLevels) - 1 by 1
                if array.includes(set, array.get(heatLevels, i)) == false
                    array.push(set, array.get(heatLevels, i))
                    array.push(finBox, line.new(startHeat, array.get(heatLevels, i), time + time - time[3], array.get(heatLevels, i), color = color.new(color.white, 100), xloc = xloc.bar_time))
                    array.push(finBox2, box.new(bar_index + 3, array.get(heatLevels, i) + syminfo.mintick, bar_index + 5, array.get(heatLevels, i), bgcolor = na, border_color = color.new(color.white, 100), text = '', text_color = color.white, text_size = sz, text_font_family = fonT))
        else 
            for i = 0 to array.size(finBox) - 1 by 1
                line.set_x2(array.get(finBox, i), time)
                box.set_left(array.get(finBox2, i), bar_index + 3)
                box.set_right(array.get(finBox2, i), bar_index + 5)

    fX = 0.0
    for i = 0 to array.size(set) - 1 by 1
        for x = 0 to array.size(ticks3) - 1 by 1
            if array.get(set, i) == array.get(ticks3, x)
                if data == 'Volume'
                    if heatOnly == false
                        fX := math.max(array.get(volCountUpFin, x), array.get(volCountDnFin, x), fX)
                        box.set_text(array.get(finBox2, i), '+' + str.tostring(array.get(volCountUpFin, x), format.volume) + ' | -' + str.tostring(array.get(volCountDnFin, x), format.volume))
                else if data == 'Vol. Delta'
                    if heatOnly == false
                        fX := math.max(math.abs(array.get(volCountUpFin, x) - array.get(volCountDnFin, x)), fX)
                        box.set_text(array.get(finBox2, i), array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 ? '+' + str.tostring(array.get(volCountUpFin, x) - array.get(volCountDnFin, x), format.volume) : str.tostring(array.get(volCountUpFin, x) - array.get(volCountDnFin, x), format.volume))
                else if data == 'OI Delta (BINANCE PERP FUTURES ONLY)'
                    if heatOnly == false
                        fX := math.max(math.abs(array.get(volCountUpFin, x) + array.get(volCountDnFin, x)), fX)
                        box.set_text(array.get(finBox2, i), array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 ? '+' + str.tostring(array.get(volCountUpFin, x) + array.get(volCountDnFin, x), format.volume) : str.tostring(array.get(volCountUpFin, x) + array.get(volCountDnFin, x), format.volume))

                if heatOnly == false
                    if data != 'OI Delta (BINANCE PERP FUTURES ONLY)'

                        box.set_text_color(array.get(finBox2, i), array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 ? c1 : c)
                        line.set_color(array.get(finBox, i), array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 ? color.new(c1, 85) : color.new(c, 85))
                        line.set_color(array.get(finBox, i), array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) >= low and line.get_y1(array.get(finBox, i)) <= high ? c1 : array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) >= low and line.get_y1(array.get(finBox, i)) <= high ? c : array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) > high or array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) < low ? color.new(c, 85) : array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) > high or array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) < low ? color.new(c1, 85) : na)

                    else 
                        box.set_text_color(array.get(finBox2, i), array.get(volCountUpFin, x) + array.get(volCountDnFin, x) < 0 ? c1 : c)
                        line.set_color(array.get(finBox, i), array.get(volCountUpFin, x) + array.get(volCountDnFin, x) < 0 ? color.new(c1, 85) : color.new(c, 85))
                        line.set_color(array.get(finBox, i), array.get(volCountUpFin, x) + array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) >= low and line.get_y1(array.get(finBox, i)) <= high ? c1 : array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) >= low and line.get_y1(array.get(finBox, i)) <= high ? c : array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) > high or array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 and line.get_y1(array.get(finBox, i)) < low ? color.new(c, 85) : array.get(volCountUpFin, x) + array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) > high or array.get(volCountUpFin, x) + array.get(volCountDnFin, x) < 0 and line.get_y1(array.get(finBox, i)) < low ? color.new(c1, 85) : na)

    var array<line> connectOr = array.new_line()

    if heat == true and heatOnly == false
        if array.size(finBox) > 0
            for i = 0 to array.size(finBox) - 1 by 1
                array.push(connectOr, line.new(bar_index - 200, line.get_y1(array.get(finBox, i)), bar_index + 7, line.get_y1(array.get(finBox, i)), color = na))
            if array.size(set) > 3
                for i = 2 to array.size(set) - 3 by 1
                    for x = 0 to array.size(ticks3) - 1 by 1
                        if array.get(set, i) == array.get(ticks3, x)
                            linefill.new(array.get(connectOr, i - 2), array.get(connectOr, i + 2), color = data != 'OI Delta (BINANCE PERP FUTURES ONLY)' ? array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 ? color.new(c, 95) : color.new(c1, 95) : array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 ? color.new(c, 95) : color.new(c1, 95))

    if heatOnly == true and array.size(finBox) > 0 and array.size(set) > 3
        for i = 2 to array.size(set) - 3 by 1
            for x = 0 to array.size(ticks3) - 1 by 1
                if array.get(set, i) == array.get(ticks3, x)
                    linefill.new(array.get(finBox, i - 2), array.get(finBox, i + 2), color = data != 'OI Delta (BINANCE PERP FUTURES ONLY)' ? array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 ? color.new(c, 95) : color.new(c1, 95) : array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 ? color.new(c, 95) : color.new(c1, 95))
    if array.size(finBox2) > 0
        for i = 0 to array.size(set) - 1 by 1
            for x = 0 to array.size(ticks3) - 1 by 1
                if array.get(set, i) == array.get(ticks3, x)

                    finSW = switch data

                        'Volume' => '+' + str.tostring(array.get(volCountUpFin, x), format.volume) + ' | -' + str.tostring(array.get(volCountDnFin, x), format.volume)
                        'Vol. Delta' => str.tostring(array.get(volCountUpFin, x) - array.get(volCountDnFin, x), format.volume)
                        'OI Delta (BINANCE PERP FUTURES ONLY)' => str.tostring(array.get(volCountUpFin, x) + array.get(volCountDnFin, x), format.volume)

                    color finCol = na


                    strinG = 'OI Delta (BINANCE PERP FUTURES ONLY)'

                    switch  
                        data != strinG and array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 => 
                    	    finCol := c
                    	    finCol
                        data != strinG and array.get(volCountUpFin, x) - array.get(volCountDnFin, x) < 0 => 
                    	    finCol := c1
                    	    finCol
                        data == strinG and array.get(volCountUpFin, x) + array.get(volCountDnFin, x) >= 0 => 
                    	    finCol := c
                    	    finCol
                        data == strinG and array.get(volCountUpFin, x) - array.get(volCountDnFin, x) >= 0 => 
                    	    finCol := c1
                    	    finCol


                    box.set_text(array.get(finBox2, i), finSW)
                    box.set_text_color(array.get(finBox2, i), finCol)

    for i = 0 to array.size(set) - 1 by 1
        for x = 0 to array.size(ticks3) - 1 by 1
            if heatOnly == false
                if array.get(set, i) == array.get(ticks3, x)
                    if data == 'Vol. Delta'
                        if math.abs(array.get(volCountUpFin, x) - array.get(volCountDnFin, x)) == fX
                            line.set_color(array.get(finBox, i), c2)
                            box.set_text_color(array.get(finBox2, i), c2)
                            if eX == true
                                line.set_x1(array.get(finBox, i), bar_index - 100)
                            break
                    else if data == 'Volume'
                        if array.get(volCountUpFin, x) == fX or array.get(volCountDnFin, x) == fX
                            line.set_color(array.get(finBox, i), c2)
                            box.set_text_color(array.get(finBox2, i), c2)
                            if eX == true
                                line.set_x1(array.get(finBox, i), bar_index - 100)
                            break
                    else if data == 'OI Delta (BINANCE PERP FUTURES ONLY)'
                        if math.abs(array.get(volCountUpFin, x) + array.get(volCountDnFin, x)) == fX
                            line.set_color(array.get(finBox, i), c2)
                            box.set_text_color(array.get(finBox2, i), c2)
                            if eX == true
                                line.set_x1(array.get(finBox, i), bar_index - 100)
                            break

            if hideAll == false
                line.delete(endLine)
                if heatOnly == false
                    line.delete(array.get(finBox, i))
                    box.delete(array.get(finBox2, i))

    if heatOnly == true
        if array.size(label.all) > 0
            for i = 0 to array.size(label.all) - 1 by 1
                label.delete(array.shift(label.all))

if array.size(lettersBox) == 1
    label.set_y(array.get(lettersBox, 0), ohlc4)

if array.size(lettersBoxClassic) == 1
    box.set_bottom(array.get(lettersBoxClassic, 0), low)
    box.set_top(array.get(lettersBoxClassic, 0), high)

if barstate.islast and classic == true and array.size(box.all) > 0
    miN = 1e8
    for i = 0 to array.size(box.all) - 1 by 1
        miN := math.min(miN, box.get_left(array.get(box.all, i)))
        miN
    for i = 0 to bar_index - miN by 1
        line.new(bar_index[i], high[i], bar_index[i], low[i], color = color.white)
        line.new(bar_index[i], close[i], bar_index[i], open[i], color = close[i] > open[i] ? color.green : color.red, width = 10)
    if array.size(label.all) > 0
        for i = 0 to array.size(label.all) - 1 by 1
            if label.get_x(array.get(label.all, i)) < miN
                label.set_color(array.get(label.all, i), na)
                label.set_textcolor(array.get(label.all, i), na)
