Search code examples
pine-scriptpine-script-v5

How to remove old lines when low goes below it?


I am drawing support near pivot levels with a condition that there should be a doji (logic for it in the code) at the pivot point. This is plotting too many lines, even the lines which are not support anymore. I want older lines to be removed when the low goes below the support line.

I am not able to figure out the code to remove the lines when the low breaks it.

Basically, I only want to show those lines which have not been broken.

In the image attached, I have marked the lines that should not be visible as the levels are broken

Levels on the chart

Below is my code:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kunthu

//@version=5
indicator("SR", "SR", true)

pivotBars                 = input.int(5)

var float pivotBarOpen    = 0.
var float pivotBarHigh    = 0.
var float pivotBarLow     = 0.
var float pivotBarClose   = 0.

pivotHighValue            = ta.pivothigh(pivotBars,pivotBars)
pivotLowValue             = ta.pivotlow(pivotBars,pivotBars)

pivotHighTrue             = na(pivotHighValue) ? false : true
pivotLowTrue              = na(pivotLowValue) ? false : true
doji                      = (high-low)/5 > math.abs(open-close)
barcolor(doji ? color.white : na, title = "Doji Bars", editable = false)

var line supportBottom    = na


dojiSupportCondition      = pivotLowTrue and doji[pivotBars] and not ta.crossunder(low, ta.valuewhen(pivotLowTrue, low[pivotBars], 1))

if dojiSupportCondition
    pivotBarOpen          := open[pivotBars]
    pivotBarHigh          := high[pivotBars]
    pivotBarLow           := low[pivotBars]
    pivotBarClose         := close[pivotBars]
    supportBottom         := line.new(bar_index - pivotBars, pivotLowValue, last_bar_index, pivotLowValue, xloc.bar_index, color = color.green)

Solution

  • I put the lines into an array and then looped through it to check if the lines have been undercut, deleting any that have.

    //@version=5
    indicator("SR", "SR", true)
    
    pivotBars                 = input.int(5)
    
    var float pivotBarOpen    = 0.
    var float pivotBarHigh    = 0.
    var float pivotBarLow     = 0.
    var float pivotBarClose   = 0.
    
    pivotHighValue            = ta.pivothigh(pivotBars,pivotBars)
    pivotLowValue             = ta.pivotlow(pivotBars,pivotBars)
    
    pivotHighTrue             = na(pivotHighValue) ? false : true
    pivotLowTrue              = na(pivotLowValue) ? false : true
    doji                      = (high-low)/5 > math.abs(open-close)
    barcolor(doji ? color.white : na, title = "Doji Bars", editable = false)
    
    var line supportBottom    = na
    var lineArr = array.new<line>()
    
    
    dojiSupportCondition      = pivotLowTrue and doji[pivotBars] and not ta.crossunder(low, ta.valuewhen(pivotLowTrue, low[pivotBars], 1))
    
    if dojiSupportCondition
        pivotBarOpen          := open[pivotBars]
        pivotBarHigh          := high[pivotBars]
        pivotBarLow           := low[pivotBars]
        pivotBarClose         := close[pivotBars]
        lineArr.unshift(line.new(bar_index - pivotBars, pivotLowValue, last_bar_index, pivotLowValue, xloc.bar_index, color = color.green))
    
    if lineArrMain.size() > 0 and lineArrBand.size() > 0
        for [idx,m] in lineArrMain
            if low < m.get_y1()
                m.delete()
                line.delete(lineArrBand.get(idx))
                lineArrMain.remove(idx)
                lineArrBand.remove(idx)
    
    if lineArrMain.size() > 0 and lineArrBand.size() > 0
        for i = 0 to lineArrMain.size()-1
            linefill.new(lineArrMain.get(i), lineArrBand.get(i), #00ff0022)