[ENT] Indicators
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Edgar Ovsepyan/Lancelot6303/ENT

//@version=5
indicator('[ENT] Indicators', overlay=true)
var grp23 = 'NewYorkMidnight по NY GTM -4. Стандарт 00:00'
//NYM//
sess2Show = input.bool(true, title='', inline='2', group=grp23)
sess2Hour = input.int(0, title='', minval=0, maxval=0, inline='2', group=grp23)
sess2Minute = input.int(0, title='', minval=0, maxval=0, inline='2', group=grp23)
sess2Text = input.string("NYM                    ", title='', inline='2', group=grp23)
sess2Col = input.color(color.rgb(81, 19, 138), title='', inline="2", group=grp23)
inSession2 = hour == sess2Hour and minute == sess2Minute and second == 0



var grp24 = 'Настройки NYM'
MAX_LINES = input.int(1, title='История открытий', minval=1, maxval=1, group=grp24)
extendHours = input.int(16, title='Длина линии в часах', minval=16, maxval=16, group=grp24)
linesWidth = input.int(2, title='Ширина линии', minval=1, maxval=2, group=grp24)
_labelSize = input.string("Normal", title='Размер текста', options=['Small', 'Normal'], group=grp24)
labelSize = _labelSize == "Small" ? size.small : _labelSize == "Normal" ? size.normal : size.huge


var line[] l1 = array.new_line(0)
var line[] l2 = array.new_line(0)
var line[] l3 = array.new_line(0)

var label[] la1 = array.new_label(0)
var label[] la2 = array.new_label(0)
var label[] la3 = array.new_label(0)

_extend = extendHours * 3600000
    
if inSession2 and not inSession2[1] and sess2Show
    array.push(l2, line.new(time, open, time + _extend, open, xloc=xloc.bar_time, color=sess2Col, width=linesWidth))
    array.push(la2, label.new(time + _extend, open, sess2Text, xloc=xloc.bar_time, style=label.style_none, size=labelSize, textcolor=sess2Col))
    


// Delete lines if more than max
if array.size(l1) > MAX_LINES
    line.delete(array.shift(l1))
    label.delete(array.shift(la1))
if array.size(l2) > MAX_LINES
    line.delete(array.shift(l2))
    label.delete(array.shift(la2))
if array.size(l3) > MAX_LINES
    line.delete(array.shift(l3))
    label.delete(array.shift(la3))

//DO//
daily_time  = request.security(syminfo.tickerid, 'D', time,  lookahead=barmerge.lookahead_on)
daily_open  = request.security(syminfo.tickerid, 'D', open,  lookahead=barmerge.lookahead_on)
daily_close = request.security(syminfo.tickerid, 'D', close, lookahead=barmerge.lookahead_on)

var dailyopen    = input(defval=true, title='Daily Open')
var extend_right = 20
var show_open    = dailyopen and timeframe.isintraday and timeframe.multiplier <= 60


DOcolor        = close >= daily_open
daily_color    = DOcolor ? color.rgb(0, 0, 0, 53) : color.rgb(0, 0, 0, 53)
rightbar(bars) =>
    time_close + (time - time[1]) * bars

if barstate.islast
    if show_open and DOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=daily_time, x2=rightbar(extend_right), y1=daily_open, y2=daily_open, color = daily_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar(extend_right), y=daily_open, text='Open ' + str.tostring(daily_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, daily_time)
        line.set_x2(open_line, rightbar(extend_right))
        line.set_y1(open_line, daily_open)
        line.set_y2(open_line, daily_open)
        label.set_x(open_label, rightbar(extend_right))
        label.set_y(open_label, daily_open)
        
    if show_open and not DOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=daily_time, x2=rightbar(extend_right), y1=daily_open, y2=daily_open, color = daily_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar(extend_right), y=daily_open, text='Open ' + str.tostring(daily_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, daily_time)
        line.set_x2(open_line, rightbar(extend_right))
        line.set_y1(open_line, daily_open)
        line.set_y2(open_line, daily_open)
        label.set_x(open_label, rightbar(extend_right))
        label.set_y(open_label, daily_open)

//weeklyOpen
weekly_time  = request.security(syminfo.tickerid, 'W', time,  lookahead=barmerge.lookahead_on)
weekly_open  = request.security(syminfo.tickerid, 'W', open,  lookahead=barmerge.lookahead_on)
weekly_close = request.security(syminfo.tickerid, 'W', close, lookahead=barmerge.lookahead_on)

var weeklyopen    = input(defval=true, title='Weekly Open')
var extend_right1 = 20
var showweekly_open    = weeklyopen and timeframe.isintraday and timeframe.multiplier <= 60


WOcolor        = close >= weekly_open
weekly_color    = WOcolor ? color.rgb(0, 0, 0, 53) : color.rgb(0, 0, 0, 53)
rightbar1(bars) =>
    time_close + (time - time[1]) * bars

if barstate.islast
    if showweekly_open and WOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=weekly_time, x2=rightbar1(extend_right1), y1=weekly_open, y2=weekly_open, color = weekly_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar1(extend_right1), y=weekly_open, text='Weekly Open ' + str.tostring(weekly_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, weekly_time)
        line.set_x2(open_line, rightbar1(extend_right1))
        line.set_y1(open_line, weekly_open)
        line.set_y2(open_line, weekly_open)
        label.set_x(open_label, rightbar1(extend_right1))
        label.set_y(open_label, weekly_open)
        
    if showweekly_open and not WOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=weekly_time, x2=rightbar1(extend_right1), y1=weekly_open, y2=weekly_open, color = weekly_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar1(extend_right1), y=weekly_open, text='Weekly Open ' + str.tostring(weekly_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, weekly_time)
        line.set_x2(open_line, rightbar1(extend_right1))
        line.set_y1(open_line, weekly_open)
        line.set_y2(open_line, weekly_open)
        label.set_x(open_label, rightbar1(extend_right1))
        label.set_y(open_label, weekly_open)

//MonthlyOpen
monthly_time  = request.security(syminfo.tickerid, 'M', time,  lookahead=barmerge.lookahead_on)
monthly_open  = request.security(syminfo.tickerid, 'M', open,  lookahead=barmerge.lookahead_on)
monthly_close = request.security(syminfo.tickerid, 'M', close, lookahead=barmerge.lookahead_on)

var monthlyopen    = input(defval=true, title='Monthly Open')
var extend_right2 = 20
var showmonthly_open    = monthlyopen and timeframe.isintraday and timeframe.multiplier <= 60


MOcolor        = close >= monthly_open
monthly_color    = MOcolor ? color.rgb(0, 0, 0, 53) : color.rgb(0, 0, 0, 53)
rightbar2(bars) =>
    time_close + (time - time[1]) * bars

if barstate.islast
    if showmonthly_open and MOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=monthly_time, x2=rightbar2(extend_right2), y1=monthly_open, y2=monthly_open, color = monthly_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar2(extend_right2), y=monthly_open, text='Monthly Open ' + str.tostring(monthly_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, monthly_time)
        line.set_x2(open_line, rightbar2(extend_right2))
        line.set_y1(open_line, monthly_open)
        line.set_y2(open_line, monthly_open)
        label.set_x(open_label, rightbar2(extend_right2))
        label.set_y(open_label, monthly_open)
        
    if showmonthly_open and not MOcolor and not barstate.isconfirmed and barstate.isrealtime
        var open_line  = line.new(x1=monthly_time, x2=rightbar2(extend_right2), y1=monthly_open, y2=monthly_open, color = monthly_color, width=1, xloc = xloc.bar_time)
        var open_label = label.new(x=rightbar2(extend_right2), y=monthly_open, text='Monthly Open ' + str.tostring(monthly_open), style = label.style_none, textcolor = #ff0000, size = size.small, xloc = xloc.bar_time)
        line.set_x1(open_line, monthly_time)
        line.set_x2(open_line, rightbar2(extend_right2))
        line.set_y1(open_line, monthly_open)
        line.set_y2(open_line, monthly_open)
        label.set_x(open_label, rightbar2(extend_right2))
        label.set_y(open_label, monthly_open)

//PDH&PDL

disp_dp = input(true, title='PDH/PDL')

firstbar(resolution) =>
    ch = 0
    if resolution == 'Y'
        t = year(time('D'))
        ch := ta.change(t) != 0 ? 1 : 0
        ch
    else
        t = time(resolution)
        ch := ta.change(t) != 0 ? 1 : 0
        ch
    ch

pp_res = 'D'
bars_sinse = 0
bars_sinse := firstbar(pp_res) ? 0 : bars_sinse[1] + 1

phigh_d = request.security(syminfo.tickerid, 'D', high[1], barmerge.gaps_off, barmerge.lookahead_on)
plow_d = request.security(syminfo.tickerid, 'D', low[1], barmerge.gaps_off, barmerge.lookahead_on)

y_th = line.new(bar_index[math.min(bars_sinse, 300)], disp_dp ? phigh_d : na, bar_index, disp_dp ? phigh_d : na, color=color.rgb(0, 0, 0, 56), style=line.style_solid, width=1, extend=extend.none)

y_tl = line.new(bar_index[math.min(bars_sinse, 300)], disp_dp ? plow_d : na, bar_index, disp_dp ? plow_d : na, color=color.rgb(0, 0, 0, 60), style=line.style_solid, width=1, extend=extend.none)

line.delete(y_th[1])

line.delete(y_tl[1])

label_yh = label.new(bar_index, disp_dp ? phigh_d : na, text='PDH', textcolor = color.rgb(0, 0, 0, 52), style=label.style_none)

label_yl = label.new(bar_index, disp_dp ? plow_d : na, text='PDL', textcolor = #00000063,style=label.style_none)

label.delete(label_yh[1])

label.delete(label_yl[1])

// -------------------- INPUTS --------------------

var GRP0 = "=============== Часовой пояс ==============="
hoursOffsetInput = input.string(title='Часовой пояс NY', defval='-4', options=['-4'], group=GRP0)
newDayHr = input.int(17, title='Начало дня по NY 17 не менять!', minval=0, maxval=23, group=GRP0)

// --- 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
// --- CONSTANTS ---

var grp1 = '-------------------- Сессии - выставить время по NY --------------'
session1Text = input.string('London Open', title='', inline='1', group=grp1)
session1Time = input.session(title='', defval='0400-0700', inline='1', group=grp1)
session1Color = input.color(color.new(color.blue, 0), title='', inline='1', group=grp1)
session1Show = input.bool(true, title='', inline='1', group=grp1)

session2Text = input.string('NY Open', title='', inline='2', group=grp1)
session2Time = input.session(title='', defval='0900-1100', inline='2', group=grp1)
session2Color = input.color(color.new(color.red, 0), title='', inline='2', group=grp1)
session2Show = input.bool(true, title='', inline='2', group=grp1)

session3Text = input.string('London Close', title='', inline='3', group=grp1)
session3Time = input.session(title='', defval='1100-1300', inline='3', group=grp1)
session3Color = input.color(color.new(color.lime, 0), title='', inline='3', group=grp1)
session3Show = input.bool(true, title='', inline='3', group=grp1)

session4Text = input.string('Tokyo', title='', inline='4', group=grp1)
session4Time = input.session(title='', defval='1900-0300', inline='4', group=grp1)
session4Color = input.color(color.new(#d89b19, 0), title='', inline='4', group=grp1)
session4Show = input.bool(true, title='', inline='4', group=grp1)

bgType = input.string(title='Type', defval='Line', options=['Line'], inline='5', group=grp1)
linePosition = input.string(title='Lines Position', defval='Bottom', options=['Top', 'Bottom'], inline='5', group=grp1)


var grp2 = "-------------------- Табло сессий --------------------"
showTable = input.bool(true, title='Показать табло', inline='1', group=grp2)
showNextSession = input.bool(true, title='Показать следующую сессию', inline='1', group=grp2)
theme = input.string(title='Цвет темы табло', defval='Light', options=['Dark', 'Light'], inline='2', group = grp2)
position = input.string(title='Позиция табло', defval='Top Right', options=['Top Right'], inline='2', group = grp2)
alignment = input.string(title='Позиция сессий', defval='Horizontal', options=['Vertical', 'Horizontal'], inline='3', group = grp2)
noSessionText = input.string('Нет сессий', title='Нет сессий', inline='3', group=grp2)

var grp3 = "-------------------- Daily Dividers --------------------"
showDailyDividers = input(title='Show Daily Dividers ?', defval=true, inline='1', group=grp3)
showDOW = input(true, title='Show days of week', inline='1', group=grp3)
divider_text_color = input(color.blue, 'Divider and Text Color', group=grp3)
dayLabelOffset = input.int(4, title='Days label offset', minval=1, maxval=15, group=grp3)
// -------------------- INPUTS --------------------


// -------------------- FUNCTIONS --------------------
_color1 = theme == "Dark" ? color.white : color.black

vline(BarIndex, Color, LineStyle, LineWidth) =>
    LineLengthMult = 10
    LineLength = ta.atr(100) * 100
    line.new(BarIndex, low-LineLength, BarIndex, high+LineLength, extend=extend.both, color=Color, style=LineStyle, width=LineWidth)
    // return_1 = line.new(BarIndex, low - ta.tr, BarIndex, high + ta.tr, xloc.bar_index, extend=extend.both, color=Color, style=LineStyle, width=LineWidth)
    // return_1

timeTillPeriodEnd(sess, _type = 1) =>
    sessionEnd = array.get(str.split(sess, '-'), _type)
    int endHour = math.round(str.tonumber(str.substring(sessionEnd, 0, 2)))
    int endMinute = math.round(str.tonumber(str.substring(sessionEnd, 2, 4)))
    tsNow = timestamp(TIMEZONE, year(time, TIMEZONE), month(time, TIMEZONE), dayofmonth(time, TIMEZONE), hour(time, TIMEZONE), minute(time, TIMEZONE))
    tsEnd = timestamp(TIMEZONE, year(time, TIMEZONE), month(time, TIMEZONE), dayofmonth(time, TIMEZONE), endHour, endMinute)
    timeRemaining = tsEnd - tsNow < 0 ? math.round(((tsEnd+86400000) - tsNow) / 1000) : math.round((tsEnd - tsNow) / 1000)
    
    days = math.floor(timeRemaining / 86400)
    hours = math.floor((timeRemaining % 86400) / 3600)
    minutes = math.floor((timeRemaining % 3600) / 60)
    seconds = timeRemaining % 60
    [hours, minutes, seconds]

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)
// -------------------- FUNCTIONS --------------------


// -------------------- 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
// -------------------- SESSIONS --------------------

// -------------------- PLOT --------------------
_linPos = linePosition == 'Top' ? location.top : location.bottom
plotshape(not na(sess1) and bgType == 'Line' and timeframe.isintraday == true, title='London', style=shape.square, location=_linPos, color=session1Color, size=size.auto)
plotshape(not na(sess2) and bgType == 'Line' and timeframe.isintraday == true, title='NY', style=shape.square, location=_linPos, color=session2Color, size=size.auto)
plotshape(not na(sess3) and bgType == 'Line' and timeframe.isintraday == true, title='LC', style=shape.square, location=_linPos, color=session3Color, size=size.auto)
plotshape(not na(sess4) and bgType == 'Line' and timeframe.isintraday == true, title='Asia', style=shape.square, location=_linPos, color=session4Color, size=size.auto)
// -------------------- PLOT --------------------

// -------------------- SESSIONS LOGIC --------------------
sessions_arr = array.new_string(0)

if session1Show
    array.push(sessions_arr, '1')
if session2Show
    array.push(sessions_arr, '2')
if session3Show
    array.push(sessions_arr, '3')
if session4Show
    array.push(sessions_arr, '4')


_findSess(_i) =>
    arr_size = array.size(sessions_arr) - 1
    _index = array.indexof(sessions_arr, _i)
    _next = _i
    
    if _index < arr_size
        _next := array.get(sessions_arr, _index + 1)
    else
        _next := array.get(sessions_arr, 0)
    _next

_currentSess = noSessionText
_currentSessCol = _color1
var _nextSess = noSessionText
var _nextSessCol = _color1
var _ni = '0'

end_hours = 0
end_minutes = 0
end_seconds = 0

start_hours = 0
start_minutes = 0
start_seconds = 0

// Current Sessions
if(sess1)
    _currentSess := session1Text
    _currentSessCol := session1Color
    _ni := _findSess('1')
    [_h, _m, _s] = timeTillPeriodEnd(session1Time)
    end_hours := _h
    end_minutes := _m
    end_seconds := _s
else if(sess2)
    _currentSess := session2Text
    _currentSessCol := session2Color
    _ni := _findSess('2')
    [_h, _m, _s] = timeTillPeriodEnd(session2Time)
    end_hours := _h
    end_minutes := _m
    end_seconds := _s
else if(sess3)
    _currentSess := session3Text
    _currentSessCol := session3Color
    _ni := _findSess('3')
    [_h, _m, _s] = timeTillPeriodEnd(session3Time)
    end_hours := _h
    end_minutes := _m
    end_seconds := _s
else if(sess4)
    _currentSess := session4Text
    _currentSessCol := session4Color
    _ni := _findSess('4')
    [_h, _m, _s] = timeTillPeriodEnd(session4Time)
    end_hours := _h
    end_minutes := _m
    end_seconds := _s
else
    _currentSess := noSessionText
    _currentSessCol := _color1
    end_hours := 0
    end_minutes := 0
    end_seconds := 0

// Next Session
if _ni == '1'
    _nextSess := session1Text
    _nextSessCol := session1Color
    [_h, _m, _s] = timeTillPeriodEnd(session1Time, 0)
    start_hours := _h
    start_minutes := _m
    start_seconds := _s
else if _ni == '2'
    _nextSess := session2Text
    _nextSessCol := session2Color
    [_h, _m, _s] = timeTillPeriodEnd(session2Time, 0)
    start_hours := _h
    start_minutes := _m
    start_seconds := _s
else if _ni == '3'
    _nextSess := session3Text
    _nextSessCol := session3Color
    [_h, _m, _s] = timeTillPeriodEnd(session3Time, 0)
    start_hours := _h
    start_minutes := _m
    start_seconds := _s
else if _ni == '4'
    _nextSess := session4Text
    _nextSessCol := session4Color
    [_h, _m, _s] = timeTillPeriodEnd(session4Time, 0)
    start_hours := _h
    start_minutes := _m
    start_seconds := _s
else
    _nextSess := noSessionText
    _nextSessCol := _color1
    start_hours := 0
    start_minutes := 0
    start_seconds := 0

// -------------------- SESSIONS LOGIC --------------------


// -------------------- TABLE --------------------
_position = switch position
    "Top Left" => position.top_left
    "Top Right" => position.top_right
    "Bottom Left" => position.bottom_left
    => position.bottom_right


end_txt = str.tostring(end_hours) + "h " + str.tostring(end_minutes) + "m"
start_txt = str.tostring(start_hours) + "h " + str.tostring(start_minutes) + "m"

var table _table = table.new(_position, columns = 2, rows = 4, border_width=3)

_vert = alignment == 'Vertical'

if showTable
    table.cell(_table, column = 0, row = 0, text = _currentSess, text_color=color.new(_currentSessCol, 0), bgcolor=color.new(_currentSessCol, 90))
    table.cell(_table, column = _vert ? 0 : 1, row = _vert ? 1 : 0, text = end_txt, text_color=color.new(_currentSessCol, 0))
    
    if showNextSession
        table.cell(_table, column = 0, row = 2, text = _nextSess, text_color=color.new(_nextSessCol, 0), bgcolor=color.new(_nextSessCol, 90))
        table.cell(_table, column = _vert ? 0 : 1, row = _vert ? 3 : 2, text = start_txt, text_color=color.new(_nextSessCol, 0))
// -------------------- TABLE --------------------

// -------------------- DAILY DIVIDERS --------------------
tickerExchangeOffset = 5
int dayLabelStartTime = 1

_rin = resolutionInMinutes()

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

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

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

if isStartTime and showDailyDividers
    vline(bar_index, divider_text_color, 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)

txtMonH = "Понедельник"
txtTueH = "Вторник"
txtWedH = "Среда"
txtThuH = "Четверг"
txtFriH = "Пятница"
txtSatH = "Суббота"
txtSunH = "Воскресенье"

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

// -------------------- DAILY DIVIDERS --------------------
// Watermark ENT
/////////////////////////////////////////////////
//                  FONTS
/////////////////////////////////////////////////
//{
var FONT1 = array.new<string>(0)

if barstate.isfirst
    array.push(FONT1, "░█████╗░,██████╗░,░█████╗░,██████╗░,███████╗,███████╗,░██████╗░,██╗░░██╗,██╗,░░░░░██╗,██╗░░██╗,██╗░░░░░,███╗░░░███╗,███╗░░██╗,░█████╗░,██████╗░,░██████╗░,██████╗░,░██████╗,████████╗,██╗░░░██╗,██╗░░░██╗,░██╗░░░░░░░██╗,██╗░░██╗,██╗░░░██╗,███████╗,░█████╗░,░░███╗░░,██████╗░,██████╗░,░░██╗██╗,███████╗,░█████╗░,███████╗,░█████╗░,░█████╗░,░░░░")
    array.push(FONT1, "██╔══██╗,██╔══██╗,██╔══██╗,██╔══██╗,██╔════╝,██╔════╝,██╔════╝░,██║░░██║,██║,░░░░░██║,██║░██╔╝,██║░░░░░,████╗░████║,████╗░██║,██╔══██╗,██╔══██╗,██╔═══██╗,██╔══██╗,██╔════╝,╚══██╔══╝,██║░░░██║,██║░░░██║,░██║░░██╗░░██║,╚██╗██╔╝,╚██╗░██╔╝,╚════██║,██╔══██╗,░████║░░,╚════██╗,╚════██╗,░██╔╝██║,██╔════╝,██╔═══╝░,╚════██║,██╔══██╗,██╔══██╗,░░░░")
    array.push(FONT1, "███████║,██████╦╝,██║░░╚═╝,██║░░██║,█████╗░░,█████╗░░,██║░░██╗░,███████║,██║,░░░░░██║,█████═╝░,██║░░░░░,██╔████╔██║,██╔██╗██║,██║░░██║,██████╔╝,██║██╗██║,██████╔╝,╚█████╗░,░░░██║░░░,██║░░░██║,╚██╗░██╔╝,░╚██╗████╗██╔╝,░╚███╔╝░,░╚████╔╝░,░░███╔═╝,██║░░██║,██╔██║░░,░░███╔═╝,░█████╔╝,██╔╝░██║,██████╗░,██████╗░,░░░░██╔╝,╚█████╔╝,╚██████║,░░░░")
    array.push(FONT1, "██╔══██║,██╔══██╗,██║░░██╗,██║░░██║,██╔══╝░░,██╔══╝░░,██║░░╚██╗,██╔══██║,██║,██╗░░██║,██╔═██╗░,██║░░░░░,██║╚██╔╝██║,██║╚████║,██║░░██║,██╔═══╝░,╚██████╔╝,██╔══██╗,░╚═══██╗,░░░██║░░░,██║░░░██║,░╚████╔╝░,░░████╔═████║░,░██╔██╗░,░░╚██╔╝░░,██╔══╝░░,██║░░██║,╚═╝██║░░,██╔══╝░░,░╚═══██╗,███████║,╚════██╗,██╔══██╗,░░░██╔╝░,██╔══██╗,░╚═══██║,░░░░")
    array.push(FONT1, "██║░░██║,██████╦╝,╚█████╔╝,██████╔╝,███████╗,██║░░░░░,╚██████╔╝,██║░░██║,██║,╚█████╔╝,██║░╚██╗,███████╗,██║░╚═╝░██║,██║░╚███║,╚█████╔╝,██║░░░░░,░╚═██╔═╝░,██║░░██║,██████╔╝,░░░██║░░░,╚██████╔╝,░░╚██╔╝░░,░░╚██╔╝░╚██╔╝░,██╔╝╚██╗,░░░██║░░░,███████╗,╚█████╔╝,███████╗,███████╗,██████╔╝,╚════██║,██████╔╝,╚█████╔╝,░░██╔╝░░,╚█████╔╝,░█████╔╝,░░░░")
    array.push(FONT1, "╚═╝░░╚═╝,╚═════╝░,░╚════╝░,╚═════╝░,╚══════╝,╚═╝░░░░░,░╚═════╝░,╚═╝░░╚═╝,╚═╝,░╚════╝░,╚═╝░░╚═╝,╚══════╝,╚═╝░░░░░╚═╝,╚═╝░░╚══╝,░╚════╝░,╚═╝░░░░░,░░░╚═╝░░░,╚═╝░░╚═╝,╚═════╝░,░░░╚═╝░░░,░╚═════╝░,░░░╚═╝░░░,░░░╚═╝░░░╚═╝░░,╚═╝░░╚═╝,░░░╚═╝░░░,╚══════╝,░╚════╝░,╚══════╝,╚══════╝,╚═════╝░,░░░░░╚═╝,╚═════╝░,░╚════╝░,░░╚═╝░░░,░╚════╝░,░╚════╝░,░░░░")
var FONT_REF = array.from("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"," ")
//}
/////////////////////////////////////////////////
//                 SETTINGS
/////////////////////////////////////////////////
//{
var watermarkent    = input(defval=true, title='Watermark')
WATERMARK_TEXT      = input.string("ENT", "Text")
WATERMARK_STYLE     = input.string("Type1", "Style", options=["Type1"])
WATERMARK_COLOR     = input.color(color.new(color.gray,85), "Color")
WATERMARK_SIZE_OPT  = input.string("Normal", "Size", options=["Large","Normal"])
WATERMARK_POS_OPT   = input.string("Middle-Center", "Position", options=["Middle-Center"])
WATERMARK_REP_ROW   = input.int(1, "Количество: Vertical ", inline="repetition")
WATERMARK_REP_COL   = input.int(1, "Horizontal ",  inline="repetition")


WATERMARK_SIZE = switch WATERMARK_SIZE_OPT
    "Large"  => size.large
    "Normal" => size.normal

WATERMARK_POS = switch WATERMARK_POS_OPT
    "Middle-Center" => position.middle_center

FONT = switch WATERMARK_STYLE
    "Type1" => FONT1


//}
/////////////////////////////////////////////////
//                  MAIN
/////////////////////////////////////////////////
var watermark_table = table.new(WATERMARK_POS, WATERMARK_REP_COL, WATERMARK_REP_ROW, na, na)


gen_watermark(message) =>
    msg = str.split(str.upper(message), "")
    idx_list = array.new<int>(0)
    for ii=0 to array.size(msg)-1
        for jj=0 to array.size(FONT_REF)-1
            if array.get(msg, ii) == array.get(FONT_REF, jj)
                array.push(idx_list, jj)
                break
    

    
    watermark_width = 0
    watermark_msg = ""
    for ii=0 to array.size(FONT)-1
        watermark_msg := watermark_msg
        font_line = str.split(array.get(FONT,ii), ",")
        for jj=0 to array.size(idx_list)-1
            idx = array.get(idx_list, jj)
            watermark_msg := watermark_msg + array.get(font_line, idx)
            watermark_width := ( ii == 0 )? watermark_width + str.length(array.get(font_line, idx)) : watermark_width
        watermark_msg := watermark_msg + "\n"
    

        
    for mm=0 to WATERMARK_REP_COL-1
        for nn=0 to WATERMARK_REP_ROW-1
           
           
            table.cell(watermark_table, mm, nn, watermark_msg, text_color=WATERMARK_COLOR, text_halign=text.align_left, text_size=WATERMARK_SIZE)


if barstate.islastconfirmedhistory and watermarkent == true
    gen_watermark(WATERMARK_TEXT)
