I am facing this problem in my custom indicator on trading where i am able to draw the zones but i want to remove those zones which are either tested (price has come in contact with the zone) or violated (price which went crossed the zones)
Code:
//@version=5
indicator("SD Zone with Rectangle and Lines", overlay=true)
// Parameters
explosiveThreshold = 1.5
boringThreshold = 0.5
maxBoringCandles = 5
atrValue = ta.atr(14)
bodySize = math.abs(close - open)
isExplosiveCandle = bodySize > (atrValue * explosiveThreshold)
isBoring = math.abs(close - open) < boringThreshold * math.abs(high - low)
// Variables for tracking zones
var float highestBoring = na
var float lowestBoring = na
var int boringCount = 0
var bool isZone = false
var line highestLine = na
var line lowestLine = na
var box zoneBox = na
// Track the highest and lowest of boring candles before the current explosive candle
if (isExplosiveCandle)
highestBoring := na
lowestBoring := na
boringCount := 0
// Find boring candles before the explosive candle
for i = 1 to maxBoringCandles
if (isBoring[i])
boringCount := boringCount + 1
highestBoring := na(highestBoring) ? high[i] : math.max(highestBoring, high[i])
lowestBoring := na(lowestBoring) ? low[i] : math.min(lowestBoring, low[i])
else
break
// Mark the zone if there are enough boring candles
isZone := boringCount >= 1
// Draw lines and rectangle for the zone
if (isZone)
// Draw horizontal lines at highest and lowest points
highestLine := line.new(x1=bar_index - boringCount, y1=highestBoring, x2=bar_index + 50, y2=highestBoring, color=color.blue, width=2, style=line.style_solid)
lowestLine := line.new(x1=bar_index - boringCount, y1=lowestBoring, x2=bar_index + 50, y2=lowestBoring, color=color.red, width=2, style=line.style_solid)
// Draw rectangle for the zone, starting from the boring candle high and low
zoneBox := box.new(left=bar_index - boringCount, top=highestBoring, right=bar_index + 50, bottom=lowestBoring, border_color=color.yellow, border_width=1, border_style=line.style_solid, bgcolor=color.new(color.yellow, 90))
// Coloring the candles
barcolor(isExplosiveCandle ? color.white : isBoring ? color.rgb(0, 4, 255) : na)
Trading View stock image
New contributor
akshmit saxena is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.