All time Fibonacci [ Unlimited ]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Babak Safarzadeh

//@version=5

indicator(title="All time Fibonacci V2", shorttitle="All Time V2 [Unlimited]", overlay=true)

// -------------------------------------
// ----------- Inputs ------------------
// -------------------------------------
I_timeframe = input.timeframe("D",title = "Fibo Timefarame")

bool light_mode = input.bool(true, 'light Mode', group="Fibo Timefarame")

linewidth = input( 1 , title = " All time Level Line width")
linewidth0 = input( 2 , title = " Gold Level Line width")
linewidth1 = input( 1 , title = " Mid Level's Line width ")

l23 = input(0.236)
l35 = input(0.34)
l38 = input(0.382)
l50 = input(0.50)
l61 = input(0.618)
l65 = input(0.66)
l78 = input(0.786)

color i_col23 = (light_mode ?   #787878 : #1f1f1f)
color i_col35 = (light_mode ?   #787878 :#1f1f1f)
color i_col38 = (light_mode ?   #787878 :#1f1f1f)
color i_col50 = (light_mode ?   color.rgb(209, 72, 68,50) :#000000)
color i_colGold61 = (light_mode ?   #787878 :#1f1f1f)
color i_colGold65 = (light_mode ?   #787878 :#1f1f1f)
color i_col78 = (light_mode ?   #787878 :#1f1f1f)

Dtc = input.color(color.rgb(120, 120, 120, 100),"Fill GoldP Downtrend")
Utc = input.color(color.rgb(120, 120, 120, 100),"Fill GoldP Uptrend")
// ---------- Calculs ------------------

hBand = request.security(syminfo.tickerid, I_timeframe, ta.highest(bar_index + 1)) 
lBand = request.security(syminfo.tickerid, I_timeframe, ta.lowest(bar_index + 1)) 

fibl23 = (hBand - lBand) * l23 + lBand
fibl35 = (hBand - lBand) * l35 + lBand
fibl38 = (hBand - lBand) * l38 + lBand
fibl50 = (hBand - lBand) * l50 + lBand
fibl61 = (hBand - lBand) * l61 + lBand
fibl65 = (hBand - lBand) * l65 + lBand
fibl78 = (hBand - lBand) * l78 + lBand

linelow = line.new(bar_index , lBand,  bar_index-1 , lBand,  extend=extend.both , color = color.gray , width = linewidth)
line.delete(linelow[1])
text1 = label.new(bar_index + 50, lBand, text = str.tostring(lBand,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = color.gray)
label.delete(text1[1])
line0 = line.new(bar_index, fibl23,  bar_index-1 , fibl23,  extend=extend.both , color = i_col23, width = linewidth)
line.delete(line0[1])
text2 = label.new(bar_index + 50, fibl23, text = str.tostring(fibl23,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_col23)
label.delete(text2[1])
line1x = line.new(bar_index, fibl35,  bar_index-1 , fibl35,  extend=extend.both,color = i_col35, width = linewidth, style = line.style_dashed)
line.delete(line1x[1])
text3 = label.new(bar_index + 50, fibl35, text = str.tostring(fibl35,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_col35)
label.delete(text3[1])
line1 = line.new(bar_index, fibl38,  bar_index-1 , fibl38,  extend=extend.both,color = color.rgb(209, 72, 68), width = linewidth0)
line.delete(line1[1])
text4 = label.new(bar_index + 50, fibl38, text = str.tostring(fibl38,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_col38)
label.delete(text4[1])
linefill.new(line1x,line1, color = Dtc)
line2 = line.new(bar_index, fibl50 ,  bar_index-1 , fibl50 ,  extend=extend.both,color = color.rgb(56, 189, 96) , width = linewidth0)
line.delete(line2[1])
text5 = label.new(bar_index + 50, fibl50, text = str.tostring(fibl50,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_col50)
label.delete(text5[1])
line3 = line.new(bar_index, fibl61,  bar_index-1 , fibl61,  extend=extend.both, color = color.rgb(209, 72, 68), width = linewidth0)
line.delete(line3[1])
text6 = label.new(bar_index + 50, fibl61, text = str.tostring(fibl61,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_colGold61)
label.delete(text6[1])
line4 = line.new(bar_index, fibl65,  bar_index-1 , fibl65,  extend=extend.both, color = i_colGold65, width = linewidth, style = line.style_dashed)
line.delete(line3[1])
text7 = label.new(bar_index + 50, fibl65, text = str.tostring(fibl65,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_colGold65)
label.delete(text7[1])
linefill.new(line3,line4, color = Utc)
line5 = line.new(bar_index, fibl78,  bar_index-1 , fibl78,  extend=extend.both, color = i_col78, width = linewidth)
line.delete(line3[1])
text8 = label.new(bar_index + 50, fibl78, text = str.tostring(fibl78,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = i_col78)
label.delete(text8[1])
lineh = line.new(bar_index, hBand,  bar_index-1 , hBand,  extend=extend.both , color = color.gray, width = linewidth)
line.delete(lineh[1])
text9 = label.new(bar_index + 50, hBand, text = str.tostring(hBand,"#.##" + " $"), style=label.style_none , size = size.small,  textcolor = color.gray)
label.delete(text9[1])


fibb023 = (fibl23 - lBand) * l23 + lBand 
fibb0231x = (fibl23 - lBand) * l35 + lBand
fibb0231 = (fibl23 - lBand) * l38 + lBand
fibb0232 = (fibl23 - lBand) * l50 + lBand
fibb0233 = (fibl23 - lBand) * l61 + lBand
fibb0234 = (fibl23 - lBand) * l65 + lBand
fibb0235 = (fibl23 - lBand) * l78 + lBand 

line023 = line.new(bar_index, fibb023,  bar_index-1 , fibb023,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line023[1])
line0231x = line.new(bar_index, fibb0231x,  bar_index-1 , fibb0231x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line0231x[1])
line0231 = line.new(bar_index, fibb0231,  bar_index-1 , fibb0231,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line0231[1])
linefill.new(line0231x,line0231, color = Dtc)
line0232 = line.new(bar_index, fibb0232 ,  bar_index-1 , fibb0232 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line0232[1])
line0233 = line.new(bar_index, fibb0233,  bar_index-1 , fibb0233,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line0233[1])
line0234 = line.new(bar_index, fibb0234,  bar_index-1 , fibb0234,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line0234[1])
linefill.new(line0233,line0234, color = Utc)
line0235 = line.new(bar_index, fibb0235,  bar_index-1 , fibb0235,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line0235[1])


fibb2338 = (fibl38 - fibl23) * l23 + fibl23 
fibb23381x = (fibl38 - fibl23) * l35 + fibl23
fibb23381 = (fibl38 - fibl23) * l38 + fibl23
fibb23382 = (fibl38 - fibl23) * l50 + fibl23
fibb23383 = (fibl38 - fibl23) * l61 + fibl23
fibb23384 = (fibl38 - fibl23) * l65 + fibl23
fibb23385 = (fibl38 - fibl23) * l78 + fibl23 

line2338 = line.new(bar_index, fibb2338,  bar_index-1 , fibb2338,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line2338[1])
line23381x = line.new(bar_index, fibb23381x,  bar_index-1 , fibb23381x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line23381x[1])
line23381 = line.new(bar_index, fibb23381,  bar_index-1 , fibb23381,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line23381[1])
linefill.new(line23381x,line23381, color = Dtc)
line23382 = line.new(bar_index, fibb23382 ,  bar_index-1 , fibb23382 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line23382[1])
line23383 = line.new(bar_index, fibb23383,  bar_index-1 , fibb23383,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line23383[1])
line23384 = line.new(bar_index, fibb23384,  bar_index-1 , fibb23384,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line23384[1])
linefill.new(line23383,line23384, color = Utc)
line23385 = line.new(bar_index, fibb23385,  bar_index-1 , fibb23385,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line23385[1])

fibb3850 = (fibl50 - fibl38) * l23 + fibl38 
fibb38501x = (fibl50 - fibl38) * l35 + fibl38
fibb38501 = (fibl50 - fibl38) * l38 + fibl38
fibb38502 = (fibl50 - fibl38) * l50 + fibl38
fibb38503 = (fibl50 - fibl38) * l61 + fibl38
fibb38504 = (fibl50 - fibl38) * l65 + fibl38
fibb38505 = (fibl50 - fibl38) * l78 + fibl38 

line3850 = line.new(bar_index, fibb3850,  bar_index-1 , fibb3850,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line3850[1])
line38501x = line.new(bar_index, fibb38501x,  bar_index-1 , fibb38501x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line38501x[1])
line38501 = line.new(bar_index, fibb38501,  bar_index-1 , fibb38501,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line38501[1])
linefill.new(line38501x,line38501, color = Dtc)
line38502 = line.new(bar_index, fibb38502 ,  bar_index-1 , fibb38502 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line38502[1])
line38503 = line.new(bar_index, fibb38503,  bar_index-1 , fibb38503,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line38503[1])
line38504 = line.new(bar_index, fibb38504,  bar_index-1 , fibb38504,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line38504[1])
linefill.new(line38503,line38504, color = Utc)
line38505 = line.new(bar_index, fibb38505,  bar_index-1 , fibb38505,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line38505[1])

fibb5061 = (fibl61 - fibl50) * l23 + fibl50 
fibb50611x = (fibl61 - fibl50) * l35 + fibl50
fibb50611 = (fibl61 - fibl50) * l38 + fibl50
fibb50612 = (fibl61 - fibl50) * l50 + fibl50
fibb50613 = (fibl61 - fibl50) * l61 + fibl50
fibb50614 = (fibl61 - fibl50) * l65 + fibl50
fibb50615 = (fibl61 - fibl50) * l78 + fibl50 

line5061 = line.new(bar_index, fibb5061,  bar_index-1 , fibb5061,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line5061[1])
line50611x = line.new(bar_index, fibb50611x,  bar_index-1 , fibb50611x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line50611x[1])
line50611 = line.new(bar_index, fibb50611,  bar_index-1 , fibb50611,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line50611[1])
linefill.new(line50611x,line50611, color = Dtc)
line50612 = line.new(bar_index, fibb50612 ,  bar_index-1 , fibb50612 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line50612[1])
line50613 = line.new(bar_index, fibb50613,  bar_index-1 , fibb50613,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line50613[1])
line50614 = line.new(bar_index, fibb50614,  bar_index-1 , fibb50614,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line50614[1])
linefill.new(line50613,line50614, color = Utc)
line50615 = line.new(bar_index, fibb50615,  bar_index-1 , fibb50615,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line50615[1])

fibb6178 = (fibl78 - fibl61) * l23 + fibl61 
fibb61781x = (fibl78 - fibl61) * l35 + fibl61
fibb61781 = (fibl78 - fibl61) * l38 + fibl61
fibb61782 = (fibl78 - fibl61) * l50 + fibl61
fibb61783 = (fibl78 - fibl61) * l61 + fibl61
fibb61784 = (fibl78 - fibl61) * l65 + fibl61
fibb61785 = (fibl78 - fibl61) * l78 + fibl61 

line6178 = line.new(bar_index, fibb6178,  bar_index-1 , fibb6178,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line6178[1])
line61781x = line.new(bar_index, fibb61781x,  bar_index-1 , fibb61781x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line61781x[1])
line61781 = line.new(bar_index, fibb61781,  bar_index-1 , fibb61781,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line61781[1])
linefill.new(line61781x,line61781, color = Dtc)
line61782 = line.new(bar_index, fibb61782 ,  bar_index-1 , fibb61782 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line61782[1])
line61783 = line.new(bar_index, fibb61783,  bar_index-1 , fibb61783,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line61783[1])
line61784 = line.new(bar_index, fibb61784,  bar_index-1 , fibb61784,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line61784[1])
linefill.new(line61783,line61784, color = Utc)
line61785 = line.new(bar_index, fibb61785,  bar_index-1 , fibb61785,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line61785[1])


fibb78h = (hBand - fibl78) * l23 + fibl78 
fibb78h1x = (hBand - fibl78) * l35 + fibl78
fibb78h1 = (hBand - fibl78) * l38 + fibl78
fibb78h2 = (hBand - fibl78) * l50 + fibl78
fibb78h3 = (hBand - fibl78) * l61 + fibl78
fibb78h4 = (hBand - fibl78) * l65 + fibl78
fibb78h5 = (hBand - fibl78) * l78 + fibl78 

line78h = line.new(bar_index, fibb78h,  bar_index-1 , fibb78h,  extend=extend.both , color = i_col23 , width = linewidth1, style = line.style_solid)
line.delete(line78h[1])
line78h1x = line.new(bar_index, fibb78h1x,  bar_index-1 , fibb78h1x,  extend=extend.both,color = i_col35, width = linewidth1, style = line.style_dashed)
line.delete(line78h1x[1])
line78h1 = line.new(bar_index, fibb78h1,  bar_index-1 , fibb78h1,  extend=extend.both,color = i_col38, width = linewidth1, style = line.style_solid)
line.delete(line78h1[1])
linefill.new(line78h1x ,line78h1, color = Dtc)
line78h2 = line.new(bar_index, fibb78h2 ,  bar_index-1 , fibb78h2 ,  extend=extend.both,color = i_col50 , width = linewidth1, style = line.style_solid)
line.delete(line78h2[1])
line78h3 = line.new(bar_index, fibb78h3,  bar_index-1 , fibb78h3,  extend=extend.both, color = i_colGold61, width = linewidth1, style = line.style_solid )
line.delete(line78h3[1])
line78h4 = line.new(bar_index, fibb78h4,  bar_index-1 , fibb78h4,  extend=extend.both, color = i_colGold65, width = linewidth1, style = line.style_dashed)
line.delete(line78h4[1])
linefill.new(line78h3,line78h4, color =Utc)
line78h5 = line.new(bar_index, fibb78h5,  bar_index-1 , fibb78h5,  extend=extend.both, color = i_col78, width = linewidth1, style = line.style_solid)
line.delete(line78h5[1])

//////// base line

basePeriods = input.int(31, minval=1, title="Base Line Length")
donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
baseLine = donchian(basePeriods)
plot(baseLine , color =  close > baseLine ? color.green : color.red, title="Base Line")


plotRibbon = input(title="Plot Ribbon", defval=false,group = "MA Settings")
src = input(close, title="Source",group = "MA Settings")
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
		"ZEMA" => ema1 = ta.ema(source,length) , ema2 = ta.ema(ema1,length) , dif = ema1 - ema2 , zlag = ema1 + dif
		"TEMA" => tema1 = ta.ema(source,length) , tema2 = ta.ema(tema1,length) , tema3 = ta.ema(tema2,length) ,temap = 3 * (tema1 - tema2) + tema3
		"HMA"  => ta.wma(2*ta.wma(source, length/2)-ta.wma(source, length), math.floor(math.sqrt(length)))
		"TRAMA" => ama = 0. , hh = math.max(math.sign(ta.change(ta.highest(length))),0) , ll = math.max(math.sign(ta.change(ta.lowest(length))*-1),0) ,tc = math.pow(ta.sma(hh or ll ? 1 : 0,length),2) , ama := nz(ama[1]+tc*(src-ama[1]),src)
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA","ZEMA","TEMA","HMA" , "TRAMA"],group = "MA Settings")

len2 = input.int(20, minval=1, title="Length3",group = "MA Settings")
out2 = ma(src, len2,typeMA)
EM3 = plot(out2  , color= close > out2 ? color.green : color.red, title="MA1")

len3 = input.int(99, minval=1, title="Length3",group = "MA Settings")
out3 = ma(src, len3,typeMA)
EM4 = plot(out3 , color= close > out3 ? color.green : color.red, title="MA2")



stop_timeframe = input.timeframe("D",title = "ATR Stoploss")
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"] , group= "ATR Stoploss")
ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)
//Calculat_atr = ma_function(ta.tr(true), length)
texttoatrs = request.security(syminfo.tickerid, stop_timeframe, close - ma_function(ta.tr(true), 14))
texttoatrt = request.security(syminfo.tickerid, stop_timeframe,close + ma_function(ta.tr(true), 14))

//textxsh = label.new(bar_index + 20, close, text = str.tostring(texttoatrs,"#.##") + " Long Stoploss" , style=label.style_none , size = size.small,  textcolor = color.green )
//label.delete(textxsh[1])
//textxL = label.new(bar_index + 20, open, text = str.tostring(texttoatrt,"#.##") + " Short Stoploss" , style=label.style_none , size = size.small,  textcolor = color.red )
//label.delete(textxL[1])

///ribbon
length1 = 20
length2 = 25
length3 = 30
length4 = 35
length5 = 40
length6 = 45
length7 = 50
length8 = 55

out1x = ma(src, length1 ,typeMA)
out2x = ma(src, length2 ,typeMA)
out3x = ma(src, length3 ,typeMA)
out4x = ma(src, length4 ,typeMA)
out5x = ma(src, length5 ,typeMA)
out6x = ma(src, length6 ,typeMA)
out7x = ma(src, length7 ,typeMA)
out8x = ma(src, length8 ,typeMA)

l1 = plot(plotRibbon ? out1x : na, title="MA-1", color=#f5eb5d, linewidth=2)
l2 =plot(plotRibbon ? out2x : na, title="MA-2", color=#f5b771, linewidth=2)
l3 =plot(plotRibbon ? out3x : na, title="MA-3", color=#f5b056, linewidth=2)
l4 =plot(plotRibbon ? out4x : na, title="MA-4", color=#f57b4e, linewidth=2)
l5 =plot(plotRibbon ? out5x : na, title="MA-5", color=#f56d58, linewidth=2)
l6 =plot(plotRibbon ? out6x : na, title="MA-6", color=#f57d51, linewidth=2)
l7 =plot(plotRibbon ? out7x : na, title="MA-7", color=#f55151, linewidth=2)
l8 =plot(plotRibbon ? out8x : na, title="MA-8", color= #aa2707, linewidth=2)

//rsivalue
rsios = ta.rsi(close,14)

color = if rsios > 70
    #eb383b
else if rsios < 30
    #137077
else if rsios <70 and rsios > 30
    color.black
// Table output
if barstate.islast
    table_row = table.new(position = position.bottom_right, columns = 3, rows = 4)
    table.cell(table_row, 1,1, "Stop loss")
    table.cell(table_row, 1,2 , str.tostring(texttoatrs,"#.##") + "$ Long Stoploss", bgcolor =  color.rgb(19,112,119) , text_color = color.white )
	table.cell(table_row, 1,3 , str.tostring(texttoatrt,"#.##") + "$  Short Stoploss", bgcolor =  #eb383b , text_color = color.white)
	table.cell(table_row, 2,1, "RSI & MA ")
	table.cell(table_row, 2,2 , str.tostring(rsios,"#.##"), bgcolor = color, text_color = color.white )
	table.cell(table_row, 2,3 , close > out3 ? "▲ MA":"▼ MA", bgcolor = close < out3 ? color.rgb(235,56,59) : color.rgb(19,112,119) , text_color = color.white )



