High/Low Fibs using Bullish Anchors 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thebearfib
//
//@version=5
//
indicator(title = "High/Low Fibs Anchors Bear", 
  shorttitle='Bear Fibs Anchors', 
  format=format.price, 
  precision=2, 
  overlay = true)

//~ https://www.tradingview.com/script/7poJ0BTX-HSV-and-HSL-gradient-Tools-Built-in-Drop-in-replacement/
import kaigouthro/hsvColor/15 as HSV
o(col, o)  => color.new(col, o)

//~ https://www.tradingview.com/script/D1igBUy9-arrays/
import HeWhoMustNotBeNamed/arraymethods/1

_8 = input(false,'═══════  INPUTS  ═══════════════════════')
bool _flip        = input.bool(true, 'Flip Fibs 🔃')
int lookback      = input.int(defval = 1000, title = "High/Low Lookback ⚓", minval = 0)

bool showlab      = input.bool(defval = true,  title = "𝚂𝚑𝚘𝚠 Labels & Lines")
bool showPrices   = input.bool(defval = true,  title = "𝚂𝚑𝚘𝚠 Prices")

_8a = input(false,'═══════  MOVE LINES  ════════════════════')
int extndPrices   = input.int(defval = 20, title = "Move Prices to Right ➡️")
int extndRT       = input.int(defval = 20, title = "Extend Lines Right ➡️")

_8b = input(false,'═══════  FIB LEVELS  ════════════════════')
var FL              = "Set Fib Levels"

color fib0col     =input( #dd09c1,    '',                           inline = "12",  group = FL)//Bottom Anchor & New Low
color fib100col   =input( #dd09c1,    '',                           inline = "5",   group = FL)//Top Anchor & New High
color fib200col   =input( #dd0909,    '',                           inline = "1",   group = FL)//Final Target
color fib1618col  =input( #73ff00,    '',                           inline = "2",   group = FL)//Targer #2
color fib150col   =input( #df7f09,    '',                           inline = "3",   group = FL)//Test Level
color fib1272col  =input( #73ff00,    '',                           inline = "4",   group = FL)//Target #1
color fib854col   =input( #8c8d84,    '',                           inline = "6",   group = FL)//Soft Level
color fib782col   =input( #0925df,    '',                           inline = "7",   group = FL)//Hard Level
color fib650col   =input( #ddac09,    '',                           inline = "8",   group = FL)//BreakOut Level
color fib618col   =input( #c8ff00fb,  '',                           inline = "9",   group = FL)//Retrace Level #2
color fib500col   =input( #ff0000,    '',                           inline = "10",  group = FL)//Retrace Level #1
color fib382col   =input( #55055f,    '',                           inline = "11",  group = FL)//Invaldation Line

var color [] fibArrayc  = array.from(fib0col,fib382col,fib500col,fib618col,fib650col,fib782col,fib854col,fib100col,fib1272col,fib150col,fib1618col,fib200col)

float fib0Value     = input.float(0.000, ' ',                         inline = "12",  group = FL) //
float fib382Value   = input.float(0.382, ' ',                         inline = "11",  group = FL)//Invaldation Line
float fib500Value   = input.float(0.500, ' ',                         inline = "10",  group = FL)//Retrace Level #1
float fib618Value   = input.float(0.618, ' ',                         inline = "9",   group = FL)//Retrace Level #2
float fib650Value   = input.float(0.650, ' ',                         inline = "8",   group = FL)//BreakOut Level
float fib782Value   = input.float(0.782, ' ',                         inline = "7",   group = FL)//Hard Level
float fib854Value   = input.float(0.854, ' ',                         inline = "6",   group = FL)//Soft Level
float fib100Value   = input.float(1.000, ' ',                         inline = "5",   group = FL)//Top Anchor & New High
float fib1272Value  = input.float(1.272, ' ',                         inline = "4",   group = FL)//Target #1
float fib150Value   = input.float(1.500, ' ',                         inline = "3",   group = FL)//Test Level
float fib1618Value  = input.float(1.618, ' ',                         inline = "2",   group = FL)//Targer #2
float fib200Value   = input.float(2.000, ' ',                         inline = "1",   group = FL)//Final Target

bool _fib0Value     = input.bool(true, 'Anchor & New High/Low',       inline = "12",  group = FL) //Bottom Anchor & New Low
bool _fib382Value   = input.bool(true, 'Invaldation Line',            inline = "11",  group = FL)//Invaldation Line
bool _fib500Value   = input.bool(true, 'Retrace Level #1',            inline = "10",  group = FL)//Retrace Level #1
bool _fib618Value   = input.bool(true, 'Retrace Level #2',            inline = "9",   group = FL)//Retrace Level #2
bool _fib650Value   = input.bool(true, 'BreakOut Level',              inline = "8",   group = FL)//BreakOut Level
bool _fib782Value   = input.bool(true, 'Hard Level',                  inline = "7",   group = FL)//Hard Level
bool _fib854Value   = input.bool(true, 'Soft Level',                  inline = "6",   group = FL)//Soft Level
bool _fib100Value   = input.bool(true, 'Anchor & New High/Low',       inline = "5",   group = FL)//Top Anchor & New High
bool _fib1272Value  = input.bool(true, 'Target #1',                   inline = "4",   group = FL)//Target #1
bool _fib150Value   = input.bool(true, 'Test Level',                  inline = "3",   group = FL)//Test Level
bool _fib1618Value  = input.bool(true, 'Target #2',                   inline = "2",   group = FL)//Targer #2
bool _fib200Value   = input.bool(true, 'Final Target',                inline = "1",   group = FL)//Final Target

var float [] fibArray  = array.from(fib0Value,fib382Value,fib500Value,fib618Value,fib650Value,fib782Value,fib854Value,fib100Value,fib1272Value,fib150Value,fib1618Value,fib200Value)

var line u   = na
var line d   = na
var line p   = na
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————— Fib Level Calcs  —————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
if barstate.islast
    line.all.flush(), label.all.flush()

    arrHL = array.new_float()
    for i = 0 to lookback - 1
        arrHL.push(high[i])
        arrHL.push(low[i])

    u.delete()
    d.delete()

    u := line.new(bar_index - (lookback - 1), 
      arrHL.max(),
      bar_index + extndRT, 
      arrHL.max(), 
      color = o(fib0col, 60), 
      width=3)

    d := line.new(bar_index - (lookback - 1), 
      arrHL.min(), 
      bar_index + extndRT, 
      arrHL.min(), 
      color = o(fib100col, 60), 
      width=3)

    s12 = u.get_y2()
    s22 = d.get_y2()
    dist = s12 - s22

    fibsBullf = array.from(s12,s12 - dist * 0.386, s12 - dist * 0.500, s12 - dist * 0.618,  s12 - dist * 0.650, s12 - dist * 0.786, s12 - dist * 0.854, s22,  s12 - dist * 1.272,  s12 - dist * 1.500, s12 - dist * 1.618, s12 - dist * 2.00)
    fibsBearf = array.from(s22,s22 + dist * 0.386, s22 + dist * 0.500, s22 + dist * 0.618,  s22 + dist * 0.650, s22 + dist * 0.786, s22 + dist * 0.854, s12,  s22 + dist * 1.272,  s22 + dist * 1.500, s22 + dist * 1.618, s22 + dist * 2.00)

    fibs = _flip == true ? fibsBullf : fibsBearf

    for i = 0 to fibs.size() - 1
        [style, col] = switch 
            i == 0  => [line.style_dashed, o(fib0col, 60)]
            i == 1  => [line.style_dashed,   fib382col]
            i == 2  => [line.style_dashed,   fib500col]
            i == 3  => [line.style_dashed,   fib618col]
            i == 4  => [line.style_dashed,   fib650col]
            i == 5  => [line.style_dashed,   fib782col]
            i == 6  => [line.style_dashed,   fib854col]
            i == 7  => [line.style_dashed, o(fib100col, 60)]
            i == 8  => [line.style_dashed,   fib1272col]
            i == 9  => [line.style_dashed,   fib150col]
            i == 10 => [line.style_dashed,   fib1618col]
            i == 11 => [line.style_dashed,   fib200col]

        kaicol =HSV.hsv_gradient(i, 0, fibs.size() - 1, fib0col, fib100col)
        
        if showlab
            line.new(bar_index - (lookback - 1), fibs.get(i), bar_index + extndRT, fibs.get(i), width = 1, style = style, color = o(col, 20))
            label.new(bar_index + extndPrices + extndRT, fibs.get(i), xloc = xloc.bar_index, text = str.tostring(fibs.get(i), format.mintick), style = label.style_label_left, textcolor = o(col, 20), color = #ffffff00)
        if showPrices
            label.new(bar_index + extndRT, fibs.get(i), xloc = xloc.bar_index, text = str.tostring(fibArray.get(i)), style = label.style_label_left, textcolor = o(col, 20), color = #ffffff00)

_calc1 = (u.get_y2() - d.get_y2())
_calc2 = (u.get_y2() - d.get_y2()) / d.get_y2() * 100 
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————— Table Inputs  ————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
_8c = input(false,'═══════  TABLE INPUTS  ══════════════════')
position3c   = input.string("Bottom Right","🔃- Table Position",
  options = ["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Center", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"])

tableposGo3 = switch position3c
    
    "Top Right"     => position.top_right
    "Top Center"    => position.top_center
    "Top Left"      => position.top_left
    "Middle Right"  => position.middle_right
    "Middle Center" => position.middle_center
    "Middle Left"   => position.middle_left
    "Bottom Right"  => position.bottom_right
    "Bottom Center" => position.bottom_center
    "Bottom Left"   => position.bottom_left
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————— Inputs  ——————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
_fontSize1  = input.string("Normal","🗚 - Table Font Size", options = [ "Auto", "Tiny", "Small", "Normal", "Large", "Huge"])
  
_fontSize   = switch _fontSize1
    
    "Normal"    => size.normal
    "Auto"      => size.auto
    "Tiny"      => size.tiny
    "Small"     => size.small
    "Large"     => size.large
    "Huge"      => size.huge

bool showxA       = input.bool(defval = false,  title = "𝚂𝚑𝚘𝚠 Lookback in Bars")
bool showxB       = input.bool(defval = false,  title = "𝚂𝚑𝚘𝚠 Diff $ & % ")


if barstate.islast or barstate.islastconfirmedhistory
    var table tabu = table.new(tableposGo3, 99, 99,
      bgcolor = color.rgb(45, 44, 44,100),  
      frame_color=color.rgb(59, 58, 58,100),
      frame_width = 3,  
      border_color =color.rgb(19, 19, 19,100),
      border_width = 1)

    if showxA
        table.cell(tabu, 1, 5, 'ɴᴏ ʟᴏᴏᴋʙᴀᴄᴋ ʙᴀʀs:',text_halign = text.align_left,text_color = color.gray,text_size = _fontSize)
        table.cell(tabu, 2, 5, str.tostring(lookback),text_halign =text.align_right,  text_color =color.white ,text_size = _fontSize ,text_font_family=font.family_monospace)
    if showxB
        table.cell(tabu, 1, 6, 'ᴅᴇʟᴛᴀ ɪɴ ᴅᴏʟʟᴀʀs: $',text_halign = text.align_left,text_color = color.gray,text_size = _fontSize)
        table.cell(tabu, 2, 6, str.tostring(_calc1,format.mintick),text_halign = text.align_right,text_color = color.white, text_size = _fontSize,  text_font_family=font.family_monospace)

        table.cell(tabu, 1, 7, 'ᴅᴇʟᴛᴀ ɪɴ ᴘᴇʀᴄᴇɴᴛ:',text_halign = text.align_left,text_color = color.gray,text_size = _fontSize)
        table.cell(tabu, 2, 7, str.tostring(_calc2, format.percent),text_halign = text.align_right,text_color =color.white,text_size = _fontSize,text_font_family=font.family_monospace)