本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ============================================================================
// Liquidity Magnet Predictor (LMP)
//
// Estimates which nearby liquidity pool price is most likely to be pulled
// toward, using:
// - Equal highs / equal lows (clustered swing pivots = resting stop liquidity)
// - Single swing highs / lows
// - Previous day high/low (PDH/PDL), previous week high/low (PWH/PWL)
// - Premarket high/low (needs Extended Trading Hours on the chart)
//
// Each pool gets a "magnet strength" = (type weight + confluence) x recency
// decay / (1 + distance in ATR / distance scale). The strongest pool above and
// the strongest below are compared to produce a relative pull.
//
// IMPORTANT
// - Liquidity is INFERRED from price structure. Pine Script cannot see order
// books, resting stops, or real order flow.
// - "Relative pull" is a heuristic score, NOT a probability or a forecast.
// - Educational tool only - not financial advice.
// ============================================================================
indicator("Liquidity Magnet Predictor", shorttitle="LMP", overlay=true, max_lines_count=500, max_labels_count=500)
// ============================== INPUTS =====================================
G1 = "Detection"
pivLen = input.int(5, "Swing pivot length", minval=2, maxval=30, group=G1)
tolAtr = input.float(0.15, "Equal high/low tolerance (x ATR)", minval=0.02, step=0.01, group=G1)
maxLevels = input.int(30, "Max tracked swing/equal levels", minval=5, maxval=100, group=G1)
maxAge = input.int(600, "Level expiry (bars)", minval=50, group=G1)
sweepClose = input.bool(false, "Sweep requires a CLOSE beyond the level (off = wick)", group=G1)
showSwept = input.bool(false, "Keep swept levels on chart (dotted gray)", group=G1)
G2 = "Previous-session levels"
showPD = input.bool(true, "Previous day high/low", group=G2)
showPW = input.bool(true, "Previous week high/low", group=G2)
showPM = input.bool(true, "Premarket high/low", group=G2)
G3 = "Magnet scoring"
wSwing = input.float(1.0, "Weight: single swing level", minval=0.1, step=0.1, group=G3)
wEqualStep = input.float(0.75, "Weight: added per extra equal touch", minval=0.0, step=0.05, group=G3)
wPD = input.float(2.0, "Weight: previous day high/low", minval=0.1, step=0.1, group=G3)
wPW = input.float(2.5, "Weight: previous week high/low", minval=0.1, step=0.1, group=G3)
wPM = input.float(1.5, "Weight: premarket high/low", minval=0.1, step=0.1, group=G3)
distScale = input.float(3.0, "Distance scale (ATR)", minval=0.5, step=0.5, group=G3, tooltip="Larger = distant pools keep more pull.")
decayBars = input.int(300, "Recency half-life (bars)", minval=20, group=G3)
useTilt = input.bool(true, "Trend tilt: x1.15 for pools on the EMA50 side of price", group=G3)
G4 = "Display"
showTable = input.bool(true, "Show dashboard", group=G4)
showLabels = input.bool(true, "Show magnet labels", group=G4)
showProj = input.bool(true, "Show projection arrow to predicted magnet", group=G4)
projBars = input.int(15, "Projection length (bars)", minval=3, maxval=100, group=G4)
// ============================== CALCULATIONS ===============================
atr = ta.atr(14)
ema50 = ta.ema(close, 50)
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
newDay = timeframe.change("D")
var float dHi = na
var float dLo = na
var float pmHigh = na
var float pmLow = na
var float rthHi = na
var float rthLo = na
if newDay
pmHigh := na
pmLow := na
rthHi := na
rthLo := na
dHi := (newDay or na(dHi)) ? high : math.max(dHi, high)
dLo := (newDay or na(dLo)) ? low : math.min(dLo, low)
if session.ispremarket
pmHigh := na(pmHigh) ? high : math.max(pmHigh, high)
pmLow := na(pmLow) ? low : math.min(pmLow, low)
else
rthHi := na(rthHi) ? high : math.max(rthHi, high)
rthLo := na(rthLo) ? low : math.min(rthLo, low)
// "Live" = not yet traded through
pdhLive = showPD and not na(pdh) and not (dHi > pdh)
pdlLive = showPD and not na(pdl) and not (dLo < pdl)
pwhLive = showPW and not na(pwh) and not (dHi > pwh)
pwlLive = showPW and not na(pwl) and not (dLo < pwl)
pmhLive = showPM and not na(pmHigh) and not session.ispremarket and not (rthHi > pmHigh)
pmlLive = showPM and not na(pmLow) and not session.ispremarket and not (rthLo < pmLow)
// ============================== LEVEL STORE ================================
var array<float> lvPrice = array.new<float>()
var array<int> lvBar = array.new<int>()
var array<int> lvTouch = array.new<int>()
var array<int> lvSide = array.new<int>() // +1 = highs (buy-side liquidity above), -1 = lows (sell-side below)
var array<line> lvLine = array.new<line>()
f_remove(int i, bool del) =>
if del
line.delete(array.get(lvLine, i))
array.remove(lvPrice, i)
array.remove(lvBar, i)
array.remove(lvTouch, i)
array.remove(lvSide, i)
array.remove(lvLine, i)
0
f_reg(float p, int side, float tol, int pbar) =>
bool merged = false
int n = array.size(lvPrice)
if n > 0
for i = 0 to n - 1
if not merged and array.get(lvSide, i) == side and math.abs(array.get(lvPrice, i) - p) <= tol
float np = side == 1 ? math.max(array.get(lvPrice, i), p) : math.min(array.get(lvPrice, i), p)
array.set(lvPrice, i, np)
array.set(lvTouch, i, array.get(lvTouch, i) + 1)
array.set(lvBar, i, pbar)
line l = array.get(lvLine, i)
line.set_y1(l, np)
line.set_y2(l, np)
line.set_width(l, 2)
line.set_style(l, line.style_solid)
line.set_color(l, side == 1 ? color.new(color.red, 0) : color.new(color.green, 0))
merged := true
if not merged
array.push(lvPrice, p)
array.push(lvBar, pbar)
array.push(lvTouch, 1)
array.push(lvSide, side)
line ln = line.new(pbar, p, bar_index, p, color=side == 1 ? color.new(color.red, 45) : color.new(color.green, 45), style=line.style_dashed, width=1)
array.push(lvLine, ln)
merged
f_sweepUp(float lvl) => not na(lvl) and high > lvl and high[1] <= lvl
f_sweepDn(float lvl) => not na(lvl) and low < lvl and low[1] >= lvl
f_grabUp(float lvl) => f_sweepUp(lvl) and close < lvl
f_grabDn(float lvl) => f_sweepDn(lvl) and close > lvl
// External-level sweeps (computed on every bar)
extUp = (showPD and f_sweepUp(pdh)) or (showPW and f_sweepUp(pwh)) or (showPM and not session.ispremarket and f_sweepUp(pmHigh))
extDn = (showPD and f_sweepDn(pdl)) or (showPW and f_sweepDn(pwl)) or (showPM and not session.ispremarket and f_sweepDn(pmLow))
extGrabUp = (showPD and f_grabUp(pdh)) or (showPW and f_grabUp(pwh)) or (showPM and not session.ispremarket and f_grabUp(pmHigh))
extGrabDn = (showPD and f_grabDn(pdl)) or (showPW and f_grabDn(pwl)) or (showPM and not session.ispremarket and f_grabDn(pmLow))
var string lastSweep = "none yet"
bool sweptUp = false
bool sweptDn = false
bool grabUp = false
bool grabDn = false
// Only mutate stored levels on confirmed bars (no realtime repaint of the store)
if barstate.isconfirmed and not na(atr)
float tol = tolAtr * atr
if not na(ph)
f_reg(ph, 1, tol, bar_index - pivLen)
if not na(pl)
f_reg(pl, -1, tol, bar_index - pivLen)
int nLv = array.size(lvPrice)
if nLv > 0
for i = nLv - 1 to 0
float p = array.get(lvPrice, i)
int sd = array.get(lvSide, i)
line l = array.get(lvLine, i)
bool sw = false
if sd == 1
sw := (sweepClose ? close : high) > p
else
sw := (sweepClose ? close : low) < p
bool expired = bar_index - array.get(lvBar, i) > maxAge
if sw
if sd == 1
sweptUp := true
if high > p and close < p
grabUp := true
else
sweptDn := true
if low < p and close > p
grabDn := true
if sw and showSwept and not expired
line.set_x2(l, bar_index)
line.set_style(l, line.style_dotted)
line.set_color(l, color.new(color.gray, 40))
line.set_width(l, 1)
f_remove(i, false)
else if sw or expired
f_remove(i, true)
else
line.set_x2(l, bar_index)
while array.size(lvPrice) > maxLevels
f_remove(0, true)
sweptUp := sweptUp or extUp
sweptDn := sweptDn or extDn
grabUp := grabUp or extGrabUp
grabDn := grabDn or extGrabDn
if grabUp
lastSweep := "Buy-side GRAB (swept high, closed back below) " + str.format_time(time, "MM/dd HH:mm", syminfo.timezone)
else if grabDn
lastSweep := "Sell-side GRAB (swept low, closed back above) " + str.format_time(time, "MM/dd HH:mm", syminfo.timezone)
else if sweptUp
lastSweep := "Buy-side swept " + str.format_time(time, "MM/dd HH:mm", syminfo.timezone)
else if sweptDn
lastSweep := "Sell-side swept " + str.format_time(time, "MM/dd HH:mm", syminfo.timezone)
// ============================== PLOTS / ALERTS =============================
plot(showPD ? pdh : na, "PDH", color.new(color.red, 20), 1, plot.style_linebr)
plot(showPD ? pdl : na, "PDL", color.new(color.green, 20), 1, plot.style_linebr)
plot(showPW ? pwh : na, "PWH", color.new(color.maroon, 10), 2, plot.style_linebr)
plot(showPW ? pwl : na, "PWL", color.new(color.teal, 10), 2, plot.style_linebr)
plot(showPM ? pmHigh : na, "Premarket High", color.new(color.fuchsia, 25), 1, plot.style_linebr)
plot(showPM ? pmLow : na, "Premarket Low", color.new(color.fuchsia, 25), 1, plot.style_linebr)
plotshape(grabUp, "Buy-side liquidity grab", shape.triangledown, location.abovebar, color.red, size=size.tiny)
plotshape(grabDn, "Sell-side liquidity grab", shape.triangleup, location.belowbar, color.green, size=size.tiny)
alertcondition(sweptUp, "Buy-side liquidity swept", "Buy-side liquidity (high) swept on {{ticker}}")
alertcondition(sweptDn, "Sell-side liquidity swept", "Sell-side liquidity (low) swept on {{ticker}}")
alertcondition(grabUp, "Buy-side liquidity grab", "Buy-side liquidity grab (swept high, closed back below) on {{ticker}}")
alertcondition(grabDn, "Sell-side liquidity grab", "Sell-side liquidity grab (swept low, closed back above) on {{ticker}}")
// ============================== PREDICTION (last bar) ======================
var table tb = table.new(position.top_right, 2, 9, bgcolor=color.new(color.black, 15), border_width=1, border_color=color.new(color.gray, 60))
var line projLine = na
var label labA = na
var label labB = na
f_cand(array<float> P, array<float> W, array<string> N, float price, float w, string nm) =>
if not na(price)
array.push(P, price)
array.push(W, w)
array.push(N, nm)
0
f_p(float x) => na(x) ? "n/a" : str.tostring(x, format.mintick)
f_row(int r, string k, string v, color vc) =>
table.cell(tb, 0, r, k, text_color=color.silver, text_halign=text.align_left, text_size=size.small)
table.cell(tb, 1, r, v, text_color=vc, text_halign=text.align_left, text_size=size.small)
if barstate.islast and not na(atr)
array<float> cP = array.new<float>()
array<float> cW = array.new<float>()
array<string> cN = array.new<string>()
int nl = array.size(lvPrice)
if nl > 0
for i = 0 to nl - 1
int tc = array.get(lvTouch, i)
int sd = array.get(lvSide, i)
float base = tc >= 2 ? wSwing + wEqualStep * (tc - 1) : wSwing
float age = bar_index - array.get(lvBar, i)
float w = base / (1 + age / decayBars)
string nm = tc >= 2 ? ((sd == 1 ? "EQH x" : "EQL x") + str.tostring(tc)) : (sd == 1 ? "Swing High" : "Swing Low")
f_cand(cP, cW, cN, array.get(lvPrice, i), w, nm)
f_cand(cP, cW, cN, pdhLive ? pdh : na, wPD, "PDH")
f_cand(cP, cW, cN, pdlLive ? pdl : na, wPD, "PDL")
f_cand(cP, cW, cN, pwhLive ? pwh : na, wPW, "PWH")
f_cand(cP, cW, cN, pwlLive ? pwl : na, wPW, "PWL")
f_cand(cP, cW, cN, pmhLive ? pmHigh : na, wPM, "PM High")
f_cand(cP, cW, cN, pmlLive ? pmLow : na, wPM, "PM Low")
int nc = array.size(cP)
float confTol = tolAtr * atr * 2
float bestA = 0.0
float bestB = 0.0
int iA = -1
int iB = -1
if nc > 0
for i = 0 to nc - 1
float p = array.get(cP, i)
bool above = p > close
float conf = 0.0
for j = 0 to nc - 1
if j != i
float q = array.get(cP, j)
if (q > close) == above and math.abs(q - p) <= confTol
conf += 0.5 * array.get(cW, j)
float d = math.abs(p - close) / atr
float s = (array.get(cW, i) + conf) / (1 + d / distScale)
if useTilt
if above and close > ema50
s *= 1.15
if not above and close < ema50
s *= 1.15
if above
if s > bestA
bestA := s
iA := i
else
if s > bestB
bestB := s
iB := i
float pA = iA >= 0 ? array.get(cP, iA) : na
float pB = iB >= 0 ? array.get(cP, iB) : na
string nA = iA >= 0 ? array.get(cN, iA) : "none"
string nB = iB >= 0 ? array.get(cN, iB) : "none"
float dA = na(pA) ? na : (pA - close) / atr
float dB = na(pB) ? na : (close - pB) / atr
float tot = bestA + bestB
float pullUp = tot > 0 ? bestA / tot * 100 : na
bool upWins = bestA >= bestB
bool anyPool = iA >= 0 or iB >= 0
float tgtP = not anyPool ? na : upWins ? pA : pB
string tgtN = not anyPool ? "none" : upWins ? nA : nB
float tgtD = not anyPool ? na : upWins ? dA : dB
if not na(projLine)
line.delete(projLine)
if not na(labA)
label.delete(labA)
if not na(labB)
label.delete(labB)
if showProj and anyPool
projLine := line.new(bar_index, close, bar_index + projBars, tgtP, style=line.style_arrow_right, width=2, color=upWins ? color.red : color.green)
if showLabels
if iA >= 0
labA := label.new(bar_index + 2, pA, (upWins ? "★ " : "") + "▲ " + nA + " " + f_p(pA) + " (" + str.tostring(dA, "#.0") + " ATR)", style=label.style_label_left, color=color.new(color.red, upWins ? 10 : 55), textcolor=color.white, size=size.small)
if iB >= 0
labB := label.new(bar_index + 2, pB, (not upWins ? "★ " : "") + "▼ " + nB + " " + f_p(pB) + " (" + str.tostring(dB, "#.0") + " ATR)", style=label.style_label_left, color=color.new(color.green, not upWins ? 10 : 55), textcolor=color.white, size=size.small)
if showTable
f_row(0, "LIQUIDITY MAGNET", "heuristic, not a forecast", color.yellow)
f_row(1, "Predicted pull", not anyPool ? "no active pools" : upWins ? "▲ UP toward buy-side liquidity" : "▼ DOWN toward sell-side liquidity", not anyPool ? color.gray : upWins ? color.red : color.lime)
f_row(2, "Target pool", not anyPool ? "n/a" : tgtN + " @ " + f_p(tgtP), color.white)
f_row(3, "Distance", na(tgtD) ? "n/a" : str.tostring(tgtD, "#.0") + " ATR (" + str.tostring(math.abs(tgtP - close) / close * 100, "#.00") + "%)", color.white)
f_row(4, "Relative pull", na(pullUp) ? "n/a" : str.tostring(pullUp, "#") + "% up / " + str.tostring(100 - pullUp, "#") + "% down", color.white)
f_row(5, "Strongest above", iA >= 0 ? nA + " @ " + f_p(pA) + " (" + str.tostring(bestA, "#.00") + ")" : "none", color.white)
f_row(6, "Strongest below", iB >= 0 ? nB + " @ " + f_p(pB) + " (" + str.tostring(bestB, "#.00") + ")" : "none", color.white)
f_row(7, "Pools tracked", str.tostring(nc) + " (swing/equal: " + str.tostring(nl) + ")", color.white)
f_row(8, "Last sweep", lastSweep, color.orange)