OPEN-SOURCE SCRIPT
Auto Fib Retracement Advanced

//version=5
indicator("Auto Fib Retracement Advanced", overlay=true, max_lines_count=500) // Increase max_lines_count
leftBars = input.int(10, "Pivot Left Bars")
rightBars = input.int(10, "Pivot Right Bars")
extendRight = input.bool(true, "Extend Lines Right")
swingHigh = ta.pivothigh(high, leftBars, rightBars)
swingLow = ta.pivotlow(low, leftBars, rightBars)
var float lastHighPrice = na
var int lastHighBar = na
var float lastLowPrice = na
var int lastLowBar = na
// Arrays to store line IDs for management
var lines = array.new_line()
levels_values = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
// Update pivot points and redraw lines when a new pivot is confirmed
if not na(swingHigh) or not na(swingLow)
if not na(swingHigh)
lastHighPrice := swingHigh
lastHighBar := bar_index
if not na(swingLow)
lastLowPrice := swingLow
lastLowBar := bar_index
// Delete existing lines before drawing new ones
for i = 0 to array.size(lines) - 1
line.delete(array.get(lines, i))
array.clear(lines)
if not na(lastHighPrice) and not na(lastLowPrice)
isUptrend = lastHighPrice > lastLowPrice
fibRange = math.abs(lastHighPrice - lastLowPrice)
// Draw new lines
for i = 0 to array.size(levels_values) - 1
levelValue = array.get(levels_values, i)
priceLevel = isUptrend ? lastLowPrice + fibRange * levelValue : lastHighPrice - fibRange * levelValue
// Use line.new to create persistent horizontal lines
newLine = line.new(x1=lastLowBar, y1=priceLevel, x2=bar_index + (extendRight ? 500 : 0), y2=priceLevel, color=color.gray, style=line.style_dashed)
array.push(lines, newLine)
indicator("Auto Fib Retracement Advanced", overlay=true, max_lines_count=500) // Increase max_lines_count
leftBars = input.int(10, "Pivot Left Bars")
rightBars = input.int(10, "Pivot Right Bars")
extendRight = input.bool(true, "Extend Lines Right")
swingHigh = ta.pivothigh(high, leftBars, rightBars)
swingLow = ta.pivotlow(low, leftBars, rightBars)
var float lastHighPrice = na
var int lastHighBar = na
var float lastLowPrice = na
var int lastLowBar = na
// Arrays to store line IDs for management
var lines = array.new_line()
levels_values = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
// Update pivot points and redraw lines when a new pivot is confirmed
if not na(swingHigh) or not na(swingLow)
if not na(swingHigh)
lastHighPrice := swingHigh
lastHighBar := bar_index
if not na(swingLow)
lastLowPrice := swingLow
lastLowBar := bar_index
// Delete existing lines before drawing new ones
for i = 0 to array.size(lines) - 1
line.delete(array.get(lines, i))
array.clear(lines)
if not na(lastHighPrice) and not na(lastLowPrice)
isUptrend = lastHighPrice > lastLowPrice
fibRange = math.abs(lastHighPrice - lastLowPrice)
// Draw new lines
for i = 0 to array.size(levels_values) - 1
levelValue = array.get(levels_values, i)
priceLevel = isUptrend ? lastLowPrice + fibRange * levelValue : lastHighPrice - fibRange * levelValue
// Use line.new to create persistent horizontal lines
newLine = line.new(x1=lastLowBar, y1=priceLevel, x2=bar_index + (extendRight ? 500 : 0), y2=priceLevel, color=color.gray, style=line.style_dashed)
array.push(lines, newLine)
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.