本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ATH Fib Retracement ($0 base)", shorttitle = "ATH Fib", overlay = true, max_lines_count = 20, max_labels_count = 20)
// ── Inputs ──
show0 = input.bool(true, "ATH (0%)", group = "Levels")
show1 = input.bool(true, "Level 1", inline = "l1", group = "Levels")
pct1 = input.float(23.6, "%", minval = 0, maxval = 100, step = 0.1, inline = "l1", group = "Levels")
show2 = input.bool(true, "Level 2", inline = "l2", group = "Levels")
pct2 = input.float(38.2, "%", minval = 0, maxval = 100, step = 0.1, inline = "l2", group = "Levels")
show3 = input.bool(true, "Level 3", inline = "l3", group = "Levels")
pct3 = input.float(50.0, "%", minval = 0, maxval = 100, step = 0.1, inline = "l3", group = "Levels")
show4 = input.bool(true, "Level 4", inline = "l4", group = "Levels")
pct4 = input.float(61.8, "%", minval = 0, maxval = 100, step = 0.1, inline = "l4", group = "Levels")
show5 = input.bool(true, "Level 5", inline = "l5", group = "Levels")
pct5 = input.float(78.6, "%", minval = 0, maxval = 100, step = 0.1, inline = "l5", group = "Levels")
showTbl = input.bool(true, "Show info table", group = "Display")
// ── All-time high: monthly history + this chart's own bars ──
athCalc() =>
var float m = na
m := na(m) ? high : math.max(m, high)
m
htfAth = request.security(syminfo.tickerid, "M", athCalc(), lookahead = barmerge.lookahead_off)
localAth = athCalc()
ath = math.max(nz(htfAth, localAth), localAth)
dd = 1 - close / ath // drawdown from ATH (0 = at ATH)
// Hidden values (useful later for the Pine Screener / Data Window)
plot(ath, "ATH", display = display.data_window)
plot(dd * 100, "Drawdown %", display = display.data_window)
// ── Draw levels on the last bar ──
levels = array.from(0.0, pct1 / 100, pct2 / 100, pct3 / 100, pct4 / 100, pct5 / 100)
shows = array.from(show0, show1, show2, show3, show4, show5)
cols = array.from(color.gray, color.red, color.orange, color.yellow, color.green, color.teal)
var array<line> lns = array.new<line>()
var array<label> lbs = array.new<label>()
if barstate.islast
for l in lns
line.delete(l)
for lb in lbs
label.delete(lb)
array.clear(lns)
array.clear(lbs)
for i = 0 to array.size(levels) - 1
if array.get(shows, i)
r = array.get(levels, i)
p = ath * (1 - r)
c = array.get(cols, i)
array.push(lns, line.new(bar_index - 1, p, bar_index, p, extend = extend.both, color = c, style = line.style_dashed, width = 1))
array.push(lbs, label.new(bar_index + 3, p, str.format("{0,number,0.0}% {1}", r * 100, str.tostring(p, format.mintick)), style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small))
// ── Info table ──
var table tbl = table.new(position.top_right, 2, 4, bgcolor = color.new(color.black, 30), border_width = 1)
if barstate.islast and showTbl
zone = "Above all levels"
deepest = 0.0
for i = 1 to array.size(levels) - 1
lv = array.get(levels, i)
if dd >= lv and lv >= deepest
deepest := lv
zone := "Past " + str.format("{0,number,0.0}%", lv * 100)
table.cell(tbl, 0, 0, "ATH", text_color = color.white, text_size = size.small)
table.cell(tbl, 1, 0, str.tostring(ath, format.mintick), text_color = color.white, text_size = size.small)
table.cell(tbl, 0, 1, "Price", text_color = color.white, text_size = size.small)
table.cell(tbl, 1, 1, str.tostring(close, format.mintick), text_color = color.white, text_size = size.small)
table.cell(tbl, 0, 2, "Off ATH", text_color = color.white, text_size = size.small)
table.cell(tbl, 1, 2, str.format("{0,number,0.0}%", dd * 100), text_color = color.white, text_size = size.small)
table.cell(tbl, 0, 3, "Zone", text_color = color.white, text_size = size.small)
table.cell(tbl, 1, 3, zone, text_color = color.white, text_size = size.small)