// This Pine Scriptâ„¢ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=5 indicator("OS", overlay=true, max_boxes_count=500, max_lines_count=500) numCandles = 1 mitigateFVGBox = input(true, title="Show Mitigated", tooltip = "Disable for a cleaner chart that only indicates the mitigating candles. You can also use the alert function to notify you of when a mitigation occurs") mitigatedColor = input(color.rgb(179, 74, 74, 92), title="Mitigated Color") showUp = input(true, title="BISI", tooltip = "This must be enabled for all buy functions. Adjust transparency for visibility") colorUp = input(color.new(#74afdf, 100), title="Bullish FVG Color") showDown = input(true, title="SIBI", tooltip = "This must be enabled for all sell functions. Adjust transparency for visibility") colorDown = input(color.new(#5499c7, 100), title="Bearish FVG Color") showCE = input(false, title="Show CE") CEColor = input(color.new(#7290e2, 69), title="CE Color") CEStyle = input.string(line.style_dotted, title="CE Line Style", options=[line.style_dotted, line.style_solid, line.style_dashed]) CECondition = false colorNone = input(color.new(#383737, 86), title="FVG/Mitigation Box") maxBoxesCount = input.int(1, title="Show More Boxes", minval=1) var array boxUpArr = array.new(0) var array lineUpArr = array.new(0) var array boxDownArr = array.new(0) var array lineDownArr = array.new(0) var int lastMitigatedCandleUp = na var int lastMitigatedCandleDown = na var bool fillingFVG = false downCE = high[1] + (low[3] - high[1]) / 2 upCE = low[1] - (low[1] - high[3]) / 2 if low[3] > high[1] and bar_index > numCandles and showDown boxDownArr.push(box.new(bar_index - numCandles, low[3], bar_index, high[1], bgcolor=colorDown, border_color=colorNone)) lineDownArr.push(line.new(bar_index - numCandles, downCE, bar_index, downCE, color=showCE ? CEColor : colorNone, style=CEStyle)) lastMitigatedCandleDown := bar_index fillingFVG := true if high[3] < low[1] and bar_index > numCandles and showUp boxUpArr.push(box.new(bar_index - numCandles, low[1], bar_index, high[3], bgcolor=colorUp, border_color=colorNone)) lineUpArr.push(line.new(bar_index - numCandles, upCE, bar_index, upCE, color=showCE ? CEColor : colorNone, style=CEStyle)) lastMitigatedCandleUp := bar_index fillingFVG := true extendAndRemoveBox(array boxArray, array lineArray, simple bool isBull, int maxSize, int lastMitigatedCandle_local) => var int lastMitigatedCandle_local_copy = lastMitigatedCandle_local if boxArray.size() > 0 for i = boxArray.size() - 1 to 0 line ln = lineArray.get(i) box bx = boxArray.get(i) bx.set_right(bar_index) ln.set_x2(bar_index) float boxHigh = isBull ? bx.get_top() : bx.get_bottom() float boxLow = isBull ? bx.get_bottom() : bx.get_top() if high >= boxHigh and low <= boxLow boxArray.remove(i) lineArray.remove(i) if mitigateFVGBox bx.set_bgcolor(mitigatedColor) ln.set_color(mitigatedColor) lastMitigatedCandle_local_copy := bar_index if boxArray.size() > maxSize box.delete(boxArray.shift()) line.delete(lineArray.shift()) lastMitigatedCandle_local_copy // Call the modified function lastMitigatedCandleDown := extendAndRemoveBox(boxDownArr, lineDownArr, true, maxBoxesCount, lastMitigatedCandleDown) lastMitigatedCandleUp := extendAndRemoveBox(boxUpArr, lineUpArr, true, maxBoxesCount, lastMitigatedCandleUp) // Plotting last mitigated candles as triangles plotshape(series=bar_index == lastMitigatedCandleDown and fillingFVG, title="BISI Mitigated", color=color.rgb(175, 76, 76, 15), style=shape.circle, location=location.abovebar, size=size.tiny) plotshape(series=bar_index == lastMitigatedCandleUp and fillingFVG, title="SIBI Mitigated", color=color.rgb(79, 133, 54, 18), style=shape.circle, location=location.belowbar, size=size.tiny) // Alert when FVG gets mitigated alertcondition(condition=bar_index == lastMitigatedCandleDown or bar_index == lastMitigatedCandleUp, title="FVG Mitigated", message="FVG Mitigated")