[FrizLabz]FVG Bar
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FFriZz

//@version=5
indicator('[FrizLabz]FVG Bar',overlay = true,max_lines_count = 500,max_labels_count = 500,max_boxes_count = 500,max_bars_back = 5000,explicit_plot_zorder = true)

import FFriZz/BoxLine_Lib/1 as obj

//Inputs
fvg_i =  input.bool(true,'FVG | On/Off?',group = 'FVG') 
cross_i =  input.bool(true,'Delete FVG when Completely filled',group = 'FVG') 
crossAdj_i = input.bool(true,'Adjust FVG when Filled/Mitigated[Delete Must be',group = 'FVG') 
fvg_w = input.int(2,'FVG Width',group = 'FVG')
count_i = input.int(100,'Display Count',group = 'FVG')
bullFvg_c = input.color(#00ff00,'Bull',group='FVG Colors',inline='C')
bearFvg_c = input.color(#ff0000,'Bear',group='FVG Colors',inline='C')

//Variables
var line[] aBull = array.new<line>()
var line[] aBear = array.new<line>()
bearFvg = high < low[2] and fvg_i   
bullFvg = low  > high[2] and fvg_i
fvgC = bullFvg ? bullFvg_c : bearFvg_c

//FVG Edit Function
fvgEdit(aBull,aBear,count) =>
    
    for s = 0 to 1
        line[] aLine = aBull
        int aSize = array.size(aBull)
        
        switch s
            1 => aSize := array.size(aBear), aLine := aBear
        
        if aSize > 0
            for i = aSize - 1 to 0
                getLine = array.get(aLine,i)
                y1 = line.get_y1(getLine)
                y2 = line.get_y2(getLine)
                cross = low < y2
                crossAdj = low < y1
                
                switch s
                    1 => cross := high > y2, crossAdj := high > y1

                if crossAdj_i and crossAdj and cross_i
                    switch s
                        0 => line.set_y1(getLine,low)
                        1 => line.set_y1(getLine,high)
                if cross_i and cross
                    line.delete(array.remove(aLine,i))
    
        if barstate.islast
            if aSize > count
                line.delete(array.pop(aLine))

//ifs and make FVG                     
if bullFvg 
    array.unshift(aBull,
     line.new(
     bar_index - 1, 
     low,
     bar_index - 1,
     high[2],
     width = fvg_w,
     color = fvgC)) 
if bearFvg 
    array.unshift(aBear,
     line.new(
     bar_index - 1, 
     high,
     bar_index - 1,
     low[2],
     width = fvg_w,
     color = fvgC)) 

//Fun Call     
fvgEdit(aBull,aBear,count_i)     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     