//@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) ------------------------------------//
//------------------------------------------------------------------------------------------------------------------------------------------------------//
alertcondition(ut_buy, 'UT Long Alert', 'UT Long')
alertcondition(ut_sell, 'UT Short Alert', 'UT Short')
// === Draw Signal Lines for Next 5 Bars ===
var line[] ut_buyLines = array.new()
var line[] ut_sellLines = array.new()
if ut_buy
ut_lineBuy = line.new(x1=bar_index, y1=high, x2=bar_index + 3, y2=high, color=color.green, style=line.style_solid, width=1) // Olive with light transparency
array.push(ut_buyLines, ut_lineBuy)
if ut_sell
ut_lineSell = line.new(x1=bar_index, y1=low, x2=bar_index + 3, y2=low, color=color.red, style=line.style_solid, width=1) // Plum with light transparency
array.push(ut_sellLines, ut_lineSell)
Please enhance this trding view and indicator make it more effienct and clean
Looking at your comprehensive indicator that combines Fair Value Gaps, Order Blocks, Support/Resistance, Trendlines, and UT Bot Alerts, I can help you create a more efficient and cleaner version. Here's an enhanced version with improved organization, performance optimizations, and cleaner code:
//@version=5
indicator("Advanced Market Structure Suite", shorttitle="AMS Suite", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// ═══════════════════════════════════════════════════════════════════════════
// GENERAL SETTINGS & UTILITIES
// ═══════════════════════════════════════════════════════════════════════════
// Master Controls
g_general = "Master Controls"
enableFVG = input.bool(true, "Fair Value Gaps", group=g_general)
enableOB = input.bool(true, "Order Blocks", group=g_general)
enableSR = input.bool(true, "Support & Resistance", group=g_general)
enableTL = input.bool(true, "Trendlines", group=g_general)
enableUT = input.bool(true, "UT Bot Alerts", group=g_general)
// Color Theme
g_theme = "Color Theme"
bullishColor = input.color(#26a69a, "Bullish Color", group=g_theme)
bearishColor = input.color(#ef5350, "Bearish Color", group=g_theme)
neutralColor = input.color(#9e9e9e, "Neutral Color", group=g_theme)
// Utility Functions
f_getLineStyle(styleStr) =>
switch styleStr
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_solid
f_getLabelSize(sizeStr) =>
switch sizeStr
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.small
// ═══════════════════════════════════════════════════════════════════════════
// FAIR VALUE GAPS MODULE
// ═══════════════════════════════════════════════════════════════════════════
// FVG Settings
g_fvg = "Fair Value Gaps"
fvg_tf = input.timeframe("D", "Higher Timeframe", group=g_fvg)
fvg_showHTF = input.bool(true, "Show HTF Gaps", group=g_fvg)
fvg_showCurrent = input.bool(true, "Show Current TF Gaps", group=g_fvg)
fvg_fillByMid = input.bool(false, "Fill by Midpoint", group=g_fvg)
fvg_deleteOnFill = input.bool(true, "Delete on Fill", group=g_fvg)
fvg_showLabels = input.bool(true, "Show Labels", group=g_fvg)
fvg_maxGaps = input.int(20, "Max Gaps per Type", minval=1, maxval=100, group=g_fvg)
// FVG Style
g_fvgStyle = "FVG Style"
fvg_bullOpacity = input.int(90, "Bull FVG Opacity", minval=0, maxval=100, group=g_fvgStyle)
fvg_bearOpacity = input.int(90, "Bear FVG Opacity", minval=0, maxval=100, group=g_fvgStyle)
fvg_borderWidth = input.int(1, "Border Width", minval=0, maxval=5, group=g_fvgStyle)
fvg_borderStyle = input.string("Solid", "Border Style", options=["Solid", "Dashed", "Dotted"], group=g_fvgStyle)
// FVG Data Storage
type FVGData
box gapBox
line midLine
label gapLabel
float fillLevel
bool isBullish
int createdBar
var array<FVGData> fvgArray = array.new<FVGData>()
// FVG Functions
f_createFVG(float top, float bottom, int barIndex, bool isBullish, string tfText) =>
if not enableFVG
return
// Limit number of gaps
if array.size(fvgArray) >= fvg_maxGaps * 2
oldestFVG = array.shift(fvgArray)
box.delete(oldestFVG.gapBox)
if not na(oldestFVG.midLine)
line.delete(oldestFVG.midLine)
if not na(oldestFVG.gapLabel)
label.delete(oldestFVG.gapLabel)
mid = (top + bottom) / 2
bgColor = isBullish ? color.new(bullishColor, fvg_bullOpacity) : color.new(bearishColor, fvg_bearOpacity)
borderColor = fvg_borderWidth > 0 ? (isBullish ? bullishColor : bearishColor) : na
fvgData = FVGData.new()
fvgData.isBullish := isBullish
fvgData.createdBar := barIndex
fvgData.fillLevel := isBullish ? top : bottom
fvgData.gapBox := box.new(
left=barIndex, right=barIndex + 1, top=top, bottom=bottom,
bgcolor=bgColor, border_color=borderColor,
border_width=fvg_borderWidth, border_style=f_getLineStyle(fvg_borderStyle),
extend=extend.right)
if fvg_fillByMid
fvgData.midLine := line.new(
x1=barIndex, y1=mid, x2=barIndex + 1, y2=mid,
color=color.new(color.white, 85), extend=extend.right)
fvgData.fillLevel := mid
if fvg_showLabels
fvgData.gapLabel := label.new(
x=barIndex + 10, y=mid, text=tfText + " FVG",
style=label.style_none, textcolor=color.white, size=size.small)
array.push(fvgArray, fvgData)
// FVG Detection
f_detectFVG() =>
if not enableFVG
return
// Current timeframe
if fvg_showCurrent and bar_index > 2
// Bullish FVG
if low > high[2] and close > open
f_createFVG(close, high[2], bar_index, true, "")
// Bearish FVG
if high < low[2] and close < open
f_createFVG(low[2], close, bar_index, false, "")
// Higher timeframe
if fvg_showHTF
htf_data = request.security(syminfo.tickerid, fvg_tf, [open, high, low, close, bar_index], lookahead=barmerge.lookahead_off)
if barstate.isconfirmed and not na(htf_data)
[htf_o, htf_h, htf_l, htf_c, htf_bar] = htf_data
// Add HTF FVG detection logic here if needed
// FVG Management
f_manageFVGs() =>
if not enableFVG or array.size(fvgArray) == 0
return
for i = array.size(fvgArray) - 1 to 0
fvg = array.get(fvgArray, i)
// Check if FVG is filled
filled = fvg.isBullish ? high >= fvg.fillLevel : low <= fvg.fillLevel
if filled
if fvg_deleteOnFill
box.delete(fvg.gapBox)
if not na(fvg.midLine)
line.delete(fvg.midLine)
if not na(fvg.gapLabel)
label.delete(fvg.gapLabel)
array.remove(fvgArray, i)
else
box.set_extend(fvg.gapBox, extend.none)
box.set_right(fvg.gapBox, bar_index)
if not na(fvg.midLine)
line.set_extend(fvg.midLine, extend.none)
line.set_x2(fvg.midLine, bar_index)
// ═══════════════════════════════════════════════════════════════════════════
// ORDER BLOCKS MODULE
// ═══════════════════════════════════════════════════════════════════════════
// OB Settings
g_ob = "Order Blocks"
ob_showBull = input.bool(true, "Show Bullish OB", group=g_ob)
ob_showBear = input.bool(true, "Show Bearish OB", group=g_ob)
ob_showMitigated = input.bool(true, "Show Mitigated OB", group=g_ob)
ob_showLabels = input.bool(true, "Show Labels", group=g_ob)
ob_maxBlocks = input.int(10, "Max Order Blocks", minval=1, maxval=50, group=g_ob)
// OB Style
g_obStyle = "Order Block Style"
ob_opacity = input.int(90, "OB Opacity", minval=0, maxval=100, group=g_obStyle)
ob_borderWidth = input.int(1, "Border Width", minval=0, maxval=5, group=g_obStyle)
ob_labelSize = input.string("Small", "Label Size", options=["Tiny", "Small", "Normal", "Large"], group=g_obStyle)
// OB Data Storage
type OBData
box obBox
label obLabel
float mitigationLevel
bool isBullish
bool isMitigated
int createdBar
var array<OBData> obArray = array.new<OBData>()
// OB Functions
f_createOB(float top, float bottom, int barIndex, bool isBullish) =>
if not enableOB
return
// Limit number of OBs
if array.size(obArray) >= ob_maxBlocks
oldestOB = array.shift(obArray)
box.delete(oldestOB.obBox)
if not na(oldestOB.obLabel)
label.delete(oldestOB.obLabel)
bgColor = isBullish ? color.new(bullishColor, ob_opacity) : color.new(bearishColor, ob_opacity)
borderColor = ob_borderWidth > 0 ? (isBullish ? bullishColor : bearishColor) : na
obData = OBData.new()
obData.isBullish := isBullish
obData.createdBar := barIndex
obData.mitigationLevel := isBullish ? top : bottom
obData.isMitigated := false
obData.obBox := box.new(
left=barIndex, right=bar_index + 50, top=top, bottom=bottom,
bgcolor=bgColor, border_color=borderColor,
border_width=ob_borderWidth, extend=extend.right)
if ob_showLabels
labelY = isBullish ? bottom - ta.atr(14) * 0.1 : top + ta.atr(14) * 0.1
obData.obLabel := label.new(
x=barIndex, y=labelY, text="OB",
color=borderColor, textcolor=color.white,
style=isBullish ? label.style_label_up : label.style_label_down,
size=f_getLabelSize(ob_labelSize))
array.push(obArray, obData)
// OB Detection
f_detectOB() =>
if not enableOB or bar_index < 3
return
// Bullish OB: Down-Up-Up pattern
bullishOB = low[2] < low[3] and low[1] > low[2] and (low[1] > high[2] or low > high[2])
// Bearish OB: Up-Down-Down pattern
bearishOB = high[2] > high[3] and high[1] < high[2] and (high[1] < low[2] or high < low[2])
if bullishOB and ob_showBull
f_createOB(high[2], low[2], bar_index - 2, true)
if bearishOB and ob_showBear
f_createOB(high[2], low[2], bar_index - 2, false)
// OB Management
f_manageOBs() =>
if not enableOB or array.size(obArray) == 0
return
for i = array.size(obArray) - 1 to 0
ob = array.get(obArray, i)
if not ob.isMitigated and bar_index > ob.createdBar
mitigated = ob.isBullish ? low <= ob.mitigationLevel : high >= ob.mitigationLevel
if mitigated
ob.isMitigated := true
box.set_extend(ob.obBox, extend.none)
box.set_right(ob.obBox, bar_index)
if ob_showMitigated
box.set_bgcolor(ob.obBox, color.new(neutralColor, ob_opacity))
box.set_border_color(ob.obBox, neutralColor)
else
box.delete(ob.obBox)
if not na(ob.obLabel)
label.delete(ob.obLabel)
array.remove(obArray, i)
// ═══════════════════════════════════════════════════════════════════════════
// SUPPORT & RESISTANCE MODULE
// ═══════════════════════════════════════════════════════════════════════════
// SR Settings
g_sr = "Support & Resistance"
sr_lookback = input.int(360, "Lookback Period", minval=50, maxval=500, group=g_sr)
sr_strength = input.int(2, "SR Strength", minval=1, maxval=5, group=g_sr)
sr_showZones = input.bool(true, "Show Zones", group=g_sr)
sr_zoneWidth = input.float(0.5, "Zone Width %", minval=0, maxval=2, step=0.1, group=g_sr)
sr_maxLevels = input.int(5, "Max SR Levels", minval=1, maxval=10, group=g_sr)
// SR Style
g_srStyle = "SR Style"
sr_lineWidth = input.int(2, "Line Width", minval=1, maxval=5, group=g_srStyle)
sr_lineStyle = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"], group=g_srStyle)
sr_opacity = input.int(70, "Zone Opacity", minval=0, maxval=100, group=g_srStyle)
// SR Data Storage
type SRLevel
float price
int touches
bool isResistance
line srLine
line upperZone
line lowerZone
linefill zoneFill
var array<SRLevel> srLevels = array.new<SRLevel>()
// SR Functions
f_findSRLevels() =>