Fibonacci Plot [ABA Invest]]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © ABA Invest

//@version=5
indicator("Fibonacci Plot [ABA Invest]]", overlay=true, max_lines_count=500)

versi               = input.string('Standard','Fibonacci Type',options=['Musang', 'Standard'])
endpoints           = input.string('Wick-Wick','Drawing point',options=['Wick-Wick', 'Wick-Body', 'Body-Body'])
time_start          = input.time(0,'Time Start',confirm=true)
time_end            = input.time(0,'Time End',confirm=true)

//----

fib_0               = input.bool(true, '0   ',inline='inline1',group='Fibonacci')
fib_0_level         = 1
fib_0_color         = input(#e91e63,'',inline='inline1',group='Fibonacci')
fib_0_text          = '0'

fib_0236            = input.bool(true, '0.236',inline='inline2',group='Fibonacci')
fib_0236_level      = 0.236
fib_0236_color      = input(#ff5d00,'',inline='inline2',group='Fibonacci')
fib_0236_text       = '0.236'

fib_0382            = input.bool(true, '0.382',inline='inline2',group='Fibonacci')
fib_0382_level      = 0.382
fib_0382_color      = input(#ff5d00,'',inline='inline2',group='Fibonacci')
fib_0382_text       = '0.382'

fib_05              = input.bool(true, '0.5',inline='inline2',group='Fibonacci')
fib_05_level        = 0.5
fib_05_color        = input(#ff5d00,'',inline='inline2',group='Fibonacci')
fib_05_text         = '0.5'

fib_0618            = input.bool(true, '0.618',inline='inline2',group='Fibonacci')
fib_0618_level      = 0.618
fib_0618_color      = input(#ff5d00,'',inline='inline2',group='Fibonacci')
fib_0618_text       = '0.618'

fib_0786            = input.bool(true, '0.786',inline='inline2',group='Fibonacci')
fib_0786_level      = 0.786
fib_0786_color      = input(#ff5d00,'',inline='inline2',group='Fibonacci')
fib_0786_text       = '0.786'

fib_1               = input.bool(true, '1',inline='inline1',group='Fibonacci')
fib_1_level         = 0.
fib_1_color         = input(#e91e63,'',inline='inline1',group='Fibonacci')
fib_1_text          = '1'

fib_1618            = input.bool(true, '1.618',inline='inline5',group='Fibonacci')
fib_1618_level      = -.618
fib_1618_color      = input(#0cb51a,'',inline='inline5',group='Fibonacci')
fib_1618_text       = versi == "Standard" ? "1.618" : 'TP1'

fib_188             = input.bool(true, '1.88',inline='inline5',group='Fibonacci')
fib_188_level       = -.88
fib_188_color       = input(#0cb51a,'',inline='inline5',group='Fibonacci')
fib_188_text        = 'TP1'

fib_2618            = input.bool(true, '2.618',inline='inline6',group='Fibonacci')
fib_2618_level      = -1.618
fib_2618_color      = input(#423ab7,'',inline='inline6',group='Fibonacci')
fib_2618_text       = versi == "Standard" ? "2.618" : 'TP2'

fib_288             = input.bool(true, '2.88',inline='inline6',group='Fibonacci')
fib_288_level       = -1.88
fib_288_color       = input(#673ab7,'',inline='inline6',group='Fibonacci')
fib_288_text        = 'TP2'

fib_3618            = input.bool(true, '3.618',inline='inline7',group='Fibonacci')
fib_3618_level      = -2.618
fib_3618_color      = input(#673ab7,'',inline='inline7',group='Fibonacci')
fib_3618_text       = '3.618'

fib_4618            = input.bool(true, '4.618',inline='inline8',group='Fibonacci')
fib_4618_level      = -3.618
fib_4618_color      = input(#64b5f6,'',inline='inline8',group='Fibonacci')
fib_4618_text       = '4.618'

fib_323             = input.bool(true, '3.23',inline='inline8',group='Fibonacci')
fib_323_level       = -3.23
fib_323_color       = input(#64b5f6,'',inline='inline8',group='Fibonacci')
fib_323_text        = 'CC'

fib_388             = input.bool(true, '3.88',inline='inline8',group='Fibonacci')
fib_388_level       = -3.88
fib_388_color       = input(#673ab7,'',inline='inline8',group='Fibonacci')
fib_388_text        = 'CC'

connecting_line     = input(true,'Retrace Line',inline='inline7',group='Fibonacci')
line_color          = input(color.gray,'',inline='inline7',group='Fibonacci')

//----

var fib             = array.new_float(0)
var fib_color       = array.new_color(0)
var fib_str         = array.new_string()
if barstate.isfirst
    if versi == 'Musang'
        if fib_1618
            array.push(fib,fib_1618_level)
            array.push(fib_color,fib_1618_color)
            array.push(fib_str,fib_1618_text)
        if fib_188
            array.push(fib,fib_188_level)
            array.push(fib_color,fib_188_color)
            array.push(fib_str,fib_188_text)
        if fib_05
            array.push(fib,fib_05_level)
            array.push(fib_color,fib_05_color)
            array.push(fib_str,fib_05_text)
        if fib_2618
            array.push(fib,fib_2618_level)
            array.push(fib_color,fib_2618_color)
            array.push(fib_str,fib_2618_text)
        if fib_288
            array.push(fib,fib_288_level)
            array.push(fib_color,fib_288_color)
            array.push(fib_str,fib_288_text)
        if fib_1
            array.push(fib,fib_1_level)
            array.push(fib_color,fib_1_color)
            array.push(fib_str,fib_1_text)
        if fib_323
            array.push(fib,fib_323_level)
            array.push(fib_color,fib_323_color)
            array.push(fib_str,fib_323_text)
        if fib_388
            array.push(fib,fib_388_level)
            array.push(fib_color,fib_388_color)
            array.push(fib_str,fib_388_text)

    if versi == 'Standard'
        if fib_05
            array.push(fib,fib_05_level)
            array.push(fib_color,fib_05_color)
            array.push(fib_str,fib_05_text)
        if fib_1
            array.push(fib,fib_1_level)
            array.push(fib_color,fib_1_color)
            array.push(fib_str,fib_1_text)
        if fib_0618
            array.push(fib,fib_0618_level)
            array.push(fib_color,fib_0618_color)
            array.push(fib_str,fib_0618_text)
        if fib_0382
            array.push(fib,fib_0382_level)
            array.push(fib_color,fib_0382_color)
            array.push(fib_str,fib_0382_text)
        if fib_0786
            array.push(fib,fib_0786_level)
            array.push(fib_color,fib_0786_color)
            array.push(fib_str,fib_0786_text)
        if fib_0236
            array.push(fib,fib_0236_level)
            array.push(fib_color,fib_0236_color)
            array.push(fib_str,fib_0236_text)
        if fib_0
            array.push(fib,fib_0_level)
            array.push(fib_color,fib_0_color)
            array.push(fib_str,fib_0_text)
        if fib_1618
            array.push(fib,fib_1618_level)
            array.push(fib_color,fib_1618_color)
            array.push(fib_str,fib_1618_text)
        if fib_2618
            array.push(fib,fib_2618_level)
            array.push(fib_color,fib_2618_color)
            array.push(fib_str,fib_2618_text)
        if fib_3618
            array.push(fib,fib_3618_level)
            array.push(fib_color,fib_3618_color)
            array.push(fib_str,fib_3618_text)
        if fib_4618
            array.push(fib,fib_4618_level)
            array.push(fib_color,fib_4618_color)
            array.push(fib_str,fib_4618_text)

//----

n = bar_index
dt = time-time[1]

start_n = ta.valuewhen(time == time_start,n,0)
end_n = ta.valuewhen(time == time_end,n,0)

y_1 = ta.valuewhen(time == time_start,close,0)
y_2 = ta.valuewhen(time == time_end,close,0)
diff = y_2 > y_1

is_ekor_ekor = endpoints == "Wick-Wick"
is_ekor_body = endpoints == "Wick-Body"
is_body_body = endpoints == "Body-Body"

start_high_y = ta.valuewhen(time == time_start,high,0)
start_low_y = ta.valuewhen(time == time_start,low,0)
start_open_y = ta.valuewhen(time == time_start,open,0)
start_close_y = ta.valuewhen(time == time_start,close,0)
end_high_y = ta.valuewhen(time == time_end,high,0)
end_low_y = ta.valuewhen(time == time_end,low,0)
end_open_y = ta.valuewhen(time == time_end,open,0)
end_close_y = ta.valuewhen(time == time_end,close,0)

start_y = 0.0
end_y = 0.0

if is_ekor_body
    start_y := diff ? start_low_y : start_high_y
    end_y := ta.valuewhen(time == time_end,close,0)
if is_ekor_ekor
    start_y := diff ? start_low_y : start_high_y
    end_y := diff ? end_high_y : end_low_y
if is_body_body
    start_y := diff ? start_open_y : start_close_y
    end_y := diff ? end_close_y : end_open_y

diff_n = end_n - start_n
diff_y = start_y - end_y

//----

if n == math.max(start_n,end_n)
    if connecting_line
        line.new(start_n,start_y,end_n,end_y,color=line_color)
    
    for i = 0 to array.size(fib)-1
        level = end_y + array.get(fib,i)*diff_y
        line.new(start_n,level,end_n,level,color=array.get(fib_color,i))
        
        start_ext = math.max(start_n,end_n)
        line.new(start_ext,level,start_ext+1,level,color=array.get(fib_color,i),extend=extend.right,style=line.style_dashed)
        
        sty = label.style_label_right
        label.new(math.min(start_n,end_n),level,str.tostring(array.get(fib_str,i)),color=#00000000, style=sty,textcolor=array.get(fib_color,i),textalign=text.align_center,size=size.small)
              
