ICT Opening Gaps [MK]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © malk1903
//@version=5
indicator(title="ICT Opening Gaps [MK]", shorttitle = 'NWOG/NDOG/VI [MK]',overlay=true, max_boxes_count = 500, max_labels_count = 500)
i_maxtf             = 1440
disp                = timeframe.isintraday and timeframe.multiplier <= i_maxtf

volimba = input.bool(defval=true, title='Daily Volume Imbalance', group='Enable/Disable Section---------------------------------------------', inline = '01')
MOG = input.bool(defval=true, title='NMOG', group='Enable/Disable Section---------------------------------------------', inline = '01b')
prevgaps_m = input.int(20, "", group='Enable/Disable Section---------------------------------------------', inline = '01b')
bool showOnHistory_m = input.bool(true, "Show Previous NMOGs", group='Enable/Disable Section---------------------------------------------', inline = '01b')
WOG = input.bool(defval=true, title='NWOG', group='Enable/Disable Section---------------------------------------------', inline = '02')
prevgaps = input.int(5, "", group='Enable/Disable Section---------------------------------------------', inline = '02')
bool showOnHistory = input.bool(true, "Show Previous NWOGs", group='Enable/Disable Section---------------------------------------------', inline = '02')
sessiongaps = input.bool(defval=true, title='NDOG', group='Enable/Disable Section---------------------------------------------', inline = '03')
prevgapsdy = input.int(5, "", group='Enable/Disable Section---------------------------------------------', inline = '03')
bool showOnHistorydy = input.bool(false, "Show Previous NDOGs", group='Enable/Disable Section---------------------------------------------', inline = '03')
showBox = input.bool(true, "RTH Opening Gap ", group='Enable/Disable Section---------------------------------------------', inline = '05', tooltip = "Shows the standard RTH gap from the close at 4pm to 9:30am open (New York Time)")
timeZone = input.string("GMT-4", title="Time Zone", options=["GMT-10","GMT-9","GMT-8","GMT-7","GMT-6","GMT-5","GMT-4","GMT-3","GMT-2","GMT-1","GMT","GMT+1","GMT+2","GMT+3","GMT+4","GMT+5","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10"], group="Timezone")

/////////////////////////////////////////////Volume Imbalances
boxlength = 1
viMaxBoxSet = input.int(defval=6, title="Maximum VI Box Displayed", group='Daily Volume Imbalances-----------------------------------------', inline='1')
mitig_typeVI          =  "Body"
UseBodyForMitigationVI =   mitig_typeVI == "Body" ? true : false
VOLimb_extend = true
tfvi = "D"
color c_box = input.color(color.new(color.silver,30), 'Volume Imbalance Box', group='Daily Volume Imbalances-----------------------------------------', inline='3')
color c_bord = input.color(color.new(color.silver,100), 'Volume Imbalance Border', group='Daily Volume Imbalances-----------------------------------------', inline='3')
color mit_c = input.color(color.new(color.orange,100), title = 'Fully Mitigated Box', group='Daily Volume Imbalances-----------------------------------------', inline='4')
color mitbord_c = input.color(color.new(color.orange,100), title = 'Fully Mitigated Border', group='Daily Volume Imbalances-----------------------------------------', inline='4', tooltip ="Full mitigation is when imbalance has been passed through with buy side + sell side")
i_BoxBordervi = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group='Daily Volume Imbalances-----------------------------------------', inline="5")
BoxBorderw = input.int(defval=0, title="Width", minval = 0,maxval =4, step = 1, group='Daily Volume Imbalances-----------------------------------------', inline='5')
novolsun = input.bool(defval=true, title="Dont Show Daily Volume Imbalance on top of New Week Open Gap", tooltip="Daily Volume Imbalances and New Week Opening Gaps will both be displayed in the same area, Enabling this checkbox will remove the Daily volume imbalance so it does not overlap the New Week Opening Gap", inline="6")
plotBoxLabelvi = false
BoxLabelColorvi = color.new(color.white,0)
BoxLabelSizevi = size.small

var box[]   greenboxes     = array.new_box()
var box[]   redboxes     = array.new_box()

line_style_function(input_var) =>
    switch input_var
        "dotted (┈)" => line.style_dotted
        "dashed (╌)" => line.style_dashed
        => line.style_solid

BoxBordervi = line_style_function(i_BoxBordervi)

//Data
open0 = request.security(syminfo.tickerid, tfvi, open[0], barmerge.gaps_on, barmerge.lookahead_on)
open1 = request.security(syminfo.tickerid, tfvi, open[1], barmerge.gaps_on, barmerge.lookahead_on)
high0 = request.security(syminfo.tickerid, tfvi, high[0], barmerge.gaps_on, barmerge.lookahead_on)
high1 = request.security(syminfo.tickerid, tfvi, high[1], barmerge.gaps_on, barmerge.lookahead_on)
low0 = request.security(syminfo.tickerid, tfvi, low[0], barmerge.gaps_on, barmerge.lookahead_on)
low1 = request.security(syminfo.tickerid, tfvi, low[1], barmerge.gaps_on, barmerge.lookahead_on)
close0 = request.security(syminfo.tickerid, tfvi, close[0], barmerge.gaps_on, barmerge.lookahead_on)
close1 = request.security(syminfo.tickerid, tfvi, close[1], barmerge.gaps_on, barmerge.lookahead_on)

UpVoid_old = close1 < open0
UpVoid = open0 > close1 and low0 <= high1 and close0 > close1 and open0 > open1
UpVoidsize = open0 - close1

//is candle and precandle both red or both green
bothred = false
bothgreen = false
if close0 > open0 and close1 > open1
    bothgreen := true
    
if (close0 < open0 and close1 < open1) //or (close0 < open0 and close1 < open1)
    bothred := true
    
//timeframe good
tfgoodwk = false
if not timeframe.isweekly
    tfgoodwk := true
tfgoodmth = false
if not timeframe.ismonthly
    tfgoodmth :=true

//is first candle[1] green or red?
col_1grn = false
if close1 > open1
    col_1grn := true
col_0grn = false
if close0 > open0
    col_0grn := true

VIMB_SUN = time('1', "1800-1801",timeZone)//Time input used for removing D volume imbalance/Sunday Opening Gap overlap

if UpVoid and UpVoidsize > 0 and volimba and tfgoodwk and tfgoodmth //and disp//and novimb
    BOX1 = box.new(left=bar_index[1], top=col_0grn ? open0 : close0, right=bar_index[0] + boxlength, bottom = col_1grn ? close1 : open1, border_style=BoxBordervi, text_halign=text.align_right, text_valign=text.align_center, text_size=BoxLabelSizevi, text_color=BoxLabelColorvi, border_width = BoxBorderw)
    if VIMB_SUN and novolsun
        box.set_bgcolor(BOX1, color.new(color.black,100))
        box.set_border_color(BOX1, color.new(color.black,100))
    else
        box.set_bgcolor(BOX1, c_box)
        box.set_border_color(BOX1, c_bord)
    if array.size(greenboxes) > viMaxBoxSet
        box.delete(array.shift(greenboxes))
    array.push(greenboxes, BOX1)

BottomVoid_old = close1 > open0
BottomVoid = open0 < close1 and open0 < open1 and high0 >= low1 and close0 < close1 and close0 < open1
BottomVoidsize = close1 - open0
//  
if BottomVoid and BottomVoidsize > 0 and volimba and tfgoodwk and tfgoodmth //and disp //and novimb
    BOX2 = box.new(left=bar_index[1], top = col_1grn ? open1 : close1, right=bar_index[0] + boxlength, bottom=col_0grn ? close0 : open0, border_style=BoxBordervi, text_halign=text.align_right, text_valign=text.align_center, text_size=BoxLabelSizevi, text_color=BoxLabelColorvi, border_width = BoxBorderw)
    if VIMB_SUN and novolsun
        box.set_bgcolor(BOX2, color.new(color.black,100))
        box.set_border_color(BOX2, color.new(color.black,100))
    else
        box.set_bgcolor(BOX2, c_box)
        box.set_border_color(BOX2, c_bord)
    if array.size(redboxes) > viMaxBoxSet
        box.delete(array.shift(redboxes))
    array.push(redboxes, BOX2)

if barstate.isconfirmed //and tfgood3
    if array.size(greenboxes) > 0 
        for i = array.size(greenboxes) - 1 to 0 by 1
            tbox2 = array.get(greenboxes, i)
            top2 = box.get_top(tbox2)
            bottom2 = box.get_bottom(tbox2)
            ago2 = box.get_left(tbox2)
            if VOLimb_extend
                box.set_right(tbox2, bar_index + 1)
            //////////////////Use body for mitigation
            //greenboxes+red candle
            redc = open0-close0
            greenc = close0-open0
            if UseBodyForMitigationVI
            //greenboxes+red candle
                if (open0 > top2 and close0 < bottom2 and redc>0) or (open0 < bottom2 and close0 > top2 and greenc>0)
                    box.set_bgcolor(tbox2,mit_c)
                    box.set_border_color(tbox2, mitbord_c)
                    box.set_right(tbox2, last_bar_index)
            // //greenboxes+green candle 
            //     if open0 < bottom2 and close0 > top2 and greenc>0 
            //         box.set_bgcolor(tbox2,mit_c)
            //         box.set_border_color(tbox2, mitbord_c)
            //         box.set_text_color(tbox2, mitbord_c)
            //         box.set_right(tbox2, last_bar_index)
            //         array.remove(greenboxes, i)
            if not UseBodyForMitigationVI
            //greenboxes+red candle
                if (high0 > top2 and low0 < bottom2 and redc>0) or (low0 < bottom2 and high0 > top2 and greenc>0)
                    box.set_bgcolor(tbox2,mit_c)
                    box.set_border_color(tbox2, mitbord_c)
                    box.set_right(tbox2, last_bar_index)
        //    //greenboxes+green candle 
        //         if low0 < bottom2 and high0 > top2 and greenc>0
        //             box.set_bgcolor(tbox2,mit_c)
        //             box.set_border_color(tbox2, mitbord_c)
        //             box.set_text_color(tbox2, mitbord_c)
        //             box.set_right(tbox2, last_bar_index)
        //             array.remove(greenboxes, i)

           
    if array.size(redboxes) > 0 //and UseBodyForMitigationVI
        for i = array.size(redboxes) - 1 to 0 by 1
            tbox3 = array.get(redboxes, i)
            top3 = box.get_top(tbox3)
            bottom3 = box.get_bottom(tbox3)
            ago3 = box.get_left(tbox3)
            redc = open0-close0
            greenc = close0-open0
            if VOLimb_extend
                box.set_right(tbox3, bar_index + 1)
            //redboxes+green candle
            if UseBodyForMitigationVI
                if (open0 < bottom3 and close0 > top3 and greenc>0) or (open0 > top3 and close0 < bottom3 and redc>0)//and UseBodyForMitigationVI
                    box.set_bgcolor(tbox3,mit_c)
                    box.set_border_color(tbox3, mitbord_c)
                    box.set_right(tbox3, last_bar_index)
            // //redboxes+red candle
            //     if open0 > top3 and close0 < bottom3 and redc>0 //and UseBodyForMitigationVI
            //         box.set_bgcolor(tbox3,mit_c)
            //         box.set_border_color(tbox3,mit_c)
            //         box.set_text_color(tbox3, mitbord_c)
            //         box.set_right(tbox3, last_bar_index)         
            //         array.remove(redboxes, i)
            if not UseBodyForMitigationVI
            //redboxes+green candle
                if (low0 < bottom3 and high0 > top3 and greenc>0) or (high0 > top3 and low0 < bottom3 and redc>0)
                    box.set_bgcolor(tbox3,mit_c)
                    box.set_border_color(tbox3, mitbord_c)
                    box.set_right(tbox3, last_bar_index)
            // //redboxes+red candle
            //     if high0 > top3 and low0 < bottom3 and redc>0
            //         box.set_bgcolor(tbox3,mit_c)
            //         box.set_border_color(tbox3,mit_c)
            //         box.set_text_color(tbox3, mitbord_c)
            //         box.set_right(tbox3, last_bar_index)         
            //         array.remove(redboxes, i)

////////////////////////////////////////////////////////////////////////////////////New Week Opening Gap

isNewbar(res) =>
    t = time(res)
    not na(t) and (na(t[1]) or t > t[1])

type gap
    float high
    float low
    int start
    int end

color boxBg_curr = input.color(color.new(color.aqua,70), "Current", group = "New Week Opening Gap----------------------------------------------", inline="3")
color boxBg = input.color(color.new(color.blue,80), "Box", group = "New Week Opening Gap----------------------------------------------", inline="3")
color boxBc = input.color(color.new(color.white,100), "Border", group = "New Week Opening Gap----------------------------------------------", inline="3")
i_BoxBordernw = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Week Opening Gap----------------------------------------------", inline="3")
BoxBorderwnw = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group = "New Week Opening Gap----------------------------------------------", inline='3')
color boxMl = input.color(color.new(color.silver,0), "Midline", group = "New Week Opening Gap----------------------------------------------", inline="4")
i_mid_line_style = input.string(defval="dotted (┈)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Week Opening Gap----------------------------------------------", inline="4")
mid_line_width = input.int(2, "Width", group = "New Week Opening Gap----------------------------------------------", inline="4")
showhlprices = input.bool(defval=false, title="Show H/L price labels")

BoxBordernw = line_style_function(i_BoxBordernw)
mid_line_style = line_style_function(i_mid_line_style)

getGapData() =>
    gap.new(math.max(close[1], open), math.min(close[1], open), time, time_close)

gap weeklyOpenGap = request.security(syminfo.tickerid, "1W", getGapData(), lookahead =  barmerge.lookahead_on)
int currentDayTimeClose = request.security(syminfo.tickerid, "1D", time_close, lookahead =  barmerge.lookahead_on)
var box[]   sgbox     = array.new_box()
var line[]   sgline   = array.new_line() 
var label[] p_labelsh = array.new_label() 
var label[] p_labelsl = array.new_label() 
var line gapmid = na
var label plabelsh = na

linegap = 0.0
linegap := (weeklyOpenGap.high+weeklyOpenGap.low)/2

if isNewbar("W") and (timeframe.isdaily or timeframe.isintraday) and WOG and disp
    weeklyOpenGapBox = box.new(bar_index, weeklyOpenGap.high, last_bar_index, weeklyOpenGap.low, xloc = xloc.bar_index, bgcolor = boxBg_curr, border_width = 1, border_color = boxBc)
    if showhlprices
        plabelsh :=label.new(last_bar_index,linegap, text="high: " + str.tostring(weeklyOpenGap.high) + " low: " + str.tostring(weeklyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_left, size=size.normal)
    //plabelsl :=label.new(last_bar_index,linegap, text=str.tostring(weeklyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_up)
    if array.size(sgbox) > prevgaps
        box.delete(array.shift(sgbox))
        box.set_bgcolor(weeklyOpenGapBox[1],color.white)
            //box.set_right(sgbox, last_bar_index) 
    array.push(sgbox, weeklyOpenGapBox)
    if array.size(p_labelsh) > prevgaps
        label.delete(array.shift(p_labelsh))
    array.push(p_labelsh, plabelsh)

    gapmid := line.new(x1=bar_index, y1=linegap, x2= last_bar_index, y2=linegap, xloc=xloc.bar_index, color=boxMl, style=mid_line_style, width=mid_line_width)
    if array.size(sgline) > prevgaps
        line.delete(array.shift(sgline))
    array.push(sgline, gapmid)

    if not showOnHistory
        box.delete(weeklyOpenGapBox[1])
        line.delete(gapmid[1])

    highlight=true
    if highlight
        box.set_bgcolor(weeklyOpenGapBox[1],boxBg)

////////////////////////////////////////////////////////////////////////////////////New Month Opening Gap

color boxBg_curr_m = input.color(color.new(color.lime,30), "Current", group = "New Month Opening Gap--------------------------------------------", inline="3")
color boxBg_m = input.color(color.new(color.lime,50), "Box", group = "New Month Opening Gap--------------------------------------------", inline="3")
color boxBc_m = input.color(color.new(color.white,100), "Border", group = "New Month Opening Gap--------------------------------------------", inline="3")
i_BoxBordernw_m = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Month Opening Gap--------------------------------------------", inline="3")
BoxBorderwnw_m = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group = "New Month Opening Gap--------------------------------------------", inline='3')
color boxMl_m = input.color(color.new(color.silver,0), "Midline", group = "New Month Opening Gap--------------------------------------------", inline="4")
i_mid_line_style_m = input.string(defval="dotted (┈)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Month Opening Gap--------------------------------------------", inline="4")
mid_line_width_m = input.int(2, "Width", group = "New Month Opening Gap--------------------------------------------", inline="4")
showhlprices_m = input.bool(defval=false, title="Show H/L price labels", group = "New Month Opening Gap--------------------------------------------", inline="4")

BoxBordernw_m = line_style_function(i_BoxBordernw_m)
mid_line_style_m = line_style_function(i_mid_line_style_m)

gap monthlyOpenGap = request.security(syminfo.tickerid, "M", getGapData(), lookahead =  barmerge.lookahead_on)
var box[]   sgbox_m     = array.new_box()
var line[]   sgline_m   = array.new_line() 
var label[] p_labelsh_m = array.new_label() 
var label[] p_labelsl_m = array.new_label() 
var line gapmid_m = na
var label plabelsh_m = na

linegap_m = 0.0
linegap_m := (monthlyOpenGap.high+monthlyOpenGap.low)/2

if isNewbar("M") and (timeframe.isdaily or timeframe.isintraday or timeframe.isweekly or timeframe.ismonthly) and MOG //and disp
    monthlyOpenGapBox = box.new(bar_index, monthlyOpenGap.high, last_bar_index, monthlyOpenGap.low, xloc = xloc.bar_index, bgcolor = boxBg_curr_m, border_width = BoxBorderwnw_m, border_color = boxBc_m)
    if showhlprices_m
        plabelsh_m :=label.new(last_bar_index,linegap_m, text="high: " + str.tostring(monthlyOpenGap.high) + " low: " + str.tostring(monthlyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_left, size=size.normal)
    //plabelsl :=label.new(last_bar_index,linegap, text=str.tostring(weeklyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_up)
    if array.size(sgbox_m) > prevgaps_m
        box.delete(array.shift(sgbox_m))
        box.set_bgcolor(monthlyOpenGapBox[1],color.white)
            //box.set_right(sgbox, last_bar_index) 
    array.push(sgbox_m, monthlyOpenGapBox)
    if array.size(p_labelsh_m) > prevgaps_m
        label.delete(array.shift(p_labelsh_m))
    array.push(p_labelsh_m, plabelsh_m)

    gapmid_m := line.new(x1=bar_index, y1=linegap_m, x2= last_bar_index, y2=linegap_m, xloc=xloc.bar_index, color=boxMl_m, style=mid_line_style_m, width=mid_line_width_m)
    if array.size(sgline_m) > prevgaps_m
        line.delete(array.shift(sgline_m))
    array.push(sgline_m, gapmid_m)

    if not showOnHistory_m
        box.delete(monthlyOpenGapBox[1])
        line.delete(gapmid_m[1])

    highlight=true
    if highlight
        box.set_bgcolor(monthlyOpenGapBox[1],boxBg_m)
////////////////////////////////////////////////////////////////////////////////////Daily Opening Gap

color boxBg_currdy = input.color(color.new(color.white,100), "Current", group = "New Day Opening Gap----------------------------------------------", inline="3")
color boxBgdy = input.color(color.new(color.red,100), "Box", group = "New Day Opening Gap----------------------------------------------", inline="3")
color boxBcdy = input.color(color.new(color.white,100), "Border", group = "New Day Opening Gap----------------------------------------------", inline="3")
i_BoxBorderdy = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Day Opening Gap----------------------------------------------", inline="3")
BoxBorderwdy = input.int(defval=1, title="Width", minval = 1,maxval =4, step = 1, group = "New Day Opening Gap----------------------------------------------", inline='3')
color day_hl = input.color(color.new(color.red,0), "High/Low", group = "New Day Opening Gap----------------------------------------------", inline="4")
i_day_line_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], group = "New Day Opening Gap----------------------------------------------", inline="4")
day_line_width = input.int(2, "Width", group = "New Day Opening Gap----------------------------------------------", inline="4")
showhlpricesdy = input.bool(defval=false, title="Show H/L price labels", group = "New Day Opening Gap----------------------------------------------", inline="5")
NDOG_sym = input.bool(defval=true,title="⭐ Symbol for Current NDOG", group = "New Day Opening Gap----------------------------------------------", inline="5")

BoxBorderdy = line_style_function(i_BoxBorderdy)
day_line_style = line_style_function(i_day_line_style)

gap dailyOpenGap = request.security(syminfo.tickerid, "1D", getGapData(), lookahead =  barmerge.lookahead_on)

var box[]   sgboxdy     = array.new_box()
var line[]   hi_linedy   = array.new_line() 
var line[]   lo_linedy   = array.new_line()
var label[] p_labelshdy = array.new_label() 
var label[] p_labelsldy = array.new_label() 
var line d_hi = na
var line d_lo = na
var label plabelshdy = na
var label label_sym = na

linegapdy = 0.0
linegapdy := (dailyOpenGap.high+dailyOpenGap.low)/2

isMon() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.monday and close ? 1 : 0
isTue() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.tuesday and close ? 1 : 0
isWed() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.wednesday and close ? 1 : 0
isThu() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.thursday and close ? 1 : 0
isFri() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.friday and close ? 1 : 0
isSun() => dayofweek(time('D',"","America/New_York"), ("GMT-4")) == dayofweek.sunday and close ? 1 : 0

weekdays = isMon() or isTue() or isWed() or isThu() or isFri() or isSun()

if isNewbar("D") and (timeframe.isdaily or timeframe.isintraday) and sessiongaps and weekdays and disp
    dailyOpenGapBox = box.new(bar_index, dailyOpenGap.high, last_bar_index, dailyOpenGap.low, xloc = xloc.bar_index, bgcolor = boxBg_currdy, border_width = BoxBorderwdy, border_color = boxBcdy)
    if showhlpricesdy
        plabelshdy :=label.new(last_bar_index,linegapdy, text="high: " + str.tostring(dailyOpenGap.high) + " low: " + str.tostring(dailyOpenGap.low), xloc=xloc.bar_index, style=label.style_label_left, size=size.normal, color=color.red)

    if array.size(sgboxdy) > prevgapsdy
        box.delete(array.shift(sgboxdy))
        box.set_bgcolor(dailyOpenGapBox[1],color.white)
 
    array.push(sgboxdy, dailyOpenGapBox)
    if array.size(p_labelshdy) > prevgapsdy
        label.delete(array.shift(p_labelshdy))
    array.push(p_labelshdy, plabelshdy)

    d_hi := line.new(x1=bar_index, y1=dailyOpenGap.high, x2= last_bar_index, y2=dailyOpenGap.high, xloc=xloc.bar_index, color=day_hl, style=day_line_style, width=day_line_width)
    d_lo := line.new(x1=bar_index, y1=dailyOpenGap.low, x2= last_bar_index, y2=dailyOpenGap.low, xloc=xloc.bar_index, color=day_hl, style=day_line_style, width=day_line_width)
    if array.size(hi_linedy) > prevgapsdy
        line.delete(array.shift(hi_linedy))
    array.push(hi_linedy, d_hi)
    if array.size(lo_linedy) > prevgapsdy
        line.delete(array.shift(lo_linedy))
    array.push(lo_linedy, d_lo)
    if NDOG_sym
        label_sym := label.new(x=last_bar_index, y=(dailyOpenGap.high+dailyOpenGap.low) / 2, text='⭐', xloc=xloc.bar_index, color=color.new(color.yellow,100), textcolor=color.white,size=size.normal, textalign = text.align_center, style=label.style_label_left)
        label.delete(label_sym[1])

    if not showOnHistorydy
        box.delete(dailyOpenGapBox[1])
        line.delete(d_hi[1])
        line.delete(d_lo[1])

    highlight=true
    if highlight
        box.set_bgcolor(dailyOpenGapBox[1],boxBgdy)
  
///////////////////////////////////////////////////////////RTH Gaps II
longDay = 24*1000*3600
isSPY = str.tostring(syminfo.ticker) =="SPY"? true:false
_16_15= timestamp(timeZone, year, month, dayofmonth, 16, 15, 00)
_9_30 = timestamp(timeZone, year, month, dayofmonth, 9, 30, 00)
colorNone = color.new(color.white, 100)

boxColor = input.color(color.new(color.purple, 75), "Standard Gap Box", inline ='-1', group = 'RTH Gaps II---------------------------------------------')
borderColor=input.color(color.new(color.purple, 0), "Border", inline ='-1', group = 'RTH Gaps II---------------------------------------------')
border_w = input.int(defval=1, minval=1, maxval=4, step =1, title='Width', inline ='-1', group = 'RTH Gaps II---------------------------------------------')
boxesToShow =input.int(3, "Historical Boxes to Show", inline = '1', group = 'RTH Gaps II---------------------------------------------')
showTxt = input.bool(true, "Text", inline='2', group = 'RTH Gaps II---------------------------------------------')
boxTxtCol = input.color(color.new(color.gray, 30), "", inline ='2', group = 'RTH Gaps II---------------------------------------------')
ext_lb = input.bool(false, title="Extend Boxes to Current Bar", inline ='2', group = 'RTH Gaps II---------------------------------------------')
hoursfwd = input.float(defval=1, minval=0.5, maxval=6.5, step=0.5, title="Project Hours Forward", inline ='2', group = 'RTH Gaps II---------------------------------------------')

RThmid_cl = input.color(color.new(color.white,0), "Midline", inline ='3', group = 'RTH Gaps II---------------------------------------------')
i_RTHmid_line_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], inline ='3', group = 'RTH Gaps II---------------------------------------------')
RTHmid_line_width = input.int(2, "Width", inline ='3', group = 'RTH Gaps II---------------------------------------------')

RTh2575_cl = input.color(color.new(color.white,0), "25%/75%", inline ='4', group = 'RTH Gaps II---------------------------------------------')
i_RTH2575_line_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], inline ='4', group = 'RTH Gaps II---------------------------------------------')
RTH2575_line_width = input.int(2, "Width", inline ='4', group = 'RTH Gaps II---------------------------------------------')

linesToShow=input.int(3, "Historical Lines", group = 'RTH close lines & label')
show4pmLine =input.bool(false, "Previous RTH close line", inline ='2', group = 'RTH close lines & label')
show4pmLab = input.bool(false, "Price Label", inline ='2', group = 'RTH close lines & label')
extendLineRight = input.bool (false, "Extend Lines Right", inline="2", group = 'RTH close lines & label')
fourpmColor = input.color(color.new(color.silver, 10), "Line", inline ='3', group = 'RTH close lines & label')
i_RTH_line_style = input.string(defval="solid (─)", title="", options=["solid (─)", "dotted (┈)", "dashed (╌)"], inline="3", group = 'RTH close lines & label')
RTH_w = input.int(defval=3, title="Width", inline="3", group = 'RTH close lines & label')
extChoiceLine = extendLineRight?extend.right:extend.none

RTH_line_style = line_style_function(i_RTH_line_style)
RTHmid_line_style = line_style_function(i_RTHmid_line_style)
RTH2575_line_style = line_style_function(i_RTH2575_line_style)

day = math.round(hoursfwd*1000*3600)

_minute = 1000*60
fourpmCl =  ta.valuewhen(time_close ==_16_15, close, 0)
var line line = na
var label lab1 =na
var line[]   RTH_mid   = array.new_line()
var line[]   RTH_75   = array.new_line()
var line[]   RTH_25   = array.new_line()
var line RTHmid_ln = na
var line RTH75_ln = na
var line RTH25_ln = na
var array<line> lineArr = array.new<line>(0)
var array<label> labelArr = array.new<label>(0)
var box bx = na
var array<box> boxArr = array.new<box>(0)
if time == _9_30 and showBox //and pre_range and disp_RTHsess
    bx:=box.new(time, fourpmCl, ext_lb ? last_bar_time : time+day,open, xloc = xloc.bar_time,bgcolor = boxColor, border_color = borderColor, text =showTxt?'RTH\nGap':"", text_color = boxTxtCol, text_size = size.normal, text_valign =text.align_center, text_halign = text.align_right, border_width=border_w)
    array.push(boxArr,bx)
    if array.size(boxArr)>boxesToShow
        box.delete(array.shift(boxArr))
    RTH75 = (((fourpmCl+open)/2) + (fourpmCl))/2 //mid + top/2
    RTH75_ln:=line.new(time,RTH75, ext_lb ? last_bar_time : time+day,RTH75, xloc.bar_time, color=RTh2575_cl, style=RTH2575_line_style, width=RTH2575_line_width)
    if array.size(RTH_75) > boxesToShow - 1
        line.delete(array.shift(RTH_75))
    array.push(RTH_75, RTH75_ln)
    RTH25 = (((fourpmCl+open)/2) + (open))/2 //mid + bottom/2
    RTH25_ln:=line.new(time,RTH25, ext_lb ? last_bar_time : time+day,RTH25, xloc.bar_time, color=RTh2575_cl, style=RTH2575_line_style, width=RTH2575_line_width)
    if array.size(RTH_25) > boxesToShow - 1
        line.delete(array.shift(RTH_25))
    array.push(RTH_25, RTH25_ln)
    RTHmid_ln:=line.new(time,(fourpmCl+open)/2, ext_lb ? last_bar_time : time+day,(fourpmCl+open)/2, xloc.bar_time, color=RThmid_cl, style=RTHmid_line_style, width=RTHmid_line_width)
    if array.size(RTH_mid) > boxesToShow - 1
        line.delete(array.shift(RTH_mid))
    array.push(RTH_mid, RTHmid_ln)

    line:=line.new(time, fourpmCl, last_bar_time, fourpmCl, xloc=xloc.bar_time, color = show4pmLine?fourpmColor:colorNone, extend =extChoiceLine , style=RTH_line_style, width =RTH_w)
    array.push(lineArr,line)
    lab1:=label.new(last_bar_time,fourpmCl, str.tostring(fourpmCl), xloc=xloc.bar_time, textcolor = show4pmLab?fourpmColor:colorNone, style=label.style_label_left, color=color.new(color.black,100))
    array.push(labelArr,lab1)
    if array.size(lineArr)>linesToShow
        line.delete(array.shift(lineArr))
    if array.size(labelArr)>linesToShow
        label.delete(array.shift(labelArr))
    













