Auto Fibonacci [Misu]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©Misu
//@version=5
indicator("Auto Fibonacci [Misu]", overlay = true, shorttitle="Auto Fibo [Misu]", max_lines_count = 500, max_labels_count = 500)

import Fontiramisu/fontilab/12 as fontilab
// import Fontiramisu/fontLib/87 as fontilab 


// ] —————— Input Vars —————— [
// Get user input
var devTooltip              = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var depthTooltip            = "The minimum number of bars that will be taken into account when analyzing pivots."
src                         = input.source(close, "Source", group="Settings")
thresholdMultiplier         = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip, group="Pivot Settings")
depth                       = input.int(title="Depth", defval=50, minval=1, tooltip=depthTooltip, group="Pivot Settings")

isColorAll                  = input.bool(true, "", group="UI Settings", inline="color")
colorAll                    = input.color(color.white, "Color All Lines ----", group="UI Settings", inline="color")

rightOffset                 = input.int(25, "Line Offset Right", group="UI Settings", inline="offset")
leftOffset                  = input.int(20, "Left", group="UI Settings", inline="offset")

isfib0000                   = input.bool(true, "", group="UI Settings", inline="0")
nFib0000                    = input.float(0, "", step=0.01, group="UI Settings", inline="0")
colorFib0000                = input.color(color.white, "", group="UI Settings", inline="0")

isfib0206                   = input.bool(false, "", group="UI Settings", inline="0")
nFib0206                    = input.float(0.206, "", step=0.01, group="UI Settings", inline="0")
colorFib0206                = input.color(color.white, "", group="UI Settings", inline="0")

isfib0382                   = input.bool(true, "", group="UI Settings", inline="0.382")
nFib0382                    = input.float(0.382, "", step=0.01, group="UI Settings", inline="0.382")
colorFib0382                = input.color(color.white, "", group="UI Settings", inline="0.382")

isfib0500                   = input.bool(false, "", group="UI Settings", inline="0.382")
nFib0500                    = input.float(0.5, "", step=0.01, group="UI Settings", inline="0.382")
colorFib0500                = input.color(color.white, "", group="UI Settings", inline="0.382")

isfib0618                   = input.bool(true, "", group="UI Settings", inline="0.618")
nFib0618                    = input.float(0.618, "", step=0.01, group="UI Settings", inline="0.618")
colorFib0618                = input.color(color.white, "", group="UI Settings", inline="0.618")

isfib0786                   = input.bool(true, "", group="UI Settings", inline="0.618")
nFib0786                    = input.float(0.718, "", step=0.01, group="UI Settings", inline="0.618")
colorFib0786                = input.color(color.white, "", group="UI Settings", inline="0.618")

isfib1000                   = input.bool(true, "", group="UI Settings", inline="1")
nFib1000                    = input.float(1, "", step=0.01, group="UI Settings", inline="1")
colorFib1000                = input.color(color.white, "", group="UI Settings", inline="1")

isfib1414                   = input.bool(false, "", group="UI Settings", inline="1")
nFib1414                    = input.float(1.414, "", step=0.01, group="UI Settings", inline="1")
colorFib1414                = input.color(color.white, "", group="UI Settings", inline="1")

isfib1618                   = input.bool(false, "", group="UI Settings", inline="1.618")
nFib1618                    = input.float(1.618, "", step=0.01, group="UI Settings", inline="1.618")
colorFib1618                = input.color(color.white, "", group="UI Settings", inline="1.618")

isfib2000                   = input.bool(false, "", group="UI Settings", inline="1.618")
nFib2000                    = input.float(2, "", step=0.01, group="UI Settings", inline="1.618")
colorFib2000                = input.color(color.white, "", group="UI Settings", inline="1.618")

isfib2618                   = input.bool(false, "", group="UI Settings", inline="2.618")
nFib2618                    = input.float(2.618, "", step=0.01, group="UI Settings", inline="2.618")
colorFib2618                = input.color(color.white, "", group="UI Settings", inline="2.618")

// ] —————— Find Dev Pivots —————— [
// Prepare pivot variables
var line lineLast           = na
var int iLast               = 0 // Index last
var int iPrev               = 0 // Index previous
var float pLast             = 0 // Price last
var float pLastHigh         = 0 // Price last
var float pLastLow          = 0 // Price last
var isHighLast              = false // If false then the last pivot was a pivot low 
isPivotFound                = false

// Get pivot information from dev pivot finding function
[dupLineLast, dupIsHighLast, dupIPrev, dupILast, dupPLast, dupPLastHigh, dupPLastLow] = 
     fontilab.getDeviationPivots(thresholdMultiplier, depth, lineLast, isHighLast, iLast, pLast, true, close, high, low)


if not na(dupIsHighLast)
    lineLast        := dupLineLast
    isHighLast      := dupIsHighLast
    iPrev           := dupIPrev
    iLast           := dupILast
    pLast           := dupPLast
    pLastHigh       := dupPLastHigh
    pLastLow        := dupPLastLow
    isPivotFound    := true
    

// ] —————— Find Trend —————— [
var tDirUp      = true
var pDirStrike  = 0
var upB         = close
var lowB        = close
var midB        = close
var nStrike     = 2

var fib0000     = close
var fib0206     = close
var fib0382     = close
var fib0500     = close
var fib0618     = close
var fib0786     = close
var fib1000     = close
var fib1414     = close
var fib1618     = close
var fib2000     = close
var fib2618     = close

// Get trend and up/low Bands.
[midBDup, upBDup, lowBDup, pDirStrikeDup, tDirUpDup] = fontilab.getInterTrend(src, upB, lowB, pLast, tDirUp, pDirStrike, isPivotFound, isHighLast, 2, 0.5, depth)
pDirStrike      := nz(pDirStrikeDup)
tDirUp          := nz(tDirUpDup)
upB             := nz(upBDup)
lowB            := nz(lowBDup)
midB            := nz(midBDup)

// Logic fibo direction.
var state       = 1
var fiboDirUp   = true
var lastState   = 1
state           := pDirStrike < nStrike ? 0 : not tDirUp ? -1 : tDirUp == 1 ? 1 : nz(state[1])
lastState       := state != state[1] ? state[1] : lastState
fiboDirUp       := state == 0 ? lastState == 1 : state == 1

// Calculate fibs levels.
rangeD          = upB - lowB
fib0000         := fiboDirUp ? upB - nFib0000 * rangeD : lowB + nFib0000 * rangeD
fib0206         := fiboDirUp ? upB - nFib0206 * rangeD : lowB + nFib0206 * rangeD
fib0382         := fiboDirUp ? upB - nFib0382 * rangeD : lowB + nFib0382 * rangeD
fib0500         := fiboDirUp ? upB - nFib0500 * rangeD : lowB + nFib0500 * rangeD
fib0618         := fiboDirUp ? upB - nFib0618 * rangeD : lowB + nFib0618 * rangeD
fib0786         := fiboDirUp ? upB - nFib0786 * rangeD : lowB + nFib0786 * rangeD
fib1000         := fiboDirUp ? upB - nFib1000 * rangeD : lowB + nFib1000 * rangeD
fib1414         := fiboDirUp ? upB - nFib1414 * rangeD : lowB + nFib1414 * rangeD
fib1618         := fiboDirUp ? upB - nFib1618 * rangeD : lowB + nFib1618 * rangeD
fib2000         := fiboDirUp ? upB - nFib2000 * rangeD : lowB + nFib2000 * rangeD
fib2618         := fiboDirUp ? upB - nFib2618 * rangeD : lowB + nFib2618 * rangeD

// Last Update Barindex.
var barLastUpdate = 0
barLastUpdate   := midB != midB[1] ? bar_index : barLastUpdate


// ] —————— Plot —————— [
var fib0000Line     = line.new(0, low, bar_index, high)
var fib0000Label    = label.new(bar_index, low, text="Init")

var fib0206Line     = line.new(0, low, bar_index, high)
var fib0206Label    = label.new(bar_index, low, text="Init")

var fib0382Line     = line.new(0, low, bar_index, high)
var fib0382Label    = label.new(bar_index, low, text="Init")

var fib0500Line     = line.new(0, low, bar_index, high)
var fib0500Label    = label.new(bar_index, low, text="Init")

var fib0618Line     = line.new(0, low, bar_index, high)
var fib0618Label    = label.new(bar_index, low, text="Init")

var fib0786Line     = line.new(0, low, bar_index, high)
var fib0786Label    = label.new(bar_index, low, text="Init")

var fib1000Line     = line.new(0, low, bar_index, high)
var fib1000Label    = label.new(bar_index, low, text="Init")

var fib1414Line     = line.new(0, low, bar_index, high)
var fib1414Label    = label.new(bar_index, low, text="Init")

var fib1618Line     = line.new(0, low, bar_index, high)
var fib1618Label    = label.new(bar_index, low, text="Init")

var fib2000Line     = line.new(0, low, bar_index, high)
var fib2000Label    = label.new(bar_index, low, text="Init")

var fib2618Line     = line.new(0, low, bar_index, high)
var fib2618Label    = label.new(bar_index, low, text="Init")

labelOffset = rightOffset - 5 

if isfib0000
    line.delete(fib0000Line)
    label.delete(fib0000Label)
    fib0000Line     := line.new(barLastUpdate - leftOffset, fib0000, bar_index + rightOffset, fib0000, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0000,  width=1)
    fib0000Label    := label.new(x=bar_index + labelOffset, y = fib0000, xloc=xloc.bar_index, text=str.tostring(nFib0000), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0000, textalign=text.align_center)

if isfib0206
    line.delete(fib0206Line)
    label.delete(fib0206Label)
    fib0206Line     := line.new(barLastUpdate - leftOffset, fib0206, bar_index + rightOffset, fib0206, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0206,  width=1)
    fib0206Label    := label.new(x=bar_index + labelOffset, y = fib0206, xloc=xloc.bar_index, text=str.tostring(nFib0206), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0206, textalign=text.align_center)

if isfib0382
    line.delete(fib0382Line)
    label.delete(fib0382Label)
    fib0382Line     := line.new(barLastUpdate - leftOffset, fib0382, bar_index + rightOffset, fib0382, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0382,  width=1)
    fib0382Label    := label.new(x=bar_index + labelOffset, y = fib0382, xloc=xloc.bar_index, text=str.tostring(nFib0382), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0382, textalign=text.align_center)

if isfib0500
    line.delete(fib0500Line)
    label.delete(fib0500Label)
    fib0500Line     := line.new(barLastUpdate - leftOffset, fib0500, bar_index + rightOffset, fib0500, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0500,  width=1)
    fib0500Label    := label.new(x=bar_index + labelOffset, y = fib0500, xloc=xloc.bar_index, text=str.tostring(nFib0500), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0500, textalign=text.align_center)

if isfib0618
    line.delete(fib0618Line)
    label.delete(fib0618Label)
    fib0618Line     := line.new(barLastUpdate - leftOffset, fib0618, bar_index + rightOffset, fib0618, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0618,  width=1)
    fib0618Label    := label.new(x=bar_index + labelOffset, y = fib0618, xloc=xloc.bar_index, text=str.tostring(nFib0618), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0618, textalign=text.align_center)

if isfib0786
    line.delete(fib0786Line)
    label.delete(fib0786Label)
    fib0786Line     := line.new(barLastUpdate - leftOffset, fib0786, bar_index + rightOffset, fib0786, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib0786,  width=1)
    fib0786Label    := label.new(x=bar_index + labelOffset, y = fib0786, xloc=xloc.bar_index, text=str.tostring(nFib0786), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib0786, textalign=text.align_center)

if isfib1000
    line.delete(fib1000Line)
    label.delete(fib1000Label)
    fib1000Line     := line.new(barLastUpdate - leftOffset, fib1000, bar_index + rightOffset, fib1000, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib1000,  width=1)
    fib1000Label    := label.new(x=bar_index + labelOffset, y = fib1000, xloc=xloc.bar_index, text=str.tostring(nFib1000), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib1000, textalign=text.align_center)

if isfib1414
    line.delete(fib1414Line)
    label.delete(fib1414Label)
    fib1414Line     := line.new(barLastUpdate - leftOffset, fib1414, bar_index + rightOffset, fib1414, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib1414,  width=1)
    fib1414Label    := label.new(x=bar_index + labelOffset, y = fib1414, xloc=xloc.bar_index, text=str.tostring(nFib1414), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib1414, textalign=text.align_center)

if isfib1618
    line.delete(fib1618Line)
    label.delete(fib1618Label)
    fib1618Line     := line.new(barLastUpdate - leftOffset, fib1618, bar_index + rightOffset, fib1618, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib1618,  width=1)
    fib1618Label    := label.new(x=bar_index + labelOffset, y = fib1618, xloc=xloc.bar_index, text=str.tostring(nFib1618), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib1618, textalign=text.align_center)

if isfib2000
    line.delete(fib2000Line)
    label.delete(fib2000Label)
    fib2000Line     := line.new(barLastUpdate - leftOffset, fib2000, bar_index + rightOffset, fib2000, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib2000,  width=1)
    fib2000Label    := label.new(x=bar_index + labelOffset, y = fib2000, xloc=xloc.bar_index, text=str.tostring(nFib2000), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib2000, textalign=text.align_center)

if isfib2618
    line.delete(fib2618Line)
    label.delete(fib2618Label)
    fib2618Line     := line.new(barLastUpdate - leftOffset, fib2618, bar_index + rightOffset, fib2618, xloc=xloc.bar_index, color=isColorAll ? colorAll : colorFib2618,  width=1)
    fib2618Label    := label.new(x=bar_index + labelOffset, y = fib2618, xloc=xloc.bar_index, text=str.tostring(nFib2618), style=label.style_none, size=size.small, textcolor=isColorAll ? colorAll : colorFib2618, textalign=text.align_center)

// linefill.new(fib0786Line, fib1000Line, color.blue)

// ]