Open-source scriptindicator/v1
Auto Fibonacci Retracement
Plot Fibonacci retracement levels from the latest confirmed alternating market swing.
#fibonacci#pivots#retracement
Description
Auto Fibonacci Retracement detects the latest confirmed alternating pivot pair and projects the 0, 0.236, 0.382, 0.5, 0.618, 0.786, and 1 levels. The 0.382–0.618 golden zone is highlighted automatically. Pivot depth, projection length, colors, and price labels are configurable.
Open-source script. Everyone can inspect the source code, use the script on a chart, and save an editable copy.
Release notes
Version history · 1 published version
Version 1CurrentChart updatedAuto Fibonacci Retracement · Aug 31, 2026
Source codempl-2.0View sourceHide source
// Draw retracement levels across the latest two confirmed alternating pivots.
indicator("Auto Fibonacci Retracement", { overlay: true, maxLines: 10, maxLabels: 10, maxLineFills: 2 });
const depth = input.int("Pivot depth", 8, { min: 2, max: 50 });
const projectionBars = input.int("Projection bars", 30, { min: 1, max: 200 });
const showPrices = input.bool("Show prices", true);
const baseColor = input.color("Level color", "#22d3ee");
const goldenColor = input.color("Golden zone color", "#f59e0b");
const highs = pivothigh(high, depth, depth);
const lows = pivotlow(low, depth, depth);
const pivots = [];
function acceptPivot(kind, index, price) {
const candidate = { kind, index, price };
const previous = pivots[pivots.length - 1];
if (!previous) {
pivots.push(candidate);
return;
}
if (previous.kind === kind) {
const moreExtreme = kind === "high" ? price >= previous.price : price <= previous.price;
if (moreExtreme) pivots[pivots.length - 1] = candidate;
return;
}
pivots.push(candidate);
}
for (let confirmation = 0; confirmation < close.length; confirmation++) {
const pivotIndex = confirmation - depth;
if (pivotIndex < 0) continue;
if (Number.isFinite(highs[confirmation])) acceptPivot("high", pivotIndex, highs[confirmation]);
if (Number.isFinite(lows[confirmation])) acceptPivot("low", pivotIndex, lows[confirmation]);
}
if (pivots.length >= 2) {
const start = pivots[pivots.length - 2];
const finish = pivots[pivots.length - 1];
const levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1];
const lines = [];
const right = close.length - 1 + projectionBars;
levels.forEach((level) => {
const price = start.price + (finish.price - start.price) * level;
const isGoldenBoundary = level === 0.382 || level === 0.618;
const id = line.new({
x1: start.index,
y1: price,
x2: right,
y2: price,
xloc: "index",
color: isGoldenBoundary ? goldenColor : baseColor,
width: isGoldenBoundary ? 2 : 1,
style: level === 0 || level === 1 ? "solid" : "dashed",
extend: "right",
});
lines.push(id);
label.new({
x: right,
y: price,
xloc: "index",
text: showPrices ? `${level.toFixed(3)} · ${price.toFixed(2)}` : level.toFixed(3),
position: "right",
color: "#0f172a",
textColor: isGoldenBoundary ? goldenColor : baseColor,
size: "small",
});
});
linefill.new(lines[2], lines[4], { color: goldenColor, opacity: 0.12 });
}
Community discussion
0 comments
No comments yet. Start the discussion.