本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Liquidity Aging", overlay=true, max_labels_count=500, max_lines_count=500)
// --- Inputs ---
leftLen = input.int(5, "Swing Left Bars", minval=1)
rightLen = input.int(5, "Swing Right Bars", minval=1)
useClose = input.bool(false, "Break requires a close (off = wick is enough)")
minAge = input.int(20, "Min. wait for label (bars)", minval=1)
urgentAge = input.int(200, "URGENT threshold (bars)", minval=1)
maxAge = input.int(1000, "Max. level lifetime (bars) - older levels are removed", minval=50)
maxLevels = input.int(50, "Max. tracked levels", minval=1, maxval=200)
oldAge = input.int(100, "Red threshold (bars)", minval=10)
showLive = input.bool(true, "Show pending levels as lines")
showLong = input.bool(true, "Long (high taken) labels")
showShort = input.bool(true, "Short (low taken) labels")
showUrgent = input.bool(true, "Show URGENT marker")
doAlert = input.bool(false, "Send alert when a label appears")
urgentOnly = input.bool(false, "Alert only for URGENT")
showDash = input.bool(true, "Show dashboard (top right)")
// --- Level lists ---
var array<float> hiP = array.new<float>()
var array<int> hiI = array.new<int>()
var array<line> hiL = array.new<line>()
var array<float> loP = array.new<float>()
var array<int> loI = array.new<int>()
var array<line> loL = array.new<line>()
var int lastLblBar = na
// --- Scan for broken levels: [oldest idx, count, price of oldest level] ---
// Levels older than maxAge are removed silently without being counted.
scan(array<float> pr, array<int> ix, array<line> ln, bool isHigh) =>
int oldest = na
float oldestLvl = na
int cnt = 0
int n = array.size(pr)
if n > 0
for i = n - 1 to 0
lvl = array.get(pr, i)
idx = array.get(ix, i)
l = array.get(ln, i)
// Expired level: remove silently
if bar_index - idx > maxAge
if not na(l)
line.delete(l)
array.remove(pr, i)
array.remove(ix, i)
array.remove(ln, i)
continue
src = useClose ? close : (isHigh ? high : low)
hit = isHigh ? src > lvl : src < lvl
if hit
cnt += 1
if na(oldest) or idx < oldest
oldest := idx
oldestLvl := lvl
if not na(l)
line.set_x2(l, bar_index)
array.remove(pr, i)
array.remove(ix, i)
array.remove(ln, i)
[oldest, cnt, oldestLvl]
// --- Add a new level ---
addLevel(array<float> pr, array<int> ix, array<line> ln, float price, int pIdx) =>
line nl = na
if showLive
nl := line.new(pIdx, price, bar_index, price, color=color.new(color.gray, 60), style=line.style_dotted)
array.push(pr, price)
array.push(ix, pIdx)
array.push(ln, nl)
if array.size(pr) > maxLevels
o = array.get(ln, 0)
if not na(o)
line.delete(o)
array.shift(pr)
array.shift(ix)
array.shift(ln)
// --- Extend and recolor pending lines ---
refresh(array<int> ix, array<line> ln, color baseCol) =>
int n = array.size(ln)
if showLive and n > 0
for i = 0 to n - 1
l = array.get(ln, i)
if not na(l)
age = bar_index - array.get(ix, i)
transp = 80 - math.min(age / float(oldAge), 1.0) * 70
line.set_x2(l, bar_index)
line.set_color(l, color.new(baseCol, transp))
ph = ta.pivothigh(high, leftLen, rightLen)
pl = ta.pivotlow(low, leftLen, rightLen)
ageColor(int age) => age >= oldAge ? color.red : age >= oldAge / 2 ? color.orange : color.green
if barstate.isconfirmed
// LONG: highs taken -> label BELOW the candle
[oH, cH, lvH] = scan(hiP, hiI, hiL, true)
if showLong and cH > 0
age = bar_index - oH
urgent = showUrgent and age > urgentAge
if age >= minAge or urgent
lastLblBar := bar_index
sweep = close < lvH
base = (sweep ? "S" : "") + str.tostring(age) + (cH > 1 ? " (" + str.tostring(cH) + ")" : "")
txt = urgent ? "URGENT\n" + base : base
label.new(bar_index, low, txt, style=label.style_label_up,
color=urgent ? color.new(color.purple, 0) : color.new(ageColor(age), 15),
textcolor=color.white, size=urgent ? size.normal : size.small)
if doAlert and (not urgentOnly or urgent)
alert((urgent ? "URGENT " : "") + "LONG liquidity taken: " + base, alert.freq_once_per_bar_close)
// SHORT: lows taken -> label ABOVE the candle
[oL, cL, lvL] = scan(loP, loI, loL, false)
if showShort and cL > 0
age = bar_index - oL
urgent = showUrgent and age > urgentAge
if age >= minAge or urgent
lastLblBar := bar_index
sweep = close > lvL
base = (sweep ? "S" : "") + str.tostring(age) + (cL > 1 ? " (" + str.tostring(cL) + ")" : "")
txt = urgent ? "URGENT\n" + base : base
label.new(bar_index, high, txt, style=label.style_label_down,
color=urgent ? color.new(color.purple, 0) : color.new(ageColor(age), 15),
textcolor=color.white, size=urgent ? size.normal : size.small)
if doAlert and (not urgentOnly or urgent)
alert((urgent ? "URGENT " : "") + "SHORT liquidity taken: " + base, alert.freq_once_per_bar_close)
// New pivots
if not na(ph)
addLevel(hiP, hiI, hiL, ph, bar_index - rightLen)
if not na(pl)
addLevel(loP, loI, loL, pl, bar_index - rightLen)
refresh(hiI, hiL, color.red)
refresh(loI, loL, color.teal)
// --- Dashboard (top right) ---
// Pending level closest to price: [distance %, age]
nearest(array<float> pr, array<int> ix, bool above) =>
float best = na
int bestAge = na
int n = array.size(pr)
if n > 0
for i = 0 to n - 1
lvl = array.get(pr, i)
d = above ? lvl - close : close - lvl
if d >= 0 and (na(best) or d < best)
best := d
bestAge := bar_index - array.get(ix, i)
float pct = na(best) ? na : best / close * 100.0
[pct, bestAge]
var table dash = table.new(position.top_right, 3, 5, border_width=1,
frame_color=color.new(color.gray, 50), frame_width=1,
border_color=color.new(color.gray, 70))
if showDash and barstate.islast
nH = array.size(hiP)
nL = array.size(loP)
oldH = nH > 0 ? bar_index - array.min(hiI) : na
oldL = nL > 0 ? bar_index - array.min(loI) : na
[pctH, ageH] = nearest(hiP, hiI, true)
[pctL, ageL] = nearest(loP, loI, false)
hdrBg = color.new(color.gray, 30)
bodyBg = color.new(color.gray, 85)
fg = chart.fg_color
table.cell(dash, 0, 0, "LIQUIDITY", text_color=color.white, bgcolor=hdrBg, text_size=size.small)
table.cell(dash, 1, 0, "Highs ▲", text_color=color.white, bgcolor=color.new(color.red, 30), text_size=size.small)
table.cell(dash, 2, 0, "Lows ▼", text_color=color.white, bgcolor=color.new(color.teal, 30), text_size=size.small)
table.cell(dash, 0, 1, "Pending", text_color=fg, bgcolor=bodyBg, text_size=size.small, text_halign=text.align_left)
table.cell(dash, 1, 1, str.tostring(nH), text_color=fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 2, 1, str.tostring(nL), text_color=fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 0, 2, "Oldest (bars)", text_color=fg, bgcolor=bodyBg, text_size=size.small, text_halign=text.align_left)
table.cell(dash, 1, 2, na(oldH) ? "-" : str.tostring(oldH), text_color=(not na(oldH) and oldH > urgentAge) ? color.purple : fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 2, 2, na(oldL) ? "-" : str.tostring(oldL), text_color=(not na(oldL) and oldL > urgentAge) ? color.purple : fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 0, 3, "Nearest", text_color=fg, bgcolor=bodyBg, text_size=size.small, text_halign=text.align_left)
table.cell(dash, 1, 3, na(pctH) ? "-" : str.tostring(pctH, "#.##") + "% (" + str.tostring(ageH) + ")", text_color=fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 2, 3, na(pctL) ? "-" : str.tostring(pctL, "#.##") + "% (" + str.tostring(ageL) + ")", text_color=fg, bgcolor=bodyBg, text_size=size.small)
table.cell(dash, 0, 4, "Last label", text_color=fg, bgcolor=bodyBg, text_size=size.small, text_halign=text.align_left)
table.cell(dash, 1, 4, na(lastLblBar) ? "-" : str.tostring(bar_index - lastLblBar) + " bars ago", text_color=fg, bgcolor=bodyBg, text_size=size.small)
table.merge_cells(dash, 1, 4, 2, 4)