本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Liquidity Zones | Volume Heatmap", shorttitle="Liquidity Map", overlay=true, max_boxes_count=200)
// Swing-based liquidity proxy. Volume colors show relative traded volume at each pivot,
// not actual resting orders from an exchange order book.
string grpZones = "Liquidity Zone Detection"
int pivotLeft = input.int(5, "Pivot bars left", minval=1, maxval=50, group=grpZones)
int pivotRight = input.int(5, "Pivot bars right", minval=1, maxval=50, group=grpZones)
int volumeLength = input.int(50, "Average volume length", minval=2, group=grpZones)
float minVolumeRatio = input.float(0.8, "Minimum relative volume", minval=0.1, step=0.1, group=grpZones)
float strongVolumeRatio = input.float(2.5, "Strong relative volume", minval=0.2, step=0.1, group=grpZones)
string grpStyle = "Heatmap Style"
int atrLength = input.int(14, "ATR length", minval=1, group=grpStyle)
float zoneAtrWidth = input.float(0.18, "Zone thickness (ATR multiplier)", minval=0.02, step=0.02, group=grpStyle)
int maxZones = input.int(30, "Maximum active zones", minval=5, maxval=100, group=grpStyle)
bool showLabels = input.bool(true, "Show zone labels", group=grpStyle)
bool fadeOnTouch = input.bool(true, "Fade zones as price covers them", group=grpStyle)
string buyLabel = input.string("BSL", "Buy-side label", group=grpStyle)
string sellLabel = input.string("SSL", "Sell-side label", group=grpStyle)
color labelColor = input.color(color.white, "Label text color", group=grpStyle)
string labelSizeInput = input.string("Tiny", "Label text size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=grpStyle)
color buyWeakColor = input.color(color.rgb(115, 45, 145), "Buy-side low-volume color", group=grpStyle)
color buyStrongColor = input.color(color.rgb(255, 35, 190), "Buy-side high-volume color", group=grpStyle)
color sellWeakColor = input.color(color.rgb(30, 105, 150), "Sell-side low-volume color", group=grpStyle)
color sellStrongColor = input.color(color.rgb(30, 235, 220), "Sell-side high-volume color", group=grpStyle)
string grpLifecycle = "Zone Lifecycle"
string swingZonePolicy = input.string("Remove after sweep", "BSL/SSL zones", options=["Remove after sweep", "Keep after sweep"], group=grpLifecycle)
string lowVolumePolicy = input.string("Remove after full traversal", "LVN zones", options=["Remove after full traversal", "Keep until retest volume"], group=grpLifecycle)
float lvnFillThreshold = input.float(2.0, "LVN invalidation volume (× average bar volume)", minval=0.1, step=0.1, group=grpLifecycle)
string grpLowVolume = "Low-Volume Zones"
bool showLowVolumeZones = input.bool(false, "Show low-volume zones", group=grpLowVolume)
float maxLowVolumeRatio = input.float(0.5, "Maximum relative volume", minval=0.05, maxval=1.0, step=0.05, group=grpLowVolume)
string lowVolumeLabel = input.string("LVN", "Low-volume label", group=grpLowVolume)
color lowVolumeColor = input.color(color.rgb(235, 210, 90), "Low-volume zone color", group=grpLowVolume)
f_label_size(string choice) =>
choice == "Small" ? size.small : choice == "Normal" ? size.normal : choice == "Large" ? size.large : choice == "Huge" ? size.huge : size.tiny
float volumeAverage = ta.sma(volume, volumeLength)
float atr = ta.atr(atrLength)
float pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
float pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)
var box[] zones = array.new_box()
var float[] zoneTops = array.new_float()
var float[] zoneBottoms = array.new_float()
var int[] zoneSides = array.new_int() // +1 = buy-side above highs; -1 = sell-side below lows
var float[] zoneStrengths = array.new_float() // relative-volume strength, normalized to 0..1
var float[] zoneProgresses = array.new_float() // maximum fraction covered by price so far
var int[] zoneBornBars = array.new_int()
var bool[] zoneIsLowVolume = array.new_bool()
var float[] zoneRetestVolumes = array.new_float()
var float[] zoneAverageVolumes = array.new_float()
// Add a buy-side liquidity proxy above a confirmed swing high.
if not na(pivotHigh) and not na(volume[pivotRight]) and not na(volumeAverage[pivotRight]) and volumeAverage[pivotRight] > 0 and not na(atr[pivotRight])
float relativeVolume = volume[pivotRight] / volumeAverage[pivotRight]
if relativeVolume >= minVolumeRatio
float zoneWidth = math.max(atr[pivotRight] * zoneAtrWidth, syminfo.mintick * 2)
float zoneBottom = pivotHigh
float zoneTop = pivotHigh + zoneWidth
float strength = math.max(0.0, math.min(1.0, (relativeVolume - minVolumeRatio) / math.max(strongVolumeRatio - minVolumeRatio, 0.1)))
int transparency = int(88 - strength * 58)
color zoneColor = color.from_gradient(strength, 0.0, 1.0, buyWeakColor, buyStrongColor)
color fillColor = color.new(zoneColor, transparency)
color borderColor = color.new(zoneColor, math.max(transparency - 12, 0))
string zoneText = showLabels ? buyLabel + " " + str.tostring(relativeVolume, "#.0") + "x vol" : ""
if array.size(zones) >= maxZones
box.delete(array.shift(zones))
array.shift(zoneTops)
array.shift(zoneBottoms)
array.shift(zoneSides)
array.shift(zoneStrengths)
array.shift(zoneProgresses)
array.shift(zoneBornBars)
array.shift(zoneIsLowVolume)
array.shift(zoneRetestVolumes)
array.shift(zoneAverageVolumes)
box newZone = box.new(left=bar_index - pivotRight, top=zoneTop, right=bar_index, bottom=zoneBottom, xloc=xloc.bar_index, bgcolor=fillColor, border_color=borderColor, border_width=1, text=zoneText, text_color=color.new(labelColor, 15), text_size=f_label_size(labelSizeInput), text_halign=text.align_left, text_valign=text.align_center)
array.push(zones, newZone)
array.push(zoneTops, zoneTop)
array.push(zoneBottoms, zoneBottom)
array.push(zoneSides, 1)
array.push(zoneStrengths, strength)
array.push(zoneProgresses, 0.0)
array.push(zoneBornBars, bar_index)
array.push(zoneIsLowVolume, false)
array.push(zoneRetestVolumes, 0.0)
array.push(zoneAverageVolumes, volumeAverage[pivotRight])
// Add a sell-side liquidity proxy below a confirmed swing low.
if not na(pivotLow) and not na(volume[pivotRight]) and not na(volumeAverage[pivotRight]) and volumeAverage[pivotRight] > 0 and not na(atr[pivotRight])
float relativeVolume = volume[pivotRight] / volumeAverage[pivotRight]
if relativeVolume >= minVolumeRatio
float zoneWidth = math.max(atr[pivotRight] * zoneAtrWidth, syminfo.mintick * 2)
float zoneTop = pivotLow
float zoneBottom = pivotLow - zoneWidth
float strength = math.max(0.0, math.min(1.0, (relativeVolume - minVolumeRatio) / math.max(strongVolumeRatio - minVolumeRatio, 0.1)))
int transparency = int(88 - strength * 58)
color zoneColor = color.from_gradient(strength, 0.0, 1.0, sellWeakColor, sellStrongColor)
color fillColor = color.new(zoneColor, transparency)
color borderColor = color.new(zoneColor, math.max(transparency - 12, 0))
string zoneText = showLabels ? sellLabel + " " + str.tostring(relativeVolume, "#.0") + "x vol" : ""
if array.size(zones) >= maxZones
box.delete(array.shift(zones))
array.shift(zoneTops)
array.shift(zoneBottoms)
array.shift(zoneSides)
array.shift(zoneStrengths)
array.shift(zoneProgresses)
array.shift(zoneBornBars)
array.shift(zoneIsLowVolume)
array.shift(zoneRetestVolumes)
array.shift(zoneAverageVolumes)
box newZone = box.new(left=bar_index - pivotRight, top=zoneTop, right=bar_index, bottom=zoneBottom, xloc=xloc.bar_index, bgcolor=fillColor, border_color=borderColor, border_width=1, text=zoneText, text_color=color.new(labelColor, 15), text_size=f_label_size(labelSizeInput), text_halign=text.align_left, text_valign=text.align_center)
array.push(zones, newZone)
array.push(zoneTops, zoneTop)
array.push(zoneBottoms, zoneBottom)
array.push(zoneSides, -1)
array.push(zoneStrengths, strength)
array.push(zoneProgresses, 0.0)
array.push(zoneBornBars, bar_index)
array.push(zoneIsLowVolume, false)
array.push(zoneRetestVolumes, 0.0)
array.push(zoneAverageVolumes, volumeAverage[pivotRight])
// Mark unusually quiet candles as low-volume zones (LVNs).
if showLowVolumeZones and not na(volume) and not na(volumeAverage) and volumeAverage > 0
float relativeVolume = volume / volumeAverage
if relativeVolume <= maxLowVolumeRatio
float candleRange = math.max(high - low, syminfo.mintick * 2)
float zoneTop = (high + low) * 0.5 + candleRange * 0.5
float zoneBottom = (high + low) * 0.5 - candleRange * 0.5
float strength = math.max(0.0, math.min(1.0, (maxLowVolumeRatio - relativeVolume) / maxLowVolumeRatio))
int transparency = int(86 - strength * 48)
color fillColor = color.new(lowVolumeColor, transparency)
color borderColor = color.new(lowVolumeColor, math.max(transparency - 12, 0))
string zoneText = showLabels ? lowVolumeLabel + " " + str.tostring(relativeVolume, "#.00") + "x vol" : ""
if array.size(zones) >= maxZones
box.delete(array.shift(zones))
array.shift(zoneTops)
array.shift(zoneBottoms)
array.shift(zoneSides)
array.shift(zoneStrengths)
array.shift(zoneProgresses)
array.shift(zoneBornBars)
array.shift(zoneIsLowVolume)
array.shift(zoneRetestVolumes)
array.shift(zoneAverageVolumes)
box newZone = box.new(left=bar_index, top=zoneTop, right=bar_index, bottom=zoneBottom, xloc=xloc.bar_index, bgcolor=fillColor, border_color=borderColor, border_width=1, text=zoneText, text_color=color.new(labelColor, 15), text_size=f_label_size(labelSizeInput), text_halign=text.align_left, text_valign=text.align_center)
array.push(zones, newZone)
array.push(zoneTops, zoneTop)
array.push(zoneBottoms, zoneBottom)
array.push(zoneSides, 0)
array.push(zoneStrengths, strength)
array.push(zoneProgresses, 0.0)
array.push(zoneBornBars, bar_index)
array.push(zoneIsLowVolume, true)
array.push(zoneRetestVolumes, 0.0)
array.push(zoneAverageVolumes, volumeAverage)
// Extend zones to the latest bar. As price moves through a zone, fade it;
// remove it after price has swept through the full zone.
int zoneIndex = 0
while zoneIndex < array.size(zones)
box currentBox = array.get(zones, zoneIndex)
float top = array.get(zoneTops, zoneIndex)
float bottom = array.get(zoneBottoms, zoneIndex)
int side = array.get(zoneSides, zoneIndex)
float strength = array.get(zoneStrengths, zoneIndex)
float oldProgress = array.get(zoneProgresses, zoneIndex)
int bornBar = array.get(zoneBornBars, zoneIndex)
bool isLowVolumeZone = array.get(zoneIsLowVolume, zoneIndex)
float retestVolume = array.get(zoneRetestVolumes, zoneIndex)
float averageVolumeAtCreation = array.get(zoneAverageVolumes, zoneIndex)
float zoneRange = math.max(top - bottom, syminfo.mintick)
if isLowVolumeZone and side == 0 and bar_index > bornBar and high >= bottom and low <= top
side := close[1] < bottom ? 2 : close[1] > top ? -2 : close >= close[1] ? 2 : -2
array.set(zoneSides, zoneIndex, side)
if isLowVolumeZone and bar_index > bornBar and not na(volume) and averageVolumeAtCreation > 0
float overlap = math.max(0.0, math.min(high, top) - math.max(low, bottom))
float barRange = math.max(high - low, syminfo.mintick)
if overlap > 0
retestVolume += volume * overlap / barRange
array.set(zoneRetestVolumes, zoneIndex, retestVolume)
float currentProgress = side == 0 ? 0.0 : side > 0 ? math.max(0.0, math.min(1.0, (high - bottom) / zoneRange)) : math.max(0.0, math.min(1.0, (top - low) / zoneRange))
float progress = fadeOnTouch ? math.max(oldProgress, currentProgress) : (currentProgress >= 1.0 ? 1.0 : oldProgress)
bool removeLvn = isLowVolumeZone and (lowVolumePolicy == "Remove after full traversal" ? progress >= 1.0 : retestVolume >= averageVolumeAtCreation * lvnFillThreshold)
bool removeSweptSwingZone = not isLowVolumeZone and progress >= 1.0 and swingZonePolicy == "Remove after sweep"
bool keepSweptSwingZone = not isLowVolumeZone and progress >= 1.0 and swingZonePolicy == "Keep after sweep"
if removeLvn or removeSweptSwingZone
box.delete(currentBox)
array.remove(zones, zoneIndex)
array.remove(zoneTops, zoneIndex)
array.remove(zoneBottoms, zoneIndex)
array.remove(zoneSides, zoneIndex)
array.remove(zoneStrengths, zoneIndex)
array.remove(zoneProgresses, zoneIndex)
array.remove(zoneBornBars, zoneIndex)
array.remove(zoneIsLowVolume, zoneIndex)
array.remove(zoneRetestVolumes, zoneIndex)
array.remove(zoneAverageVolumes, zoneIndex)
else
int baseTransparency = int(88 - strength * 58)
int fadedTransparency = isLowVolumeZone and lowVolumePolicy == "Keep until retest volume" ? int(math.min(82.0, baseTransparency + progress * 35.0)) : int(math.min(96.0, baseTransparency + progress * 65.0))
color zoneColor = keepSweptSwingZone ? color.gray : isLowVolumeZone ? lowVolumeColor : side == 1 ? color.from_gradient(strength, 0.0, 1.0, buyWeakColor, buyStrongColor) : color.from_gradient(strength, 0.0, 1.0, sellWeakColor, sellStrongColor)
if keepSweptSwingZone
fadedTransparency := 82
box.set_right(currentBox, bar_index)
box.set_bgcolor(currentBox, color.new(zoneColor, fadedTransparency))
box.set_border_color(currentBox, color.new(zoneColor, math.max(fadedTransparency - 12, 0)))
box.set_text_color(currentBox, color.new(labelColor, 15))
box.set_text_size(currentBox, f_label_size(labelSizeInput))
array.set(zoneProgresses, zoneIndex, progress)
zoneIndex += 1
// Alert when a new qualifying swing-liquidity zone is identified.
alertcondition(not na(pivotHigh) and not na(volumeAverage[pivotRight]) and volumeAverage[pivotRight] > 0 and volume[pivotRight] / volumeAverage[pivotRight] >= minVolumeRatio, "New Buy-Side Liquidity Zone", "A new buy-side swing liquidity proxy has been identified.")
alertcondition(not na(pivotLow) and not na(volumeAverage[pivotRight]) and volumeAverage[pivotRight] > 0 and volume[pivotRight] / volumeAverage[pivotRight] >= minVolumeRatio, "New Sell-Side Liquidity Zone", "A new sell-side swing liquidity proxy has been identified.")