Indicator · Pine Script v5 · On the chart

Auto Fib Extension

by TWODS-CAPITAL

Auto Fib Extension detects pivot highs and lows on the price chart and draws Fibonacci extension levels from the latest swing.

View on GitHub

GitHub stars37on the repository
Pine versionv5runs as is
Lines of code338
Settings40inputs
LicenceMIT
Last update3 years ago10 Feb 2023

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

338 lines of Pine Script v5 from Auto Drawings and Patterns/Auto-Fib-Extension.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

Auto-Fib-Extension.pine
//@version=5
indicator("Auto Fib Extension", overlay=true)

depthTooltip = "The minimum number of bars that will be taken into account when calculating the indicator."
depth = input.int(title="Depth", defval=10, minval=1, inline = "Pivots", tooltip=depthTooltip)
reverse = input(false, "Reverse")
var extendLeft = input(false, "Extend Left    |    Extend Right", inline = "Extend Lines")
var extendRight = input(true, "", inline = "Extend Lines")
var extending = extend.none
if extendLeft and extendRight
    extending := extend.both
if extendLeft and not extendRight
    extending := extend.left
if not extendLeft and extendRight
    extending := extend.right
prices = input(true, "Show Prices")
levels = input(true, "Show Levels", inline = "Levels")
levelsFormat = input.string("Values", "", options = ["Values", "Percent"], inline = "Levels")
labelsPosition = input.string("Left", "Labels Position", options = ["Left", "Right"])
var int backgroundTransparency = input.int(85, "Background Transparency", minval = 0, maxval = 100)
var float upperThreshold = 0.236
var float lowerThreshold = 1.0

var line lineLastHL = na
var line lineLastLH = na
var line lineLast = na

var float iLastH = 0
var float iLastL = 0
var float pLastH = 0
var float pLastL = 0

var float[] iPivotsH = array.new_float()
var float[] iPivotsL = array.new_float()
var float[] pPivotsH = array.new_float()
var float[] pPivotsL = array.new_float()

var isHighLast = false
var float startPrice = na
var float endPrice = na
var float diff = na

pivots(src, length, isHigh) =>
    l2 = length * 2
    c = nz(src[length])
    ok = true
    for i = 0 to l2
        if isHigh and src[i] > c
            ok := false

        if not isHigh and src[i] < c
            ok := false
    if ok
        [bar_index[length], c]
    else
        [int(na), float(na)]
[iH, pH] = pivots(high, depth / 2, true)
[iL, pL] = pivots(low, depth / 2, false)

countPivotsH = array.size(iPivotsH)
countPivotsL = array.size(iPivotsL)

if countPivotsH > 0 and countPivotsL > 0
    iLastH := array.get(iPivotsH, countPivotsH-1)
    iLastL := array.get(iPivotsL, countPivotsL-1)
    isHighLast := iLastH > iLastL
    iLastH := 1
    if isHighLast
        if not na(iH)
            pLastH := array.get(pPivotsH, countPivotsH-1)
            if pH > pLastH
                array.set(iPivotsH, countPivotsH-1, iH)
                array.set(pPivotsH, countPivotsH-1, pH)
            iH := na
    else
        if not na(iL)
            pLastL := array.get(pPivotsL, countPivotsL-1)
            if pL < pLastL
                array.set(iPivotsL, countPivotsL-1, iL)
                array.set(pPivotsL, countPivotsL-1, pL)
            iL := na

if not na(iH)
    array.push(iPivotsH, iH)
    array.push(pPivotsH, pH)

if not na(iL)
    array.push(iPivotsL, iL)
    array.push(pPivotsL, pL)

pPivotsHCopy = array.copy(pPivotsH)
pPivotsLCopy = array.copy(pPivotsL)
iPivotsHCopy = array.copy(iPivotsH)
iPivotsLCopy = array.copy(iPivotsL)

if barstate.islast
    for j = bar_index to 0
        if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
            break

        iLastH := array.pop(iPivotsHCopy)
        iLastL := array.pop(iPivotsLCopy)

        pLastH := array.pop(pPivotsHCopy)
        pLastL := array.pop(pPivotsLCopy)

        iPrevPivot = 0.0
        pPrevPivot = 0.0

        isHighLast := iLastH > iLastL

        if isHighLast
            for i = array.size(iPivotsHCopy)-1 to 0
                if i < 0
                    break
                else if array.get(iPivotsHCopy, i) < iLastL
                    break
                if array.get(pPivotsHCopy, i) > pLastH
                    iLastH := array.pop(iPivotsHCopy)
                    pLastH := array.pop(pPivotsHCopy)
                else
                    array.remove(iPivotsHCopy, i)
                    array.remove(pPivotsHCopy, i)
        else
            for i = array.size(iPivotsLCopy)-1 to 0
                if i < 0
                    break
                else if array.get(iPivotsLCopy, i) < iLastH
                    break
                if array.get(pPivotsLCopy, i) < pLastL
                    iLastL := array.pop(iPivotsLCopy)
                    pLastL := array.pop(pPivotsLCopy)
                else
                    array.remove(iPivotsLCopy, i)
                    array.remove(pPivotsLCopy, i)

        if array.size(iPivotsHCopy) == 0 or array.size(iPivotsLCopy) == 0
            break

        isHighLast := iLastH > iLastL

        if isHighLast
            iPrevPivot := array.get(iPivotsHCopy, array.size(iPivotsHCopy)-1)
            pPrevPivot := array.get(pPivotsHCopy, array.size(iPivotsHCopy)-1)
        else
            iPrevPivot := array.get(iPivotsLCopy, array.size(iPivotsLCopy)-1)
            pPrevPivot := array.get(pPivotsLCopy, array.size(iPivotsLCopy)-1)

        if isHighLast
            startPrice := pPrevPivot
            endPrice := pLastL
            diff := math.abs(startPrice - endPrice)
            if pLastH > endPrice + diff * lowerThreshold or pLastH < endPrice + diff * upperThreshold
                array.push(iPivotsLCopy, iLastL)
                array.push(pPivotsLCopy, pLastL)
                continue
            line.delete(lineLastHL)
            line.delete(lineLastLH)
            line.delete(lineLast)
            lineLastHL := line.new(int(iPrevPivot), pPrevPivot, int(iLastL), pLastL, color=color.red, width=1, style=line.style_dashed)
            lineLastLH := line.new(int(iLastL), pLastL, int(iLastH), pLastH, color=color.green, width=1, style=line.style_dashed)
            lineLast := lineLastLH
        else
            startPrice := pPrevPivot
            endPrice := pLastH
            diff := math.abs(startPrice - endPrice)
            if pLastL < endPrice - diff * lowerThreshold or pLastL > endPrice - diff * upperThreshold
                array.push(iPivotsHCopy, iLastH)
                array.push(pPivotsHCopy, pLastH)
                continue
            line.delete(lineLastHL)
            line.delete(lineLastLH)
            line.delete(lineLast)
            lineLastLH := line.new(int(iPrevPivot), pPrevPivot, int(iLastH), pLastH, color=color.red, width=1, style=line.style_dashed)
            lineLastHL := line.new(int(iLastH), pLastH, int(iLastL), pLastL, color=color.green, width=1, style=line.style_dashed)
            lineLast := lineLastHL
        break

_draw_line(price, col) =>
    var id = line.new(0, price, 0, price, color=col, width=1, extend=extending)
    if not na(lineLast)
        line.set_xy1(id, line.get_x1(lineLast), price)
        line.set_xy2(id, line.get_x2(lineLast), price)
    id

_draw_label(price, txt, txtColor) =>
    if not na(price)
        x = labelsPosition == "Left" ? line.get_x1(lineLast) : not extendRight ? line.get_x2(lineLast) : bar_index
        labelStyle = labelsPosition == "Left" ? label.style_label_right : label.style_label_left
        align = labelsPosition == "Left" ? text.align_right : text.align_left
        labelsAlignStrLeft = txt + '\n ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏ \n'
        labelsAlignStrRight = '       ' + txt + '\n ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏ \n'
        labelsAlignStr = labelsPosition == "Left" ? labelsAlignStrLeft : labelsAlignStrRight
        var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor, style=labelStyle, textalign=align, color=#00000000)
        label.set_xy(id, x, price)
        label.set_text(id, labelsAlignStr)
        label.set_textcolor(id, txtColor)

_wrap(txt) =>
    "(" + str.tostring(txt, format.mintick) + ")"

_label_txt(level, price) =>
    if not na(price)
        l = levelsFormat == "Values" ? str.tostring(level) : str.tostring(level * 100) + "%"
        (levels ? l : "") + (prices ? _wrap(price) : "")

_crossing_level(sr, r) =>
    (r > sr and r < sr[1]) or (r < sr and r > sr[1])

diff := (isHighLast ? -1 : 1) * math.abs(startPrice - endPrice)
offset = isHighLast ? line.get_y1(lineLastLH) - line.get_y2(lineLastLH) : line.get_y1(lineLastHL) - line.get_y2(lineLastHL)
offset := (isHighLast ? -1 : 1) * math.abs(offset)

processLevel(show, value, colorL, lineIdOther) =>
    float m = value
    r = (endPrice - offset) + ((reverse ? -1 : 1) * diff * m)
    if show
        lineId = _draw_line(r, colorL)
        _draw_label(r, _label_txt(m, r), colorL)
        if _crossing_level(close, r)
            alert("Autofib: " + syminfo.ticker + " crossing level " + str.tostring(value))
        if not na(lineIdOther)
            linefill.new(lineId, lineIdOther, color = color.new(colorL, backgroundTransparency))
        lineId
    else
        lineIdOther

show_0 = input(true, "", inline = "Level0")
value_0 = input(0, "", inline = "Level0")
color_0 = input(#787b86, "", inline = "Level0")

show_0_236 = input(true, "", inline = "Level0")
value_0_236 = input(0.236, "", inline = "Level0")
color_0_236 = input(#f44336, "", inline = "Level0")

show_0_382 = input(true, "", inline = "Level1")
value_0_382 = input(0.382, "", inline = "Level1")
color_0_382 = input(#81c784, "", inline = "Level1")

show_0_5 = input(true, "", inline = "Level1")
value_0_5 = input(0.5, "", inline = "Level1")
color_0_5 = input(#4caf50, "", inline = "Level1")

show_0_618 = input(true, "", inline = "Level2")
value_0_618 = input(0.618, "", inline = "Level2")
color_0_618 = input(#009688, "", inline = "Level2")

show_0_65 = input(false, "", inline = "Level2")
value_0_65 = input(0.65, "", inline = "Level2")
color_0_65 = input(#009688, "", inline = "Level2")

show_0_786 = input(true, "", inline = "Level3")
value_0_786 = input(0.786, "", inline = "Level3")
color_0_786 = input(#64b5f6, "", inline = "Level3")

show_1 = input(true, "", inline = "Level3")
value_1 = input(1, "", inline = "Level3")
color_1 = input(#787b86, "", inline = "Level3")

show_1_272 = input(false, "", inline = "Level4")
value_1_272 = input(1.272, "", inline = "Level4")
color_1_272 = input(#81c784, "", inline = "Level4")

show_1_414 = input(false, "", inline = "Level4")
value_1_414 = input(1.414, "", inline = "Level4")
color_1_414 = input(#f44336, "", inline = "Level4")

show_1_618 = input(true, "", inline = "Level5")
value_1_618 = input(1.618, "", inline = "Level5")
color_1_618 = input(#2962ff, "", inline = "Level5")

show_1_65 = input(false, "", inline = "Level5")
value_1_65 = input(1.65, "", inline = "Level5")
color_1_65 = input(#2962ff, "", inline = "Level5")

show_2_618 = input(true, "", inline = "Level6")
value_2_618 = input(2.618, "", inline = "Level6")
color_2_618 = input(#f44336, "", inline = "Level6")

show_2_65 = input(false, "", inline = "Level6")
value_2_65 = input(2.65, "", inline = "Level6")
color_2_65 = input(#f44336, "", inline = "Level6")

show_3_618 = input(true, "", inline = "Level7")
value_3_618 = input(3.618, "", inline = "Level7")
color_3_618 = input(#9c27b0, "", inline = "Level7")

show_3_65 = input(false, "", inline = "Level7")
value_3_65 = input(3.65, "", inline = "Level7")
color_3_65 = input(#9c27b0, "", inline = "Level7")

show_4_236 = input(true, "", inline = "Level8")
value_4_236 = input(4.236, "", inline = "Level8")
color_4_236 = input(#e91e63, "", inline = "Level8")

show_4_618 = input(false, "", inline = "Level8")
value_4_618 = input(4.618, "", inline = "Level8")
color_4_618 = input(#81c784, "", inline = "Level8")

show_neg_0_236 = input(false, "", inline = "Level9")
value_neg_0_236 = input(-0.236, "", inline = "Level9")
color_neg_0_236 = input(#f44336, "", inline = "Level9")

show_neg_0_382 = input(false, "", inline = "Level9")
value_neg_0_382 = input(-0.382, "", inline = "Level9")
color_neg_0_382 = input(#81c784, "", inline = "Level9")

show_neg_0_618 = input(false, "", inline = "Level10")
value_neg_0_618 = input(-0.618, "", inline = "Level10")
color_neg_0_618 = input(#009688, "", inline = "Level10")

show_neg_0_65 = input(false, "", inline = "Level10")
value_neg_0_65 = input(-0.65, "", inline = "Level10")
color_neg_0_65 = input(#009688, "", inline = "Level10")


lineId0 = processLevel(show_neg_0_65, value_neg_0_65, color_neg_0_65, line(na))
lineId1 = processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618, lineId0)
lineId2 = processLevel(show_neg_0_382, value_neg_0_382, color_neg_0_382, lineId1)
lineId3 = processLevel(show_neg_0_236, value_neg_0_236, color_neg_0_236, lineId2)
lineId4 = processLevel(show_0, value_0, color_0, lineId3)
lineId5 = processLevel(show_0_236, value_0_236, color_0_236, lineId4)
lineId6 = processLevel(show_0_382, value_0_382, color_0_382, lineId5)
lineId7 = processLevel(show_0_5, value_0_5, color_0_5, lineId6)
lineId8 = processLevel(show_0_618, value_0_618, color_0_618, lineId7)
lineId9 = processLevel(show_0_65, value_0_65, color_0_65, lineId8)
lineId10 = processLevel(show_0_786, value_0_786, color_0_786, lineId9)
lineId11 = processLevel(show_1, value_1, color_1, lineId10)
lineId12 = processLevel(show_1_272, value_1_272, color_1_272, lineId11)
lineId13 = processLevel(show_1_414, value_1_414, color_1_414, lineId12)
lineId14 = processLevel(show_1_618, value_1_618, color_1_618, lineId13)
lineId15 = processLevel(show_1_65, value_1_65, color_1_65, lineId14)
lineId16 = processLevel(show_2_618, value_2_618, color_2_618, lineId15)
lineId17 = processLevel(show_2_65, value_2_65, color_2_65, lineId16)
lineId18 = processLevel(show_3_618, value_3_618, color_3_618, lineId17)
lineId19 = processLevel(show_3_65, value_3_65, color_3_65, lineId18)
lineId20 = processLevel(show_4_236, value_4_236, color_4_236, lineId19)
lineId21 = processLevel(show_4_618, value_4_618, color_4_618, lineId20)

Overview

Depth sets how many bars define a pivot, and Reverse flips the swing direction. Users can extend lines left or right, show prices and level values or percents, and place labels on the left or right. Individual ratios from 0 through 0.786 can be shown, recolored, or hidden, with adjustable background transparency. It is a drawing indicator and does not issue entries, exits, or stops.

Details

Published by

TWODS-CAPITAL on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Depthint10depth
Reverseboolfalsereverse
Extend Left    |    Extend RightboolfalseextendLeft
extendRightbooltrueextendRight
Show Pricesbooltrueprices
Show Levelsbooltruelevels
levelsFormatstringValueslevelsFormat
Labels PositionstringLeftlabelsPosition
Background Transparencyint85backgroundTransparency
show_0booltrueshow_0
value_0int0value_0
color_0color#787b86color_0
Show all 40 settings
SettingTypeDefaultVariable
show_0_236booltrueshow_0_236
value_0_236float0.236value_0_236
color_0_236color#f44336color_0_236
show_0_382booltrueshow_0_382
value_0_382float0.382value_0_382
color_0_382color#81c784color_0_382
show_0_5booltrueshow_0_5
value_0_5float0.5value_0_5
color_0_5color#4caf50color_0_5
show_0_618booltrueshow_0_618
value_0_618float0.618value_0_618
color_0_618color#009688color_0_618
show_0_65boolfalseshow_0_65
value_0_65float0.65value_0_65
color_0_65color#009688color_0_65
show_0_786booltrueshow_0_786
value_0_786float0.786value_0_786
color_0_786color#64b5f6color_0_786
show_1booltrueshow_1
value_1int1value_1
color_1color#787b86color_1
show_1_272boolfalseshow_1_272
value_1_272float1.272value_1_272
color_1_272color#81c784color_1_272
show_1_414boolfalseshow_1_414
value_1_414float1.414value_1_414
color_1_414color#f44336color_1_414
show_1_618booltrueshow_1_618
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 10 days ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 2 years ago · None stated GitHub repository of BZ-tracker

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers