Auto Fibonacci Levels [MisterMoTA]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @author  = MisterMoTA
// © MisterMoTA
//@version=5
indicator('Auto Fibonacci Levels [MisterMoTA]', 'FIBEMO', overlay = true)

// Create an input that specifies on which time and date
lineDate = input.time(timestamp("14 November 2023 16:30"), title="Lookback start date ", group = "Date Range", confirm = true)
lbackLength = input.int(defval=16, maxval = 10000, minval = 1, title="Number of candles to look back", group = "Date Range")
extendLeft = input.int(defval=75, maxval = 10000, minval = 1, title="Distance from current bar to fib labels", group = "FIB LINES AND LABELS")
plotBullishExpansion = input.bool(true, title="Show Bullish Expansion Levels", group = "FIB LINES AND LABELS")
plotBearishExpansion = input.bool(true, title="Show Bearish Expansion Levels", group = "FIB LINES AND LABELS")
plotBullishRetracement = input.bool(false, title="Show Bullish Retracement Levels", group = "FIB LINES AND LABELS")
plotBearishRetracement = input.bool(false, title="Show Bearish RetracementLevels", group = "FIB LINES AND LABELS")
hideLabels =input.bool(false, title="Hide All Labels", group = "FIB LINES AND LABELS")
//variable for time looking back stop
lineStop = lineDate -time[lbackLength]

//=============================================================================
var g_fibs = "Fibonacci Levels"
// Get Fibonacci level user inputs
//------------------------------------------------------------------------------
show_0_236 = input(true, "", inline = "Level0", group=g_fibs)
value_0_236 = input(0.236, "", inline = "Level0", group=g_fibs)
color_0_236 = input(#f44336, "", inline = "Level0", group=g_fibs)
//------------------------------------------------------------------------------
show_0_382 = input(true, "", inline = "Level1", group=g_fibs)
value_0_382 = input(0.382, "", inline = "Level1", group=g_fibs)
color_0_382 = input(#527fd9, "", inline = "Level1", group=g_fibs)
//------------------------------------------------------------------------------
show_0_5 = input(true, "", inline = "Level1", group=g_fibs)
value_0_5 = input(0.5, "", inline = "Level1", group=g_fibs)
color_0_5 = input(#4caf50, "", inline = "Level1", group=g_fibs)
//------------------------------------------------------------------------------
show_0_618 = input(true, "", inline = "Level2", group=g_fibs)
value_0_618 = input(0.618, "", inline = "Level2", group=g_fibs)
color_0_618 = input(#009688, "", inline = "Level2", group=g_fibs)
//------------------------------------------------------------------------------
show_0_65 = input(false, "", inline = "Level2", group=g_fibs)
value_0_65 = input(0.65, "", inline = "Level2", group=g_fibs)
color_0_65 = input(#d67149, "", inline = "Level2", group=g_fibs)
//------------------------------------------------------------------------------
show_0_786 = input(true, "", inline = "Level3", group=g_fibs)
value_0_786 = input(0.786, "", inline = "Level3", group=g_fibs)
color_0_786 = input(#642abd, "", inline = "Level3", group=g_fibs)
//------------------------------------------------------------------------------
show_1 = input(true, "", inline = "Level3", group=g_fibs)
value_1 = input(1, "", inline = "Level3", group=g_fibs)
color_1 = input(#787b86, "", inline = "Level3", group=g_fibs)
//------------------------------------------------------------------------------
show_1_272 = input(false, "", inline = "Level4", group=g_fibs)
value_1_272 = input(1.272, "", inline = "Level4", group=g_fibs)
color_1_272 = input(#81c784, "", inline = "Level4", group=g_fibs)
//------------------------------------------------------------------------------
show_1_414 = input(false, "", inline = "Level4", group=g_fibs)
value_1_414 = input(1.414, "", inline = "Level4", group=g_fibs)
color_1_414 = input(#f44336, "", inline = "Level4", group=g_fibs)
//------------------------------------------------------------------------------
show_1_618 = input(true, "", inline = "Level5", group=g_fibs)
value_1_618 = input(1.618, "", inline = "Level5", group=g_fibs)
color_1_618 = input(#298377, "", inline = "Level5", group=g_fibs)
//------------------------------------------------------------------------------
show_1_65 = input(false, "", inline = "Level5", group=g_fibs)
value_1_65 = input(1.65, "", inline = "Level5", group=g_fibs)
color_1_65 = input(#e39562, "", inline = "Level5", group=g_fibs)
//------------------------------------------------------------------------------
show_2_618 = input(true, "", inline = "Level6", group=g_fibs)
value_2_618 = input(2.618, "", inline = "Level6", group=g_fibs)
color_2_618 = input(#34700a, "", inline = "Level6", group=g_fibs)
//------------------------------------------------------------------------------
show_2_65 = input(false, "", inline = "Level6", group=g_fibs)
value_2_65 = input(2.65, "", inline = "Level6", group=g_fibs)
color_2_65 = input(#f44336, "", inline = "Level6", group=g_fibs)
//------------------------------------------------------------------------------
show_3_618 = input(true, "", inline = "Level7", group=g_fibs)
value_3_618 = input(3.618, "", inline = "Level7", group=g_fibs)
color_3_618 = input(#8cad3f, "", inline = "Level7", group=g_fibs)
//------------------------------------------------------------------------------
show_3_65 = input(false, "", inline = "Level7", group=g_fibs)
value_3_65 = input(3.65, "", inline = "Level7", group=g_fibs)
color_3_65 = input(#9c27b0, "", inline = "Level7", group=g_fibs)
//------------------------------------------------------------------------------
show_4_236 = input(false, "", inline = "Level8", group=g_fibs)
value_4_236 = input(4.236, "", inline = "Level8", group=g_fibs)
color_4_236 = input(#e91e63, "", inline = "Level8", group=g_fibs)
//------------------------------------------------------------------------------
show_4_618 = input(true, "", inline = "Level8", group=g_fibs)
value_4_618 = input(4.618, "", inline = "Level8", group=g_fibs)
color_4_618 = input(#87114f, "", inline = "Level8", group=g_fibs)
//------------------------------------------------------------------------------

///Retacement levels
var r_fibs = "Fibonacci Retracement Levels"
// Get Fibonacci level user inputs
show_0_2360 = input(true, "", inline = "Level0", group=r_fibs)
value_0_2360 = input(0.236, "", inline = "Level0", group=r_fibs)
color_0_2360 = input(#493fcc, "", inline = "Level0", group=r_fibs)
//------------------------------------------------------------------------------
show_0_3820 = input(true, "", inline = "Level1", group=r_fibs)
value_0_3820 = input(0.382, "", inline = "Level1", group=r_fibs)
color_0_3820 = input(#6b188c, "", inline = "Level1", group=r_fibs)
//------------------------------------------------------------------------------
show_0_50 = input(true, "", inline = "Level1", group=r_fibs)
value_0_50 = input(0.5, "", inline = "Level1", group=r_fibs)
color_0_50 = input(#299e4c, "", inline = "Level1", group=r_fibs)
//------------------------------------------------------------------------------
show_0_6180 = input(true, "", inline = "Level2", group=r_fibs)
value_0_6180 = input(0.618, "", inline = "Level2", group=r_fibs)
color_0_6180 = input(#009688, "", inline = "Level2", group=r_fibs)
//------------------------------------------------------------------------------
show_0_650 = input(false, "", inline = "Level2", group=r_fibs)
value_0_650 = input(0.65, "", inline = "Level2", group=r_fibs)
color_0_650 = input(#d67149, "", inline = "Level2", group=r_fibs)
//------------------------------------------------------------------------------
show_0_7860 = input(true, "", inline = "Level3", group=r_fibs)
value_0_7860 = input(0.786, "", inline = "Level3", group=r_fibs)
color_0_7860 = input(#87114f, "", inline = "Level3", group=r_fibs)


// Draw the vertical line at the input's specified time and date
if barstate.islastconfirmedhistory
    line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both, 
         color=color.orange, width=2, xloc=xloc.bar_time)
    line.new(x1=lineStop, y1=high, x2=lineStop, y2=low, extend=extend.both, 
         color=color.rgb(18, 187, 244), width=2, xloc=xloc.bar_time)


var l0 = label (na)
var l20 = label (na) 
var myBox = box (na) 


//variable for getting high and low of the defined timeframe interval
var float Val_high = na
var float Val_low = na

//variables for highest and lowest values in the interval of time choosed by user
var topLine = line.new(x1=na, y1=na, x2=na, y2=na, extend =extend.right,  color=color.rgb(146, 254, 149, 15), style=line.style_dotted, width=2)
var bottomLine = line.new(x1=na, y1=na, x2=na, y2=na, extend =extend.right, color=color.rgb(255, 82, 82), style=line.style_dotted, width=2)

//decimals numbers default is 2, users can use up to 8 decimals to display fibonacci levels
decimals = input.int(2, "Decimals for fibonacci lines", maxval = 8)
var string decs =na
if decimals == 0
    decs := "#"
else if decimals == 1
    decs :="#.#"
else if decimals == 2
    decs :="#.##"
else if decimals == 3
    decs :="#.###"
else if decimals == 4
    decs :="#.####"
else if decimals == 5
    decs :="#.#####"
else if decimals == 6
    decs :="#.######"
else if decimals == 7
    decs :="#.#######"
else if decimals == 8
    decs :="#.########"

// Draw fibonacci levels as lines and return the line object ID
draw_fib_line(price, col) =>
    var id = line.new(x1=na, y1=na, x2=na, y2=na, color=col, width=1, style=line.style_solid, extend=extend.left)
    line.set_xy1(id, bar_index-200, price)
    line.set_xy2(id, bar_index + extendLeft , price)  
	id  

processLeveBearish(show, value, colorL) =>
    if show
	    fibPrice = Val_low * math.pow(Val_low/Val_high, value)
		lineId = draw_fib_line(fibPrice, colorL)
        // if close < fibPrice 
        //     line.set_style(lineId, line.style_dashed)
        lineId

drawlabelsbearish(show, value, colorL) =>
    if show and hideLabels == false
	    fibPrice = Val_low * math.pow(Val_low/Val_high, value)
		labelId =label.new(bar_index+extendLeft, fibPrice, str.tostring(value) + "  Bearish Expansion : " +  str.tostring(fibPrice, decs), color=#00000000, textcolor=colorL, style=label.style_label_left, size=size.normal)
        label.delete(labelId[1])
        labelId


processLeveBearishRetracement(show, value, colorL) =>
    if show
	    fibPrice = Val_low *  math.pow(Val_high/Val_low, value)
		lineId = draw_fib_line(fibPrice, colorL)
        // if close < fibPrice 
        //     line.set_style(lineId, line.style_dashed)
        lineId


drawlabelsbearishRetracement(show, value, colorL) =>
    if show and hideLabels == false
	    fibPrice = Val_low *  math.pow(Val_high/Val_low, value)
		labelId =label.new(bar_index+extendLeft, fibPrice, str.tostring(value) + "  Bearish Retracement : " +  str.tostring(fibPrice, decs), color=#00000000, textcolor=colorL, style=label.style_label_left, size=size.normal)
        label.delete(labelId[1])
        labelId

processLeveBbullish(show, value, colorL) =>
    if show
	    fibPrice = Val_high *  math.pow(Val_high/Val_low, value)
		lineId = draw_fib_line(fibPrice, colorL)
        // if close > fibPrice 
        //     line.set_style(lineId, line.style_dashed)
        lineId

drawlabelsBullish(show, value, colorL) =>
    if show and hideLabels == false
	    fibPrice = Val_high * math.pow(Val_high/Val_low, value)
		labelId =label.new(bar_index+extendLeft, fibPrice, str.tostring(value) + "  Bullish Expansion : " +  str.tostring(fibPrice, decs), color=#00000000, textcolor=colorL, style=label.style_label_left, size=size.normal)
        label.delete(labelId[1])
        labelId

processLeveBbullishRetracement(show, value, colorL) =>
    if show
	    fibPrice = Val_high *  math.pow(Val_low/Val_high, value)
		lineId = draw_fib_line(fibPrice, colorL)
        // if close > fibPrice 
        //     line.set_style(lineId, line.style_dashed)
        lineId

drawlabelsBullishRetracement(show, value, colorL) =>
    if show and hideLabels == false
	    fibPrice = Val_high *  math.pow(Val_low/Val_high, value)
		labelId =label.new(bar_index+extendLeft, fibPrice, str.tostring(value) + "  Bullish Retracement : " +  str.tostring(fibPrice, decs), color=#00000000, textcolor=colorL, style=label.style_label_left, size=size.normal)
        label.delete(labelId[1])
        labelId




taHi = ta.highest(high, lbackLength)
taLow =ta.lowest(low, lbackLength)


//check for user time imput and set the values for highest and lowest lines
if time <= lineDate
    Val_high := taHi
    Val_low :=  taLow

    line.set_xy1(id=topLine, x=bar_index[1]- lbackLength, y=Val_high)
    line.set_xy2(id=topLine, x=bar_index+5, y=Val_high)

    line.set_xy1(id=bottomLine, x=bar_index[1]- lbackLength, y=Val_low)
    line.set_xy2(id=bottomLine, x=bar_index+5, y=Val_low)

    l0 := label.new(bar_index[1]- lbackLength, Val_high, str.tostring(Val_high, decs) + ' is Highest of ' + str.tostring(lbackLength) + ' bars from ' + str.format_time(lineDate, "d-M-yyyy HH:mm", "UTC+3"),  color=color.rgb(25, 85, 34), textcolor=color.white, style=label.style_label_lower_right, size=size.normal)
    l20 := label.new(bar_index[1]- lbackLength, Val_low, str.tostring(Val_low, decs) + ' is Lowest of '  + str.tostring(lbackLength) + ' bars from ' + str.format_time(lineDate, "d-M-yyyy HH:mm", "UTC+3"), color=color.rgb(255, 0, 0, 25), textcolor=color.white,  style=label.style_label_upper_right, size=size.normal)

    label.delete(l0[1])
    label.delete(l20[1])
    
    myBox := box.new(bar_index-lbackLength, Val_high, bar_index, Val_low, border_color=color.rgb(155, 39, 176, 77), border_width=1,bgcolor=na)
    box.set_bgcolor(id=myBox, color=color.rgb(135, 75, 233, 77))    
    box.delete(myBox[1])


//processing the bullish expansion levels 
if (plotBullishExpansion == true)
    lineId1  =  processLeveBbullish(show_0_236, value_0_236, color_0_236)
    lineId2  =  processLeveBbullish(show_0_382, value_0_382, color_0_382)
    lineId3  =  processLeveBbullish(show_0_5, value_0_5, color_0_5)
    lineId4  =  processLeveBbullish(show_0_618, value_0_618, color_0_618)
    lineId5  =  processLeveBbullish(show_0_65, value_0_65, color_0_65)
    lineId6 =  processLeveBbullish(show_0_786, value_0_786, color_0_786)
    lineId7 =  processLeveBbullish(show_1, value_1, color_1)
    lineId8 =  processLeveBbullish(show_1_272, value_1_272, color_1_272)
    lineId9 =  processLeveBbullish(show_1_414, value_1_414, color_1_414)
    lineId10 =  processLeveBbullish(show_1_618, value_1_618, color_1_618)
    lineId11 =  processLeveBbullish(show_1_65, value_1_65, color_1_65 )
    lineId12 =  processLeveBbullish(show_2_618, value_2_618, color_2_618 )
    lineId13 =  processLeveBbullish(show_2_65, value_2_65, color_2_65 )
    lineId14 =  processLeveBbullish(show_3_618, value_3_618, color_3_618 )
    lineId15 =  processLeveBbullish(show_3_65, value_3_65, color_3_65 )
    lineId16 =  processLeveBbullish(show_4_236, value_4_236, color_4_236)
    lineId17 =  processLeveBbullish(show_4_618, value_4_618, color_4_618 )


//drawing the bullish expansion levels 
    labelId1  =  drawlabelsBullish(show_0_236, value_0_236, color_0_236)
    labelId2  =  drawlabelsBullish(show_0_382, value_0_382, color_0_382)
    labelId3  =  drawlabelsBullish(show_0_5, value_0_5, color_0_5)
    labelId4  =  drawlabelsBullish(show_0_618, value_0_618, color_0_618)
    labelId5  =  drawlabelsBullish(show_0_65, value_0_65, color_0_65)
    labelId6 =  drawlabelsBullish(show_0_786, value_0_786, color_0_786)
    labelId7 =  drawlabelsBullish(show_1, value_1, color_1)
    labelId8 =  drawlabelsBullish(show_1_272, value_1_272, color_1_272)
    labelId9 =  drawlabelsBullish(show_1_414, value_1_414, color_1_414)
    labelId10 =  drawlabelsBullish(show_1_618, value_1_618, color_1_618)
    labelId11 =  drawlabelsBullish(show_1_65, value_1_65, color_1_65 )
    labelId12 =  drawlabelsBullish(show_2_618, value_2_618, color_2_618 )
    labelId13 =  drawlabelsBullish(show_2_65, value_2_65, color_2_65 )
    labelId14 =  drawlabelsBullish(show_3_618, value_3_618, color_3_618 )
    labelId15 =  drawlabelsBullish(show_3_65, value_3_65, color_3_65 )
    labelId16 =  drawlabelsBullish(show_4_236, value_4_236, color_4_236)
    labelId17 =  drawlabelsBullish(show_4_618, value_4_618, color_4_618 )

//processing the bullish retracement levels 
if (plotBullishRetracement == true)
//negative fibs for retracement
    lineId101  =  processLeveBbullishRetracement(show_0_2360, value_0_2360, color_0_2360)
    lineId201  =  processLeveBbullishRetracement(show_0_3820, value_0_3820, color_0_3820)
    lineId301  =  processLeveBbullishRetracement(show_0_50, value_0_50, color_0_50)
    lineId401  =  processLeveBbullishRetracement(show_0_6180, value_0_6180, color_0_6180)
    lineId501  =  processLeveBbullishRetracement(show_0_650, value_0_650, color_0_650)
    lineId601 =  processLeveBbullishRetracement(show_0_7860, value_0_7860, color_0_7860)

// draw retracement levels 
    labelId101  =  drawlabelsBullishRetracement(show_0_2360, value_0_2360, color_0_2360)
    labelId201  =  drawlabelsBullishRetracement(show_0_3820, value_0_3820, color_0_3820)
    labelId301  =  drawlabelsBullishRetracement(show_0_50, value_0_50, color_0_50)
    labelId401  =  drawlabelsBullishRetracement(show_0_6180, value_0_6180, color_0_6180)
    labelId501  =  drawlabelsBullishRetracement(show_0_650, value_0_650, color_0_650)
    labelId601 =  drawlabelsBullishRetracement(show_0_7860, value_0_7860, color_0_7860)

//processing the bearish expansion levels        
if (plotBearishExpansion == true)
    lineId1  =  processLeveBearish(show_0_236, value_0_236, color_0_236)
    lineId2  =  processLeveBearish(show_0_382, value_0_382, color_0_382)
    lineId3  =  processLeveBearish(show_0_5, value_0_5, color_0_5)
    lineId4  =  processLeveBearish(show_0_618, value_0_618, color_0_618)
    lineId5  =  processLeveBearish(show_0_65, value_0_65, color_0_65)
    lineId6 =  processLeveBearish(show_0_786, value_0_786, color_0_786)
    lineId7 =  processLeveBearish(show_1, value_1, color_1)
    lineId8 =  processLeveBearish(show_1_272, value_1_272, color_1_272)
    lineId9 =  processLeveBearish(show_1_414, value_1_414, color_1_414)
    lineId10 =  processLeveBearish(show_1_618, value_1_618, color_1_618)
    lineId11 =  processLeveBearish(show_1_65, value_1_65, color_1_65 )
    lineId12 =  processLeveBearish(show_2_618, value_2_618, color_2_618 )
    lineId13 =  processLeveBearish(show_2_65, value_2_65, color_2_65 )
    lineId14 =  processLeveBearish(show_3_618, value_3_618, color_3_618 )
    lineId15 =  processLeveBearish(show_3_65, value_3_65, color_3_65 )
    lineId16 =  processLeveBearish(show_4_236, value_4_236, color_4_236)
    lineId17 =  processLeveBearish(show_4_618, value_4_618, color_4_618 )

    labelId01  =  drawlabelsbearish(show_0_236, value_0_236, color_0_236)
    labelId02  =  drawlabelsbearish(show_0_382, value_0_382, color_0_382)
    labelId03  =  drawlabelsbearish(show_0_5, value_0_5, color_0_5)
    labelId04  =  drawlabelsbearish(show_0_618, value_0_618, color_0_618)
    labelId05  =  drawlabelsbearish(show_0_65, value_0_65, color_0_65)
    labelId06 =  drawlabelsbearish(show_0_786, value_0_786, color_0_786)
    labelId07 =  drawlabelsbearish(show_1, value_1, color_1)
    labelId08 =  drawlabelsbearish(show_1_272, value_1_272, color_1_272)
    labelId09 =  drawlabelsbearish(show_1_414, value_1_414, color_1_414)
    labelId010 =  drawlabelsbearish(show_1_618, value_1_618, color_1_618)
    labelId101 =  drawlabelsbearish(show_1_65, value_1_65, color_1_65 )
    labelId012 =  drawlabelsbearish(show_2_618, value_2_618, color_2_618 )
    labelId013 =  drawlabelsbearish(show_2_65, value_2_65, color_2_65 )
    labelId014 =  drawlabelsbearish(show_3_618, value_3_618, color_3_618 )
    labelId015 =  drawlabelsbearish(show_3_65, value_3_65, color_3_65 )
    labelId016 =  drawlabelsbearish(show_4_236, value_4_236, color_4_236)
    labelId017 =  drawlabelsbearish(show_4_618, value_4_618, color_4_618 )

//processing the bearish retracement levels 
if (plotBearishRetracement == true)
//negative fibs for retracement
    lineId10122  =  processLeveBearishRetracement(show_0_2360, value_0_2360, color_0_2360)
    lineId20122  =  processLeveBearishRetracement(show_0_3820, value_0_3820, color_0_3820)
    lineId30122  =  processLeveBearishRetracement(show_0_50, value_0_50, color_0_50)
    lineId40122  =  processLeveBearishRetracement(show_0_6180, value_0_6180, color_0_6180)
    lineId50122  =  processLeveBearishRetracement(show_0_650, value_0_650, color_0_650)
    lineId60122 =  processLeveBearishRetracement(show_0_7860, value_0_7860, color_0_7860)

// draw bearish retracement levels 
    labelId10122  =  drawlabelsbearishRetracement(show_0_2360, value_0_2360, color_0_2360)
    labelId20122  =  drawlabelsbearishRetracement(show_0_3820, value_0_3820, color_0_3820)
    labelId30122  =  drawlabelsbearishRetracement(show_0_50, value_0_50, color_0_50)
    labelId40122  =  drawlabelsbearishRetracement(show_0_6180, value_0_6180, color_0_6180)
    labelId50122  =  drawlabelsbearishRetracement(show_0_650, value_0_650, color_0_650)
    labelId60122  =  drawlabelsbearishRetracement(show_0_7860, value_0_7860, color_0_7860)
