本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Trend-Based Fibonacci Extension", shorttitle="Fib Ext", overlay=true, max_lines_count=500, max_labels_count=500)
// ═════════════════════════ DATE RANGE ═════════════════════════
gAuto = "📅 Date Range"
inStart = input.time(timestamp("1 Jan 2026 00:00 +0300"), "Start Date", group=gAuto, tooltip="First bar of the range used to detect the A-B-C swing. If left empty, the last 100 bars are used.")
inEnd = input.time(timestamp("30 Nov 2026 00:00 +0300"), "End Date", group=gAuto, tooltip="Last bar of the range used to detect the A-B-C swing. If left empty, the current (last) bar is used.")
tStart = na(inStart) ? time[math.min(100, bar_index)] : inStart
tEnd = na(inEnd) ? time : inEnd
tStartOk = math.min(tStart, tEnd)
tEndOk = math.max(tStart, tEnd)
// ═════════════════════════════ VISUAL SETTINGS ═════════════════════════════
gVis = "🎨 Visual"
lineColor = input.color(color.new(color.red, 0), "Level Line Color", group=gVis, tooltip="Color of the Fibonacci extension level lines.")
lineWidth = input.int(2, "Line Width", minval=1, maxval=5, group=gVis, tooltip="Thickness of the level lines and the A-B-C path (1-5).")
lineStyle = input.string("Solid", "Line Style", options=["Solid","Dashed","Dotted"], group=gVis, tooltip="Style applied to the horizontal Fib level lines.")
extendBars = input.int(20, "Extend Right (bars)", minval=0, maxval=500, group=gVis, tooltip="How many bars to the right of point B the Fib levels are projected.")
upLblColor = input.color(color.new(color.teal, 0), "Label Color — Uptrend", group=gVis, tooltip="Background color of the Fib price labels when the trend is UP (A below B).")
downLblColor = input.color(color.new(color.red, 0), "Label Color — Downtrend", group=gVis, tooltip="Background color of the Fib price labels when the trend is DOWN (A above B).")
showLabels = input.bool(true, "Show Price Labels", group=gVis, tooltip="Displays the ratio and price value at the right end of each level line.")
showPath = input.bool(true, "Show A-B-C Trend Line", group=gVis, tooltip="Draws the A→B impulse leg and the B→C retracement leg.")
showTable = input.bool(true, "Show Info Table", group=gVis, tooltip="Reserved for an on-chart info table.")
// ═════════════ FIB LEVELS (TradingView standard set) ═════════════
gLvl = "📊 Fib Levels"
s0 = input.bool(true, "0", group=gLvl, inline="r1", tooltip="Target = C + (B - A) × ratio. Checked levels are drawn on the chart.")
s236 = input.bool(true, "0.236", group=gLvl, inline="r1")
s382 = input.bool(true, "0.382", group=gLvl, inline="r1")
s500 = input.bool(true, "0.5", group=gLvl, inline="r1")
s618 = input.bool(true, "0.618", group=gLvl, inline="r2")
s786 = input.bool(true, "0.786", group=gLvl, inline="r2")
s1000 = input.bool(true, "1", group=gLvl, inline="r2")
s1272 = input.bool(false, "1.272", group=gLvl, inline="r2")
s1414 = input.bool(false, "1.414", group=gLvl, inline="r3")
s1618 = input.bool(false, "1.618", group=gLvl, inline="r3")
s2000 = input.bool(false, "2", group=gLvl, inline="r3")
s2272 = input.bool(false, "2.272", group=gLvl, inline="r3")
s2414 = input.bool(false, "2.414", group=gLvl, inline="r4")
s2618 = input.bool(false, "2.618", group=gLvl, inline="r4")
s3000 = input.bool(false, "3", group=gLvl, inline="r4")
s3272 = input.bool(false, "3.272", group=gLvl, inline="r4")
s3414 = input.bool(false, "3.414", group=gLvl, inline="r5")
s4000 = input.bool(false, "4", group=gLvl, inline="r5")
s4236 = input.bool(false, "4.236", group=gLvl, inline="r5")
fibRatios = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0, 1.272, 1.414, 1.618, 2.0, 2.272, 2.414, 2.618, 3.0, 3.272, 3.414, 4.0, 4.236)
// ═══════════════════════════════ FUNCTIONS ═══════════════════════════════
// Calculates Fibonacci extension target prices from A-B-C points
// Formula: Target = C + (B - A) × ratio
f_fibExtension(float a, float b, float c) =>
array<float> res = array.new<float>()
float fibRange = b - a
for r in fibRatios
array.push(res, c + fibRange * r)
res
// Line style conversion
f_lineStyle(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
// ═══════════════ EXTREMES WITHIN THE DATE RANGE ══════════════
var float rHigh = na
var int rHighBar = na
var float rLow = na
var int rLowBar = na
inRange = time >= tStartOk and time <= tEndOk
if inRange
if na(rHigh) or high > rHigh
rHigh := high
rHighBar := bar_index
if na(rLow) or low < rLow
rLow := low
rLowBar := bar_index
// ═════════════ LOCKED A-B-C (UPDATE MANAGEMENT) ═════════════
var float vA = na
var float vB = na
var float vC = na
var int vAbar = na
var int vBbar = na
var int vCbar = na
var bool cLocked = false
// ═══════════════════════ OBJECT CLEANUP MANAGEMENT ═══════════════════════════
var array<line> linesArr = array.new<line>()
var array<label> labelsArr = array.new<label>()
f_clear() =>
for l in linesArr
line.delete(l)
array.clear(linesArr)
for lb in labelsArr
label.delete(lb)
array.clear(labelsArr)
// ═══════════════════════════ MAIN DRAWING (AUTOMATIC) ════════════════════════════
if barstate.islast
f_clear()
// ─── Candidate A/B/C calculation ───
if not na(rHigh) and not na(rLow) and not na(rHighBar) and not na(rLowBar)
bool upTrend = rLowBar < rHighBar
float aP = upTrend ? rLow : rHigh
float bP = upTrend ? rHigh : rLow
int aBar = upTrend ? rLowBar : rHighBar
int bBar = upTrend ? rHighBar : rLowBar
// Find the first bar after B whose CLOSE crosses B
int maxScan = math.min(bar_index, 9999)
int bOff = bar_index - bBar
int crossOff = na
for i = 0 to maxScan
if i >= bOff
break
bool crossed = upTrend ? close[i] > bP : close[i] < bP
if crossed
crossOff := i
break
// If a cross exists: extreme of the region between B and the cross bar = C
// If no cross: lowest low / highest high between B and the last bar = C
int endOff = na(crossOff) ? 0 : crossOff
float bestVal = upTrend ? low[bOff] : high[bOff]
int bestOff = bOff
for i = endOff to bOff
if upTrend ? low[i] < bestVal : high[i] > bestVal
bestVal := upTrend ? low[i] : high[i]
bestOff := i
float cP = bestVal
int cBar = bar_index - bestOff
bool crossed = not na(crossOff)
// ─── LOCKING RULES ───
bool newB = na(vBbar) or bBar != vBbar
if newB
vA := aP
vB := bP
vC := cP
vAbar := aBar
vBbar := bBar
vCbar := cBar
cLocked := crossed
else if not cLocked
vC := cP
vCbar := cBar
cLocked := crossed
// ─── DRAWING (with locked values) ───
if not na(vA) and not na(vB) and not na(vC) and not na(vAbar) and not na(vBbar)
bool upTrend = vA < vB
int xRight = bar_index + extendBars
color lblCol = upTrend ? upLblColor : downLblColor
// A-B-C trend line (optional)
if showPath
array.push(linesArr, line.new(vAbar, vA, vBbar, vB, color=color.new(lineColor, 30), width=lineWidth, style=line.style_solid))
array.push(linesArr, line.new(vBbar, vB, vCbar, vC, color=color.new(lineColor, 55), width=lineWidth, style=line.style_dashed))
// Fib levels
array<bool> vis = array.from(s0, s236, s382, s500, s618, s786, s1000, s1272, s1414, s1618, s2000, s2272, s2414, s2618, s3000, s3272, s3414, s4000, s4236)
array<float> levels = f_fibExtension(vA, vB, vC)
for i = 0 to array.size(levels) - 1
if array.get(vis, i)
float lvl = array.get(levels, i)
float ratio = array.get(fibRatios, i)
array.push(linesArr, line.new(vBbar, lvl, xRight, lvl, color=lineColor, width=lineWidth, style=f_lineStyle(lineStyle)))
if showLabels
array.push(labelsArr, label.new(xRight, lvl, str.tostring(ratio, "#.###") + "\n" + str.tostring(lvl, format.mintick), style=label.style_label_left, color=color.new(lblCol, 15), textcolor=color.white, size=size.small, textalign=text.align_left))
// A, B, C point labels
array.push(labelsArr, label.new(vAbar, vA, "A", style=label.style_label_up, color=color.new(color.teal, 0), textcolor=color.white, size=size.normal))
array.push(labelsArr, label.new(vBbar, vB, "B", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.normal))
array.push(labelsArr, label.new(vCbar, vC, "C", style=upTrend ? label.style_label_up : label.style_label_down, color=color.new(color.orange, 0), textcolor=color.white, size=size.normal))
// ═════════════════════════ USAGE INSTRUCTIONS ═════════════════════════════
// 1. The drawing is created automatically when the script is added to the chart.
// 2. Levels are TradingView's standard Fib extension set (0, 0.236, 0.382,
// 0.5, 0.618, 0.786, 1, 1.272, 1.414, 1.618, 2, 2.272, 2.414, 2.618,
// 3, 3.272, 3.414, 4, 4.236). Enabled by default: the first 7 levels.
// 3. C = extreme of the region between B and the first bar whose CLOSE crosses B;
// if there is no cross, the lowest low / highest high between B and the last bar.
// 4. A/B/C are locked: all three are refreshed only when a new B forms.
// 5. Fib price labels are colored by trend direction: uptrend = "Label Color — Uptrend",
// downtrend = "Label Color — Downtrend".