本页源码来自 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/
// © profitzonetrades
//@version=6
indicator("Profit zone trades High Volume Zones", overlay=true, max_lines_count=100, max_labels_count=100)
// ================= INPUTS =================
lookback = input.int(200, "Bars for volume profile", minval=20)
rows = input.int(30, "Number of rows (profile resolution)", minval=5, maxval=100)
maxZones = input.int(5, "Number of HVN zones to display", minval=1, maxval=20)
recalcEvery = input.int(55, "Recalculate profile every X bars", minval=5)
hvnColor = input.color(color.orange, "Line color")
hvnWidth = input.int(2, "Line width", minval=1, maxval=5)
extendBars = input.int(60, "Extend line right (bars)", minval=0)
showLabels = input.bool(true, "Show price label")
// ================= VOLUME PROFILE =================
var line[] hvnLines = array.new_line(0)
var label[] hvnLabels = array.new_label(0)
recalcNow = bar_index % recalcEvery == 0
if recalcNow and bar_index >= lookback
// Clear previous drawings
if array.size(hvnLines) > 0
for i = 0 to array.size(hvnLines) - 1
line.delete(array.get(hvnLines, i))
array.clear(hvnLines)
if array.size(hvnLabels) > 0
for i = 0 to array.size(hvnLabels) - 1
label.delete(array.get(hvnLabels, i))
array.clear(hvnLabels)
hi = ta.highest(high, lookback)
lo = ta.lowest(low, lookback)
step = (hi - lo) / rows
step := step == 0 ? syminfo.mintick : step
// Accumulate volume by price row
volArr = array.new_float(rows, 0.0)
for i = 0 to lookback - 1
c = close[i]
idx = int(math.max(0, math.min(rows - 1, math.floor((c - lo) / step))))
array.set(volArr, idx, array.get(volArr, idx) + volume[i])
// POC: row with the highest accumulated volume
pocVolume = array.max(volArr)
pocIndex = array.indexof(volArr, pocVolume)
pocPrice = lo + (pocIndex + 0.5) * step
// Draw POC last so it remains visible above the HVN line
pocLine = line.new(
bar_index - lookback, pocPrice,
bar_index + extendBars, pocPrice,
color=color.red, width=hvnWidth + 1, style=line.style_solid)
array.push(hvnLines, pocLine)
if showLabels
pocLabel = label.new(
bar_index + extendBars, pocPrice,
"POC " + str.tostring(pocPrice, format.mintick),
style=label.style_label_right,
color=color.red, textcolor=color.white, size=size.small)
array.push(hvnLabels, pocLabel)
// Select the N rows with the highest volume
volCopy = array.copy(volArr)
limit = math.min(maxZones, rows)
for k = 0 to limit - 1
maxV = -1.0
maxPos = -1
for j = 0 to array.size(volCopy) - 1
vv = array.get(volCopy, j)
if vv > maxV
maxV := vv
maxPos := j
if maxPos >= 0 and maxV > 0
rowBot = lo + maxPos * step
rowTop = rowBot + step
rowMid = (rowTop + rowBot) / 2
l = line.new(bar_index - lookback, rowMid, bar_index + extendBars, rowMid, color=hvnColor, width=hvnWidth, style=line.style_solid)
array.push(hvnLines, l)
if showLabels
lbl = label.new(bar_index + extendBars, rowMid, str.tostring(rowMid, format.mintick), style=label.style_label_left, color=color.new(hvnColor, 0), textcolor=color.black, size=size.small)
array.push(hvnLabels, lbl)
array.set(volCopy, maxPos, -1.0)