Titans Trend Lines
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mmmarsh

//@version=4
study("Trend Lines",overlay=true)
//This indicator will plot out trend lines based on recent pivot highs and lows
pivotSensitivityL=input(defval=9,type=input.integer,title="Pivot Sensitivity Left")
pivotSensitivityR=input(defval=9,type=input.integer,title="Pivot Sensitivity Right")
pivotHigh=pivothigh(pivotSensitivityL,pivotSensitivityR)
pivotLow=pivotlow(pivotSensitivityL,pivotSensitivityR)
plotchar(pivotHigh, char='H', location=location.abovebar, color=color.purple, offset=-pivotSensitivityR)
plotchar(pivotLow, char='L', location=location.belowbar, color=color.purple, offset=-pivotSensitivityR)
searchWindow=input(defval=100,type=input.integer,title="Search Window")

//Find the first pivot high
barHigh1=0
barHigh2=0
for i=0 to searchWindow
    if pivotHigh[i]>0
        barHigh1:=i
        break
//Find the second pivot high        
barHigh2:=barHigh1+1
for j=barHigh2 to barHigh2+searchWindow
    if pivotHigh[j]>0
        barHigh2:=j
        break
barHigh1:=barHigh1+pivotSensitivityR
barHigh2:=barHigh2+pivotSensitivityR

lineHigh=line.new(bar_index-barHigh2, high[barHigh2], bar_index-barHigh1, high[barHigh1], extend=extend.both, color=color.purple, style=line.style_dotted, width=2)
line.set_color(id=lineHigh[1],color=color.new(color.gray,50))
line.delete(id=lineHigh[2])

//Find the first pivot low
barLow1=0
barLow2=0
for k=0 to searchWindow
    if pivotLow[k]>0
        barLow1:=k
        break
//Find the second pivot low
barLow2:=barLow1+1
for m=barLow2 to barLow2+searchWindow
    if pivotLow[m]>0
        barLow2:=m
        break
barLow1:=barLow1+pivotSensitivityR
barLow2:=barLow2+pivotSensitivityR

lineLow=line.new(bar_index-barLow2, low[barLow2], bar_index-barLow1, low[barLow1], extend=extend.both, color=color.purple, style=line.style_dotted, width=2)
line.set_color(id=lineLow[1],color=color.new(color.gray,50))
line.delete(id=lineLow[2])

