Fair Value Gap (FVG) Underlay
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Fair Value Gap (FVG)", overlay=false)

// Calculation Inputs
bodyToWickRatioThreshold = input(0.7, "Body-to-Wick Ratio Threshold")
lookbackPeriod = input(20, "Lookback Period")

// Calculate Body-to-Wick Ratio
bodySize = math.abs(open - close)
wickSize = high - low
bodyToWickRatio = bodySize / wickSize

// Determine Fair Value Gap (FVG)
isBearishFVG = ta.change(close, 1) < 0 and bodyToWickRatio >= bodyToWickRatioThreshold
isBullishFVG = ta.change(close, 1) > 0 and bodyToWickRatio >= bodyToWickRatioThreshold

// Calculate FVG Percentage
fvgPercentage = bodyToWickRatio * 100

// Plot Fair Value Gap (FVG) as Columns
plot(isBearishFVG ? fvgPercentage : na, "Bearish FVG", color=color.fuchsia, style=plot.style_columns, linewidth=4)
plot(isBullishFVG ? fvgPercentage : na, "Bullish FVG", color=color.lime, style=plot.style_columns, linewidth=4)

// Background Color
bgcolor(isBearishFVG ? color.new(color.fuchsia, 80) : isBullishFVG ? color.new(color.lime, 80) : na)

// Bar Color
barcolor(isBearishFVG ? color.fuchsia : isBullishFVG ? color.lime : na)