TRENDLINE V1
// 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("TRENDLINE V2",overlay=true)
res1 = input.timeframe(title='Resolution Trendline', defval='')
ln = input.int(10, minval=5, title='Length of Trend')
bars = input(100, 'Bars offset') 

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

///////////////////////////////////////////////
f_round(_val, _decimals) =>
    // Rounds _val to _decimals places.
    _p = math.pow(10, _decimals)
    math.round(math.abs(_val) * _p) / _p * math.sign(_val)
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 = (y2 - y1) / (x2 - x1)
    b = y1 - m * x1
    Y = m * ts + b
    Y
///////////////////////////////////////////////

fibcalc(hi,lo,out) =>
    maxr = hi
    minr = lo
    ranr = maxr - minr
    plussatu6 = maxr - 1.618 * ranr
    satu = maxr 
    nol7 = maxr - 0.236 * ranr
    nol6 = maxr - 0.382 * ranr
    nol5 = maxr - 0.50 * ranr
    nol3 = minr + 0.382 * ranr
    nol2 = minr + 0.236 * ranr
    nol = minr
    minsatu6 = minr  + 1.618 * ranr
    out == 1 ? satu : out == 0.7 ? nol7 : out == 0.6 ? nol6 
     : out == 0.5 ? nol5 : out == 0.3 ? nol3 : out == 0.2 ? nol2 : out == -1.6 ? minsatu6 : out == 1.6 ? plussatu6 : nol


trend(resolusi,out) =>
    var linefill[] filline = array.new_linefill() 
    var line[] upline = array.new_line() 
    var line[] downline = array.new_line()  
    var line[] satu = array.new_line() 
    var line[] nol7 = array.new_line()  
    var line[] nol6 = array.new_line()  
    var line[] nol5 = array.new_line()  
    var line[] nol3 = array.new_line()  
    var line[] nol2 = array.new_line() 
    var line[] nol = array.new_line()  
    xs1 = storevalueint( reqsymbol2(res1,pivotlowtime()))
    ys1 = storevaluefloat( reqsymbol2(res1,pivotlow()))
    xs2 =  reqsymbol2(res1,pivotlowtime())
    ys2 =  reqsymbol2(res1,pivotlow())
    xs3 = storevalueint(xs1)
    ys3 = storevaluefloat(ys1)
    xr1 = storevalueint( reqsymbol2(res1,pivothightime()))
    yr1 = storevaluefloat( reqsymbol2(res1,pivothigh()))
    xr2 =  reqsymbol2(res1,pivothightime())
    yr2 =  reqsymbol2(res1,pivothigh())
    xr3 = storevalueint(xr1)
    yr3 = storevaluefloat(yr1)
    bear=  yr2<yr1 and yr1 < yr3
    bear2=  ys2<ys1 and ys1 < ys3
    bull=  yr2>yr1 and yr1 > yr3
    bull2=  ys2>ys1 and ys1 > ys3
    var float bul = na
    bul := bull and bull2 ? 1 : 2
    var float ber = na
    ber := bear and bear2 ? 1 : 2
    var int akhir = na
    var float highi = na
    var float lowi = na
    //////////////////////////suport/////////////////////////////////
    if bul != bul[1] and bul == 1
        highi := yr2
        lowi := ys3
        array.unshift(downline,line.new(xs3,ys3, time +bars *dt,geting(xs3,xs2,ys3,ys2,time + bars *dt),xloc=xloc.bar_time,color=color.green))
    if ber != ber[1] and ber == 1
        highi := yr3
        lowi := ys2
        array.unshift(upline,line.new(xr3,yr3, time +bars *dt,geting(xr3,xr2,yr3,yr2,time + bars *dt),xloc=xloc.bar_time,color=color.red))
    out == "h" ? highi : lowi
hh=trend(res1,"h")
ll=trend(res1,"l")

one=plot(fibcalc(hh,ll,1),color=color.new(color.red,80))
one7=plot(fibcalc(hh,ll,0.7),color=na)
one6=plot(fibcalc(hh,ll,0.6),color=color.green)
one5=plot(fibcalc(hh,ll,0.5),color=color.yellow,style=plot.style_circles)
one3=plot(fibcalc(hh,ll,0.3),color=color.red)
one2=plot(fibcalc(hh,ll,0.2),color=na)
nol=plot(fibcalc(hh,ll,0),color=na)
satu6=plot(fibcalc(hh,ll,1.6),color=na)
minsatu6=plot(fibcalc(hh,ll,-1.6),color=na)

fill(one,one7,color=color.new(color.red,80))
fill(one7,one6,color=color.new(#9c27b0,80))
fill(one6,one5,color=color.new(color.green,80))
fill(one5,one3,color=color.new(#f44336,80))
fill(one3,one2,color=color.new(color.blue,80))
fill(one2,nol,color=color.new(#787b86,80))
fill(minsatu6,one,color=color.new(color.lime,80))
fill(satu6,nol,color=color.new(color.maroon,80))

if time[1] !=time
    l1=label.new(time+5*dt,fibcalc(hh,ll,1),text="(100%) " + str.tostring(f_round(fibcalc(hh,ll,1),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l2=label.new(time+5*dt,fibcalc(hh,ll,0.7),text="(70%) " + str.tostring(f_round(fibcalc(hh,ll,0.7),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l3=label.new(time+5*dt,fibcalc(hh,ll,0.6),text="(60%) " + str.tostring(f_round(fibcalc(hh,ll,0.6),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l4=label.new(time+5*dt,fibcalc(hh,ll,0.5),text="(50%) " + str.tostring(f_round(fibcalc(hh,ll,0.5),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l5=label.new(time+5*dt,fibcalc(hh,ll,0.3),text="(30%) " + str.tostring(f_round(fibcalc(hh,ll,0.3),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l6=label.new(time+5*dt,fibcalc(hh,ll,0.2),text="(20%) " + str.tostring(f_round(fibcalc(hh,ll,0.2),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    l7=label.new(time+5*dt,fibcalc(hh,ll,0),text="(0%) " + str.tostring(f_round(fibcalc(hh,ll,0),2)),xloc=xloc.bar_time,textcolor=color.white,style=label.style_label_center,size=size.tiny,color=na)
    label.delete(l1[1])
    label.delete(l2[1])
    label.delete(l3[1])
    label.delete(l4[1])
    label.delete(l5[1])
    label.delete(l6[1])
    label.delete(l7[1])