I'm attempting to delete a zone (which contains 2 lines) as per the below code
delete function should be conditional:: only delete the zone (2 lines) when price breaks the lower line of the zone at any point in time after the zone has been created
//@version=4
study("zones", overlay=true)
// define a basing and explosive candles
basing_candle = ((abs(close - open)/abs(high - low)) < 0.5)
explosive_candle = (abs(close-open) / abs(high - low)) >= 0.5 and tr>tr[1]
// functions
bc_r = basing_candle and close < open
ex_g = explosive_candle and close > open
// demand zone
demand_zone = bc_r[1] and ex_g and low>=low[1] and close>open[1]
dz = if demand_zone
line.new(x1 = bar_index[1] ,y1=open[1], x2=bar_index, y2= open[1], style=line.style_solid, extend=extend.right, color=color.green, width=2)
line.new(x1 = bar_index[1] ,y1=low[1], x2=bar_index, y2= low[1], style=line.style_solid, extend=extend.right, color=color.green, width= 2)
I've taken the concept of How can I keep only the last x labels or lines? and applied it to your problem.
It's not the fastest code in the world, due to the for loop, but it'll work as you intened.
//@version=4
var maxBarsBack = 2000
study("zones", overlay=true, max_bars_back=maxBarsBack)
// define a basing and explosive candles
basing_candle = ((abs(close - open)/abs(high - low)) < 0.5)
explosive_candle = (abs(close-open) / abs(high - low)) >= 0.5 and tr>tr[1]
// functions
bc_r = basing_candle and close < open
ex_g = explosive_candle and close > open
// demand zone
demand_zone = bc_r[1] and ex_g and low>=low[1] and close>open[1]
line l1 = na
line l2 = na
dz = if demand_zone and barstate.isconfirmed
l1 := line.new(x1 = bar_index[1] ,y1=open[1], x2=bar_index, y2= open[1], style=line.style_solid, extend=extend.right, color=color.green, width=2)
l2 := line.new(x1 = bar_index[1] ,y1=low[1], x2=bar_index, y2= low[1], style=line.style_solid, extend=extend.right, color=color.green, width= 2)
for i = 1 to maxBarsBack
if not na(l1[i]) and close < low[i]
// We have identified a bar where a line was created.
line.delete(l1[i])
line.delete(l2[i])