Intra-variety Timeframe Floating Fibonacci Levels [Loxx]
// This source code is subject to the terms of the Mozilla Public license 2.0 at https://mozilla.org/MPl/2.0/
// © loxx

//@version=5
indicator("Intra-variety Timeframe Floating Fibonacci Levels [Loxx]", shorttitle = "IVTFFL [Loxx]", overlay = true, max_bars_back = 2000)

greencolor = #2DD204
redcolor = #D2042D 

darkGreenColor =  #1B7E02 
darkRedColor = #93021F

f_tickFormat() =>
    _s = str.tostring(syminfo.mintick)
    _s := str.replace_all(_s, '25', '00')
    _s := str.replace_all(_s, '5', '0')
    _s := str.replace_all(_s, '1', '0')
    _s
    
tfInMinutes(simple string tf = "") =>
    float chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)
    
float chartTFInMinutes = tfInMinutes()

sday  = "Intraday"
sweek = "Intraweek"
smonth  = "Intramonth"

tfstyle = input.string(sday, "Higher Timeframe Style", options = [sday, sweek, smonth])

level1 = input.float(0.236, "Fibonacci Level 1", minval = 0.0, step = 0.01)
level2 = input.float(0.382, "Fibonacci Level 2", minval = 0.0, step = 0.01)
level3 = input.float(0.5, "Fibonacci Level 3", minval = 0.0, step = 0.01)
level4 = input.float(0.618, "Fibonacci Level 4", minval = 0.0, step = 0.01)
level5 = input.float(0.764, "Fibonacci Level 5", minval = 0.0, step = 0.01)

if chartTFInMinutes > 60 and tfstyle == sday
    runtime.error("Error: Invalid timefame for Intradaily. You must select a timeframe less than or equal to 1 hour.")
    
if chartTFInMinutes > 120 and tfstyle == sweek
    runtime.error("Error: Invalid timefame for Intraweekly. You must select a timeframe less than or equal to 2 hours.")
    
if chartTFInMinutes > 240 and tfstyle == smonth
    runtime.error("Error: Invalid timefame for Intramonthly. You must select a timeframe less than or equal to 4 hours.")

tf_in = tfstyle == sday ? "D" : tfstyle == sweek ? "W" : "M"

daily_timestamp = request.security(syminfo.tickerid, tf_in, last_bar_time, barmerge.gaps_off, barmerge.lookahead_on)
timeout = ta.barssince(time("") == daily_timestamp)
timeout0 = timeout
timeout := nz(timeout)
timeout := timeout < 1 ? 1 : timeout

H = ta.highest(high, timeout)
L = ta.lowest(low, timeout)
W = H-L

Fib1 =H-(W*level1)
Fib2 =H-(W*level2)
Fib3 =H-(W*level3)
Fib4 =H-(W*level4)
Fib5 =H-(W*level5)

rest = timeout0 > 0

plot(rest ? Fib1 : na, "Fibonacci Level 1", color = greencolor, linewidth = 1, style = plot.style_circles)
plot(rest ? Fib2 : na, "Fibonacci Level 2", color = greencolor, linewidth = 1, style = plot.style_circles)

mid = plot(rest ? Fib3 : na, "Fibonacci Level 3", color = color.white, linewidth = 1, style = plot.style_circles)

plot(rest ? Fib4 : na, "Fibonacci Level 4", color = redcolor, linewidth = 1, style = plot.style_circles)
plot(rest ? Fib5 : na, "Fibonacci Level 5", color = redcolor, linewidth = 1, style = plot.style_circles)

hplot = plot(rest ? H : na,"Intraday High", color = greencolor, linewidth = 3, style = plot.style_stepline)
lplot = plot(rest ? L : na,"Intraday Low", color = redcolor, linewidth = 3, style = plot.style_stepline)

fill(mid, hplot, color.new(greencolor, 95))
fill(mid, lplot, color.new(redcolor, 95))

l1Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = Fib1, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= str.tostring(level1 * 100, "#.#") + "% " + str.tostring(Fib1, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  



l2Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = Fib2, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= str.tostring(level2 * 100, "#.#") + "% " + str.tostring(Fib2, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

l3Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = Fib3, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= str.tostring(level3* 100, "#.#") + "% " + str.tostring(Fib1, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

l4Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = Fib4, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= str.tostring(level4 * 100, "#.#") + "% " + str.tostring(Fib4, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

l5Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = Fib5, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= str.tostring(level5 * 100, "#.#") + "% " + str.tostring(Fib5, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

highBufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = H, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "100% " + str.tostring(Fib2, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  
         
lowBufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = L, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "0% " + str.tostring(Fib2, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

//delete
label.delete(l1Bufferlabel[1])
label.delete(l2Bufferlabel[1])
label.delete(l3Bufferlabel[1])
label.delete(l4Bufferlabel[1])
label.delete(l5Bufferlabel[1])

label.delete(highBufferlabel[1])
label.delete(lowBufferlabel[1])









