pine-scriptdelaydelete-rowindicator

Deleting an indicator line with a delay after crossing


Please help me again... there is such indicator script...

//@version=5
indicator("NHNHNHNH", overlay = true)
Kol_Linii = input(2, "Количество линий")

Signal = open[3] < close[3] and open[2] < close[2] and open[1] < close[1] and close < open and high[1] > high and high[1] > close[1] and high > open

X_for_LINIA = ta.valuewhen(Signal, bar_index, 0)
Y_for_LINIA = ta.valuewhen(Signal, high, 0)

var ArrayLine = array.new_line()

if Signal
    LINIA = line.new(X_for_LINIA, Y_for_LINIA,X_for_LINIA+1,Y_for_LINIA, extend = extend.right, color = color.fuchsia, width = 1)
    array.push(ArrayLine, LINIA)
    label.new(bar_index, close, str.tostring(array.size(ArrayLine))+" : " + str.tostring(line.get_y1(LINIA)), color= color.white, yloc=yloc.abovebar)
    
if array.size(ArrayLine) > 0
    NumberOfLine = array.size(ArrayLine) 
    Complete = false
    Index = NumberOfLine - 1
    while not(Complete)
        Line = array.get(ArrayLine, Index)
        if line.get_y1(Line) < close
            line.delete(Line)
            array.remove(ArrayLine, Index)
        Index := Index - 1
        if Index < 0
            Complete := true

It works great and performs the required actions, however, during the testing process, it became necessary to modify the actions of this indicator. At the moment, the indicator controls the appearance of a certain pattern.. At the moment the pattern is formed, a line is drawn from the High ... At the moment the candle closes behind the line, the line is removed accordingly...

BUT this turned out to be not a very convenient solution ... Help me in the concept of this script to implement the following script:

I could only make a color change after the intersection, but I can’t implement the deletion scenario after the fifth candle closes - due to my lack of competence...

Thank you...

I was able to implement only a color change, this is not presented in this script, but I cannot implement the removal with a delay of five candles...

I can't even imagine how...


Solution

  • Not the most elegant solution but works:

    //@version=5
    indicator("NHNHNHNH", overlay = true)
    Kol_Linii = input(2, "Количество линий")
    
    var BIG_NUMBER = 999999999 // safely bigger than any bar_index
    
    
    Signal = open[3] < close[3] and open[2] < close[2] and open[1] < close[1] and close < open and high[1] > high and high[1] > close[1] and high > open
    
    X_for_LINIA = ta.valuewhen(Signal, bar_index, 0)
    Y_for_LINIA = ta.valuewhen(Signal, high, 0)
    
    var ArrayLine = array.new_line()
    var ArrayLineMarkForDeletionBi = array.new<int>() // array of bar indices from which we will cound deletion timeout
    
    if Signal
        LINIA = line.new(X_for_LINIA, Y_for_LINIA,X_for_LINIA+1,Y_for_LINIA, extend = extend.right, color = color.fuchsia, width = 1)
        array.push(ArrayLine, LINIA)
        ArrayLineMarkForDeletionBi.push(999999999) // safely big number, bigger that any bar_index
        label.new(bar_index, close, str.tostring(array.size(ArrayLine))+" : " + str.tostring(line.get_y1(LINIA)), color= color.white, yloc=yloc.abovebar)
        
    if array.size(ArrayLine) > 0
        NumberOfLine = array.size(ArrayLine) 
        Complete = false
        Index = NumberOfLine - 1
        while not(Complete)
            Line = ArrayLine.get(Index)
            LineMarkBi = ArrayLineMarkForDeletionBi.get(Index)
            if Line.get_y1() < close and LineMarkBi == BIG_NUMBER
                ArrayLineMarkForDeletionBi.set(Index, bar_index)
                lb1 = label.new(Line.get_x1(), low, str.tostring(ArrayLineMarkForDeletionBi.get(Index)), color = color.yellow, style = label.style_label_up)
            if bar_index - LineMarkBi >= 5
                line.delete(Line)
                ArrayLine.remove(Index)
                ArrayLineMarkForDeletionBi.remove(Index)
            Index := Index - 1
            if Index < 0
                Complete := true
    

    More elegant would be to use user-defined type and methods:

    //@version=5
    indicator("NHNHNHNH", overlay = true)
    Kol_Linii = input(2, "Количество линий")
    
    var BIG_NUMBER = 999999999 // safely bigger than any bar_index
    
    
    Signal = open[3] < close[3] and open[2] < close[2] and open[1] < close[1] and close < open and high[1] > high and high[1] > close[1] and high > open
    
    X_for_LINIA = ta.valuewhen(Signal, bar_index, 0)
    Y_for_LINIA = ta.valuewhen(Signal, high, 0)
    
    
    type linia
        line    ln // lines
        int     bi // bar indices
        label   lb
    
    // by only accessing the object using this method we guarantee that the arrays will not go out of sync. Так надежнее :)
    method remove(linia[] this, int id) =>
        this.get(id).ln.delete()
        this.remove(id)
    
    
    
    var ArrayLinia = array.new<linia>()
    
    
    
    
    if Signal
        LINIA = line.new(X_for_LINIA, Y_for_LINIA,X_for_LINIA+1,Y_for_LINIA, extend = extend.right, color = color.fuchsia, width = 1)
        lb = label.new(bar_index, close, str.tostring(array.size(ArrayLinia))+" : " + str.tostring(line.get_y1(LINIA)), color= color.white, yloc=yloc.abovebar)
        ArrayLinia.push(linia.new(LINIA, BIG_NUMBER, lb))
        
    if array.size(ArrayLinia) > 0
        NumberOfLine = array.size(ArrayLinia) 
        Complete = false
        Index = NumberOfLine - 1
        while not(Complete)
            Linia = ArrayLinia.get(Index)
            if Linia.ln.get_y1() < close and Linia.bi == BIG_NUMBER
                Linia.bi := bar_index
            if bar_index - Linia.bi >= 5
                ArrayLinia.remove(Index)
            Index := Index - 1
            if Index < 0
                Complete := true
    
    plotchar(bar_index,"bar_index", "", location = location.top)
    plotchar(array.size(ArrayLinia), "array.size(ArrayLinia)", "", location = location.top)