本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Impulse-Aware Fibonacci", shorttitle="Impulse Fib", overlay=true, max_lines_count=50, max_labels_count=50, max_boxes_count=5)
// Click the start and the end of an impulse. The tool draws a standard Fibonacci retracement and classifies the impulse
// by its size (in ATR) and its duration (in bars). Each level shows how often price reached it after impulses of that type,
// and a shaded zone shows where retracements of that type usually end (25th to 75th percentile, median line).
// Statistics: ~4,700 impulses (zigzag 5/5 pivots, legs >= 3 ATR) on TradingView 15m data, 7 symbols (FX majors, gold, silver, WTI),
// Apr-Oct 2026, including retracements that went past the start of the impulse (depth > 1).
// They describe typical retracement depth only: no level acted as a price magnet in those tests.
tA = input.time(0, "Start", confirm = true, inline = "the start of the impulse")
pA = input.price(0, "", confirm = true, inline = "the start of the impulse")
tB = input.time(0, "End", confirm = true, inline = "the end of the impulse")
pB = input.price(0, "", confirm = true, inline = "the end of the impulse")
sizeCut = input.float(4.8, "Long impulse from (ATR)", minval = 0.5, step = 0.1, group = "Classification", tooltip = "Median impulse size in the statistics.")
barsCut = input.int(10, "Fast impulse up to (bars)", minval = 1, group = "Classification", tooltip = "Median impulse duration in the statistics.")
atrLen = input.int(14, "ATR length", minval = 1, group = "Classification")
showZone = input.bool(true, "Show typical end zone", group = "Display")
showPct = input.bool(true, "Show % reached on each level", group = "Display")
extRight = input.bool(true, "Extend lines right", group = "Display")
colLvl = input.color(#787B86, "Levels", group = "Colors")
colStar = input.color(#FF9800, "Closest level to the typical end", group = "Colors")
colZone = input.color(color.new(#FF9800, 85), "Typical end zone", group = "Colors")
// Statistics per impulse type: [p25, median, p75] depth and % of retracements that reached 0.236 / 0.382 / 0.5 / 0.618 / 0.786 / 1
// Type order: 0 = long and slow, 1 = long and fast, 2 = short and slow, 3 = short and fast
LEVELS = array.from(0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
KINDS = array.from("Long and slow", "Long and fast", "Short and slow", "Short and fast")
QUART = array.from(0.368, 0.531, 0.828, 0.557, 0.801, 1.133, 0.535, 0.814, 1.331, 0.720, 1.044, 1.593)
REACH = array.from(92.5, 72.8, 53.5, 41.2, 27.9, 16.6, 97.8, 91.5, 82.2, 68.2, 51.6, 32.9, 98.2, 88.6, 77.9, 67.6, 52.3, 39.5, 99.5, 96.2, 91.6, 83.6, 69.1, 52.9)
atr = ta.atr(atrLen)
t1 = math.min(tA, tB)
t2 = math.max(tA, tB)
bull = (tA <= tB ? pB > pA : pA > pB)
var int i1 = na
var int i2 = na
var float atr1 = na
var float legHi = na
var float legLo = na
if na(i1) and time >= t1
i1 := bar_index
atr1 := atr
if time >= t1 and time <= t2
i2 := bar_index
legHi := na(legHi) ? high : math.max(legHi, high)
legLo := na(legLo) ? low : math.min(legLo, low)
var lines = array.new<line>()
var labels = array.new<label>()
var box zone = na
levelPx(float endPx, float startPx, float r) => endPx - (endPx - startPx) * r
if barstate.islast
for l in lines
l.delete()
for lb in labels
lb.delete()
lines.clear()
labels.clear()
zone.delete()
if not na(i1) and not na(i2) and not na(atr1) and atr1 > 0 and legHi > legLo
span = legHi - legLo
endPx = bull ? legHi : legLo
startPx = bull ? legLo : legHi
sizeAtr = span / atr1
nBars = i2 - i1
isLong = sizeAtr >= sizeCut
isFast = nBars <= barsCut
k = isLong ? (isFast ? 1 : 0) : (isFast ? 3 : 2)
kind = KINDS.get(k)
q25 = QUART.get(k * 3)
med = QUART.get(k * 3 + 1)
q75 = QUART.get(k * 3 + 2)
x2 = extRight ? time + 20 * timeframe.in_seconds() * 1000 : t2
// closest standard level to the typical end (median depth)
best = 0
for i = 1 to 5
if math.abs(LEVELS.get(i) - med) < math.abs(LEVELS.get(best) - med)
best := i
if showZone
zone := box.new(t2, levelPx(endPx, startPx, q25), x2, levelPx(endPx, startPx, q75), xloc = xloc.bar_time, border_color = color(na), bgcolor = colZone)
yMed = levelPx(endPx, startPx, med)
lines.push(line.new(t2, yMed, x2, yMed, xloc = xloc.bar_time, color = colStar, style = line.style_dashed))
labels.push(label.new(x2, yMed, "Typical end " + str.tostring(med, "#.###"), xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = colStar, size = size.small))
lines.push(line.new(t1, endPx, x2, endPx, xloc = xloc.bar_time, color = colLvl, width = 2))
labels.push(label.new(t1, endPx, "0 (" + str.tostring(endPx, format.mintick) + ")", xloc = xloc.bar_time, style = label.style_label_right, color = color(na), textcolor = colLvl, size = size.small))
for i = 0 to 5
r = LEVELS.get(i)
y = levelPx(endPx, startPx, r)
col = i == best ? colStar : colLvl
txt = (i == best ? "★ " : "") + str.tostring(r) + " (" + str.tostring(y, format.mintick) + ")" + (showPct ? " reached " + str.tostring(REACH.get(k * 6 + i), "#") + " %" : "")
lines.push(line.new(t1, y, x2, y, xloc = xloc.bar_time, color = col, width = i == best or i == 5 ? 2 : 1))
labels.push(label.new(t1, y, txt, xloc = xloc.bar_time, style = label.style_label_right, color = color(na), textcolor = col, size = size.small))
head = kind + " impulse · " + str.tostring(sizeAtr, "#.#") + " ATR in " + str.tostring(nBars) + " bars"
labels.push(label.new(t1, endPx, head, xloc = xloc.bar_time, style = bull ? label.style_label_lower_left : label.style_label_upper_left, color = color.new(colStar, 85), textcolor = colStar, size = size.normal))