【问题标题】:How to delete a line when price breaks it in pine script?当价格在 pine 脚本中中断时如何删除一行?
【发布时间】:2020-11-28 15:15:08
【问题描述】:
  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)

【问题讨论】:

    标签: line pine-script


    【解决方案1】:

    我采用了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])
    

    【讨论】:

    • 感谢比约恩!我正在尝试做类似的事情,但用 4-5 行而不是 2 行。这样我得到“图纸太多。无法清理最旧的”错误。有什么办法可以解决吗?
    • @SFaz study() 函数最近添加了一个新的max_lines_count 参数。默认值为 50,允许的最大值为 500。您可以尝试使用该参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多