本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © johnmontojo04 — original code. Concepts (key levels, liquidity sweeps) are common public trading ideas.
//@version=6
indicator("Sweep Map + Key Levels [JM]", shorttitle="Sweep+Levels [JM]", overlay=true,
max_lines_count=500, max_labels_count=500, max_bars_back=5000)
// ============================================================================
// INPUTS
// ============================================================================
G_LV = "Key Levels"
showDO = input.bool(true, "Daily Open", group=G_LV, inline="do")
cDO = input.color(#2962FF, "", group=G_LV, inline="do")
showPD = input.bool(true, "Prev Day High / Low", group=G_LV, inline="pd")
cPD = input.color(#E91E63, "", group=G_LV, inline="pd")
showPDM = input.bool(false, "Prev Day Mid (50%)", group=G_LV, inline="pdm")
cPDM = input.color(#9C27B0, "", group=G_LV, inline="pdm")
showP4H = input.bool(false, "Prev 4H High / Low", group=G_LV, inline="h4")
cP4H = input.color(#795548, "", group=G_LV, inline="h4")
showWO = input.bool(true, "Weekly Open", group=G_LV, inline="wo")
cWO = input.color(#00897B, "", group=G_LV, inline="wo")
showPW = input.bool(true, "Prev Week High / Low", group=G_LV, inline="pw")
cPW = input.color(#43A047, "", group=G_LV, inline="pw")
showMO = input.bool(false, "Monthly Open", group=G_LV, inline="mo")
cMO = input.color(#3F51B5, "", group=G_LV, inline="mo")
showPM = input.bool(false, "Prev Month High / Low", group=G_LV, inline="pm")
cPM = input.color(#607D8B, "", group=G_LV, inline="pm")
G_LS = "Level Style"
lvStyleS = input.string("Dashed", "Line style", options=["Solid", "Dashed", "Dotted"], group=G_LS)
lvWidth = input.int(1, "Line width", minval=1, maxval=4, group=G_LS)
lvExt = input.int(15, "Extend right (bars)", minval=0, maxval=200, group=G_LS)
lvLabel = input.bool(true, "Show labels", group=G_LS)
lvPrice = input.bool(true, "Show price in label", group=G_LS, active=lvLabel)
lvSize = input.string("Small", "Label size", options=["Tiny", "Small", "Normal"], group=G_LS, active=lvLabel)
mergeTol = input.float(0.0, "Merge labels closer than (points)", minval=0, group=G_LS,
tooltip="If two levels sit almost on the same price, their labels are joined into one. 0 = off.")
G_SW = "Sweep Map"
swOn = input.bool(true, "Show swing liquidity + sweeps", group=G_SW)
pvLen = input.int(5, "Swing strength (bars each side)", minval=2, maxval=50, group=G_SW, active=swOn)
maxLiq = input.int(6, "Max open liquidity lines per side", minval=1, maxval=60, group=G_SW, active=swOn)
needRev = input.bool(true, "Sweep must close back inside", group=G_SW, active=swOn,
tooltip="On: a sweep counts only when price wicks through the level and the candle closes back on the other side (a reversal). Off: any wick through counts.")
keepSwept = input.int(20, "Keep last N swept lines", minval=0, maxval=200, group=G_SW, active=swOn)
cBuyLiq = input.color(color.new(#26A69A, 0), "Sell-side liquidity (lows)", group=G_SW, inline="lc")
cSellLiq = input.color(color.new(#EF5350, 0), "Buy-side liquidity (highs)", group=G_SW, inline="lc")
klSweep = input.bool(true, "Mark sweeps of key levels (PDH, PDL, PWH, PWL, 4H...)", group=G_SW)
markSize = input.string("Small", "Sweep marker size", options=["Tiny", "Small", "Normal"], group=G_SW)
G_DB = "Dashboard"
dbOn = input.bool(true, "Show level dashboard", group=G_DB)
dbPos = input.string("Top Right", "Position", options=["Top Right", "Bottom Right", "Bottom Left", "Top Left"], group=G_DB, active=dbOn)
// ============================================================================
// HELPERS
// ============================================================================
lineStyle(string s) => s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
sizeOf(string s) => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : size.small
fmt(float p) => str.tostring(p, format.mintick)
tfSec = timeframe.in_seconds(timeframe.period)
LS = lineStyle(lvStyleS)
LSZ = sizeOf(lvSize)
MSZ = sizeOf(markSize)
// time of one chart bar in ms, used to extend lines into the future with xloc.bar_time
barMs = tfSec * 1000
// previous completed period values without repainting
htf(string tf, float src) => request.security(syminfo.tickerid, tf, src[1], lookahead=barmerge.lookahead_on)
htfNow(string tf, float src) => request.security(syminfo.tickerid, tf, src, lookahead=barmerge.lookahead_on)
htfT(string tf) => request.security(syminfo.tickerid, tf, time, lookahead=barmerge.lookahead_on)
// ============================================================================
// TYPES
// ============================================================================
type Level
string name
color col
float price = na
int startT = na
bool swept = false
line ln = na
label lb = na
type Liq
float price
int t
bool isHigh
line ln = na
method draw(Level this, bool show) =>
if not show or na(this.price) or na(this.startT)
if not na(this.ln)
this.ln.delete()
this.ln := na
if not na(this.lb)
this.lb.delete()
this.lb := na
else
endT = time + barMs * lvExt
if na(this.ln)
this.ln := line.new(this.startT, this.price, endT, this.price, xloc=xloc.bar_time, color=this.col, style=LS, width=lvWidth)
else
this.ln.set_xy1(this.startT, this.price)
this.ln.set_xy2(endT, this.price)
this.ln.set_color(this.col)
if lvLabel
txt = lvPrice ? this.name + " " + fmt(this.price) : this.name
if na(this.lb)
this.lb := label.new(endT, this.price, txt, xloc=xloc.bar_time, style=label.style_label_left, color=color.new(color.white, 100), textcolor=this.col, size=LSZ)
else
this.lb.set_xy(endT, this.price)
this.lb.set_text(txt)
this.lb.set_textcolor(this.col)
true
// ============================================================================
// HIGHER TIMEFRAME DATA
// ============================================================================
dOpen = htfNow("D", open)
dT = htfT("D")
pdh = htf("D", high)
pdl = htf("D", low)
wOpen = htfNow("W", open)
wT = htfT("W")
pwh = htf("W", high)
pwl = htf("W", low)
mOpen = htfNow("M", open)
mT = htfT("M")
pmh = htf("M", high)
pml = htf("M", low)
h4T = htfT("240")
p4h = htf("240", high)
p4l = htf("240", low)
below4H = tfSec < 14400
belowD = tfSec < 86400
belowW = tfSec < 604800
// ============================================================================
// LEVEL OBJECTS
// ============================================================================
var Level lDO = Level.new("Daily Open", cDO)
var Level lPDH = Level.new("PDH", cPD)
var Level lPDL = Level.new("PDL", cPD)
var Level lPDM = Level.new("PD 50%", cPDM)
var Level lH4H = Level.new("P4H High", cP4H)
var Level lH4L = Level.new("P4H Low", cP4H)
var Level lWO = Level.new("Weekly Open", cWO)
var Level lPWH = Level.new("PWH", cPW)
var Level lPWL = Level.new("PWL", cPW)
var Level lMO = Level.new("Monthly Open", cMO)
var Level lPMH = Level.new("PMH", cPM)
var Level lPML = Level.new("PML", cPM)
setLv(Level l, float p, int st) =>
if l.price != p or l.startT != st
l.swept := false
l.price := p
l.startT := st
setLv(lDO, dOpen, dT)
setLv(lPDH, pdh, dT)
setLv(lPDL, pdl, dT)
setLv(lPDM, (pdh + pdl) / 2, dT)
setLv(lH4H, p4h, h4T)
setLv(lH4L, p4l, h4T)
setLv(lWO, wOpen, wT)
setLv(lPWH, pwh, wT)
setLv(lPWL, pwl, wT)
setLv(lMO, mOpen, mT)
setLv(lPMH, pmh, mT)
setLv(lPML, pml, mT)
// which levels are on, and in what order they are checked for sweeps
var array<Level> levels = array.from(lDO, lPDH, lPDL, lPDM, lH4H, lH4L, lWO, lPWH, lPWL, lMO, lPMH, lPML)
isOn(Level l) =>
switch l.name
"Daily Open" => showDO and belowD
"PDH" => showPD and belowD
"PDL" => showPD and belowD
"PD 50%" => showPDM and belowD
"P4H High" => showP4H and below4H
"P4H Low" => showP4H and below4H
"Weekly Open" => showWO and belowW
"PWH" => showPW and belowW
"PWL" => showPW and belowW
"Monthly Open" => showMO
"PMH" => showPM
"PML" => showPM
=> false
// draw levels on the last bar only (fast), merge labels that sit on the same price
if barstate.islast
for l in levels
l.draw(isOn(l))
if mergeTol > 0 and lvLabel
for [i, a] in levels
if isOn(a) and not na(a.lb)
for [j, b] in levels
if j > i and isOn(b) and not na(b.lb) and math.abs(a.price - b.price) <= mergeTol
a.lb.set_text(a.lb.get_text() + " / " + b.name)
b.lb.set_text("")
// ============================================================================
// SWEEP MAP: swing liquidity
// ============================================================================
var array<Liq> liqs = array.new<Liq>()
var array<line> swept = array.new<line>()
ph = ta.pivothigh(high, pvLen, pvLen)
pl = ta.pivotlow(low, pvLen, pvLen)
addLiq(float p, bool isHigh) =>
t = time[pvLen]
ln = line.new(t, p, time, p, xloc=xloc.bar_time, color=color.new(isHigh ? cSellLiq : cBuyLiq, 35), style=line.style_solid, width=1)
liqs.push(Liq.new(p, t, isHigh, ln))
// cap open lines per side: drop the oldest of that side
cnt = 0
for q in liqs
if q.isHigh == isHigh
cnt += 1
if cnt > maxLiq
for [k, q] in liqs
if q.isHigh == isHigh
q.ln.delete()
liqs.remove(k)
break
true
bullSweep = false
bearSweep = false
string sweepTxt = ""
if swOn
if not na(ph)
addLiq(ph, true)
if not na(pl)
addLiq(pl, false)
n = liqs.size()
if n > 0
for k = n - 1 to 0
q = liqs.get(k)
hit = q.isHigh ? high > q.price : low < q.price
closed = q.isHigh ? close > q.price : close < q.price
if hit
isSweep = needRev ? not closed : true
q.ln.set_x2(time)
if isSweep
q.ln.set_style(line.style_dashed)
q.ln.set_color(color.new(q.isHigh ? cSellLiq : cBuyLiq, 0))
swept.push(q.ln)
if q.isHigh
bearSweep := true
else
bullSweep := true
else
q.ln.set_color(color.new(color.gray, 70))
swept.push(q.ln)
liqs.remove(k)
else
q.ln.set_x2(time)
while swept.size() > keepSwept
swept.shift().delete()
// ============================================================================
// SWEEP MAP: key-level sweeps (once per level per period)
// ============================================================================
if klSweep
for l in levels
if isOn(l) and not l.swept and not na(l.price) and l.name != "Daily Open" and l.name != "Weekly Open" and l.name != "Monthly Open" and l.name != "PD 50%"
isHighLv = str.contains(l.name, "H") and not str.contains(l.name, "Low")
isLowSide = str.contains(l.name, "Low") or l.name == "PDL" or l.name == "PWL" or l.name == "PML"
if isLowSide
if low < l.price and (not needRev or close > l.price)
l.swept := true
bullSweep := true
sweepTxt := sweepTxt + (sweepTxt == "" ? "" : " + ") + l.name
else if isHighLv
if high > l.price and (not needRev or close < l.price)
l.swept := true
bearSweep := true
sweepTxt := sweepTxt + (sweepTxt == "" ? "" : " + ") + l.name
// ============================================================================
// MARKERS
// ============================================================================
// key-level sweeps (PDH, PDL, PWH...) get a full label; swing sweeps get a small tag
mTxt = sweepTxt == "" ? "×" : sweepTxt + " ×"
mSz = sweepTxt != "" ? MSZ : size.tiny
if bearSweep
label.new(bar_index, high, mTxt, style=label.style_label_down, color=color.new(cSellLiq, sweepTxt != "" ? 10 : 35), textcolor=color.white, size=mSz)
if bullSweep
label.new(bar_index, low, mTxt, style=label.style_label_up, color=color.new(cBuyLiq, sweepTxt != "" ? 10 : 35), textcolor=color.white, size=mSz)
// ============================================================================
// DASHBOARD
// ============================================================================
posOf(string s) => s == "Top Right" ? position.top_right : s == "Bottom Left" ? position.bottom_left : s == "Top Left" ? position.top_left : position.bottom_right
var table db = table.new(posOf(dbPos), 3, 16, bgcolor=color.new(chart.bg_color, 5), border_width=0, frame_color=color.new(chart.fg_color, 80), frame_width=1)
if dbOn and barstate.islast
db.cell(0, 0, "Level", text_color=color.gray, text_size=size.small)
db.cell(1, 0, "Price", text_color=color.gray, text_size=size.small)
db.cell(2, 0, "Dist", text_color=color.gray, text_size=size.small)
r = 1
for l in levels
if isOn(l) and not na(l.price)
d = close - l.price
db.cell(0, r, l.name + (l.swept ? " ✓ swept" : ""), text_color=l.col, text_size=size.small, text_halign=text.align_left)
db.cell(1, r, fmt(l.price), text_color=chart.fg_color, text_size=size.small)
db.cell(2, r, (d >= 0 ? "+" : "") + str.tostring(d, "#.##"), text_color=d >= 0 ? #26A69A : #EF5350, text_size=size.small)
r += 1
// ============================================================================
// ALERTS
// ============================================================================
alertcondition(bullSweep, "Bullish sweep", "Sell-side liquidity swept and closed back above on {{ticker}} {{interval}}")
alertcondition(bearSweep, "Bearish sweep", "Buy-side liquidity swept and closed back below on {{ticker}} {{interval}}")
if (bullSweep or bearSweep) and barstate.isconfirmed
alert(syminfo.ticker + ": " + (bullSweep ? "bullish" : "bearish") + " sweep" + (sweepTxt != "" ? " of " + sweepTxt : ""), alert.freq_once_per_bar_close)