I have a code shown below to detect and plot Bullish Order Blocks, Bearish Order Blocks and Fair Value Gap. Now the code to be modified for detecting and plot circles, whenever Order Blocks and FVG are overlapping every time in the chart. Green Circle plot when Bullish Order Blocks overlapping with FVG and Vice versa with Red Circle plotted [Bearish OB with FVG][Bullish OB with FVG].
I have tried many ways to get this done, but not coming through.
Code:
//------------------------------------------------------------------------------
// 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')
bearCss = input.color(color.new(#ff5d00, 80), 'Bearish OB', inline = 'bearcss', group = 'Style')
bearBreakCss = input.color(color.new(#0cb51a, 80), 'Bearish Break', inline = 'bearcss', group = 'Style')
showLabels = input(false, 'Show Historical Polarity Changes')
// 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
var float bearishObTop = na
var float bearishObBottom = na
var int bearishObTime = na
var bool bearishBreaker = false
var int bearishBreakerTime = 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)
os := high[len] > upper ? 0 : low[len] < lower ? 1 : os
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]
[top, topIndex, bottom, bottomIndex]
notransp(css) => color.rgb(color.r(css), color.g(css), color.b(css))
display(loc, top, bottom, breaker, breakerTime, css, breakCss) =>
if breaker
box.new(loc, top, breakerTime, bottom, notransp(css)
, bgcolor = css
, xloc = xloc.bar_time)
box.new(breakerTime, top, time+1, bottom, na
, bgcolor = breakCss
, extend = extend.right
, xloc = xloc.bar_time)
line.new(loc, top, breakerTime, top, xloc.bar_time, color = notransp(css))
line.new(loc, bottom, breakerTime, bottom, xloc.bar_time, color = notransp(css))
line.new(breakerTime, top, time+1, top, xloc.bar_time, extend.right, notransp(breakCss), line.style_dashed)
line.new(breakerTime, bottom, time+1, bottom, xloc.bar_time, extend.right, notransp(breakCss), line.style_dashed)
else
box.new(loc, top, time, bottom, na
, bgcolor = css
, extend = extend.right
, xloc = xloc.bar_time)
line.new(loc, top, time, top, xloc.bar_time, extend.right, notransp(css))
line.new(loc, bottom, time, bottom, xloc.bar_time, extend.right, notransp(css))
//-----------------------------------------------------------------------------
// 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
if close > bullishObTop
bullishObTop := na
bullishObBottom := na
bullishObTime := na
bullishBreaker := false
bullishBreakerTime := na
//-----------------------------------------------------------------------------
// 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
if close < bearishObBottom
bearishObTop := na
bearishObBottom := na
bearishObTime := na
bearishBreaker := false
bearishBreakerTime := na
//-----------------------------------------------------------------------------
// 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
// Detect Long and Short Entries
if fvg_found
if not na(bearishObBottom) and close < bearishObBottom
label.new(bar_index, low, 'Short Entry', color=entryLabelColor, textcolor=shortEntryColor, style=label.style_circle, size=size.small)
if not na(bullishObTop) and close > bullishObTop
label.new(bar_index, high, 'Long Entry', color=entryLabelColor, textcolor=longEntryColor, style=label.style_circle, size=size.small)
//-----------------------------------------------------------------------------
// 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)