Auto Fibo Multi Timeframe [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 Fibo Multi Timeframe [Misu]", shorttitle="Auto Fibo Multi TF [Misu]",  overlay = true, max_lines_count = 500, max_labels_count = 500)

import Fontiramisu/fontilab/12 as fontilab

// ] —————— Input Vars —————— [
timeframe       = input.timeframe(defval = '240', group="Pivot Settings")
leftBars        = input.int(defval = 2, title = "Left Bars", minval = 1, group="Pivot Settings")
rightBars       = input.int(defval = 2, title = "Right Bars", minval = 1, group="Pivot Settings")

// Fibo.
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(20, "Label Offset Right", 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")

// ] —————— Vars —————— [
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

// ] —————— Find Dev Pivots —————— [

getMultiTfPivots()=>
    float ph    = ta.pivothigh(leftBars, rightBars)
    float pl    = ta.pivotlow(leftBars, rightBars)
    phBIndexS   = ph ? time[rightBars] : na
    plBIndexS   = pl ? time[rightBars] : na

    [ph, phBIndexS, pl, plBIndexS]

// get if there if Pivot High/low and their start/end times
[ph, phBIndexS, pl, plBIndexS] = request.security(syminfo.tickerid, timeframe, getMultiTfPivots(), lookahead = barmerge.lookahead_on)

// ] —————— Fibs Handles —————— [
// Get last highs/lows.
pivothigh           = na(ph[1]) and ph ? ph : na
pivotlow            = na(pl[1]) and pl ? pl : na
var isHighLast      = true
var curPivotP       = 0.    
var lastPivotP      = 0. 
var curPivotBi      = 0    
var lastPivotBi     = 0

// Special case: where high & low detected at the same time.
if not na(pivothigh) and not na(pivotlow)
    lastPivotP          := isHighLast ? pl : ph
    curPivotP           := isHighLast ? ph : pl
    lastPivotBi         := isHighLast ? plBIndexS : phBIndexS
    curPivotBi          := isHighLast ? phBIndexS : plBIndexS
// All cases
else 
    isHighLast          := not na(pivothigh) ? true : not na(pivotlow) ? false : isHighLast
    
    lastPivotP          := not na(pivothigh) and not isHighLast[1] or not na(pivotlow) and isHighLast[1] ? curPivotP : lastPivotP
    curPivotP           := not na(pivothigh) ? ph : not na(pivotlow) ? pl : curPivotP
    
    lastPivotBi         := not na(pivothigh) and not isHighLast[1] or not na(pivotlow) and isHighLast[1] ? curPivotBi : lastPivotBi
    curPivotBi          := not na(pivothigh) ? phBIndexS : not na(pivotlow) ? plBIndexS : curPivotBi

// Logic fibo direction.
fiboDirUp           = isHighLast

// Last Update Barindex.
var barLastUpdate = 0
barLastUpdate       := lastPivotBi

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

// ] —————— 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

if isfib0000
    line.delete(fib0000Line)
    label.delete(fib0000Label)
    fib0000Line     := line.new(barLastUpdate, fib0000, time, fib0000, xloc=xloc.bar_time, 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, fib0206, time, fib0206, xloc=xloc.bar_time, 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, fib0382, time, fib0382, xloc=xloc.bar_time, 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, fib0500, time, fib0500, xloc=xloc.bar_time, 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, fib0618, time, fib0618, xloc=xloc.bar_time, 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, fib0786, time, fib0786, xloc=xloc.bar_time, 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, fib1000, time, fib1000, xloc=xloc.bar_time, 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, fib1414, time, fib1414, xloc=xloc.bar_time, 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, fib1618, time, fib1618, xloc=xloc.bar_time, 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, fib2000, time, fib2000, xloc=xloc.bar_time, 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, fib2618, time, fib2618, xloc=xloc.bar_time, 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)

// ]
