Fixed Fibonacci Support Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Source code is from Fibonacci levels MTF from © LonesomeTheBlue
// F_rank_01

//@version=5
indicator("Fixed Fibonacci Support Resistance", overlay = true, max_bars_back = 2000)
startTime  = input.time(title="Start Time Selection", defval=timestamp("8 Dec 2022 14:30 0001"), confirm = true)
endTime = input.time(title="End Time Selection",defval=timestamp("9 Dec 2022 7:30 0001"), confirm = true)
extendFibo = input.bool(false, "extend fibonacci?")
showStartEnd = input.bool(true, "Show start/end white line?")
enable1 = input.bool(defval = true, title = "Level 1", inline = "1", group = "Fibonacci Levels")
level1 = input.float(defval = 0.66, title = "", minval = 0, inline = "1", group = "Fibonacci Levels")
color1 = input.color(defval = #4caf50, title = "Support color", inline = "1", group = "Fibonacci Levels")
color6 = input.color(defval = #f23645, title = "Resistance color", inline = "1", group = "Fibonacci Levels")
enable2 = input.bool(defval = true, title = "Level 2", inline = "2", group = "Fibonacci Levels")
level2 = input.float(defval = 1, title = "", minval = 0, inline = "2", group = "Fibonacci Levels")
color2 = input.color(defval = #787b86, title = "Support color", inline = "2", group = "Fibonacci Levels")
color7 = input.color(defval = #787b86, title = "Resistance color", inline = "2", group = "Fibonacci Levels")
enable3 = input.bool(defval = true, title = "Level 3", inline = "3", group = "Fibonacci Levels")
level3 = input.float(defval = 0.616, title = "", minval = 0, inline = "3", group = "Fibonacci Levels")
color3 = input.color(defval = #4caf50, title = "Support color", inline = "3", group = "Fibonacci Levels")
color8 = input.color(defval = #f23645, title = "Resistance color", inline = "3", group = "Fibonacci Levels")
enable4 = input.bool(defval = true, title = "Level 4", inline = "4", group = "Fibonacci Levels")
level4 = input.float(defval = 0.919, title = "", minval = 0, inline = "4", group = "Fibonacci Levels")
color4 = input.color(defval = #e91e63, title = "Support color", inline = "4", group = "Fibonacci Levels")
color9 = input.color(defval = #9c27b0, title = "Resistance color", inline = "4", group = "Fibonacci Levels")
enable5 = input.bool(defval = true, title = "Level 5", inline = "5", group = "Fibonacci Levels")
level5 = input.float(defval = 0.818, title = "", minval = 0, inline = "5", group = "Fibonacci Levels")
color5 = input.color(defval = #3179f5, title = "Support color", inline = "5", group = "Fibonacci Levels")
color10 = input.color(defval = #c2185b, title = "Resistance color", inline = "5", group = "Fibonacci Levels")
enable100 = input.bool(defval = true, title = "Level 6", inline = "100", group = "Fibonacci Levels")
level100 = input.float(defval = 0.786, title = "", minval = 0, inline = "100", group = "Fibonacci Levels")
color100 = input.color(defval = #3179f5, title = "Support color", inline = "100", group = "Fibonacci Levels")
color11 = input.color(defval = #c2185b, title = "Resistance color", inline = "100", group = "Fibonacci Levels")

_barForTime(_t) =>
    var int _bar = na
    if time == _t
        _bar := last_bar_index - bar_index
    _bar

int firstTime   = _barForTime(startTime)
int secondTime  = _barForTime(endTime)
int timeLength = firstTime - secondTime

highest(source, length) =>
    float highest = na
    array_high = array.new_float(length, na)
    for i = 0 to length -1
        array.set(array_high, i, source[i])
    highest := array.max(array_high, 0)
    highest

lowest(source, length) =>
    float lowest = na
    array_low = array.new_float(length, na)
    for i = 0 to length -1
        array.set(array_low, i, source[i])
    lowest := array.min(array_low, 0)
    lowest

high_ = ta.valuewhen(time == endTime, highest(high, timeLength), 0)
low_ = ta.valuewhen(time == endTime, lowest(low, timeLength), 0)



var enabled = array.from(enable100, enable5, enable4, enable3, enable2, enable1)
var levels = array.from(level100, level5, level4, level3, level2, level1)
var colors = array.from(color100, color5, color4, color3, color2, color1, color11, color10, color9, color8, color7, color6)
mlevelsSupport = array.new_float(7, na)
mlevelsResistance = array.new_float(7, na)

for x = 0 to array.size(levels) - 1
    array.set(mlevelsSupport, x, array.get(enabled, x) ? (high_ - (high_ - low_) * array.get(levels, x)) : na)
    array.set(mlevelsResistance, x, array.get(enabled, x) ? (low_ + (high_ - low_) * array.get(levels, x)) : na)

if barstate.ishistory and showStartEnd
    startLine = line.new(bar_index[firstTime -1], close, bar_index[firstTime -1], open, extend = extend.both, color = color.white)
    line.delete(startLine[1])
    endLine = line.new(bar_index[secondTime -1], close, bar_index[secondTime -1], open, extend = extend.both, color = color.white)
    line.delete(endLine[1])

// fibonacci levels
line1 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 0), bar_index[secondTime - 1], array.get(mlevelsSupport, 0), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 0))
line2 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 1), bar_index[secondTime - 1], array.get(mlevelsSupport, 1), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 1))
line3 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 2), bar_index[secondTime - 1], array.get(mlevelsSupport, 2), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 2))
line4 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 3), bar_index[secondTime - 1], array.get(mlevelsSupport, 3), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 3))
line5 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 4), bar_index[secondTime - 1], array.get(mlevelsSupport, 4), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 4))
line6 = line.new(bar_index[firstTime], array.get(mlevelsSupport, 5), bar_index[secondTime - 1], array.get(mlevelsSupport, 5), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 5))

line7 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 0), bar_index[secondTime - 1], array.get(mlevelsResistance, 0), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 6))
line8 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 1), bar_index[secondTime - 1], array.get(mlevelsResistance, 1), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 7))
line9 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 2), bar_index[secondTime - 1], array.get(mlevelsResistance, 2), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 8))
line10 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 3), bar_index[secondTime - 1], array.get(mlevelsResistance, 3), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 9))
line11 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 4), bar_index[secondTime - 1], array.get(mlevelsResistance, 4), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 10))
line12 = line.new(bar_index[firstTime], array.get(mlevelsResistance, 5), bar_index[secondTime - 1], array.get(mlevelsResistance, 5), extend = extendFibo ? extend.right : extend.none,  color = array.get(colors, 11))

label1 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 0), str.tostring(level100), color = na, textcolor = array.get(colors, 0))
label2 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 1), str.tostring(level5), color = na, textcolor = array.get(colors, 1))
label3 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 2), str.tostring(level4), color = na, textcolor = array.get(colors, 2))
label4 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 3), str.tostring(level3), color = na, textcolor = array.get(colors, 3))
label5 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 4), str.tostring(level2), color = na, textcolor = array.get(colors, 4))
label6 = label.new(bar_index[firstTime + 10], array.get(mlevelsSupport, 5), str.tostring(level1), color = na, textcolor = array.get(colors, 5))

label7 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 0), str.tostring(level100), color = na, textcolor = array.get(colors, 6))
label8 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 1), str.tostring(level5), color = na, textcolor = array.get(colors, 7))
label9 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 2), str.tostring(level4), color = na, textcolor = array.get(colors, 8))
label10 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 3), str.tostring(level3), color = na, textcolor = array.get(colors, 9))
label11 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 4), str.tostring(level2), color = na, textcolor = array.get(colors, 10))
label12 = label.new(bar_index[firstTime + 10], array.get(mlevelsResistance, 5), str.tostring(level1), color = na, textcolor = array.get(colors, 11))

line.delete(line1[1])
line.delete(line2[1])
line.delete(line3[1])
line.delete(line4[1])
line.delete(line5[1])
line.delete(line6[1])
line.delete(line7[1])
line.delete(line8[1])
line.delete(line9[1])
line.delete(line10[1])
line.delete(line11[1])
line.delete(line12[1])

label.delete(label1[1])
label.delete(label2[1])
label.delete(label3[1])
label.delete(label4[1])
label.delete(label5[1])
label.delete(label6[1])
label.delete(label7[1])
label.delete(label8[1])
label.delete(label9[1])
label.delete(label10[1])
label.delete(label11[1])
label.delete(label12[1])