Support & Ressistance by @kaleboraciy [REUPLOAD]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kaleboraciy

//@version=4
study("Support & Ressistance by @kaleboraciy", overlay = true, max_lines_count = 500, max_labels_count = 500)

pivot_len = input(20, title = 'Pivot Points Length', type = input.integer)
inacc = input(0.5, title = 'Inaccuracy', type = input.float, step = 0.1) / 100 * close 
width = input(10, title = 'Linewidth')
width2 = input(2, title = 'Linewidth of broken line')
show_broken = input(false, title = 'Сontinue broken lines?')
start = input(timestamp("2021-01-01"), type = input.time, title = 'Start calculations from')

time_cond = time >= start

ph = pivothigh(high, pivot_len, pivot_len) 
pl = pivotlow(low, pivot_len, pivot_len)

var lines_upper = array.new_line(1)
var lines_lower = array.new_line(1)

if array.size(lines_upper) > 500
    array.shift(lines_upper)
    
if array.size(lines_lower) > 500
    array.shift(lines_lower)

// deleting bad levels

k = 0

for i = 0 to array.size(lines_upper) - 1
    line_price = line.get_y2(array.get(lines_upper, i - k))
    if high - inacc > line_price
        x1 = line.get_x1(array.get(lines_upper, i - k))
        x2 = line.get_x2(array.get(lines_upper, i - k))
        
        
        y1 = line.get_y1(array.get(lines_upper, i - k))
        
        if x1 != x2
            line.new(x1, y1, bar_index, y1, extend = extend.none, color = color.new(color.green, 50), width = width)
            line.set_x2(array.get(lines_upper, i - k), bar_index)
            alert("Green level at "+ tostring(y1) + " has broken!", freq = alert.freq_once_per_bar)
            if show_broken
                line.new(time_close[1], y1, time_close + 1, y1, extend = extend.right, color = color.new(color.green, 50), width = width2, xloc = xloc.bar_time)
            
        line.delete(array.get(lines_upper, i - k))
        array.remove(lines_upper, i - k)
        
        k := k + 1
        
k := 0
        
for i = 0 to array.size(lines_lower) - 1
    line_price = line.get_y2(array.get(lines_lower, i - k))
    if low + inacc < line_price
        x1 = line.get_x1(array.get(lines_lower, i - k))
        x2 = line.get_x2(array.get(lines_lower, i - k))
        
        
        y1 = line.get_y1(array.get(lines_lower, i - k))
        
        if x1 != x2
            line.new(x1, y1, bar_index, y1, extend = extend.none, color = color.new(color.red, 50), width = width)
            line.set_x2(array.get(lines_lower, i - k), bar_index)
            alert("Red level at "+ tostring(y1) + " has broken!", freq = alert.freq_once_per_bar)
            if show_broken
                line.new(time_close[1], y1, time_close + 1, y1, extend = extend.right, color = color.new(color.red, 50), width = width2, xloc = xloc.bar_time)
            
        line.delete(array.get(lines_lower, i - k))
        array.remove(lines_lower, i - k)
        
        k := k + 1
if ph and time_cond
    bool flag = false
    for i = 0 to array.size(lines_upper) - 1
        line_price = line.get_y2(array.get(lines_upper, i))
        if ph >= line_price - inacc and ph <= line_price + inacc
            flag := true
            line.set_color(array.get(lines_upper, i), color.new(color.green, 50))
            line.set_x2(array.get(lines_upper, i), bar_index)
    if not flag
        array.push(lines_upper, line.new(bar_index[pivot_len], ph, bar_index[pivot_len], ph, color = na, extend = extend.right, width = width))
    
if pl and time_cond
    bool flag = false
    for i = 0 to array.size(lines_lower) - 1
        line_price = line.get_y2(array.get(lines_lower, i))
        if pl >= line_price - inacc and pl <= line_price + inacc
            flag := true
            line.set_color(array.get(lines_lower, i), color.new(color.red, 50))
            line.set_x2(array.get(lines_lower, i), bar_index)
    if not flag
        array.push(lines_lower, line.new(bar_index[pivot_len], pl, bar_index[pivot_len], pl, color = na, extend = extend.right, width = width))
  
            
// plot(array.size(lines_upper))

// plot(ph)
// plot(pl)