1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
298 lines of Pine Script v6 from test/pinescripts/auto-fix-ext.pine, published under AGPL-3.0. Keep the author's credit and licence lines when you reuse it.
auto-fix-ext.pine
//@version=6
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=2, inline = "Pivots", tooltip=depthTooltip)
reverse = input(false, "Reverse", display = display.none)
var extendLeft = input(false, "Extend Left | Extend Right", inline = "Extend Lines", display = display.none)
var extendRight = input(true, "", inline = "Extend Lines", display = display.none)
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", display = display.none)
levels = input(true, "Show Levels", inline = "Levels", display = display.none)
levelsFormat = input.string("Values", "", options = ["Values", "Percent"], inline = "Levels", display = display.none, active = levels)
labelsPosition = input.string("Left", "Labels Position", options = ["Left", "Right"], display = display.none)
backgroundTransparency = input.int(85, "Background Transparency", minval = 0, maxval = 100, display = display.none)
upperThreshold = 0.236
lowerThreshold = 1.0
pivots(src, length, isHigh) =>
if bar_index >= length
price = nz(src[length])
found = true
for i = 0 to length * 2
if (isHigh and src[i] > price) or (not isHigh and src[i] < price)
found := false
break
if found
chart.point.from_time(time[length], price)
update()=>
var line lineLastHL = na
var line lineLastLH = na
var line lineLast = na
var chart.point[] pivotsH = array.new<chart.point>()
var chart.point lastH = na
var chart.point[] pivotsL = array.new<chart.point>()
var chart.point lastL = na
var isHighLast = false
var float startPrice = na
var float endPrice = na
H = pivots(high, depth / 2, true)
L = pivots(low, depth / 2, false)
countPivotsH = array.size(pivotsH)
countPivotsL = array.size(pivotsL)
if countPivotsH > 0 and countPivotsL > 0
lastH := array.get(pivotsH, countPivotsH-1)
lastL := array.get(pivotsL, countPivotsL-1)
isHighLast := lastH.time > lastL.time
if isHighLast
if not na(H)
if H.price > lastH.price
array.set(pivotsH, countPivotsH-1, H)
H := na
else
if not na(L)
if L.price < lastL.price
array.set(pivotsL, countPivotsL-1, L)
L := na
if not na(H)
array.push(pivotsH, H)
if not na(L)
array.push(pivotsL, L)
if barstate.islast and array.size(pivotsH) > 0 and array.size(pivotsL) > 0
pivotsHCopy = array.copy(pivotsH)
pivotsLCopy = array.copy(pivotsL)
while array.size(pivotsHCopy) > 0 and array.size(pivotsLCopy) > 0
lastH := array.pop(pivotsHCopy)
lastL := array.pop(pivotsLCopy)
isHighLast := lastH.time > lastL.time
pivots = isHighLast ? pivotsHCopy : pivotsLCopy
for i = array.size(pivots)-1 to 0
if i < 0
break
p = array.get(pivots, i)
if p.time < lastL.time
break
betterPrice = isHighLast ? p.price > lastH.price : p.price < lastL.price
if p.price > lastH.price
lastH := array.pop(pivots)
else
array.remove(pivots, i)
if array.size(pivotsHCopy) == 0 or array.size(pivotsLCopy) == 0
break
isHighLast := lastH.time > lastL.time
pivots := isHighLast ? pivotsHCopy : pivotsLCopy
prevPivot = array.get(pivots, array.size(pivots)-1)
startPrice := prevPivot.price
if isHighLast
endPrice := lastL.price
diff = math.abs(startPrice - endPrice)
if lastH.price > endPrice + diff * lowerThreshold or lastH.price < endPrice + diff * upperThreshold
array.push(pivotsLCopy, lastL)
continue
line.delete(lineLastHL)
line.delete(lineLastLH)
lineLastHL := line.new(prevPivot, lastL, color=color.red, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLastLH := line.new(lastL, lastH, color=color.green, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLast := lineLastLH
else
endPrice := lastH.price
diff = math.abs(startPrice - endPrice)
if lastL.price < endPrice - diff * lowerThreshold or lastL.price > endPrice - diff * upperThreshold
array.push(pivotsHCopy, lastH)
continue
line.delete(lineLastHL)
line.delete(lineLastLH)
lineLastLH := line.new(prevPivot, lastH, color=color.red, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLastHL := line.new(lastH, lastL, color=color.green, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLast := lineLastHL
break
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)
if barstate.islast and na(lineLast)
runtime.error("Not enough data to calculate Auto Fib Extension on the current symbol. Change the chart's timeframe to a lower one or select a smaller calculation depth using the indicator's `Depth` settings.")
[endPrice - offset, diff, lineLast]
[endPrice, diff, lineLast] = update()
_draw_line(price, col) =>
var id = line.new(time, price, time, price, color=col, width=1, extend=extending, xloc = xloc.bar_time)
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) : time
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, xloc = xloc.bar_time)
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])
processLevel(show, value, colorL, lineIdOther) =>
float m = value
r = endPrice + ((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", display = display.none)
value_0 = input(0, "", inline = "Level0", display = display.none, active = show_0)
color_0 = input(#787b86, "", inline = "Level0", display = display.none, active = show_0)
show_0_236 = input(true, "", inline = "Level0", display = display.none)
value_0_236 = input(0.236, "", inline = "Level0", display = display.none, active = show_0_236)
color_0_236 = input(#f44336, "", inline = "Level0", display = display.none, active = show_0_236)
show_0_382 = input(true, "", inline = "Level1", display = display.none)
value_0_382 = input(0.382, "", inline = "Level1", display = display.none, active = show_0_382)
color_0_382 = input(#81c784, "", inline = "Level1", display = display.none, active = show_0_382)
show_0_5 = input(true, "", inline = "Level1", display = display.none)
value_0_5 = input(0.5, "", inline = "Level1", display = display.none, active = show_0_5)
color_0_5 = input(#4caf50, "", inline = "Level1", display = display.none, active = show_0_5)
show_0_618 = input(true, "", inline = "Level2", display = display.none)
value_0_618 = input(0.618, "", inline = "Level2", display = display.none, active = show_0_618)
color_0_618 = input(#009688, "", inline = "Level2", display = display.none, active = show_0_618)
show_0_65 = input(false, "", inline = "Level2", display = display.none)
value_0_65 = input(0.65, "", inline = "Level2", display = display.none, active = show_0_65)
color_0_65 = input(#009688, "", inline = "Level2", display = display.none, active = show_0_65)
show_0_786 = input(true, "", inline = "Level3", display = display.none)
value_0_786 = input(0.786, "", inline = "Level3", display = display.none, active = show_0_786)
color_0_786 = input(#64b5f6, "", inline = "Level3", display = display.none, active = show_0_786)
show_1 = input(true, "", inline = "Level3", display = display.none)
value_1 = input(1, "", inline = "Level3", display = display.none, active = show_1)
color_1 = input(#787b86, "", inline = "Level3", display = display.none, active = show_1)
show_1_272 = input(false, "", inline = "Level4", display = display.none)
value_1_272 = input(1.272, "", inline = "Level4", display = display.none, active = show_1_272)
color_1_272 = input(#81c784, "", inline = "Level4", display = display.none, active = show_1_272)
show_1_414 = input(false, "", inline = "Level4", display = display.none)
value_1_414 = input(1.414, "", inline = "Level4", display = display.none, active = show_1_414)
color_1_414 = input(#f44336, "", inline = "Level4", display = display.none, active = show_1_414)
show_1_618 = input(true, "", inline = "Level5", display = display.none)
value_1_618 = input(1.618, "", inline = "Level5", display = display.none, active = show_1_618)
color_1_618 = input(#2962ff, "", inline = "Level5", display = display.none, active = show_1_618)
show_1_65 = input(false, "", inline = "Level5", display = display.none)
value_1_65 = input(1.65, "", inline = "Level5", display = display.none, active = show_1_65)
color_1_65 = input(#2962ff, "", inline = "Level5", display = display.none, active = show_1_65)
show_2_618 = input(true, "", inline = "Level6", display = display.none)
value_2_618 = input(2.618, "", inline = "Level6", display = display.none, active = show_2_618)
color_2_618 = input(#f44336, "", inline = "Level6", display = display.none, active = show_2_618)
show_2_65 = input(false, "", inline = "Level6", display = display.none)
value_2_65 = input(2.65, "", inline = "Level6", display = display.none, active = show_2_65)
color_2_65 = input(#f44336, "", inline = "Level6", display = display.none, active = show_2_65)
show_3_618 = input(true, "", inline = "Level7", display = display.none)
value_3_618 = input(3.618, "", inline = "Level7", display = display.none, active = show_3_618)
color_3_618 = input(#9c27b0, "", inline = "Level7", display = display.none, active = show_3_618)
show_3_65 = input(false, "", inline = "Level7", display = display.none)
value_3_65 = input(3.65, "", inline = "Level7", display = display.none, active = show_3_65)
color_3_65 = input(#9c27b0, "", inline = "Level7", display = display.none, active = show_3_65)
show_4_236 = input(true, "", inline = "Level8", display = display.none)
value_4_236 = input(4.236, "", inline = "Level8", display = display.none, active = show_4_236)
color_4_236 = input(#e91e63, "", inline = "Level8", display = display.none, active = show_4_236)
show_4_618 = input(false, "", inline = "Level8", display = display.none)
value_4_618 = input(4.618, "", inline = "Level8", display = display.none, active = show_4_618)
color_4_618 = input(#81c784, "", inline = "Level8", display = display.none, active = show_4_618)
show_neg_0_236 = input(false, "", inline = "Level9", display = display.none)
value_neg_0_236 = input(-0.236, "", inline = "Level9", display = display.none, active = show_neg_0_236)
color_neg_0_236 = input(#f44336, "", inline = "Level9", display = display.none, active = show_neg_0_236)
show_neg_0_382 = input(false, "", inline = "Level9", display = display.none)
value_neg_0_382 = input(-0.382, "", inline = "Level9", display = display.none, active = show_neg_0_382)
color_neg_0_382 = input(#81c784, "", inline = "Level9", display = display.none, active = show_neg_0_382)
show_neg_0_618 = input(false, "", inline = "Level10", display = display.none)
value_neg_0_618 = input(-0.618, "", inline = "Level10", display = display.none, active = show_neg_0_618)
color_neg_0_618 = input(#009688, "", inline = "Level10", display = display.none, active = show_neg_0_618)
show_neg_0_65 = input(false, "", inline = "Level10", display = display.none)
value_neg_0_65 = input(-0.65, "", inline = "Level10", display = display.none, active = show_neg_0_65)
color_neg_0_65 = input(#009688, "", inline = "Level10", display = display.none, active = show_neg_0_65)
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
The script calculates pivot points based on a user-defined depth setting and tracks significant swing points using dynamic chart points and arrays. It draws horizontal extension lines, price values, and ratio labels directly on the price chart. Inputs allow users to adjust depth sensitivity, line extensions, level toggles, custom ratio values, label formatting, and background transparency. Because the pivot arrays are evaluated and drawn on the latest bar, levels dynamically adjust as new price extremes develop.
Automatic pivot point detection based on customizable depth
Customizable Fibonacci extension level ratios and colors
Extendable line directions with left and right toggles
Configurable level value formatting, label positions, and transparency
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Depth
int
10
depth
Reverse
bool
false
reverse
Extend Left | Extend Right
bool
false
extendLeft
extendRight
bool
true
extendRight
Show Prices
bool
true
prices
Show Levels
bool
true
levels
levelsFormat
string
Values
levelsFormat
Labels Position
string
Left
labelsPosition
Background Transparency
int
85
backgroundTransparency
show_0
bool
true
show_0
value_0
int
0
value_0
color_0
color
#787b86
color_0
Show all 40 settings
Setting
Type
Default
Variable
show_0_236
bool
true
show_0_236
value_0_236
float
0.236
value_0_236
color_0_236
color
#f44336
color_0_236
show_0_382
bool
true
show_0_382
value_0_382
float
0.382
value_0_382
color_0_382
color
#81c784
color_0_382
show_0_5
bool
true
show_0_5
value_0_5
float
0.5
value_0_5
color_0_5
color
#4caf50
color_0_5
show_0_618
bool
true
show_0_618
value_0_618
float
0.618
value_0_618
color_0_618
color
#009688
color_0_618
show_0_65
bool
false
show_0_65
value_0_65
float
0.65
value_0_65
color_0_65
color
#009688
color_0_65
show_0_786
bool
true
show_0_786
value_0_786
float
0.786
value_0_786
color_0_786
color
#64b5f6
color_0_786
show_1
bool
true
show_1
value_1
int
1
value_1
color_1
color
#787b86
color_1
show_1_272
bool
false
show_1_272
value_1_272
float
1.272
value_1_272
color_1_272
color
#81c784
color_1_272
show_1_414
bool
false
show_1_414
value_1_414
float
1.414
value_1_414
color_1_414
color
#f44336
color_1_414
show_1_618
bool
true
show_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.
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
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
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
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
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
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