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

//@version=5
indicator("Pivot Support/Resistance",overlay=true)
gr="LENGTH LEFT / RIGHT"
leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High",group=gr)
rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High",group=gr)

leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=gr)
rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low",group=gr)


numberOfBars = input.int(title="Number of Bars", defval=100, minval=1)
extendParam = input.string(title="Extend", defval="NONE", options=["NONE", "BOTH", "LEFT", "RIGHT"])
lineDirection = input.string(title="Direction", defval="BOTH", options=["BOTH","UP", "DOWN"])
shCount = input.int(title="Swing Highs", defval=2, minval=2, step=2)
slCount = input.int(title="Swing Lows", defval=2, minval=2, step=2)

extendInput = switch extendParam
    "NONE" => extend.none
    "BOTH" => extend.both
    "LEFT" => extend.left
    "RIGHT" => extend.right


ph = ta.pivothigh(high,leftLenH, rightLenH)
var pha = array.new_float()
var phai = array.new_int()
pl = ta.pivotlow(low, leftLenL, rightLenL)
var pla = array.new_float()
var plai = array.new_int()

if not na(ph) and last_bar_index - bar_index <= numberOfBars
    array.push(pha,ph)
    array.push(phai, bar_index)
if not na(pl) and last_bar_index - bar_index <= numberOfBars
    array.push(pla,pl)
    array.push(plai, bar_index)
    

max_bars_back(time, 5000)

if (barstate.islast)
    //delete all lines
    a_allLines = line.all
    if array.size(a_allLines) > 0
	    for i = 0 to array.size(a_allLines) - 1
		    line.delete(array.get(a_allLines, i))
		    
    phac = array.copy(pha)
    plac = array.copy(pla)
        
    array.push(phac,close)
    array.push(plac,close)
    array.sort(phac)
    array.sort(plac)
    for i=0 to array.size(phac)-1
        if array.get(phac,i) == close
            if (i != array.size(phac) -1)
                for j=1 to shCount/2
                    if (i+j >= array.size(phac))
                        continue
                    if (lineDirection=="BOTH" or lineDirection=="UP")
                        phindex = array.lastindexof(pha, array.get(phac,i+j))                
                        line.new(array.get(phai,phindex)-leftLenH, array.get(phac,i+j), bar_index, array.get(phac,i+j),color=color.lime, extend=extendInput) 
            if (i != 0)
                for j=1 to shCount/2
                    if (i-j < 0)
                        continue
                    if (lineDirection=="BOTH" or lineDirection=="DOWN")
                        phindex = array.lastindexof(pha, array.get(phac,i-j))
                        line.new(array.get(phai,phindex)-leftLenH, array.get(phac,i-j), bar_index, array.get(phac,i-j),color=color.lime, extend=extendInput)
    for i=0 to array.size(plac)-1
        if array.get(plac,i) == close
            if (i != array.size(plac) -1)
                for j=1 to slCount/2
                    if (i+j >= array.size(plac))
                        continue
                    if (lineDirection=="BOTH" or lineDirection=="UP")
                        phindex = array.lastindexof(pla, array.get(plac,i+j))
                        line.new(array.get(plai,phindex)-leftLenL, array.get(plac,i+j), bar_index, array.get(plac,i+j), color=color.red,  extend=extendInput)
            if (i != 0)
                for j=1 to slCount/2
                    if (i-j < 0)
                        continue
                    if (lineDirection=="BOTH" or lineDirection=="DOWN")
                        phindex = array.lastindexof(pla, array.get(plac,i-j))
                        line.new(array.get(plai,phindex)-leftLenL, array.get(plac,i-j), bar_index, array.get(plac,i-j), color=color.red,  extend=extendInput)
