FVG
FVG
// Settings
//------------------------------------------------------------------------------
length = input.int(10, 'Swing Lookback', minval = 3)
showBull = input.int(3, 'Show Last Bullish OB', minval = 0)
showBear = input.int(3, 'Show Last Bearish OB', minval = 0)
useBody = input(false, 'Use Candle Body')
// Style
bullCss = input.color(color.new(#2157f3, 80), 'Bullish OB', inline =
'bullcss', group = 'Style')
bullBreakCss = input.color(color.new(#ff1100, 80), 'Bullish Break', inline =
'bullcss', group = 'Style')
// Entry style
entryLabelColor = input.color(color.white, "Entry Label Color")
shortEntryColor = input.color(color.red, "Short Entry Color")
longEntryColor = input.color(color.green, "Long Entry Color")
//-----------------------------------------------------------------------------
// UDT's
//-----------------------------------------------------------------------------
var float bullishObTop = na
var float bullishObBottom = na
var int bullishObTime = na
var bool bullishBreaker = false
var int bullishBreakerTime = na
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
swings(len) =>
var os = 0
var float top = na
var float bottom = na
var int topIndex = na
var int bottomIndex = na
upper = ta.highest(len)
lower = ta.lowest(len)
if os == 0 and os[1] != 0
top := high[len]
topIndex := bar_index[len]
if os == 1 and os[1] != 1
bottom := low[len]
bottomIndex := bar_index[len]
//-----------------------------------------------------------------------------
// Detect Swings
//-----------------------------------------------------------------------------
[top, topIndex, bottom, bottomIndex] = swings(length)
max = useBody ? math.max(close, open) : high
min = useBody ? math.min(close, open) : low
//-----------------------------------------------------------------------------
// Bullish OB
//-----------------------------------------------------------------------------
if not na(top) and close > top
minima = max[1]
maxima = min[1]
loc = time[1]
for i = 1 to length-1
minima := math.min(min[i], minima)
maxima := minima == min[i] ? max[i] : maxima
loc := minima == min[i] ? time[i] : loc
bullishObTop := maxima
bullishObBottom := minima
bullishObTime := loc
bullishBreaker := false
bullishBreakerTime := na
if not na(bullishObTop)
if close < bullishObBottom and not bullishBreaker
bullishBreaker := true
bullishBreakerTime := time
//-----------------------------------------------------------------------------
// Bearish OB
//-----------------------------------------------------------------------------
if not na(bottom) and close < bottom
minima = min[1]
maxima = max[1]
loc = time[1]
for i = 1 to length-1
maxima := math.max(max[i], maxima)
minima := maxima == max[i] ? min[i] : minima
loc := maxima == max[i] ? time[i] : loc
bearishObTop := maxima
bearishObBottom := minima
bearishObTime := loc
bearishBreaker := false
bearishBreakerTime := na
if not na(bearishObTop)
if close > bearishObTop and not bearishBreaker
bearishBreaker := true
bearishBreakerTime := time
//-----------------------------------------------------------------------------
// Check for OB and FVG overlap
//-----------------------------------------------------------------------------
fvg_found = false
for i = 1 to length - 1
if (math.min(close[i], open[i]) < high[i+1] and math.max(close[i], open[i]) >
low[i+1])
fvg_found := true
break
//-----------------------------------------------------------------------------
// Set Order Blocks
//-----------------------------------------------------------------------------
for bx in box.all
bx.delete()
for l in line.all
l.delete()
if barstate.islast
// Bullish
if showBull > 0 and not na(bullishObTop)
display(bullishObTime, bullishObTop, bullishObBottom, bullishBreaker,
bullishBreakerTime, bullCss, bullBreakCss)
// Bearish
if showBear > 0 and not na(bearishObTop)
display(bearishObTime, bearishObTop, bearishObBottom, bearishBreaker,
bearishBreakerTime, bearCss, bearBreakCss)
// Debugging labels
if not na(bullishObTop)
label.new(bullishObTime, bullishObTop, 'Bullish OB', color=color.white,
textcolor=color.green)
if not na(bearishObTop)
label.new(bearishObTime, bearishObTop, 'Bearish OB', color=color.white,
textcolor=color.red)