You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免PineScript中其他指标线条被误删除

解决PineScript中误删全局线条的问题

你当前使用line.all()获取图表中所有线条并删除,会误删同一脚本内其他指标生成的线条。解决方法是维护一个专属数组存储本指标创建的线条对象,仅清理该数组内的线条。

修改步骤

  1. 用var声明一个持久化数组,专门存储本指标生成的线条
  2. 每次调用line.new()创建线条后,将返回的线条对象添加到数组中
  3. 需要清理线条时,遍历数组删除所有线条,再清空数组

修改后的核心代码片段

替换原代码中line.all()相关的清理逻辑,同时修改线条生成的代码:

// returns auto vwaps from signals within lengths of bars or pivots
svwap(
 show1, source1, signal1, length1, barsL1, barsR1, color1, width1,
 show2, source2, signal2, length2, barsL2, barsR2, color2, width2) =>
    idxCB = bar_index              // index current bar

    valS1 = float(na)              // value signal source1
    idxS1 = int(na)                // index signal source1
    sS1V1 = float(na)              // sum of source1 * volume1
    sVol1 = float(na)              // sum of volume1
    valP1 = float(na)              // previous value of svwap1
    valC1 = float(na)              // current value of svwap1

    valS2 = float(na)              // value signal source2
    idxS2 = int(na)                // index signal source2
    sS2V2 = float(na)              // sum of source2 * volume2
    sVol2 = float(na)              // sum of volume2
    valP2 = float(na)              // previous value of svwap2
    valC2 = float(na)              // current value of svwap2
    
    var array<line> my_lines = array.new_line()  // 专属数组存储本指标线条

    if not show1 and not show2
        valC1 := float(na)
        valC2 := float(na)
        // 隐藏时清理所有本指标线条
        if array.size(my_lines) > 0
            for i = 0 to array.size(my_lines) - 1
                line.delete(array.get(my_lines, i))
            array.clear(my_lines)
    else
        // 先清理本指标已有的线条
        if array.size(my_lines) > 0
            for i = 0 to array.size(my_lines) - 1
                line.delete(array.get(my_lines, i))
            array.clear(my_lines)

        if show1                   // calc auto vwap1
            valS1 :=
             signal1 == 'Highest' ? ta.highest(source1, length1) :
             signal1 == 'Lowest'  ? ta.lowest (source1, length1) :
             signal1 == 'Highest Volume' ? ta.highest(volume, length1) :
             signal1 == 'Lowest Volume'  ? ta.lowest (volume, length1) :
             signal1 == 'Pivot High' ?
              ta.valuewhen(not na(ta.pivothigh(source1, barsL1, barsR1)), source1[barsR1], 0) :
             signal1 == 'Pivot Low'  ?
              ta.valuewhen(not na(ta.pivotlow (source1, barsL1, barsR1)), source1[barsR1], 0) :              
             float(na)
            if signal1 == 'Highest' or signal1 == 'Lowest'
                for i = 0 to length1 - 1
                    if source1[i] == valS1 
                        idxS1 := bar_index[i]
                        break
            if signal1 == 'Highest Volume' or signal1 == 'Lowest Volume'
                for i = 0 to length1 - 1
                    if volume[i] == valS1 
                        idxS1 := bar_index[i]
                        break
            if signal1 == 'Pivot High' or signal1 == 'Pivot Low'
                idxS1 :=
                 signal1 == 'Pivot High' ?
                  ta.valuewhen(not na(ta.pivothigh(source1, barsL1, barsR1)), bar_index[barsR1], 0) :
                 signal1 == 'Pivot Low'  ?
                  ta.valuewhen(not na(ta.pivotlow (source1, barsL1, barsR1)), bar_index[barsR1], 0) :
                 int(na)   

            nBars = 1 + idxCB - idxS1    
            for i = nBars - 1 to 0 // draw lines for current event vwap
                sS1V1 := nz(sS1V1) + nz(source1[i] * volume[i])
                sVol1 := nz(sVol1) + nz(volume[i])
                valP1 := nz(valC1, na)
                valC1 := sS1V1 / sVol1
                // 创建线条并添加到专属数组
                new_line = line.new(idxCB - i - 1, valP1, idxCB - i, valC1, xloc.bar_index, extend.none, color1, width = width1)
                array.push(my_lines, new_line)

        if show2                   // calc auto vwap2
            valS2 :=
             signal2 == 'Highest' ? ta.highest(source2, length2) :
             signal2 == 'Lowest'  ? ta.lowest (source2, length2) :
             signal2 == 'Highest Volume' ? ta.highest(volume, length2) :
             signal2 == 'Lowest Volume'  ? ta.lowest (volume, length2) :             
             signal2 == 'Pivot High' ?
              ta.valuewhen(not na(ta.pivothigh(source2, barsL2, barsR2)), source1[barsR2], 0) :
             signal2 == 'Pivot Low'  ?
              ta.valuewhen(not na(ta.pivotlow (source2, barsL2, barsR2)), source1[barsR2], 0) :   
             float(na)
            if signal2 == 'Highest' or signal2 == 'Lowest'
                for i = 0 to length2 - 1
                    if source2[i] == valS2 
                        idxS2 := bar_index[i]
                        break
            if signal2 == 'Highest Volume' or signal2 == 'Lowest Volume'
                for i = 0 to length2 - 1
                    if volume[i] == valS2 
                        idxS2 := bar_index[i]
                        break
            if signal2 == 'Pivot High' or signal2 == 'Pivot Low'
                idxS2 :=
                 signal2 == 'Pivot High' ?
                  ta.valuewhen(not na(ta.pivothigh(source2, barsL2, barsR2)), bar_index[barsR2], 0) :
                 signal2 == 'Pivot Low'  ?
                  ta.valuewhen(not na(ta.pivotlow (source2, barsL2, barsR2)), bar_index[barsR2], 0) :
                 int(na)   

            nBars = 1 + idxCB - idxS2     
            for i = nBars - 1 to 0 // draw lines for current event vwap
                sS2V2 := nz(sS2V2) + nz(source2[i] * volume[i])
                sVol2 := nz(sVol2) + nz(volume[i])
                valP2 := nz(valC2, na)
                valC2 := sS2V2 / sVol2
                // 创建线条并添加到专属数组
                new_line = line.new(idxCB - i - 1, valP2, idxCB - i, valC2, xloc.bar_index, extend.none, color2, width = width2)
                array.push(my_lines, new_line)
        
    [valC1, valC2]

关键说明

  • var array<line> my_lines = array.new_line():用var声明的数组会在脚本运行期间保持状态,不会每次bar都重置
  • 每次生成线条后用array.push(my_lines, new_line)将线条对象存入数组
  • 清理时仅遍历my_lines数组删除线条,不会影响其他指标的线条
  • 在not show1 and not show2分支也添加了线条清理逻辑,确保隐藏时不会残留线条

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:19:51