本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Heat Sweep | Volume Heatmap Liquidity Strategy", "HEATSWEEP", overlay = true, initial_capital = 10000, pyramiding = 0, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 1, max_boxes_count = 500, max_lines_count = 50, max_labels_count = 50, max_bars_back = 500, explicit_plot_zorder = true)
// ── Inputs ──
G_PROF = "Volume Heatmap"
G_VWAP = "Anchored VWAP"
G_ENTR = "Entries"
G_RISK = "Risk"
G_VIS = "Visuals"
lookback = input.int(100, "Profile lookback (bars)", minval = 30, maxval = 400, group = G_PROF, tooltip = "Recent bars used to build the volume profile. Each bar's volume is assigned to the price row of its close.")
rows = input.int(30, "Rows", minval = 10, maxval = 60, group = G_PROF)
minHeat = input.float(0.25, "Min level heat (0-1)", minval = 0.0, maxval = 1.0, step = 0.05, group = G_PROF, tooltip = "A swept level only counts if volume near its price is at least this share of the POC row. Lower = more trades. 0 disables the filter.")
anchorTf = input.string("Auto", "Anchor", options = ["Auto", "Session", "Week", "Month"], group = G_VWAP, tooltip = "Auto uses Session on intraday charts, Month on daily and Year on weekly or higher, so VWAP never collapses to a single bar.")
bandMult = input.float(2.5, "Band width (σ)", minval = 0.5, step = 0.1, group = G_VWAP, tooltip = "Width of the VWAP gradient in volume-weighted standard deviations. Also sets the colour range of the VWAP candles.")
swingLen = input.int(3, "Swing length", minval = 2, maxval = 30, group = G_ENTR, tooltip = "Bars on each side that confirm a swing. Smaller = more liquidity levels and more trades.")
relVolMin = input.float(1.0, "Min relative volume (x avg 20)", minval = 0.0, step = 0.1, group = G_ENTR, tooltip = "Signal candle volume must exceed this multiple of its 20-bar average. 0 disables.")
useSweep = input.bool(true, "Setup 1: liquidity sweep", group = G_ENTR, tooltip = "Wick through a recent swing level that closes back inside, on a level with meaningful volume.")
usePoc = input.bool(true, "Setup 2: POC reclaim", group = G_ENTR, tooltip = "Close crosses the volume POC with a candle in the same direction.")
useVwapFilter = input.bool(false, "VWAP location filter", group = G_ENTR, tooltip = "Sweep longs only below VWAP, sweep shorts only above. Off gives more trades.")
atrLen = input.int(14, "ATR length", minval = 1, group = G_RISK)
slMult = input.float(0.5, "Stop: ATR beyond wick / swing", minval = 0.1, step = 0.1, group = G_RISK)
rr = input.float(1.5, "Reward : Risk", minval = 0.5, step = 0.25, group = G_RISK)
riskPct = input.float(1.5, "Risk per trade (% equity)", minval = 0.1, maxval = 10.0, step = 0.1, group = G_RISK, tooltip = "Position size is set so a stop-out loses about this % of equity.")
maxLev = input.float(3.0, "Max leverage (x equity)", minval = 1.0, step = 0.5, group = G_RISK, tooltip = "Caps position value as a multiple of equity so tight stops cannot create oversized trades.")
showCandles = input.bool(true, "VWAP deviation candles", inline = "v1", group = G_VIS, tooltip = "Colours candles by distance from VWAP: bullish colour below, bearish colour above, fading to neutral near VWAP.")
showVwap = input.bool(true, "VWAP", inline = "v1", group = G_VIS)
showProfile = input.bool(true, "Heatmap profile", inline = "v2", group = G_VIS)
showDelta = input.bool(false, "Delta tags", inline = "v2", group = G_VIS, tooltip = "Estimated buy/sell delta per row. Candle direction decides the split: close below open counts as selling.")
showLevels = input.bool(true, "Liquidity zones", inline = "v3", group = G_VIS)
showTrade = input.bool(true, "Trade zones", inline = "v3", group = G_VIS, tooltip = "Shades entry-to-stop and entry-to-target while a position is open.")
showTable = input.bool(true, "Table", group = G_VIS)
profOff = input.int(25, "Profile offset (bars)", minval = 5, maxval = 100, inline = "p", group = G_VIS)
profW = input.int(30, "Width", minval = 10, maxval = 100, inline = "p", group = G_VIS, tooltip = "Offset is the gap between the last candle and the profile. Width is the maximum profile width in bars.")
bullCol = input.color(#00ffbb, "Bullish", inline = "c", group = G_VIS)
bearCol = input.color(#ff1100, "Bearish", inline = "c", group = G_VIS)
// ── Anchored VWAP ──
newPeriod = switch anchorTf
"Session" => timeframe.change("D")
"Week" => timeframe.change("W")
"Month" => timeframe.change("M")
=> timeframe.isintraday ? timeframe.change("D") : timeframe.isdaily ? timeframe.change("M") : timeframe.change("12M")
[vwap, vwapStdUp, _] = ta.vwap(hlc3, newPeriod, 1.0)
float sd = math.max(vwapStdUp - vwap, 0.0)
float upBand = vwap + sd * bandMult
float dnBand = vwap - sd * bandMult
float dev = sd > 0 ? (close - vwap) / sd : na
// ── Volume profile ──
var buyBins = array.new_float(60, 0.0)
var sellBins = array.new_float(60, 0.0)
float hi = ta.highest(high, lookback)
float lo = ta.lowest(low, lookback)
float step = (hi - lo) / rows
float maxV = 0.0
float poc = na
float profDelta = na
int pocRow = 0
if bar_index >= lookback and step > 0
array.fill(buyBins, 0.0)
array.fill(sellBins, 0.0)
for i = 0 to lookback - 1
int r = math.min(rows - 1, math.max(0, int(math.floor((close[i] - lo) / step))))
float v = nz(volume[i])
if close[i] >= open[i]
array.set(buyBins, r, array.get(buyBins, r) + v)
else
array.set(sellBins, r, array.get(sellBins, r) + v)
float totBuy = 0.0
float totSell = 0.0
for r = 0 to rows - 1
float b = array.get(buyBins, r)
float s = array.get(sellBins, r)
totBuy += b
totSell += s
if b + s > maxV
maxV := b + s
pocRow := r
if maxV > 0
poc := lo + step * (pocRow + 0.5)
profDelta := (totBuy - totSell) / (totBuy + totSell) * 100.0
// Heat (0-1) near a price: best of its row and the two neighbours
heatNear(float price) =>
float h = 0.0
if maxV > 0 and step > 0 and not na(price) and price >= lo and price <= hi
int r = math.min(rows - 1, int(math.floor((price - lo) / step)))
for k = math.max(0, r - 1) to math.min(rows - 1, r + 1)
h := math.max(h, (array.get(buyBins, k) + array.get(sellBins, k)) / maxV)
h
// ── Liquidity levels (last two swings per side) ──
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
var float hi1 = na
var float hi2 = na
var float lo1 = na
var float lo2 = na
var int hi1Bar = na
var int hi2Bar = na
var int lo1Bar = na
var int lo2Bar = na
if not na(ph)
hi2 := hi1
hi2Bar := hi1Bar
hi1 := ph
hi1Bar := bar_index - swingLen
if not na(pl)
lo2 := lo1
lo2Bar := lo1Bar
lo1 := pl
lo1Bar := bar_index - swingLen
float atr = ta.atr(atrLen)
float rvol = volume / ta.sma(volume, 20)
bool volOK = relVolMin <= 0 or rvol > relVolMin
bool swLo1 = not na(lo1) and low < lo1 and close > lo1
bool swLo2 = not na(lo2) and low < lo2 and close > lo2
bool swHi1 = not na(hi1) and high > hi1 and close < hi1
bool swHi2 = not na(hi2) and high > hi2 and close < hi2
float loLvl = swLo1 ? lo1 : swLo2 ? lo2 : na
float hiLvl = swHi1 ? hi1 : swHi2 ? hi2 : na
// ── Signals ──
bool sweepLong = useSweep and not na(loLvl) and heatNear(loLvl) >= minHeat and (not useVwapFilter or close < vwap)
bool sweepShort = useSweep and not na(hiLvl) and heatNear(hiLvl) >= minHeat and (not useVwapFilter or close > vwap)
bool pocLong = usePoc and ta.crossover(close, poc) and close > open
bool pocShort = usePoc and ta.crossunder(close, poc) and close < open
bool longSig = barstate.isconfirmed and volOK and (sweepLong or pocLong)
bool shortSig = barstate.isconfirmed and volOK and (sweepShort or pocShort)
float swingLow3 = ta.lowest(low, 3)
float swingHigh3 = ta.highest(high, 3)
// ── Strategy ──
var float longStop = na
var float longTgt = na
var float shortStop = na
var float shortTgt = na
if longSig and strategy.position_size <= 0
longStop := (sweepLong ? low : swingLow3) - atr * slMult
float dist = close - longStop
if dist > 0
longTgt := close + dist * rr
float qty = math.min(strategy.equity * riskPct / 100.0 / dist, strategy.equity * maxLev / close)
strategy.entry("Long", strategy.long, qty = qty)
if shortSig and strategy.position_size >= 0
shortStop := (sweepShort ? high : swingHigh3) + atr * slMult
float dist = shortStop - close
if dist > 0
shortTgt := close - dist * rr
float qty = math.min(strategy.equity * riskPct / 100.0 / dist, strategy.equity * maxLev / close)
strategy.entry("Short", strategy.short, qty = qty)
if strategy.position_size > 0 or longSig
strategy.exit("Long Exit", "Long", stop = longStop, limit = longTgt)
if strategy.position_size < 0 or shortSig
strategy.exit("Short Exit", "Short", stop = shortStop, limit = shortTgt)
// Swept levels are consumed so they cannot fire twice
if barstate.isconfirmed
if swLo1
lo1 := na
if swLo2
lo2 := na
if swHi1
hi1 := na
if swHi2
hi2 := na
// ── Plots ──
vwapPlot = plot(showVwap ? vwap : na, "VWAP", color = color.new(chart.fg_color, 70), linewidth = 1)
upPlot = plot(showVwap ? upBand : na, "Upper Band", display = display.none)
dnPlot = plot(showVwap ? dnBand : na, "Lower Band", display = display.none)
fill(vwapPlot, upPlot, upBand, vwap, color.new(bearCol, 99), color.new(bearCol, 92), title = "Upper VWAP Gradient")
fill(vwapPlot, dnPlot, vwap, dnBand, color.new(bullCol, 92), color.new(bullCol, 99), title = "Lower VWAP Gradient")
color candleColor = showCandles and not na(dev) ? color.from_gradient(math.abs(dev), 0, bandMult, chart.fg_color, dev > 0 ? bearCol : bullCol) : na
plotcandle(showCandles and not na(dev) ? open : na, high, low, close, "VWAP Deviation Candles", color = candleColor, wickcolor = candleColor, bordercolor = candleColor)
// Trade zones: shaded only, the level plots stay hidden
float curEntry = showTrade and strategy.position_size != 0 ? strategy.position_avg_price : na
float curStop = showTrade ? (strategy.position_size > 0 ? longStop : strategy.position_size < 0 ? shortStop : na) : na
float curTgt = showTrade ? (strategy.position_size > 0 ? longTgt : strategy.position_size < 0 ? shortTgt : na) : na
entryPlot = plot(curEntry, "Entry", display = display.none)
stopPlot = plot(curStop, "Stop", display = display.none)
tgtPlot = plot(curTgt, "Target", display = display.none)
fill(entryPlot, stopPlot, color = color.new(bearCol, 85), title = "Risk Zone")
fill(entryPlot, tgtPlot, color = color.new(bullCol, 85), title = "Reward Zone")
float gap = math.max(nz(atr) * 0.35, syminfo.mintick * 2)
plotshape(longSig and sweepLong ? low - gap : na, "Long Sweep", shape.labelup, location.absolute, bullCol, text = "▲", textcolor = chart.fg_color, size = size.small)
plotshape(longSig and not sweepLong ? low - gap : na, "Long POC", shape.labelup, location.absolute, color.new(bullCol, 35), text = "◆", textcolor = chart.fg_color, size = size.small)
plotshape(shortSig and sweepShort ? high + gap : na, "Short Sweep", shape.labeldown, location.absolute, bearCol, text = "▼", textcolor = chart.fg_color, size = size.small)
plotshape(shortSig and not sweepShort ? high + gap : na, "Short POC", shape.labeldown, location.absolute, color.new(bearCol, 35), text = "◆", textcolor = chart.fg_color, size = size.small)
// ── Heatmap profile and liquidity zones ──
var array<box> boxes = array.new<box>()
var array<label> labels = array.new<label>()
if barstate.islast
while array.size(boxes) > 0
box.delete(array.pop(boxes))
while array.size(labels) > 0
label.delete(array.pop(labels))
int left = bar_index + profOff
int right = left + profW
int guide = left - 3
if showProfile and maxV > 0
for r = 0 to rows - 1
float b = array.get(buyBins, r)
float s = array.get(sellBins, r)
float tot = b + s
if tot > 0
float rLo = lo + step * r + step * 0.06
float rHi = lo + step * (r + 1) - step * 0.06
float rMid = (rLo + rHi) / 2
float dlt = (b - s) / tot * 100.0
float strength = tot / maxV
bool isPoc = r == pocRow
color dc = color.from_gradient(dlt, -100, 100, bearCol, bullCol)
color bd = isPoc ? chart.fg_color : na
array.push(boxes, box.new(left, rHi, right, rLo, border_color = bd, border_width = 1, bgcolor = color.new(dc, 94 - int(strength * 16))))
if b > 0
int bw = math.max(1, int(math.round(b / maxV * profW)))
array.push(boxes, box.new(right - bw, rMid, right, rLo, border_color = na, bgcolor = color.new(bullCol, 15)))
if s > 0
int sw = math.max(1, int(math.round(s / maxV * profW)))
array.push(boxes, box.new(right - sw, rHi, right, rMid, border_color = na, bgcolor = color.new(bearCol, 15)))
if showDelta
string dTxt = (dlt > 0 ? "+" : "") + str.tostring(dlt, "#") + "%"
array.push(boxes, box.new(right + 1, rHi, right + 13, rLo, border_color = bd, border_width = 1, bgcolor = color.new(dc, 85), text = dTxt, text_color = chart.fg_color, text_size = size.tiny, text_halign = text.align_center))
array.push(labels, label.new(right + (showDelta ? 14 : 1), poc, "POC " + str.tostring(poc, format.mintick), color = color.new(chart.bg_color, 100), style = label.style_label_left, textcolor = chart.fg_color, size = size.small))
string pTxt = "Δ " + (profDelta > 0 ? "+" : "") + str.tostring(profDelta, "#.0") + "%"
array.push(labels, label.new(right - int(profW / 2), hi, pTxt, color = color.new(chart.bg_color, 100), style = label.style_label_down, textcolor = profDelta >= 0 ? bullCol : bearCol, size = size.small))
if showLevels
float zPad = nz(atr) * 0.12
array<float> lvPx = array.from(hi1, hi2, lo1, lo2)
array<int> lvBar = array.from(nz(hi1Bar), nz(hi2Bar), nz(lo1Bar), nz(lo2Bar))
for i = 0 to 3
float px = array.get(lvPx, i)
if not na(px)
bool isHigh = i < 2
float h = heatNear(px)
color zc = isHigh ? bearCol : bullCol
int born = array.get(lvBar, i)
array.push(boxes, box.new(born, px + zPad, guide, px - zPad, border_color = na, bgcolor = color.new(zc, 90 - int(h * 30))))
array.push(labels, label.new(guide, px, (isHigh ? "BSL " : "SSL ") + str.tostring(int(h * 100)) + "%", color = color.new(chart.bg_color, 100), style = label.style_label_left, textcolor = zc, size = size.small, tooltip = "Resting liquidity at " + str.tostring(px, format.mintick) + ". Percent = volume heat at this price (100% = POC row)."))
// ── Table ──
var table tbl = table.new(position.top_right, 2, 7, bgcolor = color.new(chart.bg_color, 15), frame_color = color.new(chart.fg_color, 85), frame_width = 1, border_color = color.new(chart.fg_color, 94), border_width = 1)
tRow(int r, string name, string value, color vc) =>
table.cell(tbl, 0, r, name, text_color = chart.fg_color, text_halign = text.align_left, text_size = size.small)
table.cell(tbl, 1, r, value, text_color = vc, text_halign = text.align_right, text_size = size.small)
if barstate.islast and showTable
string devTxt = na(dev) ? "N/A" : (dev > 0 ? "+" : "") + str.tostring(dev, "#.00") + "σ"
string pocTxt = na(poc) ? "N/A" : str.tostring(poc, format.mintick) + " | " + str.tostring(math.abs(close - poc) / atr, "#.0") + " ATR"
string dltTxt = na(profDelta) ? "N/A" : (profDelta > 0 ? "+" : "") + str.tostring(profDelta, "#.0") + "%"
float nearHi = na(hi1) ? hi2 : na(hi2) ? hi1 : math.min(hi1, hi2)
float nearLo = na(lo1) ? lo2 : na(lo2) ? lo1 : math.max(lo1, lo2)
string upTxt = na(nearHi) ? "None" : str.tostring(nearHi, format.mintick) + " | " + str.tostring(int(heatNear(nearHi) * 100)) + "%"
string dnTxt = na(nearLo) ? "None" : str.tostring(nearLo, format.mintick) + " | " + str.tostring(int(heatNear(nearLo) * 100)) + "%"
string posTxt = strategy.position_size > 0 ? "Long" : strategy.position_size < 0 ? "Short" : "Flat"
color posCol = strategy.position_size > 0 ? bullCol : strategy.position_size < 0 ? bearCol : chart.fg_color
table.cell(tbl, 0, 0, "HEAT SWEEP", text_color = chart.fg_color, text_halign = text.align_left, text_size = size.small, bgcolor = color.new(chart.fg_color, 90))
table.cell(tbl, 1, 0, "VALUE", text_color = chart.fg_color, text_halign = text.align_right, text_size = size.small, bgcolor = color.new(chart.fg_color, 90))
tRow(1, "VWAP position", devTxt, dev > 0 ? bearCol : bullCol)
tRow(2, "POC", pocTxt, chart.fg_color)
tRow(3, "Profile Δ (est.)", dltTxt, nz(profDelta) >= 0 ? bullCol : bearCol)
tRow(4, "Liquidity above", upTxt, bearCol)
tRow(5, "Liquidity below", dnTxt, bullCol)
tRow(6, "Position", posTxt, posCol)
// ── Alerts ──
alertcondition(longSig, "Long signal", "HEATSWEEP: long (sweep or POC reclaim)")
alertcondition(shortSig, "Short signal", "HEATSWEEP: short (sweep or POC reclaim)")