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

//@version=5
indicator("Trend Line Xross", shorttitle = "TLX", overlay = true)

///_________________________________________
///Practical Application of y = mx + b - Trendline Intersection Point 
///‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
//Inputs
tl1_p1_x = input.time(0,confirm = true, inline = "Trendline 1 - Point 1", title = "Time", group = "Trendline 1")
tl1_p1_y = input.price(0,confirm = true, inline = "Trendline 1 - Point 1", title = "Price", group = "Trendline 1")
tl1_p2_x = input.time(0,confirm = true, inline = "Trendline 1 - Point 2", title = "Time", group = "Trendline 1")
tl1_p2_y = input.price(0,confirm = true, inline = "Trendline 1 - Point 2", title = "Price", group = "Trendline 1")
color1 = input.color(color.teal, title = "Trend Line 1 Color", group = "Trendline 1")

tl2_p1_x = input.time(0,confirm = true, inline = "Trendline 2 - Point 1", title = "Time", group = "Trendline 2")
tl2_p1_y = input.price(0,confirm = true, inline = "Trendline 2 - Point 1", title = "Price", group = "Trendline 2")
tl2_p2_x = input.time(0,confirm = true, inline = "Trendline 2 - Point 2", title = "Time", group = "Trendline 2")
tl2_p2_y = input.price(0,confirm = true, inline = "Trendline 2 - Point 2", title = "Price", group = "Trendline 2")
color2 = input.color(color.aqua, title = "Trend Line 2 Color", group = "Trendline 2")

color3 = input.color(color.red, title = "Intersection Point Line", group = "Intersection Point")

type point
    int x
    float y

//Descramble Inputs
//Aranging inputs into "Points" in time ascending order, so that the user can input the points for each line in any order.
tl1_p1 = tl1_p1_x<tl1_p2_x?point.new(tl1_p1_x,tl1_p1_y):point.new(tl1_p2_x,tl1_p2_y) 
tl1_p2 = tl1_p1_x<tl1_p2_x?point.new(tl1_p2_x,tl1_p2_y):point.new(tl1_p1_x,tl1_p1_y)

tl2_p1 = tl2_p1_x<tl2_p2_x?point.new(tl2_p1_x,tl2_p1_y):point.new(tl2_p2_x,tl2_p2_y)
tl2_p2 = tl2_p1_x<tl2_p2_x?point.new(tl2_p2_x,tl2_p2_y):point.new(tl2_p1_x,tl2_p1_y)

//Converting Time Values to Bar Index Values
//Since interactive mode only allows users to input time, I have to convert the inputs into the useable bar_index values for consistent simple calculations.
var int tl1_b1 = na
tl1_b1 := time == tl1_p1.x?bar_index:tl1_b1
var int tl1_b2 = na
tl1_b2 := time == tl1_p2.x?bar_index:tl1_b2

var int tl2_b1 = na
tl2_b1 := time == tl2_p1.x?bar_index:tl2_b1
var int tl2_b2 = na
tl2_b2 := time == tl2_p2.x?bar_index:tl2_b2

//Calculating Slopes (Price dif / Bar dif)
tl1_slope = (tl1_p2.y - tl1_p1.y)/math.abs(tl1_b1-tl1_b2) 
tl2_slope = (tl2_p2.y - tl2_p1.y)/math.abs(tl2_b1-tl2_b2)

//Determining the bar # difference between the start of both lines
//Since both points may not have the same bar_index values, I need to calculate what the (price) value of the longer line is at the place where the shorter line starts.
//This is because I need the value of both lines on the same (VERTICAL) axis, so that I can use this as my y-intercept.
start_dif = math.max(tl1_b1,tl2_b1) - math.min(tl1_b1,tl2_b1)

//Calculating the Starting Point (Price) of each Line accounting for the offset (y - Intercept)
tl1_b = tl1_b1<tl2_b1 ? tl1_p1.y + (tl1_slope*start_dif) : tl1_p1.y
tl2_b = tl1_b1>tl2_b1 ? tl2_p1.y + (tl2_slope*start_dif)  : tl2_p1.y

//Slolve for x
x = (tl2_b-tl1_b)/(tl1_slope-tl2_slope)
// y = mx + b
x_point = (tl1_slope * x) + tl1_b

//Drawing the Trendlines
//Since changing the points re-calculates the script, I only need to draw the lines once.
//I am plotting the crossing point so that alerts can be set on it.

trend_line_1 = line.new(tl1_b1,tl1_p1.y,tl1_b2,tl1_p2.y, extend = extend.both, color = color1)
trend_line_2 = line.new(tl2_b1,tl2_p1.y,tl2_b2,tl2_p2.y, extend = extend.both, color = color2)
x_line = line.new(bar_index,x_point,bar_index+1,x_point, extend = extend.both, color = color3)

//Not displaying in the pane so that I can impose the Drawn line as the Display, I am using the plot solely for the readouts and alerts.
plot(x_point, title = "TrendLine Intersection Price", display = display.all-display.pane, color = color3) 


//Alerts
alertcondition(ta.crossover(close,x_point), "X-Point CrossOver")
alertcondition(ta.crossunder(close,x_point), "X-Point CrossUnder")
alertcondition(ta.cross(close,x_point), "X-Point Cross")
alertcondition(ta.crossover(close,line.get_price(trend_line_1,bar_index)),"Trendline 1 CrossOver")
alertcondition(ta.crossunder(close,line.get_price(trend_line_1,bar_index)),"Trendline 1 CrossUnder")
alertcondition(ta.cross(close,line.get_price(trend_line_1,bar_index)),"Trendline 1 Cross")
alertcondition(ta.crossover(close,line.get_price(trend_line_2,bar_index)),"Trendline 2 CrossOver")
alertcondition(ta.crossunder(close,line.get_price(trend_line_2,bar_index)),"Trendline 2 CrossUnder")
alertcondition(ta.cross(close,line.get_price(trend_line_2,bar_index)),"Trendline 2 Cross")