//@version=5
indicator("Merged Fair Value Gap and Order Blocks", shorttitle="FVG + OB", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// ———————————————————— FVG Global Data {
// Using current bar data for HTF highs and lows instead of security to prevent future leaking
var htfH = open
var htfL = open
if close > htfH
htfH := close
if close < htfL
htfL := close
// Var keyword to hold data in memory
var fvg_bullishgapholder = array.new_box(0)
var fvg_bearishgapholder = array.new_box(0)
var fvg_bullishmidholder = array.new_line(0)
var fvg_bearishmidholder = array.new_line(0)
var fvg_bullishlabelholder = array.new_label(0)
var fvg_bearishlabelholder = array.new_label(0)
var transparentcolor = color.new(color.white,100)
// }
// ———————————————————— OB Global Data {
// Store order blocks in arrays (prefixed with ob_ to avoid conflicts)
var ob_boxes = array.new_box()
var ob_labels = array.new_label()
var ob_types = array.new_string() // "bull" or "bear"
var ob_prices = array.new_float() // Store the mitigation price level
var ob_starts = array.new_int() // Store the start bar_index for each OB
var ob_mitigated = array.new_bool() // Track if OB is mitigated
f_gapDeletion(_currentgap, _i, _boxholder, _midholder, _labelholder) =>
array.remove(_boxholder, _i)
if i_fillByMid
currentmid = array.get(_midholder, _i)
array.remove(_midholder, _i)
if i_deleteonfill
line.delete(currentmid)
else
line.set_extend(currentmid, extend.none)
line.set_x2(currentmid, bar_index)
if i_deleteonfill
box.delete(_currentgap)
else
box.set_extend(_currentgap, extend.none)
box.set_right(_currentgap, bar_index)
if i_labeltf
currentlabel = array.get(_labelholder, _i)
array.remove(_labelholder, _i)
if i_deleteonfill
label.delete(currentlabel)
f_gapCheck(_high, _low) =>
if array.size(fvg_bullishgapholder) > 0
for i = array.size(fvg_bullishgapholder) - 1 to 0
currentgap = array.get(fvg_bullishgapholder, i)
currenttop = box.get_top(currentgap)
if i_fillByMid
currentmid = array.get(fvg_bullishmidholder, i)
currenttop := line.get_y1(currentmid)
if _high >= currenttop
f_gapDeletion(currentgap, i, fvg_bullishgapholder, fvg_bullishmidholder, fvg_bullishlabelholder)
if _high > box.get_bottom(currentgap) and _high < box.get_top(currentgap)
box.set_bottom(currentgap, _high)
if array.size(fvg_bearishgapholder) > 0
for i = array.size(fvg_bearishgapholder) - 1 to 0
currentgap = array.get(fvg_bearishgapholder, i)
currentbottom = box.get_bottom(currentgap)
if i_fillByMid
currentmid = array.get(fvg_bearishmidholder, i)
currentbottom := line.get_y1(currentmid)
if _low <= currentbottom
f_gapDeletion(currentgap, i, fvg_bearishgapholder, fvg_bearishmidholder, fvg_bearishlabelholder)
if _low < box.get_top(currentgap) and _low > box.get_bottom(currentgap)
box.set_top(currentgap, _low)
is_newbar(res) =>
t = time(res)
not na(t) and (na(t[1]) or t > t[1])
if is_newbar(i_tf)
htfH := open
htfL := open
// }
// ———————————————————— Main Logic (FVG + OB) {
// FVG Logic (only if showFVG is true)
if showFVG
// User Input, allow MTF data calculations
if is_newbar(i_tf) and (i_mtf == "Current + HTF" or i_mtf == "HTF")
f_gapLogic(sClose, htfH, sHighP2, htfL, sLowP2, sOpen, bar_index, true)
// Use current Timeframe data to provide gap logic
if (i_mtf == "Current + HTF" or i_mtf == "Current TF")
f_gapLogic(close[1], high, high[2], low, low[2], open[1], bar_index, false)
f_gapCheck(high, low)
// OB Logic (only if showOB is true)
if showOB
// Create new order blocks
if bullishOB and showBullishOB
borderStyle = f_getBorderStyle(ob_borderStyle)
borderColor = ob_borderWidth > 0 ? ob_bullishBorderColor : transparentcolor
box_id = box.new(left=bar_index - 2, right=bar_index + 499, top=candleB_high, bottom=candleB_low,
border_color=borderColor,
border_width=ob_borderWidth,
border_style=borderStyle,
bgcolor=ob_bullishColor)
if showOBLabels
labelSize = f_getLabelSize(ob_labelSize)
labelStyle = f_getLabelStyle(ob_labelStyle, true)
label_id = label.new(bar_index - 2, bullishY, "OB",
color=ob_bullishBorderColor,
textcolor=ob_labelTextColor,
style=labelStyle,
size=labelSize)
array.push(ob_labels, label_id)
else
array.push(ob_labels, label.new(na, na, na)) // Push dummy label to keep arrays aligned
array.push(ob_boxes, box_id)
array.push(ob_types, "bull")
array.push(ob_prices, candleB_high)
array.push(ob_starts, bar_index - 2)
array.push(ob_mitigated, false)
if bearishOB and showBearishOB
borderStyle = f_getBorderStyle(ob_borderStyle)
borderColor = ob_borderWidth > 0 ? ob_bearishBorderColor : transparentcolor
box_id = box.new(left=bar_index - 2, right=bar_index + 499, top=candleB_high, bottom=candleB_low,
border_color=borderColor,
border_width=ob_borderWidth,
border_style=borderStyle,
bgcolor=ob_bearishColor)
if showOBLabels
labelSize = f_getLabelSize(ob_labelSize)
labelStyle = f_getLabelStyle(ob_labelStyle, false)
label_id = label.new(bar_index - 2, bearishY, "OB",
color=ob_bearishBorderColor,
textcolor=ob_labelTextColor,
style=labelStyle,
size=labelSize)
array.push(ob_labels, label_id)
else
array.push(ob_labels, label.new(na, na, na)) // Push dummy label to keep arrays aligned
array.push(ob_boxes, box_id)
array.push(ob_types, "bear")
array.push(ob_prices, candleB_low)
array.push(ob_starts, bar_index - 2)
array.push(ob_mitigated, false)
// Check for mitigation of existing order blocks
if array.size(ob_boxes) > 0
for i = array.size(ob_boxes) - 1 to 0
box_id = array.get(ob_boxes, i)
label_id = array.get(ob_labels, i)
ob_type = array.get(ob_types, i)
price_level = array.get(ob_prices, i)
start_bar = array.get(ob_starts, i)
is_mitigated = array.get(ob_mitigated, i)
// Only check for mitigation after the OB formation
if bar_index > start_bar
// Check if order block is mitigated
if not is_mitigated
if ob_type == "bull" and low <= price_level
is_mitigated := true
if ob_type == "bear" and high >= price_level
is_mitigated := true
if is_mitigated
// Update the box to end at current bar
box.set_right(box_id, bar_index)
box.set_extend(box_id, extend.none) // Stop the box from extending
array.set(ob_mitigated, i, true)
if showMitigatedOB
// Change color to mitigated color
box.set_bgcolor(box_id, ob_mitigatedColor)
box.set_border_color(box_id, color.gray)
else
box.delete(box_id)
label.delete(label_id)
// Remove the OB from tracking arrays
array.remove(ob_boxes, i)
array.remove(ob_labels, i)
array.remove(ob_types, i)
array.remove(ob_prices, i)
array.remove(ob_starts, i)
array.remove(ob_mitigated, i)
var sr_sr_lines = array.new_line(21, na)
var sr_sr_linesH = array.new_line(21, na)
var sr_sr_linesL = array.new_line(21, na)
var sr_sr_linesF = array.new_linefill(21, na)
if bool(sr_phs) or bool(sr_pls)
for x = 0 to array.size(sr_sr_levels) - 1 by 1
array.set(sr_sr_levels, x, na)
sr_highestph := sr_prdlowest
sr_lowestpl := sr_prdhighest
sr_countpp = 0
for x = 0 to sr_prd by 1
if na(close[x])
break
if not na(sr_phs[x]) or not na(sr_pls[x])
sr_highestph := math.max(sr_highestph, nz(sr_phs[x], sr_prdlowest), nz(sr_pls[x], sr_prdlowest))
sr_lowestpl := math.min(sr_lowestpl, nz(sr_phs[x], sr_prdhighest), nz(sr_pls[x], sr_prdhighest))
sr_countpp := sr_countpp + 1
if sr_countpp > 40
break
if array.get(sr_aas, sr_countpp)
sr_upl = (bool(sr_phs[x]) ? high[x + sr_rb] : low[x + sr_rb]) + sr_cwidth
sr_dnl = (bool(sr_phs[x]) ? high[x + sr_rb] : low[x + sr_rb]) - sr_cwidth
sr_u1 := sr_countpp == 1 ? sr_upl : sr_u1
sr_d1 := sr_countpp == 1 ? sr_dnl : sr_d1
sr_tmp = array.new_bool(41, true)
sr_cnt = 0
sr_tpoint = 0
for xx = 0 to sr_prd by 1
if na(close[xx])
break
if not na(sr_phs[xx]) or not na(sr_pls[xx])
sr_chg = false
sr_cnt := sr_cnt + 1
if sr_cnt > 40
break
if array.get(sr_aas, sr_cnt)
if not na(sr_phs[xx])
if high[xx + sr_rb] <= sr_upl and high[xx + sr_rb] >= sr_dnl
sr_tpoint := sr_tpoint + 1
sr_chg := true
if not na(sr_pls[xx])
if low[xx + sr_rb] <= sr_upl and low[xx + sr_rb] >= sr_dnl
sr_tpoint := sr_tpoint + 1
sr_chg := true
if sr_chg and sr_cnt < 41
array.set(sr_tmp, sr_cnt, false)
if sr_tpoint >= sr_strengthSR
for g = 0 to 40 by 1
if not array.get(sr_tmp, g)
array.set(sr_aas, g, false)
if bool(sr_phs[x]) and sr_countpp < 21
array.set(sr_sr_levels, sr_countpp, high[x + sr_rb])
if bool(sr_pls[x]) and sr_countpp < 21
array.set(sr_sr_levels, sr_countpp, low[x + sr_rb])
// Plot (SR)
var line sr_highest_ = na
line.delete(sr_highest_)
var line sr_lowest_ = na
line.delete(sr_lowest_)
var line sr_highest_fill1 = na
line.delete(sr_highest_fill1)
var line sr_highest_fill2 = na
line.delete(sr_highest_fill2)
var line sr_lowest_fill1 = na
line.delete(sr_lowest_fill1)
var line sr_lowest_fill2 = na
line.delete(sr_lowest_fill2)
sr_hi_col = close >= sr_highestph ? sr_UpCol : sr_DnCol
sr_lo_col = close >= sr_lowestpl ? sr_UpCol : sr_DnCol
if sr_enableSR
sr_highest_ := line.new(bar_index - 360, sr_highestph, bar_index, sr_highestph, xloc.bar_index, sr_expandSR ? extend.both : extend.right, sr_hi_col, sr_shivstyle, sr_lineWidth)
sr_lowest_ := line.new(bar_index - 360, sr_lowestpl, bar_index, sr_lowestpl, xloc.bar_index, sr_expandSR ? extend.both : extend.right, sr_lo_col, sr_shivstyle, sr_lineWidth)
if sr_useHLZones
sr_highest_fill1 := line.new(bar_index - 360, sr_highestph + sr_zonePerc, bar_index, sr_highestph + sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na)
sr_highest_fill2 := line.new(bar_index - 360, sr_highestph - sr_zonePerc, bar_index, sr_highestph - sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na)
sr_lowest_fill1 := line.new(bar_index - 360, sr_lowestpl + sr_zonePerc, bar_index, sr_lowestpl + sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na)
sr_lowest_fill2 := line.new(bar_index - 360, sr_lowestpl - sr_zonePerc, bar_index, sr_lowestpl - sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na)
linefill.new(sr_highest_fill1, sr_highest_fill2, color.new(sr_hi_col, 70)) // 70 transparency
linefill.new(sr_lowest_fill1, sr_lowest_fill2, color.new(sr_lo_col, 70)) // 70 transparency
if bool(sr_phs) or bool(sr_pls)
for x = 0 to array.size(sr_sr_levels) - 1 by 1
array.set(sr_sr_levs, x, array.get(sr_sr_levels, x))
for x = 0 to array.size(sr_sr_lines) - 1 by 1
line.delete(array.get(sr_sr_lines, x))
line.delete(array.get(sr_sr_linesH, x))
line.delete(array.get(sr_sr_linesL, x))
linefill.delete(array.get(sr_sr_linesF, x))
if bool(array.get(sr_sr_levs, x)) and sr_enableSR
sr_line_col = close >= array.get(sr_sr_levs, x) ? color.new(sr_UpCol,30) : color.new(sr_DnCol,30) // Light transparency for lines
array.set(sr_sr_lines, x, line.new(bar_index - 360, array.get(sr_sr_levs, x), bar_index, array.get(sr_sr_levs, x), xloc.bar_index, sr_expandSR ? extend.both : extend.right, sr_line_col, sr_shivstyle, sr_lineWidth))
if sr_useZones
array.set(sr_sr_linesH, x, line.new(bar_index - 360, array.get(sr_sr_levs, x) + sr_zonePerc, bar_index, array.get(sr_sr_levs, x) + sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na))
array.set(sr_sr_linesL, x, line.new(bar_index - 360, array.get(sr_sr_levs, x) - sr_zonePerc, bar_index, array.get(sr_sr_levs, x) - sr_zonePerc, xloc.bar_index, sr_expandSR ? extend.both : extend.right, na))
array.set(sr_sr_linesF, x, linefill.new(array.get(sr_sr_linesH, x), array.get(sr_sr_linesL, x), color.new(sr_line_col, 70))) // 70 transparency
//------------------------------------------------------------------------------------------------------------------------------------------------------//
//--------------------------------------------------------------- Trendlines with Swing High and Low (Section 1) ------------------------------------//
//------------------------------------------------------------------------------------------------------------------------------------------------------//