Pivot Point Trend Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jadeja_rajdeep

//@version=5
indicator("Pivot Point Trend Line",overlay = true)

high_1=input.int(20,'Pivot High - 1')
high_2=input.int(15,'Pivot High - 2')

low_1=input.int(15,'Pivot Low - 1')
low_2=input.int(10,'Pivot Low - 2')

pph=ta.pivothigh(high,high_1,high_2)
ppl=ta.pivotlow(low,low_1,low_2)


var high1=0.0
var low1=0.0
var int barindexh1=na
var int barindexl1=na

var high2=0.0
var low2=0.0
var int barindexh2=na
var int barindexl2=na

var high3=0.0
var low3=0.0
var int barindexh3=na
var int barindexl3=na

var high4=0.0
var low4=0.0
var int barindexh4=na
var int barindexl4=na

var high5=0.0
var low5=0.0
var int barindexh5=na
var int barindexl5=na

var line lineh1 = na
var line lineh2 = na
var line lineh3 = na
var line lineh4 = na

var line linel1 = na
var line linel2 = na
var line linel3 = na
var line linel4 = na


if(pph)
    high5:=high4
    barindexh5:=barindexh4

    high4:=high3
    barindexh4:=barindexh3

    high3:=high2
    barindexh3:=barindexh2

    high2:=high1
    barindexh2:=barindexh1

    high1:=high[high_2]
    barindexh1:=bar_index - high_2

if(ppl)
    low5:=low4
    barindexl5:=barindexl4

    low4:=low3
    barindexl4:=barindexl3

    low3:=low2
    barindexl3:=barindexl2

    low2:=low1
    barindexl2:=barindexl1

    low1:=low[low_2]
    barindexl1:=bar_index - low_2

if(bar_index==last_bar_index)
    if(high2 > 0)
        line.delete(lineh1)
        lineh1:=line.new(x1=barindexh2,y1=high2,x2=barindexh1,y2=high1,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.red)

    if(high3 > 0)
        line.delete(lineh2)
        lineh2:=line.new(x1=barindexh3,y1=high3,x2=barindexh2,y2=high2,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.red)

    if(high4 > 0)
        line.delete(lineh3)
        lineh3:=line.new(x1=barindexh4,y1=high4,x2=barindexh3,y2=high3,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.red)

    if(high5 > 0)
        line.delete(lineh4)
        lineh4:=line.new(x1=barindexh5,y1=high5,x2=barindexh4,y2=high4,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.red)

    if(low2 > 0)
        line.delete(linel1)
        linel1:=line.new(x1=barindexl2,y1=low2,x2=barindexl1,y2=low1,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.green)

    if(low3 > 0)
        line.delete(linel2)
        linel2:=line.new(x1=barindexl3,y1=low3,x2=barindexl2,y2=low2,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.green)

    if(low4 > 0)
        line.delete(linel3)
        linel3:=line.new(x1=barindexl4,y1=low4,x2=barindexl3,y2=low3,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.green)

    if(low5 > 0)
        line.delete(linel4)
        linel4:=line.new(x1=barindexl5,y1=low5,x2=barindexl4,y2=low4,xloc=xloc.bar_index,extend=extend.right,style=line.style_solid,color=color.green)
            
            



