I have written a code to mark out pivot Zones. I am able to plot the zones and remove the ones that are broken.
I want to remove the overlapping levels and keep the newer ones amongst the overlapping ones, and I don't know how to do it.
Here is the code below
//@version=6
indicator('Support Resistance', 'SR', true, max_lines_count = 500)
// Supoort {
// SR Basic Inputs {
doji = (high - low) * 0.1 > math.abs(open - close)
bullCandle = (close > open and not doji)
bearCandle = (open > close and not doji)
srPivotBars = input.int(4, 'Support Resistance Pivot Bars', inline = 'pivotsr', group = 'pivotsr')
// Finding pivot bars Low
srPivotLowValue = ta.pivotlow(srPivotBars, srPivotBars)
// Validating Pivot
srPivotLowTrue = na(srPivotLowValue) ? false : true
// Finding Pivots 2 candle middle point when engulfing is found
pivotLowMidPoint1 = srPivotLowValue + (math.max(high[srPivotBars], high[srPivotBars + 1]) - srPivotLowValue) / 2
pivotLowMidPoint2 = srPivotLowValue + (math.max(high[srPivotBars], high[srPivotBars - 1]) - srPivotLowValue) / 2
// Region Colors
supportLineColor = color.new(#4caf50, 50) // #00ff0080
supportFillColor = color.new(#4caf50, 70) // #00ff0033
// storing the lines in the array
var array<line> supportBottom = array.new<line>()
var array<line> supportTop = array.new<line>()
BROKENSUPPORTS() =>
if supportBottom.size() > 0 and supportTop.size() > 0
for [idx, m] in supportBottom
if low < m.get_y1()
m.delete()
line.delete(supportTop.get(idx))
supportBottom.remove(idx)
supportTop.remove(idx)
//}
// Pivot Point SR {
showPPSR = input.bool(true, "Pivot Levels SR", group = "ppsr")
pivotPointLow1 = srPivotLowTrue and bearCandle[srPivotBars + 1] and bullCandle[srPivotBars]
pivotPointLow2 = srPivotLowTrue and bearCandle[srPivotBars] and bullCandle[srPivotBars - 1]
if pivotPointLow1 and showPPSR and barstate.isconfirmed
supportBottom.unshift(line.new(bar_index - srPivotBars - 1, srPivotLowValue, last_bar_index + 1, srPivotLowValue, xloc.bar_index, extend.right, color = supportLineColor))
supportTop.unshift(line.new(bar_index - srPivotBars - 1, pivotLowMidPoint1, last_bar_index + 1, pivotLowMidPoint1, xloc.bar_index, extend.right, color = supportLineColor))
if pivotPointLow2 and showPPSR and barstate.isconfirmed
supportBottom.unshift(line.new(bar_index - srPivotBars, srPivotLowValue, last_bar_index + 1, srPivotLowValue, xloc.bar_index, extend.right, color = supportLineColor))
supportTop.unshift(line.new(bar_index - srPivotBars, pivotLowMidPoint2, last_bar_index + 1, pivotLowMidPoint2, xloc.bar_index, extend.right, color = supportLineColor))
BROKENSUPPORTS()
//}
// Fill color between the lines {
if supportBottom.size() > 0 and supportTop.size() > 0
for i = 0 to supportBottom.size() - 1 by 1
linefill.new(supportBottom.get(i), supportTop.get(i), supportFillColor)
//}
//}
I tried using the similar logic as that of BROKENSUPPORTS()
below and added to the if condition, but didn't work
overlapping() =>
overlap = false
if supportBottom.size() > 0 and supportTop.size() > 0
for [idx, m] in supportTop
if low < m.get_y1() and m.get_y1() > low
overlap := true
overlap
overlap
In this code, one of the members had helped me to be able to remove the broken levels.
//@version=6
indicator('Support Resistance', 'SR', true, max_lines_count = 500)
// Supoort {
// SR Basic Inputs {
doji = (high - low) * 0.1 > math.abs(open - close)
bullCandle = (close > open and not doji)
bearCandle = (open > close and not doji)
srPivotBars = input.int(4, 'Support Resistance Pivot Bars', inline = 'pivotsr', group = 'pivotsr')
// Finding pivot bars Low
srPivotLowValue = ta.pivotlow(srPivotBars, srPivotBars)
// Validating Pivot
srPivotLowTrue = na(srPivotLowValue) ? false : true
// Finding Pivots 2 candle middle point when engulfing is found
pivotLowMidPoint1 = srPivotLowValue + (math.max(high[srPivotBars], high[srPivotBars + 1]) - srPivotLowValue) / 2
pivotLowMidPoint2 = srPivotLowValue + (math.max(high[srPivotBars], high[srPivotBars - 1]) - srPivotLowValue) / 2
// Region Colors
supportLineColor = color.new(#4caf50, 50) // #00ff0080
supportFillColor = color.new(#4caf50, 70) // #00ff0033
// storing the lines in the array
var array<line> supportBottom = array.new<line>()
var array<line> supportTop = array.new<line>()
// Overlap tolerance (you can adjust this value)
overlapTolerance = input.float(0.5, "Overlap Tolerance %", minval = 0.1, maxval = 5.0, step = 0.1, group = "pivotsr") / 100
// Function to remove overlapping levels using similar logic as BROKENSUPPORTS()
REMOVEOVERLAPPING(newBottomPrice, newTopPrice) =>
if supportBottom.size() > 0 and supportTop.size() > 0
for [idx, bottomLine] in supportBottom
existingBottomPrice = bottomLine.get_y1()
existingTopPrice = supportTop.get(idx).get_y1()
// Calculate tolerance range
toleranceRange = math.max(newTopPrice - newBottomPrice, existingTopPrice - existingBottomPrice) * overlapTolerance
// Check if new level overlaps with existing level
isOverlapping = not (newTopPrice + toleranceRange < existingBottomPrice or newBottomPrice - toleranceRange > existingTopPrice)
if isOverlapping
bottomLine.delete()
line.delete(supportTop.get(idx))
supportBottom.remove(idx)
supportTop.remove(idx)
BROKENSUPPORTS() =>
if supportBottom.size() > 0 and supportTop.size() > 0
for [idx, m] in supportBottom
if low < m.get_y1()
m.delete()
line.delete(supportTop.get(idx))
supportBottom.remove(idx)
supportTop.remove(idx)
//}
// Pivot Point SR {
showPPSR = input.bool(true, "Pivot Levels SR", group = "ppsr")
pivotPointLow1 = srPivotLowTrue and bearCandle[srPivotBars + 1] and bullCandle[srPivotBars]
pivotPointLow2 = srPivotLowTrue and bearCandle[srPivotBars] and bullCandle[srPivotBars - 1]
if pivotPointLow1 and showPPSR and barstate.isconfirmed
// Remove overlapping levels before adding new one
REMOVEOVERLAPPING(srPivotLowValue, pivotLowMidPoint1)
supportBottom.unshift(line.new(bar_index - srPivotBars - 1, srPivotLowValue, last_bar_index + 1, srPivotLowValue, xloc.bar_index, extend.right, color = supportLineColor))
supportTop.unshift(line.new(bar_index - srPivotBars - 1, pivotLowMidPoint1, last_bar_index + 1, pivotLowMidPoint1, xloc.bar_index, extend.right, color = supportLineColor))
if pivotPointLow2 and showPPSR and barstate.isconfirmed
// Remove overlapping levels before adding new one
REMOVEOVERLAPPING(srPivotLowValue, pivotLowMidPoint2)
supportBottom.unshift(line.new(bar_index - srPivotBars, srPivotLowValue, last_bar_index + 1, srPivotLowValue, xloc.bar_index, extend.right, color = supportLineColor))
supportTop.unshift(line.new(bar_index - srPivotBars, pivotLowMidPoint2, last_bar_index + 1, pivotLowMidPoint2, xloc.bar_index, extend.right, color = supportLineColor))
BROKENSUPPORTS()
//}
// Fill color between the lines {
if supportBottom.size() > 0 and supportTop.size() > 0
for i = 0 to supportBottom.size() - 1 by 1
linefill.new(supportBottom.get(i), supportTop.get(i), supportFillColor)
//}
//}