Trading sessions, Ichimoku and Classic Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Rain5369

//@version=5
indicator('Trading sessions', overlay=true)
//plot(timestamp(syminfo.timezone,year(timenow),month(timenow),dayofmonth(timenow), hour(timenow), minute(timenow)),color=color.blue)

maxBoxCount = input(title='Maximum box count', defval=21)
lineWidth = input(title='Line width', defval=1)
ExtendLine = input(false, 'Extend bars')
Extend50 = input(false, 'Display session 50%(eq)')
MaxLookbackDays = input.int(7, 'Maximum lookback days', minval=0)


defaultEuropeColor = color.new(#D64142, 65)
defaultUSAColor = color.new(#D4AA28, 65)
defaultAsiaColor = color.new(#D9739A, 65)
defaultEODColor = color.new(color.olive, 65)


day_start_bool = input.bool(title='IB box', defval=true, group='Day IB')
IB_BorderColor = input.color(title='IB : border', defval=color.new(color.white, 65), inline='IB_BorderColor', group='Day IB')
IB_BackgroundColor = input.color(title=': background', defval=color.new(color.white,65), inline='IB_BorderColor', group='Day IB')
day_start = '0000-0100'
ExtendIB = input(false, 'Display IB Bars', inline='ExtendIB', group='Day IB')
IB_low_color = input.color(title=': Low', defval=color.new(color.red, 0), inline='ExtendIB', group='Day IB')
IB_high_color = input.color(title=': high', defval=color.new(color.green, 0), inline='ExtendIB', group='Day IB')
FillIB = input(false,'Fill IB', inline='FillIB', group='Day IB')
FillIBcolor = input.color(title=' ', defval=color.new(color.white, 85), inline='FillIB', group='Day IB')



AsiaBorderColor = input.color(title='Asia : border', defval=color.new(#D9739A, 65), inline='AsiaBorderColor', group='Asia')
AsiaBackgroundColor = input.color(title=': background', defval=defaultAsiaColor, inline='AsiaBorderColor', group='Asia')
AsiaExtendedbar = input.color(title=': Extension', defval=color.new(#D9739A, 0), inline='AsiaBorderColor', group='Asia')
Asia50Color = input.color(title=': 50%(EQ)', defval=color.new(color.white, 65), inline='AsiaBorderColor', group='Asia')
asia = input.bool(title='Asia Session box', defval=true, group='Asia')
Asia = '1700-2030:1234567'


EuropeBorderColor = input.color(title='London : border', defval=color.new(#D64142, 65), inline='EuropeBorderColor', group='London')
EuropeBackgroundColor = input.color(title=': Background', defval=defaultEuropeColor, inline='EuropeBorderColor', group='London')
EuropeExtendedbar = input.color(title=': Extension', defval=color.new(#D64142, 0), inline='EuropeBorderColor', group='London')
Europe50Color = input.color(title=': 50%(EQ)', defval=color.new(color.white, 65), inline='EuropeBorderColor', group='London')
europe = input.bool(title='London Session box', defval=true, group='London')
Europe = '0000-0400:1234567'


USABorderColor = input.color(title='NY : border', defval=color.new(#D4AA28, 65), inline='USABorderColor', group='New York')
USABackgroundColor = input.color(title=': background', defval=defaultUSAColor, inline='USABorderColor', group='New York')
USAExtendedbar = input.color(title=': Extension', defval=color.new(#D4AA28, 0), inline='USABorderColor', group='New York')
USA50Color = input.color(title=': 50%(EQ)', defval=color.new(color.white, 65), inline='USABorderColor', group='New York')
usa = input.bool(title='New York Session box', defval=true, group='New York')
USA = '0630-1000:1234567'


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Bars
is_newbar(sess) =>
    t = time('D', sess, 'America/New_York')
    na(t[1]) and not na(t) or t[1] < t

is_session(sess) =>
    not na(time('D', sess, 'America/New_York'))

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////----------------------------------------------------------------------------------
//London
//----------------------------------------------------------------------------------
europeNewbar = is_newbar(Europe)
europeSession = is_session(Europe)
//europeBox = box.new(na,na,na,na,border_width=2,xloc=xloc.bar_time,border_style=line.style_solid, border_color=EuropeBorderColor, bgcolor=EuropeBackgroundColor)
//plotshape(europeNewbar,"eu new",style=shape.xcross)
if europe and europeSession

    float europeLow = na
    europeLow := if europeSession
        if europeNewbar
            low
        else
            math.min(europeLow[1], low)
    else
        europeLow[1]

    float europeHigh = na
    europeHigh := if europeSession
        if europeNewbar
            high
        else
            math.max(europeHigh[1], high)
    else
        europeHigh[1]

    int europeStart = na
    europeStart := if europeSession
        if europeNewbar
            time
        else
            math.min(europeStart[1], time)
    else
        na
    int europeEnd = na
    europeEnd := if europeSession
        if europeNewbar
            time_close
        else
            math.max(europeEnd[1], time_close)
    else
        na


    europeBox = if europeNewbar
        box.new(left=europeStart, bottom=europeLow, right=europeEnd, top=europeHigh, border_width=lineWidth, xloc=xloc.bar_time, border_style=line.style_solid, border_color=EuropeBorderColor, bgcolor=EuropeBackgroundColor)
    
    
    if not europeNewbar

        box.set_right(europeBox[1], europeEnd)
        box.set_top(europeBox[1], europeHigh)
        box.set_bottom(europeBox[1], europeLow)
    if timeframe.period == '240'
        box.delete(europeBox[maxBoxCount])


in_session_London = time(timeframe.period, Europe, 'America/New_York')
is_new_session(res, sess) =>
    t = time(res, sess, 'America/New_York')
    na(t[1]) and not na(t) or t[1] < t

new_session_London = is_new_session('0400', Europe)

is_newbar_bar(res, sess) =>
    t = time(res, sess, 'America/New_York')
    not na(t) and (na(t[1]) or t > t[1])
new_Lodon = is_newbar_bar('0001', Europe) ? 1 : 0

var float _low_London = close
var float _high_London = close

_low_London := new_session_London ? low : in_session_London ? math.min(low, _low_London[1]) : na
_high_London := new_session_London ? high : in_session_London ? math.max(high, _high_London[1]) : na

start_of_session_London = ta.valuewhen(new_Lodon == 1, high, 0)
end_session_condition_London = start_of_session_London != start_of_session_London[1] ? 0 : in_session_London ? 1 : 0
end_session_London = end_session_condition_London == 0 and end_session_condition_London[1] == 1 ? color.orange : na


v_extend_low_London = ta.valuewhen(_low_London == low, low, 0)
col_v_extend_low_London = end_session_condition_London == 0 and v_extend_low_London == v_extend_low_London[1] and ExtendLine ? EuropeExtendedbar : na
plot(v_extend_low_London, color=col_v_extend_low_London, linewidth=lineWidth, title='London Low', editable=false)

v_extend_high_London = ta.valuewhen(_high_London == high, high, 0)
col_v_extend_high_London = end_session_condition_London == 0 and v_extend_high_London == v_extend_high_London[1] and ExtendLine ? EuropeExtendedbar : na
plot(v_extend_high_London, color=col_v_extend_high_London, linewidth=lineWidth, title='London High', editable=false)

col_v_extend_50_London = end_session_condition_London == 0 and v_extend_high_London == v_extend_high_London[1] and Extend50 ? EuropeExtendedbar : na
L50 = (v_extend_low_London + v_extend_high_London)*0.5
plot(L50, color = col_v_extend_50_London, title = 'London 50%', editable=false)


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////----------------------------------------------------------------------------------
//NY
//----------------------------------------------------------------------------------
usaNewbar = is_newbar(USA)
usaSession = is_session(USA)

if usa and usaSession
    float usaLow = na
    usaLow := if usaSession
        if usaNewbar
            low
        else
            math.min(usaLow[1], low)
    else
        usaLow[1]

    float usaHigh = na
    usaHigh := if usaSession
        if usaNewbar
            high
        else
            math.max(usaHigh[1], high)
    else
        usaHigh[1]

    int usaStart = na
    usaStart := if usaSession
        if usaNewbar
            time
        else
            math.min(usaStart[1], time)
    else
        na

    int usaEnd = na
    usaEnd := if usaSession
        if usaNewbar
            time_close
        else
            math.max(usaEnd[1], time_close)
    else
        na


    usaBox = if usaNewbar
        usaBox = box.new(left=usaStart, bottom=usaLow, right=usaEnd, top=usaHigh, border_width=lineWidth, xloc=xloc.bar_time, border_style=line.style_solid, border_color=USABorderColor, bgcolor=USABackgroundColor)
    

    if not usaNewbar

        box.set_right(usaBox[1], usaEnd)
        box.set_top(usaBox[1], usaHigh)
        box.set_bottom(usaBox[1], usaLow)
    if timeframe.period == '60'
        box.delete(usaBox[maxBoxCount])

in_session_NY = time(timeframe.period, USA, 'America/New_York')

new_session_NY = is_new_session('1000', USA)

new_NY = is_newbar_bar('0630', USA) ? 1 : 0

var float _low_NY = close
var float _high_NY = close

_low_NY := new_session_NY ? low : in_session_NY ? math.min(low, _low_NY[1]) : na
_high_NY := new_session_NY ? high : in_session_NY ? math.max(high, _high_NY[1]) : na

start_of_session_value_NY = ta.valuewhen(new_NY == 1, high, 0)
end_session_condition_NY = start_of_session_value_NY != start_of_session_value_NY[1] ? 0 : in_session_NY ? 1 : 0
end_session_NY = end_session_condition_NY == 0 and end_session_condition_NY[1] == 1 ? color.orange : na

v_extend_low_NY = ta.valuewhen(_low_NY == low, low, 0)
col_v_extend_low_NY = end_session_condition_NY == 0 and v_extend_low_NY == v_extend_low_NY[1] and ExtendLine ? USAExtendedbar : na
plot(v_extend_low_NY, color=col_v_extend_low_NY, linewidth=lineWidth, title='New York Low', editable=false)

v_extend_high_NY = ta.valuewhen(_high_NY == high, high, 0)
col_v_extend_high_NY = end_session_condition_NY == 0 and v_extend_high_NY == v_extend_high_NY[1] and ExtendLine ? USAExtendedbar : na
plot(v_extend_high_NY, color=col_v_extend_high_NY, linewidth=lineWidth, title='New York High', editable=false)

col_v_extend_50_NY = end_session_condition_NY == 0 and v_extend_low_NY == v_extend_low_NY[1] and Extend50 ? USAExtendedbar : na
NY50 = (v_extend_low_NY + v_extend_high_NY)*0.5
plot(NY50, color = col_v_extend_50_NY, title = 'New York 50%', editable=false) 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////----------------------------------------------------------------------------------
//Asia 
//----------------------------------------------------------------------------------
asiaNewbar = is_newbar(Asia)
asiaSession = is_session(Asia)
if asia and asiaSession


    float asiaLow = na
    asiaLow := if asiaSession
        if asiaNewbar
            low
        else
            math.min(asiaLow[1], low)
    else
        asiaLow[1]

    float asiaHigh = na
    asiaHigh := if asiaSession
        if asiaNewbar
            high
        else
            math.max(asiaHigh[1], high)
    else
        asiaHigh[1]

    int asiaStart = na
    asiaStart := if asiaSession
        if asiaNewbar
            time
        else
            math.min(asiaStart[1], time)
    else
        na

    int asiaEnd = na
    asiaEnd := if asiaSession
        if asiaNewbar
            time_close
        else
            math.max(asiaEnd[1], time_close)
    else
        na


    asiaBox = if asiaNewbar
        box.new(left=asiaStart, bottom=asiaLow, right=asiaEnd, top=asiaHigh, border_width=lineWidth, xloc=xloc.bar_time, border_style=line.style_solid, border_color=AsiaBorderColor, bgcolor=AsiaBackgroundColor)

    if not asiaNewbar
        box.set_right(asiaBox[1], asiaEnd)
        box.set_top(asiaBox[1], asiaHigh)
        box.set_bottom(asiaBox[1], asiaLow)
    if timeframe.period == '60'
        box.delete(asiaBox[maxBoxCount])

in_session = time(timeframe.period, Asia, 'America/New_York')

new_session = is_new_session('2030', Asia)

new = is_newbar_bar('1700', Asia) ? 1 : 0

var float _low = close
var float _high = close

_low := new_session ? low : in_session ? math.min(low, _low[1]) : na
_high := new_session ? high : in_session ? math.max(high, _high[1]) : na

start_of_session_value = ta.valuewhen(new == 1, high, 0)
end_session_condition = start_of_session_value != start_of_session_value[1] ? 0 : in_session ? 1 : 0
end_session = end_session_condition == 0 and end_session_condition[1] == 1 ? color.orange : na

v_extend_low = ta.valuewhen(_low == low, low, 0)
col_v_extend_low = end_session_condition == 0 and v_extend_low == v_extend_low[1] and ExtendLine ? AsiaExtendedbar : na
plot(v_extend_low, color=col_v_extend_low, linewidth=lineWidth, title='Asia Low', editable=false)

v_extend_high = ta.valuewhen(_high == high, high, 0)
col_v_extend_high = end_session_condition == 0 and v_extend_high == v_extend_high[1] and ExtendLine ? AsiaExtendedbar : na
plot(v_extend_high, color=col_v_extend_high, linewidth=lineWidth, title='Asia High', editable=false)

col_v_extend_50 = end_session_condition == 0 and v_extend_low == v_extend_low[1] and Extend50 ? AsiaExtendedbar : na
A50 = (v_extend_low + v_extend_high)*0.5
plot(A50, color = col_v_extend_50, title = 'Asia 50%', editable=false)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


day_start_Newbar = is_newbar(day_start)
day_start_Session = is_session(day_start)
if day_start_bool and day_start_Session


    float day_start_Low = na
    day_start_Low := if day_start_Session
        if day_start_Newbar
            low
        else
            math.min(day_start_Low[1], low)
    else
        day_start_Low[1]

    float day_start_High = na
    day_start_High := if day_start_Session
        if day_start_Newbar
            high
        else
            math.max(day_start_High[1], high)
    else
        day_start_High[1]

    int day_start_Start = na
    day_start_Start := if day_start_Session
        if day_start_Newbar
            time
        else
            math.min(day_start_Start[1], time)
    else
        na

    int day_start_End = na
    day_start_End := if day_start_Session
        if day_start_Newbar
            time_close
        else
            math.max(day_start_End[1], time_close)
    else
        na


    day_start_Box = if day_start_Newbar
        box.new(left=day_start_Start, bottom=day_start_Low, right=day_start_End, top=day_start_High, border_width=lineWidth, xloc=xloc.bar_time, border_style=line.style_solid, border_color=IB_BorderColor, bgcolor=IB_BackgroundColor)

    if not day_start_Newbar
        box.set_right(day_start_Box[1], day_start_End)
        box.set_top(day_start_Box[1], day_start_High)
        box.set_bottom(day_start_Box[1], day_start_Low)
    if timeframe.period == '60'
        box.delete(day_start_Box[maxBoxCount])


IB_session = time(timeframe.period, day_start, 'America/New_York')

IB_new_session = is_new_session('0100', day_start)

IB_new = is_newbar_bar('0001', day_start) ? 1 : 0

var float IB_low = close
var float IB_high = close

IB_low := IB_new_session ? low : IB_session ? math.min(low, IB_low[1]) : na
IB_high := IB_new_session ? high : IB_session ? math.max(high, IB_high[1]) : na

start_of_IB_value = ta.valuewhen(IB_new == 1, high, 0)
end_IB_condition = start_of_IB_value != start_of_IB_value[1] ? 0 : IB_session ? 1 : 0
end_IB = end_IB_condition == 0 and end_IB_condition[1] == 1 ? color.orange : na

IB_extend_low = ta.valuewhen(IB_low == low, low, 0)
col_IB_extend_low = end_IB_condition == 0 and IB_extend_low == IB_extend_low[1] and ExtendIB ? IB_low_color : na
IBL = plot(IB_extend_low, color=col_IB_extend_low, linewidth=lineWidth, title='IB Low', editable=false)

IB_extend_high = ta.valuewhen(IB_high == high, high, 0)
col_IB_extend_high = end_IB_condition == 0 and IB_extend_high == IB_extend_high[1] and ExtendIB ? IB_high_color : na
IBH = plot(IB_extend_high, color=col_IB_extend_high, linewidth=lineWidth, title='IB High', editable=false)


fill(IBL,IBH, title = 'inblance fill', color = FillIB ? FillIBcolor : na, editable = false)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
mon = input(title='Week Days', defval=true)

plotshape(mon == true and time(timeframe.period, '0000-0001:2', 'UTC') and timeframe.isintraday, textcolor=color.new(color.white, 0), style=shape.labeldown, title='Monday', text='Monday', color=color.new(color.green, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:3', 'UTC') and timeframe.isintraday, textcolor=color.new(color.white, 0), style=shape.labeldown, title='Tuesday', text='Tuesday', color=color.new(color.green, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:4', 'UTC') and timeframe.isintraday, textcolor=color.new(color.black, 0), style=shape.labeldown, title='Wednesday', text='Wednesday', color=color.new(color.red, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:5', 'UTC') and timeframe.isintraday, textcolor=color.new(color.white, 0), style=shape.labeldown, title='Thursday', text='Thursday', color=color.new(color.green, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:6', 'UTC') and timeframe.isintraday, textcolor=color.new(color.black, 0), style=shape.labeldown, title='Friday', text='Friday', color=color.new(color.red, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:7', 'UTC') and timeframe.isintraday, textcolor=color.new(color.white, 0), style=shape.labeldown, title='Saturday', text='Saturday', color=color.new(color.black, 0), location=location.bottom, editable=false)
plotshape(mon == true and time(timeframe.period, '0000-0001:1', 'UTC') and timeframe.isintraday, textcolor=color.new(color.white, 0), style=shape.labeldown, title='Sunday', text='Sunday', color=color.new(color.black, 0), location=location.bottom, editable=false)


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Ten = input.int(9, minval=1, title='Tenkan', group = 'ichimoku')
Kij = input.int(26, minval=1, title='Kijun', group = 'ichimoku')
LeadSpan = input.int(52, minval=1, title='Senkou B', group = 'ichimoku')
Displace = input.int(26, minval=1, title='Senkou A', group = 'ichimoku')
SpanOffset = input.int(26, minval=1, title='Span Offset', group = 'ichimoku')

sts = input(false, title='Show Tenkan', group = 'ichimoku')
sks = input(false, title='Show Kijun', group = 'ichimoku')
scs = input(false, title='Show Chikou', group = 'ichimoku')
ssa = input(true, title='Show Span A', group = 'ichimoku')
ssb = input(true, title='Show Span B', group = 'ichimoku')

source = close

//Script for Ichimoku Indicator
donchian(len) =>
    math.avg(ta.lowest(len), ta.highest(len))
TS = donchian(Ten)
KS = donchian(Kij)
SpanA = math.avg(TS, KS)
SpanB = donchian(LeadSpan)
Chikou = source[Displace]
SpanAA = math.avg(TS, KS)[SpanOffset]
SpanBB = donchian(LeadSpan)[SpanOffset]

//Kumo Breakout (Long)
SpanA_Top = SpanAA >= SpanBB ? 1 : 0
SpanB_Top = SpanBB >= SpanAA ? 1 : 0

SpanA_Top2 = SpanA >= SpanB ? 1 : 0
SpanB_Top2 = SpanB >= SpanA ? 1 : 0

SpanA1 = SpanA_Top2 ? SpanA : na
SpanA2 = SpanA_Top2 ? SpanB : na

SpanB1 = SpanB_Top2 ? SpanA : na
SpanB2 = SpanB_Top2 ? SpanB : na

//plot for Tenkan and Kijun (Current Timeframe)
p1 = plot(sts and TS ? TS : na, title='Tenkan', linewidth=2, color=color.new(color.blue, 0))
p2 = plot(sks and KS ? KS : na, title='Kijun', linewidth=2, color=color.new(color.purple, 0))
p5 = plot(sks ? close : na, title='Chikou', linewidth=2, offset=-Displace, color=color.new(color.black, 0))

//Plot for Kumo Cloud (Dynamic Color)
p3 = plot(ssa and SpanA ? SpanA : na, title='SpanA', linewidth=2, offset=Displace, color=color.new(color.green, 0))
p4 = plot(ssb and SpanB ? SpanB : na, title='SpanB', linewidth=2, offset=Displace, color=color.new(color.red, 0))

p8 = plot(ssa and SpanA1 ? SpanA1 : na, title='Span A1 above', style=plot.style_linebr, linewidth=1, offset=Displace, color=color.new(color.green, 0))
p9 = plot(ssa and SpanA2 ? SpanA2 : na, title='Span A2 above', style=plot.style_linebr, linewidth=1, offset=Displace, color=color.new(color.green, 0))
p10 = plot(ssb and SpanB1 ? SpanB1 : na, title='Span B1 above', style=plot.style_linebr, linewidth=1, offset=Displace, color=color.new(color.red, 0))
p11 = plot(ssb and SpanB2 ? SpanB2 : na, title='Span B2 above', style=plot.style_linebr, linewidth=1, offset=Displace, color=color.new(color.red, 0))

fill(p8, p9, color=color.new(color.lime, 90), title='Kumo Cloud Up')
fill(p10, p11, color=color.new(color.red, 90), title='Kumo Cloud Down')

LongSpan = SpanA_Top and source[1] < SpanAA[1] and source > SpanAA or SpanB_Top and source[1] < SpanBB[1] and source > SpanBB ? 1 : 0
cupSpan = LongSpan == 1 ? LongSpan : 0


///////////////////////////////////////////////////////////////

//----------------------------------------------------------------------------------
//Vwap Pivot
//----------------------------------------------------------------------------------

interval_type = input.string('Weekly', options=['Daily', 'Weekly', 'Monthly', 'Yearly'], title='Pivot Interval', group = 'pivots')
disp_p_extend = input(true, title='Extend pivot lines', group = 'pivots')
disp_p_labels = input(true, title='Display pivot labels', group = 'pivots')
label_size = input.string('small', options=['auto', 'tiny', 'small', 'normal', 'large', 'huge'], title='Label size', group = 'pivots')
off_mult = input(15, title='label offset adjustment', group = 'pivots')

Label_color = input.color(title='Color : Label', defval=color.new(color.black, 0), inline='Label_color', group='pivots')
Pivot_color = input.color(title=' : Pivot', defval=color.new(color.blue, 0), inline='Label_color', group='pivots')
R1_color = input.color(title=' : R1', defval=color.new(color.orange, 0), inline='Label_color', group='pivots')
R2_color = input.color(title=' : R2', defval=color.new(color.orange, 0), inline='Label_color', group='pivots')
R3_color = input.color(title=' : R3', defval=color.new(color.red, 0), inline='Label_color', group='pivots')
R4_color = input.color(title=' : R4', defval=color.new(color.red, 0), inline='Label_color', group='pivots')
S1_color = input.color(title=' : S1', defval=color.new(color.green, 0), inline='Label_color', group='pivots')
S2_color = input.color(title=' : S2', defval=color.new(color.green, 0), inline='Label_color', group='pivots')
S3_color = input.color(title=' : S3', defval=color.new(color.lime, 0), inline='Label_color', group='pivots')
S4_color = input.color(title=' : S4', defval=color.new(color.lime, 0), inline='Label_color', group='pivots')

l_size = label_size == 'auto' ? size.auto : label_size == 'tiny' ? size.tiny : label_size == 'small' ? size.small : label_size == 'normal' ? size.normal : label_size == 'large' ? size.large : size.huge

new_year = ta.change(time('12M')) != 0
new_month = ta.change(time('M')) != 0
new_week = ta.change(time('W')) != 0
new_day = ta.change(time('D')) != 0

new_interval = interval_type == 'Daily' ? new_day : interval_type == 'Weekly' ? new_week : interval_type == 'Monthly' ? new_month : new_year

f_vwap(newint) =>
    float pvsum = na
    float vsum = na
    pvsum := newint ? hlc3 * volume : pvsum[1] + hlc3 * volume
    vsum := newint ? volume : vsum[1] + volume
    v = pvsum / vsum
    v

p_vwap = f_vwap(new_interval)

p_current_vwap_high = 0.0
p_current_vwap_high := new_interval ? p_vwap : math.max(p_current_vwap_high[1], p_vwap)
p_current_vwap_low = 0.0
p_current_vwap_low := new_interval ? p_vwap : math.min(p_current_vwap_low[1], p_vwap)

p_vwap_high = ta.valuewhen(new_interval, p_current_vwap_high[1], 0)
p_vwap_low = ta.valuewhen(new_interval, p_current_vwap_low[1], 0)
p_vwap_close = ta.valuewhen(new_interval, p_vwap[1], 0)
p_vwap_open = ta.valuewhen(new_interval, hlc3, 0)

v_range = p_vwap_high - p_vwap_low

pivot = (p_vwap_high + p_vwap_low + p_vwap_close) / 3


R1 = 2 * pivot - p_vwap_low
R2 = pivot + v_range
R3 = R2 + v_range
R4 = R3 + v_range

S1 = 2 * pivot - p_vwap_high
S2 = pivot - v_range
S3 = S2 - v_range
S4 = S3 - v_range

ll_offset = timenow + math.round(ta.change(time) * off_mult)

pivot_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=pivot, y2=pivot, xloc=xloc.bar_time, extend=extend.right, color=Pivot_color, width=1, style=line.style_solid) : na
line.delete(pivot_line[1])

R1_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=R1, y2=R1, xloc=xloc.bar_time, extend=extend.right, color=R1_color, width=1, style=line.style_solid) : na
line.delete(R1_line[1])
R2_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=R2, y2=R2, xloc=xloc.bar_time, extend=extend.right, color=R2_color, width=2, style=line.style_solid) : na
line.delete(R2_line[1])
R3_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=R3, y2=R3, xloc=xloc.bar_time, extend=extend.right, color=R3_color, width=1, style=line.style_solid) : na
line.delete(R3_line[1])
R4_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=R4, y2=R4, xloc=xloc.bar_time, extend=extend.right, color=R4_color, width=2, style=line.style_solid) : na
line.delete(R4_line[1])

S1_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=S1, y2=S1, xloc=xloc.bar_time, extend=extend.right, color=S1_color, width=1, style=line.style_solid) : na
line.delete(S1_line[1])
S2_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=S2, y2=S2, xloc=xloc.bar_time, extend=extend.right, color=S2_color, width=2, style=line.style_solid) : na
line.delete(S2_line[1])
S3_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=S3, y2=S3, xloc=xloc.bar_time, extend=extend.right, color=S3_color, width=1, style=line.style_solid) : na
line.delete(S3_line[1])
S4_line = disp_p_extend ? line.new(x1=timenow, x2=ll_offset, y1=S4, y2=S4, xloc=xloc.bar_time, extend=extend.right, color=S4_color, width=2, style=line.style_solid) : na
line.delete(S4_line[1])

pivot_label = disp_p_labels ? label.new(x=ll_offset, y=pivot, xloc=xloc.bar_time, yloc=yloc.price, textcolor=Pivot_color, text='pivot', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(pivot_label[1])

R1_label = disp_p_labels ? label.new(x=ll_offset, y=R1, xloc=xloc.bar_time, yloc=yloc.price, textcolor=R1_color, text='R1', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(R1_label[1])
R2_label = disp_p_labels ? label.new(x=ll_offset, y=R2, xloc=xloc.bar_time, yloc=yloc.price, textcolor=R2_color, text='R2', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(R2_label[1])
R3_label = disp_p_labels ? label.new(x=ll_offset, y=R3, xloc=xloc.bar_time, yloc=yloc.price, textcolor=R3_color, text='R3', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(R3_label[1])
R4_label = disp_p_labels ? label.new(x=ll_offset, y=R4, xloc=xloc.bar_time, yloc=yloc.price, textcolor=R4_color, text='R4', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(R4_label[1])

S1_label = disp_p_labels ? label.new(x=ll_offset, y=S1, xloc=xloc.bar_time, yloc=yloc.price, textcolor=S1_color, text='S1', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(S1_label[1])
S2_label = disp_p_labels ? label.new(x=ll_offset, y=S2, xloc=xloc.bar_time, yloc=yloc.price, textcolor=S2_color, text='S2', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(S2_label[1])
S3_label = disp_p_labels ? label.new(x=ll_offset, y=S3, xloc=xloc.bar_time, yloc=yloc.price, textcolor=S3_color, text='S3', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(S3_label[1])
S4_label = disp_p_labels ? label.new(x=ll_offset, y=S4, xloc=xloc.bar_time, yloc=yloc.price, textcolor=S4_color, text='S4', style=label.style_label_down, color=Label_color, size=l_size) : na
label.delete(S4_label[1])
