Pro Trading Art - Broadening Wedges
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © protradingart

//@version=5
indicator("Pro Trading Art - Broadening Wedges", "PTA- Broadening Wedges", 
 overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)

pivotLeg = input.int(10, "Pivot Length")
var High = 0.0
var Low = 0.0
///////////////////// PH ////////////////////////////////////////////

var broadeningTop = array.new_float(na)
var broadeningTopIndex = array.new_int(na)
var line btLineRight = na
ph = ta.pivothigh(pivotLeg, pivotLeg)

if not na(ph)
    array.push(broadeningTop, ph)
    array.push(broadeningTopIndex, bar_index-pivotLeg)


if array.size(broadeningTop) > 2
    array.shift(broadeningTop)
    array.shift(broadeningTopIndex)

if array.size(broadeningTop) >= 2
    btX1 = array.get(broadeningTopIndex, 0)
    btY1 = array.get(broadeningTop, 0)
    
    btX2 = array.get(broadeningTopIndex, 1)
    btY2 = array.get(broadeningTop, 1)
    
    line btLine = na
    
    if btY2 > btY1
        High := btY2
        btLine := line.new(x1=btX1, y1=btY1, x2=btX2, y2=btY2, width=2, color=color.lime)
        btLineRight := line.new(x1=btX2, y1=btY2, x2=bar_index, y2=btY2, 
         width=2, color=color.lime, style=line.style_dotted)
    else
        line.set_x2(btLineRight, bar_index)
        
///////////////////// PL ////////////////////////////////////////////
var broadeningBottom = array.new_float(na)
var broadeningBottomIndex = array.new_int(na)
var line bbLineRight = na

pl = ta.pivotlow(pivotLeg, pivotLeg)

if not na(pl)
    array.push(broadeningBottom, pl)
    array.push(broadeningBottomIndex, bar_index-pivotLeg)


if array.size(broadeningBottom) > 2
    array.shift(broadeningBottom)
    array.shift(broadeningBottomIndex)
    
if array.size(broadeningBottom) >= 2
    bbX1 = array.get(broadeningBottomIndex, 0)
    bbY1 = array.get(broadeningBottom, 0)
    bbX2 = array.get(broadeningBottomIndex, 1)
    bbY2 = array.get(broadeningBottom, 1)
    
    line bbLine = na

    if bbY2 > bbY1
        Low := bbY2
        bbLine := line.new(x1=bbX1, y1=bbY1, x2=bbX2, y2=bbY2, width=2, color=color.red)
        bbLineRight := line.new(x1=bbX2, y1=bbY2, x2=bar_index, y2=bbY2, 
         width=2, color=color.red, style=line.style_dotted)
    else
        line.set_x2(bbLineRight, bar_index)
    
    
longCondition = ta.crossover(close, High)
shortCondition = ta.crossunder(close, Low)

plotshape(longCondition ? low : na, style=shape.labelup, location = location.absolute, size=size.normal, color=color.rgb(6, 245, 14), text = "Long", textcolor = color.black)
plotshape(shortCondition ? high : na, style=shape.labeldown, location = location.absolute, size=size.small, color=color.rgb(250, 1, 1), text="Short", textcolor = color.white)

if longCondition
    alert("Upper Wedges Breakout In : "+ syminfo.ticker, alert.freq_once_per_bar_close)

if shortCondition
    alert("Lower Wedges Breakout In : "+ syminfo.ticker, alert.freq_once_per_bar_close)