---
description: >-
  Advanced Auto Fibonacci Retracement indicator that detects swing highs and
  lows, draws persistent Fibonacci levels between the most recent valid swing
  high/low pair, and and includes directional bias.
---

# &#x20;Auto Fibonacci Retracement

#### ✅ Features:

* Automatically detects recent **swing high/low** based on user-defined strength.
* Adjusts Fibonacci levels based on **trend direction**.
* Persistent **multi-bar lines** using `line` objects.
* Optional label display.
* Clean, professional structure, ready for enhancements.

```java
//@version=6
indicator("Advanced Auto Fibonacci Retracement", overlay=true)

// === INPUTS ===
swingStrength = input.int(5, minval=1, title="Swing Strength")
showLabels = input.bool(true, title="Show Level Labels")
showOnlyLast = input.bool(true, title="Show Only Latest Levels")

// === DETECT SWINGS ===
pivotHigh = ta.pivothigh(high, swingStrength, swingStrength)
pivotLow = ta.pivotlow(low, swingStrength, swingStrength)

var float lastHigh = na
var float lastLow = na
var bool isUptrend = na

if not na(pivotHigh)
    lastHigh := pivotHigh
if not na(pivotLow)
    lastLow := pivotLow

// Detect trend direction
if not na(lastHigh) and not na(lastLow)
    isUptrend := bar_index > bar_index[swingStrength] and lastLow > lastLow[swingStrength]

// === CALCULATE FIB LEVELS ===
var line[] fibLines = array.new_line()
var label[] fibLabels = array.new_label()

// Function to clear lines and labels
f_clear_lines_and_labels() =>
    for l in fibLines
        line.delete(l)
    array.clear(fibLines)

    for lbl in fibLabels
        label.delete(lbl)
    array.clear(fibLabels)

if not na(lastHigh) and not na(lastLow)
    if (isUptrend and lastLow < lastHigh) or (not isUptrend and lastHigh > lastLow)
        f_clear_lines_and_labels()

        float diff = math.abs(lastHigh - lastLow)

        float base = isUptrend ? lastLow : lastHigh

        float fib_0   = base
        float fib_236 = base + (isUptrend ? diff * 0.236 : -diff * 0.236)
        float fib_382 = base + (isUptrend ? diff * 0.382 : -diff * 0.382)
        float fib_5   = base + (isUptrend ? diff * 0.5   : -diff * 0.5)
        float fib_618 = base + (isUptrend ? diff * 0.618 : -diff * 0.618)
        float fib_786 = base + (isUptrend ? diff * 0.786 : -diff * 0.786)
        float fib_1   = isUptrend ? lastHigh : lastLow

        // Levels and colors
        levels = array.from(fib_0, fib_236, fib_382, fib_5, fib_618, fib_786, fib_1)
        levelNames = array.from("0.0", "23.6%", "38.2%", "50.0%", "61.8%", "78.6%", "100.0%")
        levelColors = array.from(color.gray, color.teal, color.blue, color.purple, color.orange, color.red, color.gray)

        for i = 0 to array.size(levels) - 1
            y = array.get(levels, i)
            c = array.get(levelColors, i)
            l = line.new(x1=bar_index - 50, y1=y, x2=bar_index, y2=y, color=c, width=1)
            array.push(fibLines, l)

            if showLabels
                lbl = label.new(x=bar_index, y=y, text=array.get(levelNames, i), style=label.style_label_left, color=color.new(c, 70), textcolor=color.black)
                array.push(fibLabels, lbl)

```

#### 🧠 How It Works:

* **Swings** are detected using `ta.pivothigh` / `ta.pivotlow` with adjustable strength.
* When both a high and a low are found, the script calculates the range and draws **7 Fibonacci levels**.
* The script dynamically clears and redraws the lines and labels as the market updates.
* Works in **both uptrends and downtrends**, with the levels adapting accordingly.

