Session Fibonacci Levels [QuantVue]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator('Session Fibonacci Levels [QuantVue]', overlay = true)

//----------settings----------/
var g0      = 'Sessions Settings'
tz          = input.string("GMT-4", "Time Zone", tooltip = "GMT and UTC is the same thing \nMatch this setting to bottom right time", options = ["GMT-10", "GMT-9", "GMT-8", "GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT+0", "GMT+1", "GMT+2", "GMT+3", "GMT+4", "GMT+5", "GMT+6", "GMT+7", "GMT+8", "GMT+9", "GMT+10", "GMT+10:30", "GMT+11", "GMT+12", "GMT+13", "GMT+13:45"], group = g0)
sessionTime = input.session('0600-0900', 'Session', group = g0)
showBox     = input.bool(true, 'Session Box', group = g0, inline = '1')
boxCol      = input.color(color.new(color.blue,80), 'Box Color', group = g0, inline = '1')
removeOld   = input.bool(false, 'Remove Fibs When New Session Starts', group = g0)

var g1      = 'Fibs'
showFibs    = input.bool(true, 'Show Fibs', inline = '0', group = g1)
fibStyle    = input.string('Solid', ' ', options = ['Dashed', 'Dotted', 'Solid'], inline = '0', group = g1)
showTl      = input.bool(true,'Range High to Low Line', inline = '1', group = g1)
tlColor     = input.color(color.gray, ' ', inline = '1', group = g1)
tlStyle     = input.string('Dashed', '', options = ['Dashed', 'Dotted', 'Solid'], inline = '1', group = g1)
labelLoc    = input.string('Left', ' Fib Price Location ', options = ['Left', 'Right'], inline = '2', group = g1)
textColor   = input.color(color.white, ' Text Color ', inline = '2', group = g1)
show_0      = input.bool(true, '', inline = 'Level0', group = g1)
value_0     = input.float(0, '', inline = 'Level0', group = g1)
color_0     = input.color(#787b86, '', inline = 'Level0', group = g1)
show_236    = input.bool(true, '', inline = 'Level0', group = g1)
value_236   = input.float(0.236, '', inline = 'Level0', group = g1)
color_236   = input.color(#c2c5ce, '', inline = 'Level0', group = g1)
show_382    = input.bool(true, '', inline = 'Level1', group = g1)
value_382   = input.float(0.382, '', inline = 'Level1', group = g1)
color_382   = input.color(#c2c5ce, '', inline = 'Level1', group = g1)
show_50     = input.bool(true, '', inline = 'Level1', group = g1)
value_50    = input.float(0.5, '', inline = 'Level1', group = g1)
color_50    = input.color(#c2c5ce, '', inline = 'Level1', group = g1)
show_618    = input.bool(true, '', inline = 'Level2', group = g1)
value_618   = input.float(0.618, '', inline = 'Level2', group = g1)
color_618   = input.color(#00ccff, '', inline = 'Level2', group = g1)
show_786    = input.bool(true, '', inline = 'Level2', group = g1)
value_786   = input.float(0.786, '', inline = 'Level2', group = g1)
color_786   = input.color(#c2c5ce, '', inline = 'Level2', group = g1)
show_1      = input.bool(true, '', inline = 'Level3', group = g1)
value_1     = input.float(1, '', inline = 'Level3', group = g1)
color_1     = input.color(#787b86, '', inline = 'Level3', group = g1)
show_E236   = input.bool(true, '', inline = 'Level3', group = g1)
value_E236  = input.float(-0.236, '', inline = 'Level3', group = g1)
color_E236  = input(#00e64d, '', inline = 'Level3', group = g1)
show_E382   = input.bool(true, '', inline = 'Level4', group = g1)
value_E382  = input.float(-0.382, '', inline = 'Level4', group = g1)
color_E382  = input(#00e64d, '', inline = 'Level4', group = g1)
show_E50    = input.bool(true, '', inline = 'Level4', group = g1)
value_E50   = input.float(-0.50, '', inline = 'Level4', group = g1)
color_E50   = input(#00e64d, '', inline = 'Level4', group = g1)

//----------methods----------//
method switcher(string this) =>
    switch this
        'Solid' => line.style_solid
        'Dashed' => line.style_dashed
        'Dotted' => line.style_dotted

method switcherLabel(string this) =>
    switch this
        'Above Bar' => label.style_label_down
        'Below Bar' => label.style_label_up
        'Left' => label.style_label_right
        'Right' => label.style_none

//----------variables----------//
var rangeLow = 0.0
var rangeHigh = 0.0
var firstBar = 0
var highIndex = 0
var lowIndex = 0
var box rangeBox = na

var line fibHL = na
var line fib0 = na,     var float level0 = na,      var label text0 = na
var line fib100 = na,   var float level100 = na,    var label text100 = na
var line fib236 = na,   var float level236 = na,    var label text236 = na
var line fib382 = na,   var float level382 = na,    var label text382 = na
var line fib50 = na,    var float level50 = na,     var label text50 = na
var line fib618 = na,   var float level618 = na,    var label text618 = na
var line fib786 = na,   var float level786 = na,    var label text786 = na
var line fibE236 = na,  var float levelE236 = na,   var label textE236 = na
var line fibE382 = na,  var float levelE382 = na,   var label textE382 = na
var line fibE50 = na,   var float levelE50 = na,    var label textE50 = na

lines = line.all
labels = label.all

//----------sessions----------//
session = time(timeframe.period, sessionTime, tz)
startSession  = not na(session) and na(session[1])
endSession    = na(session) and not na(session[1])

if startSession
    rangeHigh := high
    rangeLow := low
    firstBar := bar_index
    highIndex := bar_index
    lowIndex := bar_index
    if showBox
        (rangeBox[1]).delete()
        rangeBox := box.new(bar_index, rangeHigh, bar_index + 2, rangeLow, boxCol, bgcolor = boxCol)
    if removeOld
        for l in lines
            l.delete()

if time == session
    if showBox
        rangeBox.set_right(bar_index)
    if high > rangeHigh
        rangeHigh := high
        highIndex := bar_index
        rangeBox.set_top(rangeHigh)
    if low < rangeLow
        rangeLow := low
        lowIndex := bar_index
        rangeBox.set_bottom(rangeLow)

//----------create fibs----------//
if endSession
    for l in lines
        l.delete()
    for l in labels
        l.delete()
    if highIndex < lowIndex
        fibTotal = rangeHigh - rangeLow
        level0 := rangeLow + (fibTotal * value_0)
        level100 := rangeLow + (fibTotal * value_1)
        level236 := rangeLow + (fibTotal * value_236)
        level382 := rangeLow + (fibTotal * value_382)
        level50 := rangeLow + (fibTotal * value_50)
        level618 := rangeLow + (fibTotal * value_618)
        level786 := rangeLow + (fibTotal * value_786)
        levelE236 := rangeLow + (fibTotal * value_E236)
        levelE382 := rangeLow + (fibTotal * value_E382)
        levelE50 := rangeLow + (fibTotal * value_E50)
    if lowIndex < highIndex
        fibTotal = rangeHigh - rangeLow
        level0 := rangeHigh - (fibTotal * value_0)
        level100 := rangeHigh - (fibTotal * value_1)
        level236 := rangeHigh - (fibTotal * value_236)
        level382 := rangeHigh - (fibTotal * value_382)
        level50 := rangeHigh - (fibTotal * value_50)
        level618 := rangeHigh - (fibTotal * value_618)
        level786 := rangeHigh - (fibTotal * value_786)
        levelE236 := rangeHigh - (fibTotal * value_E236)
        levelE382 := rangeHigh - (fibTotal * value_E382)
        levelE50 := rangeHigh - (fibTotal * value_E50)
    if showFibs
        if showTl
            fibHL := line.new(lowIndex, rangeLow, highIndex, rangeHigh, style = tlStyle.switcher(), color = tlColor)
        if show_0
            fib0 :=  line.new(firstBar, level0, bar_index, level0, extend = extend.right, style = fibStyle.switcher(), color = color_0)
        if show_1
            fib100 := line.new(firstBar, level100, bar_index, level100, extend = extend.right, style = fibStyle.switcher(), color = color_1)
        if show_236
            fib236 :=line.new(firstBar, level236, bar_index, level236, extend = extend.right, style = fibStyle.switcher(), color = color_236)
        if show_382
            fib382 :=line.new(firstBar, level382, bar_index, level382, extend = extend.right, style = fibStyle.switcher(), color = color_382)
        if show_50
            fib50 := line.new(firstBar, level50, bar_index, level50, extend = extend.right, style = fibStyle.switcher(), color = color_50)
        if show_618
            fib618 :=line.new(firstBar, level618, bar_index, level618, extend = extend.right, style = fibStyle.switcher(), color = color_618)
        if show_786
            fib786 := line.new(firstBar, level786, bar_index, level786, extend = extend.right, style = fibStyle.switcher(), color = color_786)
        if show_E236
            fibE236 := line.new(firstBar, levelE236, bar_index, levelE236, extend = extend.right, style = fibStyle.switcher(), color = color_E236)
        if show_E382
            fibE382 := line.new(firstBar, levelE382, bar_index, levelE382, extend = extend.right, style = fibStyle.switcher(), color = color_E382)
        if show_E50
            fibE50 := line.new(firstBar, levelE50, bar_index, levelE50, extend = extend.right, style = fibStyle.switcher(), color = color_E50)
        if show_0
            text0 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level0, str.tostring(value_0) + ' (' + str.tostring(level0, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_1
            text100 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level100, str.tostring(value_1) + ' (' + str.tostring(level100, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_236
            text236 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level236, str.tostring(value_236) + ' (' + str.tostring(level236, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_382
            text382 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level382, str.tostring(value_382) + ' (' + str.tostring(level382, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_50
            text50 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level50, str.tostring(value_50) + ' (' + str.tostring(level50, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_618
            text618 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level618, str.tostring(value_618) + ' (' + str.tostring(level618, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_786
            text786 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, level786, str.tostring(value_786) +' (' + str.tostring(level786, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_E236
            textE236 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, levelE236, str.tostring(value_E236) + ' (' + str.tostring(levelE236, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_E382
            textE382 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, levelE382, str.tostring(value_E382) + ' (' + str.tostring(levelE382, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)
        if show_E50
            textE50 := label.new(labelLoc == 'Left' ? firstBar - 1 : bar_index + 2, levelE50, str.tostring(value_E50) + ' (' + str.tostring(levelE50, format.mintick) + ')', 
                 style = labelLoc.switcherLabel(), color = color.new(color.white,100), textcolor = textColor)  

if barstate.islast and labelLoc == 'Right'
    for l in labels
        l.set_x(bar_index + 3)

if na(session)
    if lowIndex < highIndex and low <= level618
        alert(str.tostring(value_618) + ' Fib Hit')
    if highIndex < lowIndex and high >= level618
        alert(str.tostring(value_618) + ' Fib Hit')
    if lowIndex < highIndex and high >= levelE236
        alert(str.tostring(value_E236) + ' Fib Extension Hit')
    if highIndex < lowIndex and low <= levelE236
        alert(str.tostring(value_E236) + ' Fib Extension Hit')
