Pivot Support & Resistance [DeltaAlgo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DeltaAlgo

//@version=5
indicator("Pivot Support & Resistance [DeltaAlgo]", overlay = true, max_lines_count = 500)

// ----------------- }}
// --- user inputs - }}
// ----------------- }}

period    = input.int(10, "Pivot Period", 3, 100, 1, "", "", "pivot s&r settings")
maxLine   = input.int(3, "Maximum Lines", 1, 100, 1, "if set at 3 there will be 6 total, 3 support & 3 resistance", "", "pivot s&r settings")

resCol    = input.color(#ff5252, "S & R", "", "colors", "pivot s&r settings")
supCol    = input.color(#4caf50, "", "", "colors", "pivot s&r settings")
lineWidth = input.int(2, "", 0, 5, 1, "Resistance Line Colors\n\nSupport Line Colors\n\nLine Width", "colors", "pivot s&r settings")

// ------------------------------- }}
// ----- pivot s&Resistance ------ }}
// ------------------------------- }}

pivotHigh = ta.pivothigh(period, period)
pivotLow  = ta.pivotlow(period, period)

var resLine = array.new_line()
var supLine = array.new_line()

hh = not na(pivotHigh)
ll = not na(pivotLow)

// -------- Resistance Lines ---- }}

if hh
    newHighLine = line.new(bar_index[period], high[period], bar_index, high[period], color = resCol, width = lineWidth)
    array.push(resLine, newHighLine)

    if array.size(resLine) > maxLine
        line.delete(array.get(resLine, 0))
        array.remove(resLine, 0)

for i = array.size(resLine) - 1 to 0 

    if array.size(resLine) - 1 > 0
        line.set_x2(array.get(resLine, i), bar_index)
        highPrice = line.get_y1(array.get(resLine, i))

        if close > highPrice
            line.delete(array.get(resLine, i))
            array.remove(resLine, i)

// -------- Support Lines --- }}

if ll
    newLowLine = line.new(bar_index[period], low[period], bar_index, low[period], color = supCol, width = lineWidth)
    array.push(supLine, newLowLine)

    if array.size(supLine) > maxLine
        line.delete(array.get(supLine, 0))
        array.remove(supLine, 0)

for i = array.size(supLine) - 1 to 0

    if array.size(supLine) - 1 > 0
        line.set_x2(array.get(supLine, i), bar_index)
        lowPrice = line.get_y1(array.get(supLine, i))

        if close < lowPrice
            line.delete(array.get(supLine, i))
            array.remove(supLine, i)


// ----------------------------------------- }}
// ------ End Of Script -------------------- }}
// ----------------------------------------- }}
