Triangle line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nicoakim

//@version=5
indicator("Triangle Line",overlay=true)
res1 = input.timeframe(title='Resolution Trendline', defval='')
ln = input.int(10, minval=5, title='Length of Trend')
bars = input(100, 'Bars offset') 
color col = input.color(color.yellow)

///////////////////////////////////////////////
reqsymbol(resolusi,value) => request.security(syminfo.tickerid,resolusi,value)
dt = time - time[1]

///////////////////////////////////////////////
pivothigh() =>
    hprice = 0.0
    swh = ta.pivothigh(high, ln, ln)
    hprice := not na(swh) ? swh : hprice[1]
    hprice
pivothightime() =>
    var int hprice = na
    swh = ta.pivothigh(high, ln, ln)
    hprice := not na(swh) ? time[ln] : hprice[1]
    hprice
pivotlow() =>
    lprice = 0.0
    swl = ta.pivotlow(low, ln, ln)
    lprice := not na(swl) ? swl : lprice[1]
    lprice
pivotlowtime() =>
    var int lprice = na
    swl = ta.pivotlow(low, ln,ln)
    lprice := not na(swl) ? time[ln] : lprice
    lprice
///////////////////////////////////////////////

storevalueint(value2) =>
    var int storei = na
    if value2 != value2[1]
        storei := value2[1]
    storei
storevaluefloat(value) =>
    var float store = na
    if value != value[1]
        store := value[1]
    store
    
///////////////////////////////////////////////

geting(x1,x2,y1,y2,ts) =>
    m = (y1 - y2) / (x1 - x2)
    b = y1 - m * x1
    Y = m * ts + b
    Y

////////////////////////////////////////i/j//////
abc(resolusi) =>
    xs1 = storevalueint( reqsymbol(res1,pivotlowtime()))
    ys1 = storevaluefloat( reqsymbol(res1,pivotlow()))
    xs2 =  reqsymbol(res1,pivotlowtime())
    ys2 =  reqsymbol(res1,pivotlow())
    xr1 = storevalueint( reqsymbol(res1,pivothightime()))
    yr1 = storevaluefloat( reqsymbol(res1,pivothigh()))
    xr2 =  reqsymbol(res1,pivothightime())
    yr2 =  reqsymbol(res1,pivothigh())
    breakout = ta.crossover(close,geting(xr1,xr2,yr1,yr2,time))
    breakdown= ta.crossunder(close, geting(xs1,xs2,ys1,ys2,time))
    var line s = line.new(na, na, na, na, xloc.bar_time, extend.none, color.new(color.yellow,99), line.style_solid, 1)
    var line r = line.new(na, na, na, na, xloc.bar_time, extend.none, color.new(color.yellow,99), line.style_solid, 1)
    var line sx = line.new(na, na, na, na, xloc.bar_time, extend.none, color.new(color.yellow,99), line.style_dashed, 1)
    var line rx = line.new(na, na, na, na, xloc.bar_time, extend.none, color.new(color.yellow,99), line.style_dashed, 1)
    if ys1 < ys2 and yr1 > yr2  
        line.set_xy1(s, xs1, ys1)
        line.set_xy2(s, time+bars*dt,ys1)
        line.set_xy1(r, xr1, yr1)
        line.set_xy2(r, time+bars*dt,yr1)
        line.set_xy1(sx, xs1, ys1)
        line.set_xy2(sx, time +bars*dt,geting(xs1,xs2,ys1,ys2,time + bars * dt))
        line.set_xy1(rx, xr1, yr1)
        line.set_xy2(rx, time +bars *dt,geting(xr1,xr2,yr1,yr2,time + bars *dt))
        linefill.new(sx,rx,color=color.new(col,80))

abc(res1)


