Auto Fibonacci with .88
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @TrendCrypto2022

//@version=5
indicator("Fibonacci", overlay=true, max_lines_count = 500)

//Input
buyonly    =  input.bool   (title="Find entry  Long",     defval=true,   group="Set up Find entry")
sellonly   =  input.bool    (title="Find entry Short",   defval=false,   group="Set up Find entry")

leftbars    = input.int(10,     minval=1, title='Pivot Detection: Left Bars', group = "Set up Pivot High/Low")
rightbars   = input.int(5,      minval=1, title='Pivot Detection: Right Bars', group = "Set up Pivot High/Low")

width_fibo          = input.int(2,      minval=1, title='Width Fibonacci levels',   group = "Custom Selection",             inline = "1")
width_sup_res       = input.int(2,      minval=1, title='Width Sup/Res levels',     group = "Custom Selection",             inline = "2")
width_trendline     = input.int(2,      minval=1, title='Width Trendline',     group = "Custom Selection")

extend_fibo         = input.int(5,      minval=1, title='Extention',                group = "Custom Selection",             inline = "1")
color_fibo_label    = input.color(color.new(color.yellow, 0), title="Color Labels", group = "Custom Selection",             inline = "1")
extend_sup_res      = input.int(5,      minval=1, title='Extention',                group = "Custom Selection",             inline = "2")


// Pivots
ph = ta.pivothigh(high,     leftbars, rightbars)
pl = ta.pivotlow(low,       leftbars, rightbars)

//Caculate Support/Resistant levels based on Pivots
phvalue1    = ta.valuewhen(ph, high[rightbars], 0)
phbar1      = ta.valuewhen(ph, bar_index[rightbars], 0),        phv1low = ta.valuewhen(ph, close[rightbars]>open[rightbars] ? close[rightbars] : open[rightbars], 0)
phvalue2    = ta.valuewhen(ph, high[rightbars], 1)
phbar2      = ta.valuewhen(ph, bar_index[rightbars], 1),        phv2low = ta.valuewhen(ph, close[rightbars]>open[rightbars] ? close[rightbars] : open[rightbars], 1)
phbar3      = ta.valuewhen(ph, bar_index[rightbars], 2),
phvalue3    = ta.valuewhen(ph, high[rightbars], 2)

plvalue1    = ta.valuewhen(pl, low[rightbars], 0)
plbar1      = ta.valuewhen(pl, bar_index[rightbars], 0),        plv1low = ta.valuewhen(pl, close[rightbars]<open[rightbars] ? close[rightbars] : open[rightbars], 0)
plvalue2    = ta.valuewhen(pl, low[rightbars], 1)
plbar2      = ta.valuewhen(pl, bar_index[rightbars], 1)
plbar3      = ta.valuewhen(pl, bar_index[rightbars], 2),        plv2low = ta.valuewhen(pl, close[rightbars]<open[rightbars] ? close[rightbars] : open[rightbars], 1)
plvalue3    = ta.valuewhen(pl, low[rightbars], 2)

plotshape(ph, style=shape.diamond, location=location.abovebar, color=color.new(color.red, 0),    title='Pivot High',    offset=-rightbars)
plotshape(pl, style=shape.diamond, location=location.belowbar, color=color.new(color.green, 0),  title='Pivot Low',     offset=-rightbars)


//Calculate trendlines
_slope(x1, x2, y1, y2)  =>
    m = (y2 - y1) / (x2 - x1)
    m
get_y_oxy(m, x1, y1)    =>
    b = y1 - m * x1
    b
get_y(m, b, ts)         =>
    Y = m * ts + b
    Y
int         res_x1 = na
float       res_y1 = na
int         res_x2 = na
float       res_y2 = na
int         sup_x1 = na
float       sup_y1 = na
int         sup_x2 = na
float       sup_y2 = na
res_x1 := ph ? phbar2 : res_x1[1]
res_y1 := ph ? phvalue2 : res_y1[1]
res_x2 := ph ? phbar3 : res_x2[1]
res_y2 := ph ? phvalue3 : res_y2[1]
res_m = _slope(res_x1, res_x2, res_y1, res_y2)
res_b = get_y_oxy(res_m, res_x1, res_y1)
res_y = get_y(res_m, res_b, bar_index)

sup_x1 := pl ? plbar2 : sup_x1[1]
sup_y1 := pl ? plvalue2 : sup_y1[1]
sup_x2 := pl ? plbar3 : sup_x2[1]
sup_y2 := pl ? plvalue3 : sup_y2[1]
sup_m = _slope(sup_x1, sup_x2, sup_y1, sup_y2)
sup_b = get_y_oxy(sup_m, sup_x1, sup_y1)
sup_y = get_y(sup_m, sup_b, bar_index)

// Setup Alert
alert_input         = input.string(title='Set alert Long Trade when price test',    defval='Fibo 0.5', options=['Fibo 0.382', 'Fibo 0.5', 'Fibo 0.618', 'Fibo 0.786', 'Support Zone', 'Trendline'], group='Set up Alert')
alertshort_input    = input.string(title='Set alert Short Trade when price test',   defval='Fibo 0.5', options=['Fibo 0.382', 'Fibo 0.5', 'Fibo 0.618', 'Fibo 0.786', 'Resistant Zone', 'Trendline'], group='Set up Alert')

distance_x = timenow + math.round(ta.change(time) * 1)



//Draw Fibonacci levels, Support levels and labels
if ph and phvalue1 > phvalue2 and buyonly == true
    line.new(plbar1, plvalue1,                              phbar1, phvalue1,                                                   style=line.style_arrow_right, color=color.lime, width = 2)
    line.new(plbar1, phvalue1,                              phbar1+ rightbars+extend_fibo, phvalue1,                               style=line.style_solid, color=color.new(color.silver, 60), width = width_fibo)
    line.new(plbar1, phvalue1-(phvalue1-plvalue1)*0.88,    phbar1+ rightbars+extend_fibo, phvalue1-(phvalue1-plvalue1)*0.88,     style=line.style_solid, color=color.new(color.red, 60), width = width_fibo)
    line.new(plbar1, phvalue1-(phvalue1-plvalue1)*0.382,    phbar1+ rightbars+extend_fibo, phvalue1-(phvalue1-plvalue1)*0.382,     style=line.style_solid, color=color.new(#81c784, 60), width = width_fibo)
    line.new(plbar1, phvalue1-(phvalue1-plvalue1)*0.500,    phbar1+ rightbars+extend_fibo, phvalue1-(phvalue1-plvalue1)*0.500,     style=line.style_solid, color=color.new(color.green, 60), width = width_fibo)
    line.new(plbar1, phvalue1-(phvalue1-plvalue1)*0.618,    phbar1+ rightbars+extend_fibo, phvalue1-(phvalue1-plvalue1)*0.618,     style=line.style_solid, color=color.new(#089981, 60), width = width_fibo)
    line.new(plbar1, phvalue1-(phvalue1-plvalue1)*0.786,    phbar1+ rightbars+extend_fibo, phvalue1-(phvalue1-plvalue1)*0.786,     style=line.style_solid, color=color.new(color.aqua, 60), width = width_fibo)
    
    if close<phvalue1-(phvalue1-plvalue1)*0.382 and alert_input == 'Fibo 0.382'
        alert(message = "Fibo 0.382", freq = alert.freq_once_per_bar)
    if close<phvalue1-(phvalue1-plvalue1)*0.500 and alert_input == 'Fibo 0.5'
        alert(message = "Fibo 0.5", freq = alert.freq_once_per_bar)
    if close<phvalue1-(phvalue1-plvalue1)*0.618 and alert_input == 'Fibo 0.618'
        alert(message = "Fibo 0.618", freq = alert.freq_once_per_bar)
    if close<phvalue1-(phvalue1-plvalue1)*0.786 and alert_input == 'Fibo 0.786'
        alert(message = "Fibo 0.786", freq = alert.freq_once_per_bar)
    if close<phvalue2                           and alert_input == 'Support Zone'
        alert(message = "Support Zone", freq = alert.freq_once_per_bar)
    if close<res_y + res_y*0.005                and alert_input == 'Trendline'
        alert(message = "Trendline",    freq = alert.freq_once_per_bar)
    var label labelfibo0 = na
    label.delete(labelfibo0)
    labelfibo0 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0,        text='0', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo88 = na
    label.delete(labelfibo88)
    labelfibo88 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0.88,  text='0.88', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo382 = na
    label.delete(labelfibo382)
    labelfibo382 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0.382,  text='0.382', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo5 = na
    label.delete(labelfibo5)
    labelfibo5 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0.5,      text='0.5', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo618 = na
    label.delete(labelfibo618)
    labelfibo618 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0.618,  text='0.618', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo786 = na
    label.delete(labelfibo786)
    labelfibo786 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*0.786,  text='0.786', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo1 = na
    label.delete(labelfibo1)
    labelfibo1 := label.new(x=distance_x, y=phvalue1-(phvalue1-plvalue1)*1, text='1', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)


//Draw Fibonacci levels, Resistant levels and labels
if pl and plvalue1 < plvalue2 and sellonly == true
    line.new(phbar1, phvalue1,                              plbar1, plvalue1,                                                   style=line.style_arrow_right, color=color.red, width = 2)
    line.new(phbar1, plvalue1,                              plbar1+ rightbars+extend_fibo, plvalue1,                               style=line.style_solid, color=color.new(color.silver, 60), width = width_fibo)
    line.new(plbar1, plvalue1-(plvalue1-phvalue1)*0.88,    plbar1+ rightbars+extend_fibo, plvalue1-(plvalue1-phvalue1)*0.88,     style=line.style_solid, color=color.new(color.red, 60), width = width_fibo)
    line.new(plbar1, plvalue1-(plvalue1-phvalue1)*0.382,    plbar1+ rightbars+extend_fibo, plvalue1-(plvalue1-phvalue1)*0.382,     style=line.style_solid, color=color.new(#81c784, 60), width = width_fibo)
    line.new(plbar1, plvalue1-(plvalue1-phvalue1)*0.500,    plbar1+ rightbars+extend_fibo, plvalue1-(plvalue1-phvalue1)*0.500,     style=line.style_solid, color=color.new(color.green, 60), width = width_fibo)
    line.new(plbar1, plvalue1-(plvalue1-phvalue1)*0.618,    plbar1+ rightbars+extend_fibo, plvalue1-(plvalue1-phvalue1)*0.618,     style=line.style_solid, color=color.new(#089981, 60), width = width_fibo)
    line.new(plbar1, plvalue1-(plvalue1-phvalue1)*0.786,    plbar1+ rightbars+extend_fibo, plvalue1-(plvalue1-phvalue1)*0.786,     style=line.style_solid, color=color.new(color.aqua, 60), width = width_fibo)
    line.new(phbar1, phvalue1,                              plbar1+ rightbars+extend_fibo, phvalue1,                               style=line.style_solid, color=color.new(color.purple, 60), width = width_fibo)
    
    if close>plvalue1-(plvalue1-phvalue1)*0.382 and alertshort_input == 'Fibo 0.382'
        alert(message = "Fibo 0.382", freq = alert.freq_once_per_bar)
    if close>plvalue1-(plvalue1-phvalue1)*0.500 and alertshort_input == 'Fibo 0.5'
        alert(message = "Fibo 0.5", freq = alert.freq_once_per_bar)
    if close>plvalue1-(plvalue1-phvalue1)*0.618 and alertshort_input == 'Fibo 0.618'
        alert(message = "Fibo 0.618", freq = alert.freq_once_per_bar)
    if close>plvalue1-(plvalue1-phvalue1)*0.786 and alertshort_input == 'Fibo 0.786'
        alert(message = "Fibo 0.786", freq = alert.freq_once_per_bar)
    if close>plvalue2                           and alertshort_input == 'Resistant Zone'
        alert(message = "Resistant Zone",   freq = alert.freq_once_per_bar)
    if close>sup_y - sup_y*0.005                and alertshort_input == 'Trendline'
        alert(message = "Trendline",        freq = alert.freq_once_per_bar)
    var label labelfibo0 = na
    label.delete(labelfibo0)
    labelfibo0 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0,        text='0', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo88 = na
    label.delete(labelfibo88)
    labelfibo88 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0.88,  text='0.88', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo382 = na
    label.delete(labelfibo382)
    labelfibo382 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0.382,  text='0.382', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo5 = na
    label.delete(labelfibo5)
    labelfibo5 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0.5,      text='0.5', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo618 = na
    label.delete(labelfibo618)
    labelfibo618 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0.618,  text='0.618', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo786 = na
    label.delete(labelfibo786)
    labelfibo786 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*0.786,  text='0.786', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)
    var label labelfibo1 = na
    label.delete(labelfibo1)
    labelfibo1 := label.new(x=distance_x, y=plvalue1-(plvalue1-phvalue1)*1,        text='1', color=color.new(#000000, 100), textcolor = color_fibo_label, size=size.small, style=label.style_label_left, xloc=xloc.bar_time, yloc=yloc.price)