ICT - GAPs and Volume Imbalance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vulnerable_human_x

//@version=5
indicator("ICT - GAPS, BPR, Volume & Price Imbalance", shorttitle = "ICT - Imbalances [VHX]", overlay=true)


//Volume Imbalance
showbullvi = input.bool(true, "Show Bullish Volume Imbalance", group="Volume Imbalance")
showbearvi = input.bool(true, "Show Bearish Volume Imbalance", group="Volume Imbalance")
bullimbalance = input.color(color.new(#00e640,90), "Bullish VI Color", group="Volume Imbalance", inline="1")
bearimbalance = input.color(color.new(#e30000,90), "Bearish VI Color", group="Volume Imbalance", inline="1")
viMaxBoxSet = input.int(defval=5, title='Maximum Box Displayed', minval=1, maxval=100, group="Volume Imbalance", tooltip='Minimum = 1, Maximum = 100')
vimitigationtype = input.string(defval="Engulf", title="VI Mitigation Type", options = ["Engulf","Mitigate"], group="Volume Imbalance", tooltip = "Body close below Bullish VI and above Bearish VI required for Engulf Mitigation Type")
extendvibox = input.bool(true, title="Extend Unmitigated VI Boxes", group="Volume Imbalance")
extendallvis = input.bool(false,title="Extend all VI Boxes", group="Volume Imbalance")

//Gaps
showgaps = input.bool(true, "Show Gaps in Price", group="GAP")
gapcolor = input.color(color.new(#4ec1f7, 90), "GAP Color", group="GAP")
gapsMaxBoxSet = input.int(defval=7, title='Maximum Box Displayed', minval=1, maxval=100, group="GAP", tooltip='Minimum = 1, Maximum = 100')
gapmitigationtype = input.string(defval="C.E.", title="GAP Mitigation Type", options = ["Engulf","C.E."], group="GAP", tooltip = "Body close abov/below a GAP is required for Engulf Mitigation Type. 50% mitigation required Consequent Encroachment Type.")
extendgapbox = input.bool(true, title="Extend Unmitigated GAP Boxes", group="GAP")

//FVG and BPR
plotFVG = input.bool(true, "FVG BOX", inline="18", group="FVG/BPR")
plotBPR = input.bool(false, "BPR BOX", inline="18", group="FVG/BPR")
showifvg = input.bool(true,"I.FVG", inline="18", group="FVG/BPR")

fvgBullColor = input.color(defval=color.new(color.green, 90), title='Bullish FVG',inline="1", group="FVG/BPR")
fvgBearColor = input.color(defval=color.new(color.red, 90), title='Bearish FVG',inline="1", group="FVG/BPR")

bprBullColor = input.color(defval=color.new(color.green, 90), title='Bullish BPR',inline="2", group="FVG/BPR")
bprBearColor = input.color(defval=color.new(color.red, 90), title='Bearish BPR',inline="2", group="FVG/BPR")

ifvgBullColor = input.color(defval=color.new(color.green, 90), title='Bullish I.FVG',inline="3", group="FVG/BPR")
ifvgBearColor = input.color(defval=color.new(color.red, 90), title='Bearish I.FVG',inline="3", group="FVG/BPR")



var float bprprecision = input.float(0.00005,"BPR Precision",step=0.00001, group="FVG/BPR")
fvgMaxBoxSet = input.int(defval=10, title='Maximum Box Displayed', minval=1, maxval=100, group="FVG/BPR", tooltip='Minimum = 1, Maximum = 100')
fvgmitigationtype = input.string(defval="Engulf", title="FVG Mitigation", options = ["Engulf","Mitigate"], group="FVG/BPR", tooltip = "Body close below Bullish FVG and above Bearish FVG required for Engulf Mitigation Type")
cefvg = input.bool(false,title="C.E. FVG", group="FVG/BPR")
cetransparency = input.int(50,title="C.E. Transparency", group="FVG/BPR")
extendfvgbox = input.bool(true, title="Extend Unmitigated FVG/BPR Boxes", group="FVG/BPR")


//Box Styling
BoxBorder = input.string(defval=line.style_solid, title='Box Border Style', options=[line.style_dashed, line.style_dotted, line.style_solid], group="Box Style", tooltip='To disable border, set Border Width below to 0')
BorderTransparency = input.int(defval=85, title='Border Box Transparency', minval=0, maxval=100, group="Box Style")
Highlightboxtransparancy = input.int(75, "Highlight Box BG", group="Box Style", tooltip = "Highlight box background color when price is inside the box")
Highlightboxbordertransparancy = input.int(50, "Highlight Box Border", group="Box Style", tooltip = "Highlight box Border color when price is inside the box")
plotBoxLabel = input.bool(defval=true, title='Plot Label', group="Box Style")
BoxLabelSize = input.string(defval=size.tiny, title="Label Size", options=[size.huge, size.large, size.small, size.tiny, size.auto, size.normal], group="Box Style")
BoxLabelColor = input.color(defval=color.rgb(161, 163, 171), title='Label Color', group="Box Style")


filterMitBOX = input.bool(defval=true, title='Mitigated Box Color', group="Mitigated Box Style")
mitBOXColor = input.color(defval=color.new(color.gray, 85), title='Mitigated Box Color', group="Mitigated Box Style", tooltip='Set Transparency to 0 to make mitigated Boxes disappear')
MitBoxLabelColor = input.color(defval=color.rgb(161, 163, 171, 70), title='Mitigated Box Label Color', group="Mitigated Box Style")

var bool buifvgtouch = false
var bool beifvgtouch = false
var bool bufvgtouch = false
var bool befvgtouch = false
var bool buvitouch = false
var bool bevitouch = false
var bool bugaptouch = false
var bool begaptouch = false


var int _fvg = 2
var int _vi = 3
//Box Labels
var string _fvgLabel = "FVG"
var string _plus     = "+"
var string _minus    = "-"
var string _empty    = ""
var string _bprLabel = "BPR"

var box[] _bearBoxesFVG = array.new_box()
var box[] _bullBoxesFVG = array.new_box()
var box[] _gapsboxesbu = array.new_box()
var box[] _gapsboxesbe = array.new_box()
var box[] _bullishvi = array.new_box()
var box[] _bearishvi = array.new_box()
var box[] _bullishifvg = array.new_box()
var box[] _bearishifvg = array.new_box()
var line[] _bufvgce = array.new_line()
var line[] _befvgce = array.new_line()


_controlBox(_boxes, _high, _low, _type) =>
    if array.size(_boxes) > 0
        for i = array.size(_boxes) - 1 to 0 by 1
            _box = array.get(_boxes, i)
            _boxLow = box.get_bottom(_box)
            _boxHigh = box.get_top(_box)
            _boxRight = box.get_right(_box)
            if (filterMitBOX and _type == _fvg)
                if na or (bar_index == _boxRight and not((_high > _boxLow and _low < _boxLow) or (_high > _boxHigh and _low < _boxHigh)))
                    box.set_right(_box, bar_index + 1)
                else
                    if _type == _fvg
                        box.set_bgcolor(_box, mitBOXColor)
                        box.set_border_color(_box, mitBOXColor)
                        box.set_text_color(_box,MitBoxLabelColor)
            else
                if na or (bar_index == _boxRight and not((_high > _boxLow and _low < _boxLow) or (_high > _boxHigh and _low < _boxHigh)))
                    box.set_right(_box, bar_index + 1)



_controlBox2(_boxes, _high, _low, _type) =>
    if array.size(_boxes) > 0
        for i = array.size(_boxes) - 1 to 0 by 1
            _box = array.get(_boxes, i)
            _boxLow = box.get_bottom(_box)
            _boxHigh = box.get_top(_box)
            _boxRight = box.get_right(_box)
            if extendallvis
                box.set_right(_box, bar_index + 1)
            

//Volume Imbalance

isBuVI(index) =>
    (open[index] > close[index+1] and low[index] <= high[index+1] and close[index] > close[index+1] and open[index] > open[index+1])

isBeVI(index) =>
    (open[index] < close[index+1] and open[index] < open[index+1] and high[index] >= low[index+1] and close[index] < close[index+1] and close[index] < open[index+1])

//Bullish Volume Imbalance
if showbullvi and isBuVI(0)
    box _bullboxVI = na
    _bullboxVI := box.new(left=bar_index-1, top=math.min(open,close), right=bar_index+1, bottom=math.max(close[1],open[1]), bgcolor=bullimbalance,border_style=BoxBorder, border_color=color.new(bullimbalance,BorderTransparency), text=plotBoxLabel?"VI+":na, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    if array.size(_bullishvi) > viMaxBoxSet
        box.delete(array.shift(_bullishvi))
    array.push(_bullishvi, _bullboxVI)


//Bearish Volume Imbalance
if showbearvi and isBeVI(0)
    box _bearboxVI = na
    _bearboxVI := box.new(left=bar_index-1, top=math.min(close[1],open[1]), right=bar_index+1, bottom=math.max(open,close), bgcolor=bearimbalance,border_style=BoxBorder, border_color=color.new(bearimbalance,BorderTransparency),text=plotBoxLabel?"VI-":na, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    if array.size(_bearishvi) > viMaxBoxSet
        box.delete(array.shift(_bearishvi))
    array.push(_bearishvi, _bearboxVI)


_controlBox2(_bullishvi,high,low,_fvg)
_controlBox2(_bearishvi,high,low,_fvg)

//Gap
if (showgaps and high[1] < low) or (showgaps and low[1] > high)
    box _gapsbu = na
    box _gapsbe = na
    if high[1] < low
        _gapsbu := box.new(left=bar_index-1, top=math.min(open,close), right=bar_index+1, bottom=math.max(open[1],close[1]), bgcolor=gapcolor, border_color=color.new(gapcolor,BorderTransparency), text=plotBoxLabel?"GAP+":na,border_style=BoxBorder, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    else if low[1] > high
        _gapsbe := box.new(left=bar_index-1, top=math.min(open[1],close[1]), right=bar_index+1, bottom=math.max(open,close), bgcolor=gapcolor, border_color=color.new(gapcolor,BorderTransparency), text=plotBoxLabel?"GAP-":na,border_style=BoxBorder, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    if array.size(_gapsboxesbu) > gapsMaxBoxSet
        box.delete(array.shift(_gapsboxesbu))
    array.push(_gapsboxesbu, _gapsbu)
    if array.size(_gapsboxesbe) > gapsMaxBoxSet
        box.delete(array.shift(_gapsboxesbe))
    array.push(_gapsboxesbe, _gapsbe)



//fvg
isFvgUp(index) =>
    (low[index] > high[index + 2] and low[index + 1] <= high[index + 2] and high[index + 1] >= low[index])

isFvgDown(index) =>
    (high[index] < low[index + 2] and high[index] >= low[index + 1] and high[index +1] >= low[index +2])


//Balanced Price Range
isbprdown (index) =>
    low[index + 3] > high[index] and low[index + 3] < low[index + 2] and low[index + 3] < close[index + 3] and high[index + 2] >= close[index + 2] and high[index + 1] <= ((open[index + 1]) + (open[index + 1] * bprprecision)) and (low[index + 2] >= ((open[index + 2]) - (open[index + 2] * bprprecision)) or low[index + 2] >= ((close[index + 2]) - (close[index + 2] * bprprecision)))

isbprup (index) =>
    high[index + 3] < low[index] and high[index + 3] > high[index + 2] and high[index + 3] > close[index + 3] and low[index + 2] <= close[index + 2] and low[index + 1] >= ((open[index + 1]) - (open[index + 1] * bprprecision)) and (high[index + 2] <= ((open[index + 2]) + (open[index + 2] * bprprecision)) or high[index + 2] <= ((close[index + 2]) + (close[index + 2] * bprprecision)))


//Bullish FVG Box Plotting
if isbprup(0) and isFvgUp(0)
    box _bullboxFVG = na
    line _buline = na
    if plotBPR
        _bullboxFVG := box.new(left=bar_index-3, top=low[0], right=bar_index, bottom=high[3], bgcolor=bprBullColor, border_color=color.new(bprBullColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _bprLabel  + _plus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)    
    else if plotFVG   
        _bullboxFVG := box.new(left=bar_index-2, top=low[0], right=bar_index, bottom=high[2], bgcolor=fvgBullColor, border_color=color.new(fvgBullColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _fvgLabel  + _plus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
        _buline := cefvg?line.new(bar_index-2,math.avg(low,high[2]),bar_index,math.avg(low,high[2]),color=color.new(fvgBullColor,cetransparency)):na
    if array.size(_bullBoxesFVG) > fvgMaxBoxSet and array.size(_bufvgce) > fvgMaxBoxSet
        box.delete(array.shift(_bullBoxesFVG))
        line.delete(array.shift(_bufvgce))
    array.push(_bullBoxesFVG, _bullboxFVG)
    array.push(_bufvgce,_buline)
else if isFvgUp(0)
    box _bullboxFVG = na
    line _buline = na
    if plotFVG   
        _bullboxFVG := box.new(left=bar_index-2, top=low[0], right=bar_index, bottom=high[2], bgcolor=fvgBullColor, border_color=color.new(fvgBullColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _fvgLabel  + _plus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
        _buline := cefvg?line.new(bar_index-2,math.avg(low,high[2]),bar_index,math.avg(low,high[2]), color=color.new(fvgBullColor,cetransparency)):na
    if array.size(_bullBoxesFVG) > fvgMaxBoxSet and array.size(_bufvgce) > fvgMaxBoxSet
        box.delete(array.shift(_bullBoxesFVG))
        line.delete(array.shift(_bufvgce))
    array.push(_bullBoxesFVG, _bullboxFVG)
    array.push(_bufvgce,_buline)


//Bearish FVG Box Plotting  
if isbprdown(0) and isFvgDown(0)
    box _bearboxFVG = na
    line _beline = na
    if plotBPR
        _bearboxFVG := box.new(left=bar_index-3, top=low[3], right=bar_index, bottom=high[0], bgcolor=bprBearColor, border_color=color.new(bprBearColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _bprLabel  + _minus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)    
    else if plotFVG
        _bearboxFVG := box.new(left=bar_index-2, top=low[2], right=bar_index, bottom=high[0], bgcolor=fvgBearColor, border_color=color.new(fvgBearColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _fvgLabel  + _minus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)   
        _beline := cefvg?line.new(bar_index-2,math.avg(low[2],high),bar_index,math.avg(low[2],high),color=color.new(fvgBearColor,cetransparency)):na     
    if array.size(_bearBoxesFVG) > fvgMaxBoxSet and array.size(_befvgce) > fvgMaxBoxSet
        box.delete(array.shift(_bearBoxesFVG))
        line.delete(array.shift(_befvgce))
    array.push(_bearBoxesFVG, _bearboxFVG)  
    array.push(_befvgce,_beline)
else if isFvgDown(0)
    box _bearboxFVG = na
    line _beline = na
    if plotFVG
        _bearboxFVG := box.new(left=bar_index-2, top=low[2], right=bar_index, bottom=high[0], bgcolor=fvgBearColor, border_color=color.new(fvgBearColor,BorderTransparency), border_style=BoxBorder, border_width=1,
         text=plotBoxLabel ? _fvgLabel  + _minus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)    
        _beline := cefvg?line.new(bar_index-2,math.avg(low[2],high),bar_index,math.avg(low[2],high),color=color.new(fvgBearColor,cetransparency)):na 
    if array.size(_bearBoxesFVG) > fvgMaxBoxSet and array.size(_befvgce) > fvgMaxBoxSet
        box.delete(array.shift(_bearBoxesFVG))
        line.delete(array.shift(_befvgce))
    array.push(_bearBoxesFVG, _bearboxFVG)
    array.push(_befvgce,_beline)



//-----BEARISH IMPLIED FAIR VALUE GAP---------------//
isBeIFvg(index) =>
    (low[index] < low[index+2] and high[index+2] > high[index] and high[index] >= low[index+2] and math.min(open[index+2],close[index+2]) - low[index+2] > (math.max(open[index+2],close[index+2]) - math.min(open[index+2],close[index+2]))/2 and high[index] - math.max(open[index],close[index]) > (math.max(open[index],close[index]) - math.min(open[index],close[index]))/2 and high[index] < high[index+1] and (math.min(open[index+2],close[index+2]) + low[index+2])/2 > (high[index] + math.max(open[index],close[index]))/2)

//-----BULLISH IMPLIED FAIR VALUE GAP---------------//
isBuIFvg(index) =>
    (high[index] > high[index+2] and low[index+2] < low[index] and low[index] <= high[index+2] and high[index+2] - math.max(open[index+2],close[index+2]) > (math.max(open[index+2],close[index+2]) - math.min(open[index+2],close[index+2]))/2 and math.min(open[index],close[index]) - low[index] > (math.max(open[index],close[index]) - math.min(open[index],close[index]))/2 and low[index] > low[index+1] and (high[2] + math.max(open[2],close[2]))/2 < (math.min(open,close) + low)/2) 



if isBeIFvg(0) and showifvg
    box _bearboxifvg = na
    _bearboxifvg := box.new(left=bar_index-2, top=(math.min(open[2],close[2]) + low[2])/2, right=bar_index+1, bottom=(math.max(open,close) + high)/2, bgcolor=ifvgBearColor,border_style=BoxBorder, border_color=color.new(ifvgBearColor,BorderTransparency),text=plotBoxLabel?"I.FVG-":na, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    if array.size(_bearishifvg) > fvgMaxBoxSet
        box.delete(array.shift(_bearishifvg))
    array.push(_bearishifvg, _bearboxifvg)


if isBuIFvg(0) and showifvg
    box _bullboxifvg = na
    _bullboxifvg := box.new(left=bar_index-2, top=(high[2] + math.max(open[2],close[2]))/2, right=bar_index+1, bottom=(math.min(open,close) + low)/2, bgcolor=ifvgBullColor,border_style=BoxBorder, border_color=color.new(ifvgBullColor,BorderTransparency),text=plotBoxLabel?"I.FVG+":na, text_halign=text.align_right, text_valign=text.align_bottom, text_size=BoxLabelSize, text_color=BoxLabelColor)
    if array.size(_bullishifvg) > fvgMaxBoxSet
        box.delete(array.shift(_bullishifvg))
    array.push(_bullishifvg, _bullboxifvg)




//-------IMPLIED FVG EXTEND  -  MITIGATION TYPE = ENGULF -----------
//ifvg boxes extend (bullish)
if array.size(_bullishifvg) > 0 and array.size(_bufvgce) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Engulf"
    for i = array.size(_bullishifvg) - 1 to 0 by 1
        _box = array.get(_bullishifvg, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close < _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)
                

//ifvg boxes extend (bearish)
if array.size(_bearishifvg) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Engulf"
    for i = array.size(_bearishifvg) - 1 to 0 by 1
        _box = array.get(_bearishifvg, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close > _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)



// ifvg box border animation (bullish)
if array.size(_bullishifvg) > 0 and extendfvgbox and fvgmitigationtype=="Engulf"
    for i = array.size(_bullishifvg) - 1 to 0 by 1
        _box = array.get(_bullishifvg, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight and low < _boxHigh
            box.set_bgcolor(_box,color.new(ifvgBullColor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(ifvgBullColor,Highlightboxbordertransparancy))
            buifvgtouch := true

// ifvg box border animation (bearish)
if array.size(_bearishifvg) > 0 and extendfvgbox and fvgmitigationtype=="Engulf"
    for i = array.size(_bearishifvg) - 1 to 0 by 1
        _box = array.get(_bearishifvg, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight and high > _boxLow
            box.set_bgcolor(_box,color.new(ifvgBearColor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(ifvgBearColor,Highlightboxbordertransparancy))
            beifvgtouch := true

//-------IMPLIED FVG EXTEND  -  MITIGATION TYPE = MITIGATE -----------
if extendfvgbox and barstate.isconfirmed  and fvgmitigationtype=="Mitigate"
    _controlBox(_bearishifvg, high, low, _fvg)
    _controlBox(_bullishifvg, high, low, _fvg)






//-------FVG EXTEND  -  MITIGATION TYPE = ENGULF -----------
//Bullish FVG Extend
if array.size(_bullBoxesFVG) > 0 and array.size(_bufvgce) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Engulf"
    for i = array.size(_bullBoxesFVG) - 1 to 0 by 1
        _line = array.get(_bufvgce, i)
        _box = array.get(_bullBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
            line.set_x2(_line, bar_index + 1)
        else 
            if close < _boxLow and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)
                line.set_color(_line,mitBOXColor)

//Bearish FVG Extend
if array.size(_bearBoxesFVG) > 0 and array.size(_bufvgce) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Engulf"
    for i = array.size(_bearBoxesFVG) - 1 to 0 by 1
        _line = array.get(_befvgce, i)
        _box = array.get(_bearBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
            line.set_x2(_line, bar_index + 1)
        else 
            if close > _boxHigh and bar_index == _boxRight and filterMitBOX 
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)
                line.set_color(_line,mitBOXColor)


//box border animation - FVG+
if array.size(_bullBoxesFVG) > 0 and extendfvgbox and fvgmitigationtype=="Engulf"
    for i = array.size(_bullBoxesFVG) - 1 to 0 by 1
        _box = array.get(_bullBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight and low < _boxHigh
            box.set_bgcolor(_box,color.new(fvgBullColor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(fvgBullColor,Highlightboxbordertransparancy))
            bufvgtouch := true


//box border animation - FVG-
if array.size(_bearBoxesFVG) > 0 and extendfvgbox and fvgmitigationtype=="Engulf"
    for i = array.size(_bearBoxesFVG) - 1 to 0 by 1
        _box = array.get(_bearBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight and high > _boxLow
            box.set_bgcolor(_box,color.new(fvgBearColor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(fvgBearColor,Highlightboxbordertransparancy))
            befvgtouch := true



//-------FVG EXTEND  -  MITIGATION TYPE = MITIGATE -----------
//bullish FVG
if array.size(_bullBoxesFVG) > 0  and array.size(_bufvgce) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Mitigate"
    for i = array.size(_bullBoxesFVG) - 1 to 0 by 1
        _line = array.get(_bufvgce, i)
        _box = array.get(_bullBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if low >= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
            line.set_x2(_line, bar_index + 1)
        else 
            if low < _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)
                line.set_color(_line,mitBOXColor)


//Bearish FVG
if array.size(_bearBoxesFVG) > 0 and array.size(_befvgce) > 0 and extendfvgbox and barstate.isconfirmed and fvgmitigationtype=="Mitigate"
    for i = array.size(_bearBoxesFVG) - 1 to 0 by 1
        _line = array.get(_befvgce, i)
        _box = array.get(_bearBoxesFVG, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if high <= _boxLow and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
            line.set_x2(_line, bar_index + 1)
        else 
            if high > _boxLow and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)
                line.set_color(_line,mitBOXColor)





//-------VI EXTEND  -  MITIGATION TYPE = ENGULF -----------
//bullish VI extend
if array.size(_bullishvi) > 0 and extendvibox and barstate.isconfirmed and vimitigationtype=="Engulf"
    for i = array.size(_bullishvi) - 1 to 0 by 1
        _box = array.get(_bullishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close < _boxLow and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)

//Bearish VI Extend
if array.size(_bearishvi) > 0 and extendvibox and barstate.isconfirmed and vimitigationtype=="Engulf"
    for i = array.size(_bearishvi) - 1 to 0 by 1
        _box = array.get(_bearishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close > _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


//Vi Box Border Animation (bullish)
if array.size(_bullishvi) > 0 and extendvibox and vimitigationtype=="Engulf"
    for i = array.size(_bullishvi) - 1 to 0 by 1
        _box = array.get(_bullishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight and low < _boxHigh
            box.set_bgcolor(_box,color.new(bullimbalance,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(bullimbalance,Highlightboxbordertransparancy))
            buvitouch := true

//Vi Box Border Animation (bearish)
if array.size(_bearishvi) > 0 and extendvibox and vimitigationtype=="Engulf"
    for i = array.size(_bearishvi) - 1 to 0 by 1
        _box = array.get(_bearishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight and high > _boxLow
            box.set_bgcolor(_box,color.new(bearimbalance,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(bearimbalance,Highlightboxbordertransparancy))
            bevitouch := true




//-------VI EXTEND  -  MITIGATION TYPE = MITIGATE -----------
//bullish VI extend MITIGATION TYPE - MITIGATE
if array.size(_bullishvi) > 0 and extendvibox and barstate.isconfirmed and vimitigationtype=="Mitigate"
    for i = array.size(_bullishvi) - 1 to 0 by 1
        _box = array.get(_bullishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if low > _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if low <= _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


//Bearish VI Extend MITIGATION TYPE MITIGATE
if array.size(_bearishvi) > 0 and extendvibox and barstate.isconfirmed and vimitigationtype=="Mitigate"
    for i = array.size(_bearishvi) - 1 to 0 by 1
        _box = array.get(_bearishvi, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if high < _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if high >= _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)



//-------GAP EXTEND  -  MITIGATION TYPE = C.E. -----------
//gap boxes extend (bullish)
if array.size(_gapsboxesbu) > 0 and extendgapbox and barstate.isconfirmed and gapmitigationtype=="C.E."
    for i = array.size(_gapsboxesbu) - 1 to 0 by 1
        _box = array.get(_gapsboxesbu, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if low > (_boxHigh+_boxLow)/2 and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if low <= (_boxHigh+_boxLow)/2 and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


//gap boxes extend (bearish)
if array.size(_gapsboxesbe) > 0 and extendgapbox and barstate.isconfirmed  and gapmitigationtype=="C.E."
    for i = array.size(_gapsboxesbe) - 1 to 0 by 1
        _box = array.get(_gapsboxesbe, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if high < (_boxHigh+_boxLow)/2 and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if high >= (_boxHigh+_boxLow)/2 and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


//-------GAP EXTEND  -  MITIGATION TYPE = ENGULF -----------
//gap boxes extend (bullish)
if array.size(_gapsboxesbu) > 0 and extendgapbox and barstate.isconfirmed and gapmitigationtype=="Engulf"
    for i = array.size(_gapsboxesbu) - 1 to 0 by 1
        _box = array.get(_gapsboxesbu, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close < _boxLow and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


//gap boxes extend (bearish)
if array.size(_gapsboxesbe) > 0 and extendgapbox and barstate.isconfirmed  and gapmitigationtype=="Engulf"
    for i = array.size(_gapsboxesbe) - 1 to 0 by 1
        _box = array.get(_gapsboxesbe, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight
            box.set_right(_box, bar_index + 1)
        else 
            if close > _boxHigh and bar_index == _boxRight and filterMitBOX
                box.set_bgcolor(_box, mitBOXColor)
                box.set_border_color(_box, mitBOXColor)
                box.set_text_color(_box,MitBoxLabelColor)


// gap box border animation(bullish)
if array.size(_gapsboxesbu) > 0 and extendgapbox and gapmitigationtype=="Engulf"
    for i = array.size(_gapsboxesbu) - 1 to 0 by 1
        _box = array.get(_gapsboxesbu, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close >= _boxLow and bar_index == _boxRight and low < _boxHigh
            box.set_bgcolor(_box,color.new(gapcolor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(gapcolor,Highlightboxbordertransparancy))
            bugaptouch := true


// gap box border animation (bearish)
if array.size(_gapsboxesbe) > 0 and extendgapbox  and gapmitigationtype=="Engulf"
    for i = array.size(_gapsboxesbe) - 1 to 0 by 1
        _box = array.get(_gapsboxesbe, i)
        _boxLow = box.get_bottom(_box)
        _boxHigh = box.get_top(_box)
        _boxRight = box.get_right(_box)
        if close <= _boxHigh and bar_index == _boxRight and high > _boxLow
            box.set_bgcolor(_box,color.new(gapcolor,Highlightboxtransparancy))
            box.set_border_color(_box,color.new(gapcolor,Highlightboxbordertransparancy))
            begaptouch := true






//---------ALERTS----------------
alertcondition(barstate.isconfirmed and isFvgUp(0) , title="FVG+", message="FVG+ : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and isFvgDown(0) , title="FVG-", message="FVG- : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and isBuIFvg(0) , title="I.FVG+", message="I.FVG+ : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and isBeIFvg(0) , title="I.FVG-", message="I.FVG- : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and open > close[1] and low <= high[1] and close > close[1] and open > open[1],title="VI+",message="VI+ : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and open < close[1] and open < open[1] and high >= low[1] and close < close[1] and close < open[1],title="VI-",message="VI- : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and high[1] < low,title="GAP+", message="GAP+ : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(barstate.isconfirmed and low[1] > high,title="GAP-", message="GAP- : {{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")

alertcondition(bugaptouch, title="GAP(+) Mitigation", message="GAP(+) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(begaptouch, title="GAP(-) Mitigation", message="GAP(-) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(buifvgtouch, title="I.FVG(+) Mitigation", message="I.FVG(+) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(beifvgtouch, title="I.FVG(-) Mitigation", message="I.FVG(-) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(bufvgtouch, title="FVG(+) Mitigation", message="FVG(+) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(befvgtouch, title="FVG(-) Mitigation", message="FVG(-) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(buvitouch, title="VI(+) Mitigation", message="VI(+) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
alertcondition(bevitouch, title="VI(-) Mitigation", message="VI(-) Mitigated :{{exchange}}:{{ticker}} TIMEFRAME:{{interval}}")
//end of script







