[FrizLabz] FVG
// 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",overlay = true,max_lines_count = 500,max_labels_count = 500,max_boxes_count = 500,max_bars_back = 5000,explicit_plot_zorder = true)

// -- Inputs -- //
FVG = input.bool(true ,'FVGs | On/Off?',group = 'FVG Settings') 
BULLfvgc = input.color(#000000,'FVG Color : ',group = 'FVG Settings',inline = '1')
BEARfvgc = input.color(#000000,'<-- BullFVG | BearFVG --> ',group = 'FVG Settings',inline = '1')
TT = input.bool(true, 'FVG ToolTips?',group = 'FVG Settings')
bC = input.bool(false ,'Remove Bar Color?',group = 'FVG Settings') 
UTC = input.int(-5,'Time Adj UTC (Match Chart UTC Time)',group = 'FVG Settings',minval=-23,maxval = 25)
UTC := UTC * 3600000

// -- Constants -- //
fTime = '\nTime : ' + str.format("{0,date," + 'hh:mm a' + "}", time[1] + UTC)
BearFVG = high < low[2] and FVG   
BullFVG = low  > high[2] and FVG
FVGc = BullFVG ? BULLfvgc : BEARfvgc

// -- Variables -- //
var label Label = na
bool BearFVGp = false
bool BullFVGp = false


// -- Labels and FVG plot Switches -- //
// -- Bear -- //
if BearFVG
    Label := TT ? label.new(
     x = time[1], 
     y = math.avg(high,low[2]),
     text = '     ',
     xloc = xloc.bar_time,
     color = color(na),
     style = label.style_label_center,
     size = size.large,
     tooltip =  
     'Bear FVG\n---------------------' + 
     '\nSize : '   + str.tostring(low[2] - high) +
     '\nTop : '    + str.tostring(low[2]) +
     '\nMiddle : ' + str.tostring(math.avg(low[2],high)) +
     '\nBottom : ' + str.tostring(high) + fTime) : na
    BearFVGp := true
if BearFVGp == true and BearFVGp[1] == true 
    BearFVGp := false

// -- Bull -- //
if BullFVG
    Label := TT ? label.new(
     x = time[1], 
     y = math.avg(high[2],low),
     text = '     ',
     xloc = xloc.bar_time,
     color = color(na),
     style = label.style_label_center,
     size = size.large,
     tooltip =  
     'Bull FVG\n---------------------' + 
     '\nSize : '   + str.tostring(low - high[2]) +
     '\nTop : '    + str.tostring(low) +
     '\nMiddle : ' + str.tostring(math.avg(high[2],low)) +
     '\nBottom : ' + str.tostring(high[2]) + fTime) : na
    BullFVGp := true
if BullFVGp == true and BullFVGp[1] == true
    BullFVGp := false
    
bool BullGO1 = BullFVG and BullFVGp
bool BearGO1 = BearFVG and BearFVGp
bool BullGO2 = BullFVG and BullGO1 == false
bool BearGO2 = BearFVG and BearGO1 == false
    
// -- Bear-Bull FVG plots -- //
// Plots Need Sequences to prevent the fills from forming to plot lines and covering 2 bars if another FVG is on next Bar
// Can have Bear and Bull in each plot pair because you cant have a Bull and Bear FVG on the Same bar
// -- Pair 1 -- //
p1 = plot(FVG ? 
 BearGO1 ? low[2] : BullGO1 ? low : na : na,
 style = plot.style_stepline,
 offset=-1,display = display.none,
 editable = false)
p2 = plot(FVG ? 
 BearGO1 ? high : BullGO1 ? high[2] : na : na,
 style = plot.style_stepline,
 offset=-1, display = display.none,
 editable = false)
fill(p1,p2,color = FVGc[1],editable = false)

// -- Pair 2 -- //
p3 = plot(FVG ? 
 BearGO2 ? low[2] : BullGO2 ? low : na : na,
 style = plot.style_stepline,
 offset=-1,display = display.none,
 editable = false)
p4 = plot(FVG ? 
 BearGO2 ? high : BullGO2 ? high[2] : na : na,
 style = plot.style_stepline,
 offset=-1, display = display.none,
 editable = false) 
fill(p3,p4,color = FVGc[1],editable = false)

barcolor(color = bC and (BullFVG or BearFVG) ? color.new(color.white,100) : na, offset = -1, editable = false)