价格在松脚本中突破时如何删除一行?

How to delete a line when price breaks it in pine script?

  1. 我正在尝试按照以下代码删除一个区域(包含 2 行)

  2. 删除功能应该是有条件的::仅当价格在创建区域后的任何时间点突破区域的下线时才删除区域(2 行)

//@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)

我已经采用了 How can I keep only the last x labels or lines? 的概念并将其应用于您的问题。
由于 for 循环,它不是世界上最快的代码,但它会按您的意图工作。

//@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])