Daily Profile (Nephew_Sam_)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nephew_Sam_

//@version=5
indicator('Daily Profile (Nephew_Sam_)', overlay=true, max_bars_back=500, max_lines_count=500, max_boxes_count=500, max_labels_count=500)

// --------------- INPUTS ---------------
var GRP0 = "=============== INITIALIZE ==============="
hoursOffsetInput = input.string('-4', "Timezone offset (GMT)", group=GRP0, tooltip="Enter your time zone's offset (+ or -). Eg. +5 / -3 / +9:30")
newDayHr = input.int(17, title='Day starts at what hour?', minval=0, maxval=23, group=GRP0)
autoDay = input.bool(false, title='Ignore above hour and use exchange day start hour', group=GRP0, tooltip="This comes in handy when looking at different asset classes. Ie, for Forex the daily close is at 1700 (GMT-4), but for cryptos, some exhanges are at 0800 and some are at 0000")


var GRP1 = "=============== ASIA RANGE SETTINGS ==============="
asianUseRange = input.bool(true, title='Asia Range', inline='1', group=GRP1)
asianShowBg = input(title='Show Background ?', defval=true, inline='1', group=GRP1)
asianShowMid = input(title='Show Middle Line ?', defval=true, inline='2', group=GRP1)
asianExtendLines = input(title='Extend Lines ?', defval=false, inline='2', group=GRP1)
asianRangeTime = input.session(title='Session Time', defval='1700-0259', group=GRP1)
asianExtendTime = input.session(title='Extended Lines Time', defval='1700-1600', group=GRP1)
asianMaxRanges = input.int(2, title='Max Asian Ranges to show', minval=1, maxval=200, group=GRP1)


var GRP2 = "ASIA RANGE STYLES"
asianBorderWidth = input.int(1, 'Box Width', minval=1, maxval=4, inline="1", group=GRP2)
asianBoxBg = input(color.new(color.aqua, 90), 'BG Color', inline="1", group=GRP2)
asianBoxBorderColor = input(color.new(color.blue, 50), 'Border Color', inline="1", group=GRP2)
asianExtendStyle = input.string(title="Extend Style", defval="Dotted", options=["Solid", "Dashed", "Dotted"],inline="2", group=GRP2)
asianExtendLineWidth = input.int(1, 'Width', minval=1, maxval=4, inline="2", group=GRP2)
asianExtendLineColor = input(color.blue, 'Color', inline="2", group=GRP2)
asianMiddleLineWidth = input.int(1, 'Width', minval=1, maxval=4, inline="3", group=GRP2)
asianMiddleLineColor = input(color.red, 'Middle Line Color', inline="3", group=GRP2)


var GRP3 = "=============== OPEN LINES SETTINGS ==============="
showDailyOpen = input.bool(true, title='Daily Open', inline='1', group=GRP3)
dailyColor = input.color(color.red, title='', inline="1", group=GRP3)
dailyExtendHours = input.int(12, title='Line Extend hours', minval=1, maxval=48, inline="1", group=GRP3)

showMidnightOpen = input.bool(true, title='Midnight Open', inline="2", group=GRP3)
midnightColor = input.color(color.green, title='', inline="2", group=GRP3)
midnightExtendHours = input.int(12, title='Line Extend hours', minval=1, maxval=48, inline="2", group=GRP3)

showWeeklyOpen = input.bool(true, title='Weekly Open', inline="3", group=GRP3)
weeklyColor = input.color(color.green, title='', inline="3", group=GRP3)

showMonthlyOpen = input.bool(true, title='Monthly Open', inline="4", group=GRP3)
monthlyColor = input.color(color.green, title='', inline="4", group=GRP3)

var GRP4 = "OPEN LINES STYLES"
openWidth = input.int(1, title='Width', minval=1, maxval=4, inline='1', group=GRP4)
openMaxLines = input.int(3, title='Max Lines to show', minval=1, maxval=200, inline='1', group=GRP4)
openLabelSize = input.string("Small", title='Label Size', options=['Auto', 'Tiny', 'Small', 'Normal'], inline='2', group=GRP4)
openLineStyle = input.string(title="Extend Style", defval="Dashed", options=["Solid", "Dashed", "Dotted"], inline="2", group=GRP4)



var GRP5 = "=============== SESSIONS SETTINGS ==============="
session1Time = input.session(title='', defval='0200-0400', inline='1', group=GRP5)
session1Color = input.color(color.new(color.blue, 0), title='', inline='1', group=GRP5)
session1Show = input.bool(true, title='', inline='1', group=GRP5)

session2Time = input.session(title='', defval='0700-0900', inline='2', group=GRP5)
session2Color = input.color(color.new(color.red, 0), title='', inline='2', group=GRP5)
session2Show = input.bool(true, title='', inline='2', group=GRP5)

session3Time = input.session(title='', defval='1000-1100', inline='3', group=GRP5)
session3Color = input.color(color.new(color.lime, 0), title='', inline='3', group=GRP5)
session3Show = input.bool(true, title='', inline='3', group=GRP5)

session4Time = input.session(title='', defval='1900-2000', inline='4', group=GRP5)
session4Color = input.color(color.new(color.gray, 0), title='', inline='4', group=GRP5)
session4Show = input.bool(false, title='', inline='4', group=GRP5)

sessionsLinePosition = input.string(title='Dots Position', defval='Top', options=['Top', 'Bottom'], inline='5', group=GRP5)

showDailyDividers = input.bool(true, title='Show Dividers', inline='6', group=GRP5)
dividerLineTextColor = input(color.gray, 'Divider and Text Color', inline='6', group=GRP5)
dayLabelOffset = input.int(4, title='Label Offset', minval=1, maxval=48, inline="7", group=GRP5)



var GRP6 = "=============== PREVIOUS OHLC SETTINGS ==============="
line1 = input.bool(false, title="---------- DAILY ----------", group=GRP6)
showDaily = input.string(title='View', defval='HL', options=['Off', 'HL', 'OC', 'OHLC'], group=GRP6)
dailyHText = input.string(defval='PDH', title='H', inline="1", group=GRP6)
dailyLText = input.string(defval='PDL', title='L', inline="1", group=GRP6)
dailyOText = input.string(defval='PDO', title='O', inline="1", group=GRP6)
dailyCText = input.string(defval='PDC', title='C', inline="1", group=GRP6)


line2 = input.bool(false, title="---------- WEEKLY ----------", group=GRP6)
showWeekly = input.string(title='View', defval='Off', options=['Off', 'HL', 'OC', 'OHLC'], group=GRP6)
weeklyHText = input.string(defval='PWH', title='H', inline="2", group=GRP6)
weeklyLText = input.string(defval='PWL', title='L', inline="2", group=GRP6)
weeklyCText = input.string(defval='PWC', title='C', inline="2", group=GRP6)
weeklyOText = input.string(defval='PWO', title='O', inline="2", group=GRP6)

line3 = input.bool(false, title="---------- MONTHLY ----------", group=GRP6)
showMonthly = input.string(title='View', defval='Off', options=['Off', 'HL', 'OC', 'OHLC'], group=GRP6)
monthlyHText = input.string(defval='PMH', title='H', inline="3", group=GRP6)
monthlyLText = input.string(defval='PML', title='L', inline="3", group=GRP6)
monthlyCText = input.string(defval='PMC', title='C', inline="3", group=GRP6)
monthlyOText = input.string(defval='PMO', title='O', inline="3", group=GRP6)


var GRP7 = "---------- PREVIOUS OHLC STYLES ----------"
o_color = input.color(color.new(color.gray, 50), "Open Color", inline="1", group = GRP7)
c_color = input.color(color.new(color.gray, 50), "Close Color", inline="1", group = GRP7)
h_color = input.color(color.new(color.red, 50), "High Color", inline="2", group = GRP7)
l_color = input.color(color.new(color.lime, 50), "Low Color", inline="2", group = GRP7)
ohlcLabelPosition = input.string(title='Label Position', defval='End', options=['Start', 'End'], inline="2", group=GRP2)
ohlcLineStyle = input.string(title="Extend Style", defval="Solid", options=["Solid", "Dashed", "Dotted"], inline="2", group=GRP7)
ohlcOffset = input.int(1, title='Offset', minval=0, maxval=2, inline='3', group=GRP7)
ohlcMaxLines = input.int(3, title='Max Lines to show', minval=1, maxval=200, inline='3', group=GRP7)
ohlcExtendToCurrent = input.bool(true, title='Extend lines only to current candle', group=GRP7)
// --------------- INPUTS ---------------



// --------------- CONSTANTS ---------------
TIMEZONE = "GMT"+hoursOffsetInput
ISNEWDAY = dayofweek != dayofweek[1]
TRANSPARENT = color.new(color.white, 100)
exchangeOffset = hour(timenow, TIMEZONE) - hour(timenow, syminfo.timezone)
HOUR = hour + exchangeOffset <= 23 ? hour + exchangeOffset : (hour + exchangeOffset) - 24

// label.new(bar_index, high, str.tostring(HOUR))
// --------------- CONSTANTS ---------------



// --------------- FUNCTIONS ---------------
reso(exp, res) => request.security(syminfo.tickerid, res, exp, lookahead=barmerge.lookahead_on)

getLineStyle(_style) =>
    _linestyle = _style == "Solid" ? line.style_solid : _style == "Dashed" ? line.style_dashed : line.style_dotted
    _linestyle
    
getlabelSize(_labelSize) =>
    _size = _labelSize == "auto" ? size.auto : _labelSize == "Tiny" ? size.tiny : _labelSize == "Small" ? size.small : _labelSize == "Normal" ? size.normal : size.huge
    _size

addToArray(_array, _val) =>
    array.push(_array, _val)

resolutionInMinutes() =>
    resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 : timeframe.isminutes ? 1. : timeframe.isdaily ? 60. * 24 : timeframe.isweekly ? 60. * 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
    int(resInMinutes)

tfInMinutes(simple string tf = "") =>
    float chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)

inTimeframe(_t) => tfInMinutes(_t) > tfInMinutes(timeframe.period)

getOhlcData(_t, _var, _offset) =>
    o = reso(open[_offset], _t)
    h = reso(high[_offset], _t)
    l = reso(low[_offset], _t)
    c = reso(close[_offset], _t)
    show = _var != "Off" and inTimeframe(_t)
    _time = time(_t)
    newbar = na(_time[1]) or _time > _time[1]
    hl = _var == 'HL' or _var == 'OHLC'
    oc = _var == 'OC' or _var == 'OHLC'

    [o, h, l, c, show, newbar, hl, oc]


vline(_bar_index, _col, _style=line.style_dashed, _width=2) =>
    LineLengthMult = 10
    LineLength = ta.atr(100) * 100
    line.new(_bar_index, low-LineLength, _bar_index, high+LineLength, extend=extend.both, color=_col, style=_style, width=_width)

getOhlcLabelPos(_tf) =>
    ohlcLabelPosition == "End" and not ohlcExtendToCurrent ? time_close(_tf) : time

getLastElementInArray(_arr) =>
    _size = array.size(_arr)
    _el = _size > 0 ? array.get(_arr, _size-1) : na
    _el
// --------------- FUNCTIONS ---------------



// --------------- ASIAN RANGE ---------------
if asianUseRange
    inAsianSession = not na(time(timeframe.period, asianRangeTime, TIMEZONE))
    inAsianExtendSession = not na(time(timeframe.period, asianExtendTime, TIMEZONE))

    startTime = 0
    startTime := inAsianSession and not inAsianSession[1] ? time : startTime[1]

    extendStartTime = 0
    extendStartTime := inAsianExtendSession and not inAsianExtendSession[1] ? time : extendStartTime[1]
    
    var box[] asianBoxes = array.new_box(0)
    var line[] topLines = array.new_line(0)
    var line[] bottomLines = array.new_line(0)
    var line[] middleLines = array.new_line(0)

    var highVal = 0.0
    var lowVal = 0.0

    if inAsianSession and not inAsianSession[1]
        highVal := high
        lowVal := low

    if inAsianSession and timeframe.isintraday
        if inAsianSession[1]
            if array.size(asianBoxes) > 0
                box.delete(array.pop(asianBoxes))
            if array.size(topLines) > 0
                line.delete(array.pop(topLines))
            if array.size(bottomLines) > 0
                line.delete(array.pop(bottomLines))
            if array.size(middleLines) > 0
                line.delete(array.pop(middleLines))

        if low < lowVal
            lowVal := low
        if high > highVal
            highVal := high

        addToArray(asianBoxes, box.new(left=startTime,
             top=highVal,
             right=time,
             bottom=lowVal,
             xloc=xloc.bar_time,
             bgcolor=asianShowBg ? asianBoxBg : TRANSPARENT,
             border_width=asianBorderWidth,
             border_color=asianBoxBorderColor))

        if asianExtendLines
            addToArray(topLines, line.new(startTime, highVal, time, highVal, xloc=xloc.bar_time, color=asianExtendLineColor, style=getLineStyle(asianExtendStyle), width=asianExtendLineWidth))
            addToArray(bottomLines, line.new(startTime, lowVal, time, lowVal, xloc=xloc.bar_time, color=asianExtendLineColor, style=getLineStyle(asianExtendStyle), width=asianExtendLineWidth))

        if asianShowMid
            midVal = (highVal + lowVal) / 2
            addToArray(middleLines, line.new(startTime, midVal, time, midVal, xloc=xloc.bar_time, color=asianMiddleLineColor, style=line.style_dotted, width=asianMiddleLineWidth))

    if asianExtendLines and inAsianExtendSession and not inAsianSession
        line.delete(array.pop(topLines))
        line.delete(array.pop(bottomLines))
        line.delete(array.pop(middleLines))
        addToArray(topLines, line.new(startTime, highVal, time, highVal, xloc=xloc.bar_time, color=asianExtendLineColor, style=getLineStyle(asianExtendStyle), width=asianExtendLineWidth))
        addToArray(bottomLines, line.new(startTime, lowVal, time, lowVal, xloc=xloc.bar_time, color=asianExtendLineColor, style=getLineStyle(asianExtendStyle), width=asianExtendLineWidth))
        if asianShowMid
            midVal = (highVal + lowVal) / 2
            addToArray(middleLines, line.new(startTime, midVal, time, midVal, xloc=xloc.bar_time, color=asianMiddleLineColor, style=line.style_dotted, width=asianMiddleLineWidth))

    // Delete lines if more than max
    if array.size(asianBoxes) > asianMaxRanges
        box.delete(array.shift(asianBoxes))
    if array.size(topLines) > asianMaxRanges
        line.delete(array.shift(topLines))
    if array.size(bottomLines) > asianMaxRanges
        line.delete(array.shift(bottomLines))
    if array.size(middleLines) > asianMaxRanges
        line.delete(array.shift(middleLines))

// --------------- ASIAN RANGE ---------------



// --------------- TIME LINES ---------------
var line[] dailyLines = array.new_line(0)
var line[] midnightLines = array.new_line(0)
var line[] weeklyLines = array.new_line(0)
var line[] monthlyLines = array.new_line(0)

var label[] dailyLabels = array.new_label(0)
var label[] midnightLabels = array.new_label(0)
var label[] weeklyLabels = array.new_label(0)
var label[] monthlyLabels = array.new_label(0)

inDaily = HOUR == 17 and minute == 0
inMidnight = HOUR == 0 and minute == 0
inWeek = weekofyear != weekofyear[1]
inMonth = month != month[1]

[ohlc_w_o, ohlc_w_h, ohlc_w_l, ohlc_w_c, ohlc_w_show, ohlc_w_newbar, ohlc_w_hl, ohlc_w_oc] = getOhlcData("W", showWeeklyOpen ? "OC" : "Off", 0)
[ohlc_m_o, ohlc_m_h, ohlc_m_l, ohlc_m_c, ohlc_m_show, ohlc_m_newbar, ohlc_m_hl, ohlc_m_oc] = getOhlcData("M", showMonthlyOpen ? "OC" : "Off", 0)

LABEL_SPACE = "                       "

if inDaily and not inDaily[1] and showDailyOpen
    _extend = dailyExtendHours * 3600000
    array.push(dailyLines, line.new(time, open, time + _extend, open, xloc=xloc.bar_time, color=dailyColor, style=getLineStyle(openLineStyle), width=openWidth))
    array.push(dailyLabels, label.new(time + _extend, open, "Daily Open" + LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none, size=getlabelSize(openLabelSize), textcolor=dailyColor))

if inMidnight and not inMidnight[1] and showMidnightOpen
    _extend = midnightExtendHours * 3600000
    array.push(midnightLines, line.new(time, open, time + _extend, open, xloc=xloc.bar_time, color=midnightColor, style=getLineStyle(openLineStyle), width=openWidth))
    array.push(midnightLabels, label.new(time + _extend, open, "Midnight Open" + LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none, size=getlabelSize(openLabelSize), textcolor=midnightColor))

if ohlc_w_show and ohlc_w_newbar
    array.push(weeklyLines, line.new(time, ohlc_w_o, time, ohlc_w_o, xloc=xloc.bar_time, color=weeklyColor, style=getLineStyle(openLineStyle), width=openWidth))
    array.push(weeklyLabels, label.new(time, ohlc_w_o, "Weekly Open" + LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none, size=getlabelSize(openLabelSize), textcolor=weeklyColor))

if ohlc_m_show and ohlc_m_newbar
    array.push(monthlyLines, line.new(time, open, time, open, xloc=xloc.bar_time, color=monthlyColor, style=getLineStyle(openLineStyle), width=openWidth))
    array.push(monthlyLabels, label.new(time, open, "Monthly Open" + LABEL_SPACE, xloc=xloc.bar_time, style=label.style_none, size=getlabelSize(openLabelSize), textcolor=monthlyColor))
    

// EXTEND
if ohlc_w_show and not ohlc_w_newbar
    _line = getLastElementInArray(weeklyLines)
    _label = getLastElementInArray(weeklyLabels)
    if not na(_line)
        line.set_x2(_line, time)
    if not na(_label)
        label.set_x(_label, time)

if ohlc_m_show and not ohlc_m_newbar
    _line = getLastElementInArray(monthlyLines)
    _label = getLastElementInArray(monthlyLabels)
    if not na(_line)
        line.set_x2(_line, time)
    if not na(_label)
        label.set_x(_label, time)



// DELETE OLD
if array.size(dailyLines) > openMaxLines
    line.delete(array.shift(dailyLines))
if array.size(dailyLabels) > openMaxLines
    label.delete(array.shift(dailyLabels))
    
if array.size(midnightLines) > openMaxLines
    line.delete(array.shift(midnightLines))
if array.size(midnightLabels) > openMaxLines
    label.delete(array.shift(midnightLabels))
    
if array.size(weeklyLines) > openMaxLines
    line.delete(array.shift(weeklyLines))
if array.size(weeklyLabels) > openMaxLines
    label.delete(array.shift(weeklyLabels))
    
if array.size(monthlyLines) > openMaxLines
    line.delete(array.shift(monthlyLines))
if array.size(monthlyLabels) > openMaxLines
    label.delete(array.shift(monthlyLabels))
// --------------- TIME LINES ---------------



// --------------- SESSIONS ---------------
sess1 = session1Show ? time(timeframe.period, session1Time, TIMEZONE) : na
sess2 = session2Show ? time(timeframe.period, session2Time, TIMEZONE) : na
sess3 = session3Show ? time(timeframe.period, session3Time, TIMEZONE) : na
sess4 = session4Show ? time(timeframe.period, session4Time, TIMEZONE) : na

_linPos = sessionsLinePosition == 'Top' ? location.top : location.bottom
plotshape(not na(sess1) and timeframe.isintraday, title='Session 1', style=shape.square, location=_linPos, color=session1Color, size=size.auto)
plotshape(not na(sess2) and timeframe.isintraday, title='Session 2', style=shape.square, location=_linPos, color=session2Color, size=size.auto)
plotshape(not na(sess3) and timeframe.isintraday, title='Session 3', style=shape.square, location=_linPos, color=session3Color, size=size.auto)
plotshape(not na(sess4) and timeframe.isintraday, title='Session 4', style=shape.square, location=_linPos, color=session4Color, size=size.auto)


// Divider
tickerExchangeOffset = 5
int dayLabelStartTime = 1

_rin = resolutionInMinutes()

if syminfo.timezone == 'Etc/UTC'
    dayLabelStartTime += tickerExchangeOffset
    dayLabelStartTime

isStartTime = autoDay ? ISNEWDAY : HOUR == newDayHr and HOUR[1] != newDayHr //and minute == 0 
_inTimeframe = _rin <= 180 and timeframe.isintraday

// if barstate.islastHOUR
//     label.new(bar_index, high, str.tostring(hour) + "\n" + str.tostring(exchangeOffset))

txtMonH = "Monday"
txtTueH = "Tuesday"
txtWedH = "Wednesday"
txtThuH = "Thursday"
txtFriH = "Friday"
txtSatH = "Saturday"
txtSunH = "Sunday"

txtMonV = "M\no\nn\nd\na\ny"
txtTueV = "T\nu\ne\ns\nd\na\ny"
txtWedV = "W\ne\nd\nn\ne\ns\nd\na\ny"
txtThuV = "T\nh\nu\nr\ns\nd\na\ny"
txtFriV = "F\nr\ni\nd\na\ny"
txtSatV = "S\na\nt\nu\nr\nd\na\ny"
txtSunV = "S\nu\nn\nd\na\ny"

isMon() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.monday : false
isTue() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.tuesday : false
isWed() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.wednesday : false
isThu() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.thursday : false
isFri() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.friday : false
isSat() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.saturday : false
isSun() => _inTimeframe and showDailyDividers ? HOUR == dayLabelStartTime and minute == 0 and dayofweek == dayofweek.sunday : false

if isStartTime and showDailyDividers
    vline(bar_index, dividerLineTextColor, line.style_dashed, 1)

_offset = _rin <= 7 ? math.round(dayLabelOffset * 12) : _rin <= 14 ? math.round(dayLabelOffset * 6) : _rin <= 20 ? math.round(dayLabelOffset * 4) : _rin <= 30 ? math.round(dayLabelOffset * 2) : _rin <= 60 ? math.round(dayLabelOffset * 1) : math.round(dayLabelOffset * 0.2)

plotshape(isMon(), text=txtMonH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isTue(), text=txtTueH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isWed(), text=txtWedH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isThu(), text=txtThuH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isFri(), text=txtFriH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isSat(), text=txtSatH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
plotshape(isSun(), text=txtSunH, color=TRANSPARENT, offset=_offset, style=shape.circle, location=location.bottom, textcolor=dividerLineTextColor, size=size.tiny, editable=false)
// --------------- SESSIONS ---------------



// --------------- PREVIOUS OHLC ---------------
// Daily
var line[] dailyOpenLines = array.new_line(0)
var line[] dailyCloseLines = array.new_line(0)
var line[] dailyHighLines = array.new_line(0)
var line[] dailyLowLines = array.new_line(0)

var label[] dailyOpenLabels = array.new_label(0)
var label[] dailyCloseLabels = array.new_label(0)
var label[] dailyHighLabels = array.new_label(0)
var label[] dailyLowLabels = array.new_label(0)

[d_o, d_h, d_l, d_c, d_show, d_newbar, d_hl, d_oc] = getOhlcData("D", showDaily, ohlcOffset)

if d_show and d_newbar
    _timeTo = ohlcExtendToCurrent ? time : time_close("D")
    if d_oc
        addToArray(dailyOpenLines, line.new(x1=time, y1=d_o, x2=_timeTo, y2=d_o, xloc=xloc.bar_time, style=line.style_solid, color=o_color))
        addToArray(dailyCloseLines, line.new(x1=time, y1=d_c, x2=_timeTo, y2=d_c, xloc=xloc.bar_time, style=line.style_solid, color=c_color))
        addToArray(dailyOpenLabels, label.new(x=getOhlcLabelPos("D"), y= d_o, xloc=xloc.bar_time, text=dailyOText + "     ", textcolor=o_color, style=label.style_none))
        addToArray(dailyCloseLabels, label.new(x=getOhlcLabelPos("D"), y= d_c, xloc=xloc.bar_time, text=dailyCText + "     ", textcolor=c_color, style=label.style_none))
    if d_hl
        addToArray(dailyHighLines, line.new(x1=time, y1=d_h, x2=_timeTo, y2=d_h, xloc=xloc.bar_time, style=line.style_solid, color=h_color))  
        addToArray(dailyLowLines, line.new(x1=time, y1=d_l, x2=_timeTo, y2=d_l, xloc=xloc.bar_time, style=line.style_solid, color=l_color))
        addToArray(dailyHighLabels, label.new(x=getOhlcLabelPos("D"), y= d_h, xloc=xloc.bar_time, text=dailyHText + "     ", textcolor=h_color, style=label.style_none))  
        addToArray(dailyLowLabels, label.new(x=getOhlcLabelPos("D"), y= d_l, xloc=xloc.bar_time, text=dailyLText + "     ", textcolor=l_color, style=label.style_none))

// Weekly
var line[] weeklyOpenLines = array.new_line(0)
var line[] weeklyCloseLines = array.new_line(0)
var line[] weeklyHighLines = array.new_line(0)
var line[] weeklyLowLines = array.new_line(0)

var label[] weeklyOpenLabels = array.new_label(0)
var label[] weeklyCloseLabels = array.new_label(0)
var label[] weeklyHighLabels = array.new_label(0)
var label[] weeklyLowLabels = array.new_label(0)
[w_o, w_h, w_l, w_c, w_show, w_newbar, w_hl, w_oc] = getOhlcData("W", showWeekly, ohlcOffset)

if w_newbar and w_show
    _timeTo = ohlcExtendToCurrent ? time : time_close("W")
    if w_oc
        addToArray(weeklyOpenLines, line.new(x1=time, y1=w_o, x2=_timeTo, y2=w_o, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=o_color))
        addToArray(weeklyCloseLines, line.new(x1=time, y1=w_c, x2=_timeTo, y2=w_c, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=c_color))
        addToArray(weeklyOpenLabels, label.new(x=getOhlcLabelPos("W"), y= w_o, xloc=xloc.bar_time, text=weeklyOText + "     ", textcolor=o_color, style=label.style_none))
        addToArray(weeklyCloseLabels, label.new(x=getOhlcLabelPos("W"), y= w_c, xloc=xloc.bar_time, text=weeklyCText + "     ", textcolor=c_color, style=label.style_none))
    if w_hl
        addToArray(weeklyHighLines, line.new(x1=time, y1=w_h, x2=_timeTo, y2=w_h, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=h_color))  
        addToArray(weeklyLowLines, line.new(x1=time, y1=w_l, x2=_timeTo, y2=w_l, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=l_color))
        addToArray(weeklyHighLabels, label.new(x=getOhlcLabelPos("W"), y= w_h, xloc=xloc.bar_time, text=weeklyHText + "     ", textcolor=h_color, style=label.style_none))  
        addToArray(weeklyLowLabels, label.new(x=getOhlcLabelPos("W"), y= w_l, xloc=xloc.bar_time, text=weeklyLText + "     ", textcolor=l_color, style=label.style_none))


// Weekly
var line[] monthlyOpenLines = array.new_line(0)
var line[] monthlyCloseLines = array.new_line(0)
var line[] monthlyHighLines = array.new_line(0)
var line[] monthlyLowLines = array.new_line(0)

var label[] monthlyOpenLabels = array.new_label(0)
var label[] monthlyCloseLabels = array.new_label(0)
var label[] monthlyHighLabels = array.new_label(0)
var label[] monthlyLowLabels = array.new_label(0)
[m_o, m_h, m_l, m_c, m_show, m_newbar, m_hl, m_oc] = getOhlcData("M", showMonthly, ohlcOffset)

if m_newbar and m_show
    _timeTo = ohlcExtendToCurrent ? time : time_close("M")
    if m_oc
        addToArray(monthlyOpenLines, line.new(x1=time, y1=m_o, x2=_timeTo, y2=m_o, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=o_color))
        addToArray(monthlyCloseLines, line.new(x1=time, y1=m_c, x2=_timeTo, y2=m_c, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=c_color))
        addToArray(monthlyOpenLabels, label.new(x=getOhlcLabelPos("M"), y= m_o, xloc=xloc.bar_time, text=monthlyOText + "     ", textcolor=o_color, style=label.style_none))
        addToArray(monthlyCloseLabels, label.new(x=getOhlcLabelPos("M"), y= m_c, xloc=xloc.bar_time, text=monthlyCText + "     ", textcolor=c_color, style=label.style_none))
    if m_hl
        addToArray(monthlyHighLines, line.new(x1=time, y1=m_h, x2=_timeTo, y2=m_h, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=h_color))  
        addToArray(monthlyLowLines, line.new(x1=time, y1=m_l, x2=_timeTo, y2=m_l, xloc=xloc.bar_time, style=getLineStyle(ohlcLineStyle), color=l_color))
        addToArray(monthlyHighLabels, label.new(x=getOhlcLabelPos("M"), y= m_h, xloc=xloc.bar_time, text=monthlyHText + "     ", textcolor=h_color, style=label.style_none))  
        addToArray(monthlyLowLabels, label.new(x=getOhlcLabelPos("M"), y= m_l, xloc=xloc.bar_time, text=monthlyLText + "     ", textcolor=l_color, style=label.style_none))


// EXTEND LINES
if d_show and not d_newbar and ohlcExtendToCurrent
    _lineO = getLastElementInArray(dailyOpenLines)
    _lineH = getLastElementInArray(dailyHighLines)
    _lineL = getLastElementInArray(dailyLowLines)
    _lineC = getLastElementInArray(dailyCloseLines)

    _labelO = getLastElementInArray(dailyOpenLabels)
    _labelH = getLastElementInArray(dailyHighLabels)
    _labelL = getLastElementInArray(dailyLowLabels)
    _labelC = getLastElementInArray(dailyCloseLabels)
    
    if not na(_lineO)
        line.set_x2(_lineO, time)
    if not na(_labelO)
        label.set_x(_labelO, time)
    if not na(_lineH)
        line.set_x2(_lineH, time)
    if not na(_labelH)
        label.set_x(_labelH, time)
    if not na(_lineL)
        line.set_x2(_lineL, time)
    if not na(_labelL)
        label.set_x(_labelL, time)
    if not na(_lineC)
        line.set_x2(_lineC, time)
    if not na(_labelC)
        label.set_x(_labelC, time)


if w_show and not w_newbar and ohlcExtendToCurrent
    _lineO = getLastElementInArray(weeklyOpenLines)
    _lineH = getLastElementInArray(weeklyHighLines)
    _lineL = getLastElementInArray(weeklyLowLines)
    _lineC = getLastElementInArray(weeklyCloseLines)

    _labelO = getLastElementInArray(weeklyOpenLabels)
    _labelH = getLastElementInArray(weeklyHighLabels)
    _labelL = getLastElementInArray(weeklyLowLabels)
    _labelC = getLastElementInArray(weeklyCloseLabels)
    
    if not na(_lineO)
        line.set_x2(_lineO, time)
    if not na(_labelO)
        label.set_x(_labelO, time)
    if not na(_lineH)
        line.set_x2(_lineH, time)
    if not na(_labelH)
        label.set_x(_labelH, time)
    if not na(_lineL)
        line.set_x2(_lineL, time)
    if not na(_labelL)
        label.set_x(_labelL, time)
    if not na(_lineC)
        line.set_x2(_lineC, time)
    if not na(_labelC)
        label.set_x(_labelC, time)


if m_show and not m_newbar and ohlcExtendToCurrent
    _lineO = getLastElementInArray(monthlyOpenLines)
    _lineH = getLastElementInArray(monthlyHighLines)
    _lineL = getLastElementInArray(monthlyLowLines)
    _lineC = getLastElementInArray(monthlyCloseLines)

    _labelO = getLastElementInArray(monthlyOpenLabels)
    _labelH = getLastElementInArray(monthlyHighLabels)
    _labelL = getLastElementInArray(monthlyLowLabels)
    _labelC = getLastElementInArray(monthlyCloseLabels)
    
    if not na(_lineO)
        line.set_x2(_lineO, time)
    if not na(_labelO)
        label.set_x(_labelO, time)
    if not na(_lineH)
        line.set_x2(_lineH, time)
    if not na(_labelH)
        label.set_x(_labelH, time)
    if not na(_lineL)
        line.set_x2(_lineL, time)
    if not na(_labelL)
        label.set_x(_labelL, time)
    if not na(_lineC)
        line.set_x2(_lineC, time)
    if not na(_labelC)
        label.set_x(_labelC, time)


// DELETE OLD LINES
// Daily
if array.size(dailyOpenLines) > ohlcMaxLines
    line.delete(array.shift(dailyOpenLines))
if array.size(dailyOpenLabels) > ohlcMaxLines
    label.delete(array.shift(dailyOpenLabels))
if array.size(dailyCloseLines) > ohlcMaxLines
    line.delete(array.shift(dailyCloseLines))
if array.size(dailyCloseLabels) > ohlcMaxLines
    label.delete(array.shift(dailyCloseLabels))
if array.size(dailyHighLines) > ohlcMaxLines
    line.delete(array.shift(dailyHighLines))
if array.size(dailyHighLabels) > ohlcMaxLines
    label.delete(array.shift(dailyHighLabels))
if array.size(dailyLowLines) > ohlcMaxLines
    line.delete(array.shift(dailyLowLines))
if array.size(dailyLowLabels) > ohlcMaxLines
    label.delete(array.shift(dailyLowLabels))

// Weekly
if array.size(weeklyOpenLines) > ohlcMaxLines
    line.delete(array.shift(weeklyOpenLines))
if array.size(weeklyOpenLabels) > ohlcMaxLines
    label.delete(array.shift(weeklyOpenLabels))
if array.size(weeklyCloseLines) > ohlcMaxLines
    line.delete(array.shift(weeklyCloseLines))
if array.size(weeklyCloseLabels) > ohlcMaxLines
    label.delete(array.shift(weeklyCloseLabels))
if array.size(weeklyHighLines) > ohlcMaxLines
    line.delete(array.shift(weeklyHighLines))
if array.size(weeklyHighLabels) > ohlcMaxLines
    label.delete(array.shift(weeklyHighLabels))
if array.size(weeklyLowLines) > ohlcMaxLines
    line.delete(array.shift(weeklyLowLines))
if array.size(weeklyLowLabels) > ohlcMaxLines
    label.delete(array.shift(weeklyLowLabels))

// Monthly
if array.size(monthlyOpenLines) > ohlcMaxLines
    line.delete(array.shift(monthlyOpenLines))
if array.size(monthlyOpenLabels) > ohlcMaxLines
    label.delete(array.shift(monthlyOpenLabels))
if array.size(monthlyCloseLines) > ohlcMaxLines
    line.delete(array.shift(monthlyCloseLines))
if array.size(monthlyCloseLabels) > ohlcMaxLines
    label.delete(array.shift(monthlyCloseLabels))
if array.size(monthlyHighLines) > ohlcMaxLines
    line.delete(array.shift(monthlyHighLines))
if array.size(monthlyHighLabels) > ohlcMaxLines
    label.delete(array.shift(monthlyHighLabels))
if array.size(monthlyLowLines) > ohlcMaxLines
    line.delete(array.shift(monthlyLowLines))
if array.size(monthlyLowLabels) > ohlcMaxLines
    label.delete(array.shift(monthlyLowLabels))
// --------------- PREVIOUS OHLC ---------------