TrendLine Cross
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AleSaira

//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}
 //  _____  .__           _________      .__               
 // /  _  \ |  |   ____  /   _____/____  |__|___________   
 ///  /_\  \|  | _/ __ \ \_____  \\__  \ |  \_  __ \__  \  
///    |    \  |_\  ___/ /        \/ __ \|  ||  | \// __ \_
//\____|__  /____/\___  >_______  (____  /__||__|  (____  /
//        \/          \/        \/     \/               \/ 
//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}

//@version=5
indicator("TrendLine Cross", shorttitle = "TrendLine Cross" ,overlay=true, max_bars_back = 500, max_lines_count = 500)
//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}
low_time                        = input             (25, title="Low_time")
high_time                       = input             (90, title="High_time")
LowerLine_period                = input             (250, title="Higher time")
LowLine_color                   = input.color       (color.green, title="Low Line  Color")
LiquidityUp_color               = input.color       (color.rgb(208, 182, 11), title="Liquidity Up Line Color")
LowerLine_color                 = input.color       (color.rgb(41, 47, 232), title="Lower Line Color")
UpperLine_color                 = input.color       (color.rgb(19, 19, 18), title="Upper Line Color")
UpLine_color                    = input.color       (color.red, title="Up Line Color")
LiquidityDown_color             = input.color       (color.rgb(208, 182, 11), title="Liquidity Down Line Color")
width                           = input.int         (2, title="Width")

//==================////////////////////////================================/////////////////======================}
var float lowest_y1 = na
var float lowest_y2 = na
var float highest_y1 = na
var float highest_y2 = na

var float lowest_y21 = na
var int lowest_x21 = na
var float highest_y12 = na
var int highest_x12 = na

var int lowest_x2 = na
var int highest_x2 = na
var int lowest_x1 = na
var int highest_x1 = na
var float highest_y21 = na
var int highest_x21 = na
var float lowest_y12 = 100000
var int lowest_x12 = 0
//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}
// Calculate lowest and highest values

if barstate.islast
    lowest_y2 := 100000, lowest_x2 := 0, highest_y2 := 0, highest_x2 := 0, lowest_y1 := 100000, lowest_x1 := 0, highest_y1 := 0, highest_x1 := 0
    lowest_y21 := 100000, lowest_x21 := 0, lowest_y12 := 100000, lowest_x12 := 0, highest_y12 := 0,highest_x12 := 0

    for i = 1 to low_time
        lowest_y2 := low[i] < lowest_y2 ? low[i] : lowest_y2
        lowest_x2 := low[i] < lowest_y2 ? i : lowest_x2
        highest_y2 := high[i] > highest_y2 ? high[i] : highest_y2
        highest_x2 := high[i] > highest_y2 ? i : highest_x2
    
    for j = (low_time + 1) to high_time
        lowest_x1 := low[j] < lowest_y1 ? j : lowest_x1
        lowest_y1 := low[j] < lowest_y1 ? low[j] : lowest_y1
        highest_x1 := high[j] > highest_y1 ? j : highest_x1
        highest_y1 := high[j] > highest_y1 ? high[j] : highest_y1

    for i = 1 to LowerLine_period
        lowest_x21 := low[i] < lowest_y21 ? i : lowest_x21
        lowest_y21 := low[i] < lowest_y21 ? low[i] : lowest_y21
        highest_x21 := high[i] > highest_y21 ? i : highest_x21
        highest_y21 := high[i] > highest_y21 ? high[i] : highest_y21


    for j = (low_time + 1) to LowerLine_period
        lowest_x12 := low[j] < lowest_y12 ? j : lowest_x12
        lowest_y12 := low[j] < lowest_y12 ? low[j] : lowest_y12
        highest_x12 := high[j] > highest_y12 ? j : highest_x12
        highest_y12 := high[j] > highest_y12 ? high[j] : highest_y12

//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}

// Alert condition
alertcondition(condition=(lowest_x12 == bar_index) or (highest_x21 == bar_index), title="TrendLine Cross Alert", message="TrendLine Cross Detected")
//==================////////////////////////================================/////////////////======================}

// Plotting lines
sup = line.new(x1=bar_index[lowest_x1], y1=lowest_y1, x2=bar_index[lowest_x2], y2=lowest_y2, extend=extend.right, width=width, color=LowLine_color)
rea = line.new(x1=bar_index[lowest_x1], y1=lowest_y1, x2=bar_index[lowest_x2], y2=lowest_y1, extend=extend.right, width=width, color=LiquidityUp_color)
LowerLine = line.new(x1=bar_index[lowest_x21], y1=lowest_y1, x2=bar_index[lowest_x2], y2=lowest_y2, extend=extend.right, width=width, color=LowerLine_color)
doe = line.new(x1=bar_index[highest_x12], y1=highest_y12, x2=bar_index[lowest_x2], y2=lowest_y2, extend=extend.right, width=width, color=UpperLine_color)
res = line.new(x1=bar_index[highest_x1], y1=highest_y1, x2=bar_index[highest_x2], y2=highest_y2, extend=extend.right, width=width, color=UpLine_color)
liquidity = line.new(x1=bar_index[highest_x1], y1=highest_y1, x2=bar_index[highest_x2], y2=highest_y1, extend=extend.right, width=width, color=LiquidityDown_color)
line.delete(sup[1]),line.delete(res[1]),line.delete(LowerLine[1]),line.delete(doe[1]), line.delete(rea[1]),line.delete(liquidity[1])
//==================////////////////////////================================/////////////////======================}
//==================////////////////////////================================/////////////////======================}
