Tetra Trendline Indicator 2.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © egghen

//@version=5
indicator(title='Tetra Trendline Indicator 2.0', shorttitle='TTLI 2.0', overlay=true)

// Define input parameters for trendlines
showTrendline1 = input(true, title='Show Trendline 1')
trendlineColor1 = input(color.blue, title='Trendline 1 Color')
trendlineWidth1 = input(1, title='Trendline 1 Width')
trendlineLength1 = input(100, title='Trendline 1 Length')
alertOnTrendline1Break = input(true, title='Alert on Trendline 1 Break')

showTrendline2 = input(true, title='Show Trendline 2')
trendlineColor2 = input(color.red, title='Trendline 2 Color')
trendlineWidth2 = input(1, title='Trendline 2 Width')
trendlineLength2 = input(150, title='Trendline 2 Length')
alertOnTrendline2Break = input(true, title='Alert on Trendline 2 Break')

showTrendline3 = input(true, title='Show Trendline 3')
trendlineColor3 = input(color.green, title='Trendline 3 Color')
trendlineWidth3 = input(1, title='Trendline 3 Width')
trendlineLength3 = input(200, title='Trendline 3 Length')
alertOnTrendline3Break = input(true, title='Alert on Trendline 3 Break')

showTrendline4 = input(true, title='Show Trendline 4')
trendlineColor4 = input(color.orange, title='Trendline 4 Color')
trendlineWidth4 = input(1, title='Trendline 4 Width')
trendlineLength4 = input(250, title='Trendline 4 Length')
alertOnTrendline4Break = input(true, title='Alert on Trendline 4 Break')

// Calculate trendline coordinates for Trendline 1
var float tl1_x1 = na
var float tl1_y1 = na
var float tl1_x2 = na
var float tl1_y2 = na

if showTrendline1
    tl1_x1 := bar_index - trendlineLength1
    tl1_y1 := ta.lowest(low, trendlineLength1)
    tl1_x2 := bar_index
    tl1_y2 := ta.lowest(low, 1)
    tl1_y2

// Calculate trendline coordinates for Trendline 2
var float tl2_x1 = na
var float tl2_y1 = na
var float tl2_x2 = na
var float tl2_y2 = na

if showTrendline2
    tl2_x1 := bar_index - trendlineLength2
    tl2_y1 := ta.highest(high, trendlineLength2)
    tl2_x2 := bar_index
    tl2_y2 := ta.highest(high, 1)
    tl2_y2

// Calculate trendline coordinates for Trendline 3
var float tl3_x1 = na
var float tl3_y1 = na
var float tl3_x2 = na
var float tl3_y2 = na

if showTrendline3
    tl3_x1 := bar_index - trendlineLength3
    tl3_y1 := ta.lowest(low, trendlineLength3)
    tl3_x2 := bar_index
    tl3_y2 := ta.lowest(low, 1)
    tl3_y2

// Calculate trendline coordinates for Trendline 4
var float tl4_x1 = na
var float tl4_y1 = na
var float tl4_x2 = na
var float tl4_y2 = na

if showTrendline4
    tl4_x1 := bar_index - trendlineLength4
    tl4_y1 := ta.highest(high, trendlineLength4)
    tl4_x2 := bar_index
    tl4_y2 := ta.highest(high, 1)
    tl4_y2

// Plot trendlines using plot function
plot1 = plot(showTrendline1 ? tl1_y1 : na, title='Trendline 1', color=trendlineColor1, linewidth=trendlineWidth1)
plot2 = plot(showTrendline2 ? tl2_y1 : na, title='Trendline 2', color=trendlineColor2, linewidth=trendlineWidth2)
plot3 = plot(showTrendline3 ? tl3_y1 : na, title='Trendline 3', color=trendlineColor3, linewidth=trendlineWidth3)
plot4 = plot(showTrendline4 ? tl4_y1 : na, title='Trendline 4', color=trendlineColor4, linewidth=trendlineWidth4)

// Fill the area between trendlines (optional)
plot(showTrendline1 ? tl1_y1 : na, color=color.new(color.gray, 80), style=plot.style_histogram, linewidth=0)
plot(showTrendline2 ? tl2_y1 : na, color=color.new(color.gray, 80), style=plot.style_histogram, linewidth=0)
plot(showTrendline3 ? tl3_y1 : na, color=color.new(color.gray, 80), style=plot.style_histogram, linewidth=0)
plot(showTrendline4 ? tl4_y1 : na, color=color.new(color.gray, 80), style=plot.style_histogram, linewidth=0)

// Alert conditions for Trendline 1 break
alertcondition(alertOnTrendline1Break and ta.crossover(high, tl1_y1), title='Alert: Trendline 1 Break (Overbought)', message='Trendline 1 has been breached from below (Overbought)')

// Alert conditions for Trendline 2 break
alertcondition(alertOnTrendline2Break and ta.crossunder(low, tl2_y1), title='Alert: Trendline 2 Break (Oversold)', message='Trendline 2 has been breached from above (Oversold)')

// Alert conditions for Trendline 3 break
alertcondition(alertOnTrendline3Break and ta.crossover(high, tl3_y1), title='Alert: Trendline 3 Break (Overbought)', message='Trendline 3 has been breached from below (Overbought)')

// Alert conditions for Trendline 4 break
alertcondition(alertOnTrendline4Break and ta.crossunder(low, tl4_y1), title='Alert: Trendline 4 Break (Oversold)', message='Trendline 4 has been breached from above (Oversold)')

// end code