Weekly Opening GAP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
// © aaronmefford


indicator("Weekly GAP", shorttitle="WGaps", overlay=true )

daily_session = input.session("0930-1600","Session Period",options=["0930-1600"])
boxColor = input.color(color.orange,"Box Color")
showBoxes = input.bool(true,"Show Boxes")
showLines = input.bool(true,"Show Lines")
showWeek2 = input.bool(true,"Show Last Week Lines")
showWeek3 = input.bool(true,"Show Third Week Lines")
showWeek4 = input.bool(true,"Show Fourth Week Lines")
showWeek5 = input.bool(false,"Show Fifth Week Lines")
showWeek6 = input.bool(false,"Show Sixth Week Lines")
showWeek7 = input.bool(false,"Show Seventh Week Lines")
showWeek8 = input.bool(false,"Show Eighth Week Lines")
showWeek9 = input.bool(false,"Show Ninth Week Lines")
showWeek10 = input.bool(false,"Show Tenth Week Lines")

int week = na

is_session(sess) =>
    not na(time(timeframe.period, sess, "America/New_York"))

bool inSession = is_session(daily_session)

float daily_close = na
float weekly_close = na
float gap_half = na

var line[] gap_levels = array.new_line()
var box[] gap_boxes = array.new_box()

float week1_top = na
float week1_bottom = na
float week2_top = na
float week2_bottom = na
float week3_top = na
float week3_bottom = na
float week4_top = na
float week4_bottom = na
float week5_top = na
float week5_bottom = na
float week6_top = na
float week6_bottom = na
float week7_top = na
float week7_bottom = na
float week8_top = na
float week8_bottom = na
float week9_top = na
float week9_bottom = na
float week10_top = na
float week10_bottom = na

daily_close := daily_close[1]
weekly_close := weekly_close[1]
week := week[1]

week1_top := week1_top[1]
week1_bottom := week1_bottom[1]
week2_top := week2_top[1]
week2_bottom := week2_bottom[1]
week3_top := week3_top[1]
week3_bottom := week3_bottom[1]
week4_top := week4_top[1]
week4_bottom := week4_bottom[1]
week5_top := week5_top[1]
week5_bottom := week5_bottom[1]
week6_top := week6_top[1]
week6_bottom := week6_bottom[1]
week7_top := week7_top[1]
week7_bottom := week7_bottom[1]
week8_top := week8_top[1]
week8_bottom := week8_bottom[1]
week9_top := week9_top[1]
week9_bottom := week9_bottom[1]
week10_top := week10_top[1]
week10_bottom := week10_bottom[1]


if inSession[1] == true and not inSession
    daily_close := na
else if inSession[2] == true and not inSession[1] and not inSession
    daily_close := close[2]
    
if inSession
    week := weekofyear(time(timeframe.period), "America/New_York")

bool new_week = not inSession[1] and inSession and week != week[1]

if new_week
    weekly_close := na
    week1_top := na
    week1_bottom := na
    week2_top := na
    week2_bottom := na
    week3_top := na
    week3_bottom := na
    week4_top := na
    week4_bottom := na
    week5_top := na
    week5_bottom := na
    week6_top := na
    week6_bottom := na
    week7_top := na
    week7_bottom := na
    week8_top := na
    week8_bottom := na
    week9_top := na
    week9_bottom := na
    week10_top := na
    week10_bottom := na

else if new_week[1]
    weekly_close := daily_close[2]
    
    level = line.new(bar_index-1,weekly_close,bar_index,weekly_close,xloc.bar_index,extend = extend.right,color = boxColor,style = line.style_solid,width=4)
    array.push(gap_levels,level)
    
    top = math.max(open[1],weekly_close)
    bottom = math.min(open[1], weekly_close)
    gap_qtr = (top-bottom) / 4 
    array.push(gap_levels,line.new(bar_index-1,bottom+gap_qtr*1,bar_index,bottom+gap_qtr*1,xloc.bar_index,extend = extend.right,color = boxColor,style = line.style_dashed,width=1))
    array.push(gap_levels,line.new(bar_index-1,bottom+gap_qtr*2,bar_index,bottom+gap_qtr*2,xloc.bar_index,extend = extend.right,color = boxColor,style = line.style_dashed,width=1))
    array.push(gap_levels,line.new(bar_index-1,bottom+gap_qtr*3,bar_index,bottom+gap_qtr*3,xloc.bar_index,extend = extend.right,color = boxColor,style = line.style_dashed,width=1))
    array.push(gap_boxes,box.new(bar_index-1,top,bar_index+10,bottom,boxColor,2,line.style_solid,extend.right,xloc.bar_index,color.new(boxColor,80)))

    week1_top := top
    week1_bottom := bottom
    week2_top := week1_top[2]
    week2_bottom := week1_bottom[2]
    week3_top := week2_top[2]
    week3_bottom := week2_bottom[2]
    week4_top := week3_top[2]
    week4_bottom := week3_bottom[2]
    week5_top := week4_top[2]
    week5_bottom := week4_bottom[2]
    week6_top := week5_top[2]
    week6_bottom := week5_bottom[2]
    week7_top := week6_top[2]
    week7_bottom := week6_bottom[2]
    week8_top := week7_top[2]
    week8_bottom := week7_bottom[2]
    week9_top := week8_top[2]
    week9_bottom := week8_bottom[2]
    week10_top := week9_top[2]
    week10_bottom := week9_bottom[2]


if inSession
    for [lineNo,this_line] in gap_levels
        y = line.get_y2(this_line)
        if (low <= y and high >= y)
            array.remove(gap_levels,lineNo)
            
            line.set_x2(this_line,bar_index)
            line.set_extend(this_line,extend.none)

    for [lineNo,gap_box] in gap_boxes
        h = box.get_top(gap_box)
        l = box.get_bottom(gap_box)
        if (low < h and high[1] >= h)
            array.remove(gap_boxes,lineNo)
            box.set_right(gap_box,bar_index)
            box.set_extend(gap_box,extend.none)

            if low > l
                array.push(gap_boxes,box.new(bar_index,low,bar_index+1,l,boxColor,2,line.style_solid,extend.right,xloc.bar_index,color.new(boxColor,90)))
            

        else if ( high > l and low[1] <= l)
            array.remove(gap_boxes,lineNo)
            box.set_right(gap_box,bar_index)
            box.set_extend(gap_box,extend.none)
            
            if high < h
                array.push(gap_boxes,box.new(bar_index,h,bar_index+1,high,boxColor,2,line.style_solid,extend.right,xloc.bar_index,color.new(boxColor,90)))
            
                
plot(showLines?week1_top:na,"Week1 Top",color.aqua,2,plot.style_linebr,display=display.pane)
plot(showLines?week1_bottom:na,"Week1 Bottom",color.aqua,2,plot.style_linebr,display=display.pane)
plot(showWeek2?week2_top:na,"Week2 Top",color.fuchsia,2,plot.style_linebr,display=display.pane)
plot(showWeek2?week2_bottom:na,"Week2 Bottom",color.fuchsia,2,plot.style_linebr,display=display.pane)
plot(showWeek3?week3_top:na,"Week3 Top",color.teal,2,plot.style_linebr,display=display.pane)
plot(showWeek3?week3_bottom:na,"Week3 Bottom",color.teal,2,plot.style_linebr,display=display.pane)
plot(showWeek4?week4_top:na,"Week2 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek4?week4_bottom:na,"Week2 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek5?week5_top:na,"Week5 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek5?week5_bottom:na,"Week5 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek6?week6_top:na,"Week6 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek6?week6_bottom:na,"Week6 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek7?week7_top:na,"Week7 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek7?week7_bottom:na,"Week7 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek8?week8_top:na,"Week8 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek8?week8_bottom:na,"Week8 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek9?week9_top:na,"Week9 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek9?week9_bottom:na,"Week9 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek10?week10_top:na,"Week9 Top",color.lime,2,plot.style_linebr,display=display.pane)
plot(showWeek10?week10_bottom:na,"Week9 Bottom",color.lime,2,plot.style_linebr,display=display.pane)
