ICT TGIF_V2 [MK]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
//malk1903
//-------------------------------------------------------------------------------------------

indicator(title='ICT TGIF_V2 [MK]', shorttitle='ICT TGIF_V2 [MK]', overlay=true, precision=8, max_boxes_count = 500, max_lines_count = 500)

tZone = input.string("GMT-4", "Timezone", options=["GMT+0", "GMT+1", "GMT+2", "GMT+3","GMT+4","GMT+5","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10","GMT+11","GMT+12","GMT-1", "GMT-2", "GMT-3","GMT-4","GMT-5","GMT-6","GMT-7","GMT-8","GMT-9","GMT-10","GMT-11","GMT-12"], inline="0", group="TGIF-------------------------------------------------------")
i_maxtf_tgif             = input.int         (60, "Max Timeframe", 1, 240, inline="0", group="TGIF-------------------------------------------------------")
HL_clr = input.color(color.new(color.red, 0), title="Week High/Low Line", group="TGIF Levels-------------------------------------------------------", inline = "1")
wkHL = input(true, title='', group="TGIF Levels-------------------------------------------------------", inline = "1")
HLStyle   = input.string  ("Solid", "", options=["Solid", "Dotted", "Dashed"], group="TGIF Levels-------------------------------------------------------", inline = "1")
colorlevels = input.color(color.new(color.aqua, 100), title="20-30% Border", group="TGIF Levels-------------------------------------------------------", inline = "2")
fillcolor = input.color(color.new(color.aqua, 30), title="Box", group="TGIF Levels-------------------------------------------------------", inline = "2")
lineWidth = input(title='Width', defval=1, group="TGIF Levels-------------------------------------------------------", inline = "2")
txt_col = input.color(title='Text', defval=color.new(color.silver,50), group="TGIF Levels-------------------------------------------------------", inline = "3")
txt_tgif = input.bool(title='', defval=true, group="TGIF Levels-------------------------------------------------------", inline = "3")
wkOPN_clr = input.color(color.new(color.white, 0), title="Week Open Line", group="TGIF Levels-------------------------------------------------------", inline = "4")
wkOPN = input.bool(title='', defval=true, group="TGIF Levels-------------------------------------------------------", inline = "4")
WkStyle   = input.string  ("Dashed", "", options=["Solid", "Dotted", "Dashed"], group="TGIF Levels-------------------------------------------------------", inline = "4")
linewidths = input.int(defval=1, title="Line Widths", group="TGIF Levels-------------------------------------------------------", inline = "6")

//linestyles
_WkStyle   = WkStyle ==    "Solid" ? line.style_solid : WkStyle == "Dotted" ? line.style_dotted : line.style_dashed
_HLStyle   = HLStyle ==    "Solid" ? line.style_solid : HLStyle == "Dotted" ? line.style_dotted : line.style_dashed

//var
pc_20 = 0.0
pc_30 = 0.0
pc_70 = 0.0
pc_80 = 0.0

Fri_PM = input.session(defval="0830-1600", title="Friday hours to show 20-30% levels")//input.session(title='Display Period', defval='1300-1600:6', inline="2", group="Friday Only-------------------------------------------------------", tooltip="20-30% box will only be shown during this session period, default is to only show during friday PM session")
Wk_sess = '1800-1700'

//Friday New Bar
is_newbar(sess) =>
    t = time('D', sess, tZone)
    na(t[1]) and not na(t) or t[1] < t

//Weekly New Bar
is_newbar2(sess) =>
    t = time('W', sess, tZone)
    na(t[1]) and not na(t) or t[1] < t

is_session(sess) =>
    not na(time('D', sess, tZone))

is_session2(sess) =>
    not na(time('W', sess, tZone))

//Friday PM Only
Fri_Newbar = is_newbar(Fri_PM)
Fri_Session = is_session(Fri_PM)

//New Week
WeekNewbar = is_newbar2(Wk_sess)
WeekSession = is_session2(Wk_sess)

///max tf 
disp_tgif                = timeframe.isintraday and timeframe.multiplier <= i_maxtf_tgif

///////new weekbox high low and level boxes
if WeekSession and disp_tgif
    float WkLow = na
    WkLow := if WeekSession
        if WeekNewbar
            low
        else
            math.min(WkLow[1], low)
    else
        WkLow[1]

    float WkHigh = na
    WkHigh := if WeekSession
        if WeekNewbar
            high
        else
            math.max(WkHigh[1], high)
    else
        WkHigh[1]

    ///////////////////////////////////////////
    float WkOpen = na
    WkOpen := if WeekSession
        if WeekNewbar
            open
        else
            WkOpen[1]

    /////////////////////////////////////////
    int WkStart = na
    WkStart := if WeekSession
        if WeekNewbar
            time
        else
            math.min(WkStart[1], time)
    else
        na
    
    int WkEnd = na
    WkEnd := if WeekSession
        if WeekNewbar
            time_close
        else
            math.max(WkEnd[1], time_close)
    else
        na

    pc_20 := (WkHigh - WkLow) * 0.8 + WkLow
    pc_30 := (WkHigh - WkLow) * 0.7 + WkLow
    pc_80 := (WkHigh - WkLow) * 0.2 + WkLow
    pc_70 := (WkHigh - WkLow) * 0.3 + WkLow

    weeklyhigh = if WeekNewbar
        weeklyhigh = wkHL ? line.new(WkStart, WkHigh, WkEnd, WkHigh, xloc = xloc.bar_time, style = _HLStyle, width = linewidths, color = HL_clr) : na
    
    weeklyopen = if WeekNewbar
        weeklyopen = wkOPN ? line.new(WkStart, WkOpen, WkEnd, WkOpen, xloc = xloc.bar_time, style = _WkStyle, width = linewidths, color = wkOPN_clr) : na
    
    weeklylow = if WeekNewbar
        weeklylow = wkHL ? line.new(WkStart, WkLow, WkEnd, WkLow, xloc = xloc.bar_time, style = _HLStyle, width = linewidths, color = HL_clr) : na
    
    twenty30Box = if WeekNewbar
        twenty30Box = box.new(WkStart, pc_20, WkEnd, pc_30, xloc = xloc.bar_time, bgcolor = color.new(color.black,100), border_width = lineWidth, border_color = color.new(color.black,100), text = txt_tgif ? "TGIF" : na, text_halign = text.align_right, text_size=size.normal, text_color = color.new(color.black,100)) 

    eighty70Box = if WeekNewbar
        eighty70Box = box.new(WkStart, pc_70, WkEnd, pc_80, xloc = xloc.bar_time, bgcolor = color.new(color.black,100), border_width = lineWidth, border_color = color.new(color.black,100), text = txt_tgif ? "TGIF" : na, text_halign = text.align_right, text_size=size.normal, text_color = color.new(color.black,100)) 

    if not WeekNewbar
        line.set_x2(weeklyopen[1], WkEnd)
        line.set_y1(weeklyopen[1], WkOpen)
        line.set_y2(weeklyopen[1], WkOpen)
        line.set_x2(weeklylow[1], WkEnd)
        line.set_y1(weeklylow[1], WkLow)
        line.set_y2(weeklylow[1], WkLow)
        line.set_x2(weeklyhigh[1], WkEnd)
        line.set_y1(weeklyhigh[1], WkHigh)
        line.set_y2(weeklyhigh[1], WkHigh)
        box.set_right(twenty30Box[1], WkEnd)
        box.set_top(twenty30Box[1], pc_20)
        box.set_bottom(twenty30Box[1], pc_30)
        box.set_right(eighty70Box[1], WkEnd)
        box.set_top(eighty70Box[1], pc_70)
        box.set_bottom(eighty70Box[1], pc_80)

    //has the friday PM session started, is so set 20-30% boxes left to session start time
    if Fri_Session and disp_tgif
        int Fri_Start = na
        Fri_Start := if Fri_Session
            if Fri_Newbar
                time
            else
                math.min(Fri_Start[1], time)
        else
            na

        if not Fri_Newbar
            box.set_left(twenty30Box[1], Fri_Start)
            box.set_left(eighty70Box[1], Fri_Start)
     
    is_new_week = timeframe.change("W")

    var weekly_high_price = high
    var weekly_low_price = low
    var weekly_high_time = time
    var weekly_low_time = time
    //var label weekly_high_label = label.new(na, na, "Weekly\nHigh", xloc=xloc.bar_time, yloc=yloc.abovebar, color=color.green, textcolor=color.white) //used for testing week level
    //var label weekly_low_label = label.new(na, na, "Weekly\nLow", xloc=xloc.bar_time, yloc=yloc.belowbar, color=color.red, textcolor=color.white, style=label.style_label_up) //used for testing

    timeAllowed = Fri_PM//friday high/low must be within these hours
    isWithinTime = time(timeframe.period, timeAllowed + ":6", tZone)

    if (is_new_week) //finds high of week
        weekly_high_price := high
        weekly_low_price := low
        weekly_high_time := time
        weekly_low_time := time
  
    else
        if (high > weekly_high_price) //updates high of week if breaks higher
            weekly_high_price := high
            weekly_high_time := time       

        if (low < weekly_low_price)
            weekly_low_price := low
            weekly_low_time := time

    //detects if high of week was on a friday, and is so, sets the 20-30% level to visible
    fri_up = false
    if dayofweek(weekly_high_time, tZone) == 6 
        fri_up := true
    if isWithinTime and fri_up//= time(timeframe.period, timeAllowed + ":1234567")///current bar within time..not weekly_high_time     
        box.set_text_color(twenty30Box, txt_col)
        box.set_bgcolor(twenty30Box, fillcolor)
        box.set_border_color(twenty30Box, colorlevels)

    //detects if low of week was on a friday, and is so, sets the 70-80% level to visible
    fri_dn = false
    if dayofweek(weekly_low_time, tZone) == 6 
        fri_dn := true
    if isWithinTime and fri_dn//= time(timeframe.period, timeAllowed + ":1234567")///current bar within time..not weekly_high_time    
        box.set_text_color(eighty70Box, txt_col)
        box.set_bgcolor(eighty70Box, fillcolor)
        box.set_border_color(eighty70Box, colorlevels)
 





