本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Volume Profile Context | jjart", overlay = true, max_boxes_count = 500, max_lines_count = 20, max_labels_count = 20, max_bars_back = 2000)
string calculation = "Calculation"
int lookback = input.int(360, "Lookback bars", minval = 20, maxval = 1500, group = calculation)
int rows = input.int(160, "Price rows", minval = 40, maxval = 240, group = calculation)
bool closedOnly = input.bool(true, "Use closed bars only", tooltip = "Exclude the live candle. The rolling profile still changes as its window advances.", group = calculation)
float valuePercent = input.float(70, "Value area (%)", minval = 1, maxval = 100, group = calculation)
string nodes = "Volume zones"
int strength = input.int(4, "Peak neighborhood (rows)", minval = 1, maxval = 15, group = nodes)
float minimum = input.float(20, "Minimum peak volume (% of POC)", minval = 0, maxval = 100, group = nodes)
int maxZones = input.int(8, "Maximum zones", minval = 1, maxval = 20, group = nodes)
color zoneColor = input.color(color.new(color.blue, 83), "Zone color", group = nodes)
bool extendZones = input.bool(true, "Extend zones right", group = nodes)
string appearance = "Profile and levels"
bool showProfile = input.bool(true, "Show bidirectional volume profile", group = appearance)
int profileWidth = input.int(45, "Histogram width (bars)", minval = 5, maxval = 150, group = appearance)
int offset = input.int(10, "Histogram offset (bars)", minval = 1, maxval = 100, group = appearance)
color histogramColor = input.color(color.new(color.gray, 65), "Histogram color", group = appearance)
color bullishColor = input.color(color.new(color.teal, 30), "Bullish volume color", group = appearance)
color bearishColor = input.color(color.new(color.red, 38), "Bearish volume color", group = appearance)
color heatLow = input.color(color.new(#18253d, 18), "Heatmap low volume", group = appearance)
color heatHigh = input.color(color.new(#30d5c8, 4), "Heatmap high volume", group = appearance)
bool showPoc = input.bool(true, "Show POC", group = appearance)
bool showValue = input.bool(true, "Show VAH and VAL", group = appearance)
color pocColor = input.color(color.orange, "POC color", group = appearance)
color valueColor = input.color(color.aqua, "Value area color", group = appearance)
bool showLabels = input.bool(true, "Show level labels", group = appearance)
int maxLowNodes = input.int(3, "Maximum low-volume nodes", minval = 1, maxval = 12, group = nodes)
color lowNodeColor = input.color(color.new(color.purple, 82), "Low-volume node color", group = nodes)
var array<box> drawings = array.new<box>()
var array<line> guides = array.new<line>()
var array<label> captions = array.new<label>()
var table status = table.new(position.bottom_right, 1, 1)
addGuide(int start, float price, string name, color shade) =>
array.push(guides, line.new(start, price, bar_index, price, color = shade, width = name == "POC" ? 2 : 1))
if showLabels
array.push(captions, label.new(bar_index + offset, price, name + " " + str.tostring(price, format.mintick), style = label.style_none, textcolor = shade))
// Rebuild a single rolling snapshot; no historical levels or future signals are implied.
if barstate.islast
while array.size(drawings) > 0
box.delete(array.pop(drawings))
while array.size(guides) > 0
line.delete(array.pop(guides))
while array.size(captions) > 0
label.delete(array.pop(captions))
int skip = closedOnly ? 1 : 0
int count = math.min(lookback, bar_index + 1 - skip)
table.cell(status, 0, 0, "", text_color = chart.fg_color)
if count > 0
float bottom = low[skip]
float top = high[skip]
for j = skip to skip + count - 1
bottom := math.min(bottom, low[j])
top := math.max(top, high[j])
// Keep a nonzero price grid even for a flat market.
float step = math.max((top - bottom) / rows, syminfo.mintick)
array<float> bins = array.new<float>(rows, 0.0)
array<float> bullishBins = array.new<float>(rows, 0.0)
array<float> bearishBins = array.new<float>(rows, 0.0)
for j = skip to skip + count - 1
float amount = math.max(nz(volume[j]), 0)
float span = high[j] - low[j]
int first = math.max(0, math.min(rows - 1, int(math.floor((low[j] - bottom) / step))))
int last = math.max(first, math.min(rows - 1, int(math.floor((high[j] - bottom) / step))))
if span == 0
array.set(bins, first, array.get(bins, first) + amount)
if close[j] >= open[j]
array.set(bullishBins, first, array.get(bullishBins, first) + amount)
else
array.set(bearishBins, first, array.get(bearishBins, first) + amount)
else
// Uniform allocation over each candle's high-low range is an estimate,
// not exchange volume-at-price, bid/ask volume, or order flow.
for r = first to last
float overlap = math.max(0.0, math.min(high[j], bottom + (r + 1) * step) - math.max(low[j], bottom + r * step))
array.set(bins, r, array.get(bins, r) + amount * overlap / span)
if close[j] >= open[j]
array.set(bullishBins, r, array.get(bullishBins, r) + amount * overlap / span)
else
array.set(bearishBins, r, array.get(bearishBins, r) + amount * overlap / span)
float total = array.sum(bins)
float peak = array.max(bins)
if total > 0
int poc = array.indexof(bins, peak)
int lower = poc
int upper = poc
float accumulated = peak
while accumulated < total * valuePercent / 100 and (lower > 0 or upper < rows - 1)
float above = upper < rows - 1 ? array.get(bins, upper + 1) : -1.0
float below = lower > 0 ? array.get(bins, lower - 1) : -1.0
if above >= below and upper < rows - 1
upper += 1
accumulated += array.get(bins, upper)
else
lower -= 1
accumulated += array.get(bins, lower)
int start = bar_index - skip - count + 1
if showPoc
addGuide(start, bottom + (poc + 0.5) * step, "POC", pocColor)
if showValue
addGuide(start, bottom + (upper + 1) * step, "VAH", valueColor)
addGuide(start, bottom + lower * step, "VAL", valueColor)
array<float> candidates = array.new<float>(rows, -1.0)
array<float> lowCandidates = array.new<float>(rows, -1.0)
for r = 0 to rows - 1
float v = array.get(bins, r)
bool isPeak = v > 0 and v >= peak * minimum / 100
for neighbor = math.max(0, r - strength) to math.min(rows - 1, r + strength)
// Resolve equal-volume plateaus deterministically toward the lower row.
if neighbor != r and (array.get(bins, neighbor) > v or (neighbor < r and array.get(bins, neighbor) == v))
isPeak := false
if isPeak
array.set(candidates, r, v)
bool isTrough = v > 0
for neighbor = math.max(0, r - strength) to math.min(rows - 1, r + strength)
if neighbor != r and (array.get(bins, neighbor) < v or (neighbor < r and array.get(bins, neighbor) == v))
isTrough := false
if isTrough
array.set(lowCandidates, r, v)
if showProfile and v > 0
int length = math.max(1, int(math.round(profileWidth * v / peak)))
float balance = (array.get(bullishBins, r) - array.get(bearishBins, r)) / v
color intensity = color.from_gradient(v / peak, 0.0, 1.0, heatLow, heatHigh)
color directional = balance > 0.15 ? color.new(bullishColor, 10) : balance < -0.15 ? color.new(bearishColor, 10) : intensity
color heatColor = r >= lower and r <= upper ? color.from_gradient(v / peak, 0.0, 1.0, color.new(valueColor, 82), directional) : directional
array.push(drawings, box.new(bar_index + offset, bottom + (r + 1) * step, bar_index + offset + length, bottom + r * step, border_color = na, bgcolor = heatColor))
for selection = 1 to maxZones
float best = array.max(candidates)
if best < 0
break
int r = array.indexof(candidates, best)
array.push(drawings, box.new(start, bottom + (r + 1) * step, bar_index, bottom + r * step, border_color = na, bgcolor = zoneColor, extend = extendZones ? extend.right : extend.none))
array.set(candidates, r, -1.0)
for selection = 1 to maxLowNodes
float best = array.max(lowCandidates)
if best < 0
break
int r = array.indexof(lowCandidates, best)
array.push(drawings, box.new(start, bottom + (r + 1) * step, bar_index, bottom + r * step, border_color = na, bgcolor = lowNodeColor, extend = extendZones ? extend.right : extend.none))
array.set(lowCandidates, r, -1.0)
else
table.cell(status, 0, 0, "No positive volume in the selected window.", text_color = chart.fg_color)
else
table.cell(status, 0, 0, "Waiting for a closed bar.", text_color = chart.fg_color)