Fibo-Auto
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MrMagic09

//@version=5
indicator("Auto-Fibo",shorttitle="Fibo+",overlay=true)
src1=input.source(high,"SourceHigh",tooltip="High is recommended for fibonacci retracement")
src2=input.source(low,"SourceLow",tooltip="Low is recommended for fibonacci retracement")
bars=input.int(1000,"Bars",minval=2)//check bar number
res=ta.highest(src1,bars)
lineLength=input.int(title="Line Length",defval=20,minval=10,step=5)
sup=ta.lowest(src2,bars)
lowestbar=ta.lowestbars(src2,bars)
highestbar=ta.lowestbars(src1,bars)
lowbar=math.abs(ta.lowestbars(src1,bars))
highbar=math.abs(ta.highestbars(src2,bars))
//Finding pivot points
plot(sup,title="Bottom",linewidth=2,trackprice=true,show_last=1,display=display.none,color=color.red)
plot(res,trackprice=true,linewidth=2,title="Top",show_last=1,display=display.none,color=color.red)
control = lowbar > highbar ? true : false //checking which is left (lowest or highest)
//fibo levels
fibo2681= control == false ? sup+(res-sup)*2681/1000 : res-(res-sup)*2681/1000
fibo1681= control == false ? sup+(res-sup)*1681/1000 : res-(res-sup)*1681/1000
fibo786 = control == false ? sup+(res-sup)*786/1000 : res-(res-sup)*786/1000
fibo618 = control == false ? sup+(res-sup)*618/1000 : res-(res-sup)*618/1000
fibo500 = control == false ? sup+(res-sup)*500/1000 : res-(res-sup)*500/1000
fibo382 = control == false ? sup+(res-sup)*382/1000 : res-(res-sup)*382/1000
fibo236 = control == false ? sup+(res-sup)*236/1000 : res-(res-sup)*236/1000
//plotting
plot(fibo2681,color=color.lime,linewidth=2,title="2.681",display=display.none,trackprice=true,show_last=1)
plot(fibo1681,color=color.purple,linewidth=2,title="1.681",display=display.none,trackprice=true,show_last=1)
plot(fibo786,color=color.red,linewidth=2,title="0.786",trackprice=true,show_last=1)
plot(fibo618,color=color.blue,linewidth=2,title="0.618",trackprice=true,show_last=1)
plot(fibo500,color=color.green,linewidth=2,title="0.500",trackprice=true,show_last=1)
plot(fibo382,color=color.lime,linewidth=2,title="0.382",trackprice=true,show_last=1)
plot(fibo236,color=color.purple,linewidth=2,title="0.236",trackprice=true,show_last=1)
FindingBarIndex(bars1)=>
    int barindex=1
    a=ta.highest(high,bars)
    for i=1 to bars1
        if high[i]!=a
            barindex+=1
        if high[i]==a
            break
    barindex        
//plotting pivot points     
plot(ta.highest(high,bars),title="Top",offset=-FindingBarIndex(bars)+lineLength,show_last=lineLength,linewidth=3,color=color.red)
FindingBarIndex2(bars2)=>
    int barindex1=1
    b=ta.lowest(low,bars)
    for c=1 to bars2
        if low[c]!=b
            barindex1+=1
        if low[c]==b
            break
    barindex1        
plot(ta.lowest(low,bars),title="Bottom",offset=-FindingBarIndex2(bars)+lineLength,show_last=lineLength,linewidth=3,color=color.blue)