How to implement line removal after closing the candle above it

128 Views Asked by At

I ask for help. I write an indicator, but I'm stuck - I can't find a solution.

And so - there is some signal in history, a horizontal line is automatically drawn from it - an array is created. I adjust the number of displayed lines accordingly.

Accordingly, the displayed lines on their continuation have intersections with candles - that is, the candle closes above the line.

I need the lines that were crossed by the candles to be removed, well, or change color...

And accordingly, when a new signal appears, new lines appear...

Removal or color change occurs strictly after closing the candle above the line...

I tried a bunch of options - but not a single one works correctly...

I attach a code that I could not go beyond...

Thank you...

//@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 b = array.new_float()
array.push(b, Y_for_LINIA)

peresechenie = ta.crossover(close, Y_for_LINIA)
var p = array.new_bool()
array.push(p, peresechenie)

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)
    var a = array.new_line()
    array.push(a, LINIA)
    
    var line_price = array.new_float()
    array.push(line_price, line.get_price(LINIA, bar_index))

     
    if array.size(a) > Kol_Linii
        ln = array.shift(a)
        line.delete(ln) `

enter image description here

1

There are 1 best solutions below

1
G.Lebret On BEST ANSWER

You should use an array to store your lines.
Then, on each close, loop over all the lines in this array.
If the Y value of the line is inferior to the actual close (= the candle close is above the line), delete the line and delete the corresponding array row. I tested this code which works well :

//@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 b = array.new_float()
array.push(b, Y_for_LINIA)

peresechenie = ta.crossover(close, Y_for_LINIA)
var p = array.new_bool()
array.push(p, peresechenie)

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

I put a label with the size of the ArrayLine array and the value of the Y line for testing purpose, you can delete it : enter image description here