# Auto Fibonacci Extension

The **Auto Fibonacci Extension Strategy** script draws based on the most recent swing high and swing low within a user-defined look back period.&#x20;

It detects buy opportunities when price breaks above the swing high and sell opportunities when price breaks below the swing low.&#x20;

```java
//@version=6
indicator("Auto Fibonacci Extension (No range function)", overlay=true)

// === INPUTS ===
lookback      = input.int(50, "Swing Lookback Period", minval=10)
fib_levels_in = input.string("1.0,1.272,1.618,2.0", "Fib Extension Levels (comma-separated)")
show_labels   = input.bool(true, "Show Buy/Sell Labels")
show_fib      = input.bool(true, "Show Fib Extension Lines")

// === FIND SWING HIGH/LOW ===
swing_high = ta.highest(high, lookback)
swing_low  = ta.lowest(low, lookback)
swing_range = swing_high - swing_low

// === FIB EXTENSION LEVELS ===
fib_levels = array.new_float()
for lvl_str in str.split(fib_levels_in, ",")
    lvl = str.tonumber(lvl_str)
    array.push(fib_levels, lvl)

// === PLOT FIB EXTENSIONS ===
if show_fib
    for i = 0 to array.size(fib_levels) - 1
        lvl = array.get(fib_levels, i)
        fib_price = swing_high + swing_range * (lvl - 1)
        line.new(bar_index - lookback, fib_price, bar_index, fib_price, color=color.purple, style=line.style_dotted, width=1)

// === SIGNALS ===
buy_signal  = ta.crossover(close, swing_high)
sell_signal = ta.crossunder(close, swing_low)

// === VISUAL OUTPUTS ===
plotshape(buy_signal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small, text="Buy")
plotshape(sell_signal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="Sell")
if show_labels
    if buy_signal
        label.new(bar_index, swing_high, "Buy\nBreakout", style=label.style_label_up, color=color.green, textcolor=color.white)
    if sell_signal
        label.new(bar_index, swing_low, "Sell\nBreakdown", style=label.style_label_down, color=color.red, textcolor=color.white)

// === ALERTS ===
alertcondition(buy_signal, title="Buy Signal", message="Price broke above recent swing high (Auto Fib Extension)")
alertcondition(sell_signal, title="Sell Signal", message="Price broke below recent swing low (Auto Fib Extension)")

// === OPTIONAL ENHANCEMENTS ===
// - Add take-profit/stop-loss suggestions based on extension or retracement levels.
// - Combine with other indicators (e.g., RSI, MACD) for confirmation.
// - Allow multi-timeframe swing detection for broader trend context.
// - Visual highlight of active extension zone.
// - Add trailing stop or dynamic risk management features.

```



### How To Use This Script in Pine Editor

{% stepper %}
{% step %}
### Copy the Script

* Copy all the code above in its entirety.
{% endstep %}

{% step %}
### Open TradingView Pine Editor&#x20;

* Navigate to tradingview.com
* Open a chart
* Click "Pine Editor" at the bottom of the screen
{% endstep %}

{% step %}
### Paste and Add the Script

* Paste the code in the Pine Editor
* Click "Add to chart"
{% endstep %}

{% step %}
### Customize Settings

* Use the gear/settings icon to adjust look back period, select your preferred Fibonacci extension levels, and toggle labels or lines as you wish.
{% endstep %}

{% step %}
### Set Alerts (Optional )

* Click the "alerts" button in TradingView and select the "Buy Signal" or "Sell Signal" from this script for automatic notifications.
{% endstep %}
{% endstepper %}

