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 abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que la republicación del código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como asesoramiento o recomendaciones financieras, de inversión, de trading o de otro tipo proporcionadas o respaldadas por TradingView. Más información en Condiciones de uso.
Script de código abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que la republicación del código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como asesoramiento o recomendaciones financieras, de inversión, de trading o de otro tipo proporcionadas o respaldadas por TradingView. Más información en Condiciones de uso.