本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("VP Levels — VAH / POC / VAL", overlay = true, max_lines_count = 100, max_labels_count = 50, max_boxes_count = 500)
// ───────────── Inputs ─────────────
grpR = "Range"
mode = input.string("Visible range", "Range mode", options = ["Visible range", "Current session", "Last N bars", "From anchor time"], group = grpR,
tooltip = "Visible range = profiles whatever bars are on screen (recalculates when you scroll/zoom).\nCurrent session = resets each period (day on intraday charts, week on daily, month on weekly).\nLast N bars = rolling window.\nAnchor time = fixed start you choose.")
lookback = input.int(300, "Bars (Last N bars mode)", minval = 10, maxval = 5000, group = grpR)
anchorT = input.time(timestamp("2026-11-13T09:30:00"), "Anchor time (Anchor mode)", group = grpR)
grpP = "Profile accuracy"
rows = input.int(150, "Price rows (resolution)", minval = 20, maxval = 480, group = grpP,
tooltip = "More rows = finer price buckets = more precise levels.")
vaPct = input.float(70, "Value area %", minval = 1, maxval = 99, group = grpP) / 100
ltfIn = input.timeframe("", "Intrabar timeframe (blank = auto)", group = grpP,
tooltip = "Volume is distributed using lower-timeframe bars. Auto picks one that suits the chart timeframe. Must be lower than or equal to the chart timeframe.")
grpH = "Histogram"
showHist = input.bool(true, "Show histogram", group = grpH)
histWidth = input.int(60, "Histogram width (bars)", minval = 5, maxval = 300, group = grpH)
hOffset = input.int(20, "Gap from last bar (bars)", minval = 0, maxval = 150, group = grpH)
cVArow = input.color(color.new(#2962FF, 0), "Value area rows", group = grpH, inline = "h")
cOutRow = input.color(color.new(#9E9E9E, 0), "Outside rows", group = grpH, inline = "h")
cPOCrow = input.color(color.new(#F23645, 0), "POC row", group = grpH, inline = "h")
grpL = "Lines"
startAt = input.string("Range start", "Lines start at", options = ["Range start", "Last bar"], group = grpL)
extRight = input.bool(false, "Extend lines to right edge", group = grpL)
cVA = input.color(color.new(#2962FF, 0), "VAH / VAL", group = grpL, inline = "l")
cPOC = input.color(color.new(#F23645, 0), "POC", group = grpL, inline = "l")
lw = input.int(1, "Line width", minval = 1, maxval = 5, group = grpL)
showLbl = input.bool(false, "Show price labels", group = grpL)
// ───────────── Extra inputs (additions) ─────────────
grpPS = "Previous session levels"
showPrev = input.bool(false, "Show previous session VAH / POC / VAL", group = grpPS,
tooltip = "Profile of the last completed session (day on intraday charts, week on daily, month on weekly).")
cPrevVA = input.color(color.new(#2962FF, 50), "pVAH / pVAL", group = grpPS, inline = "ps")
cPrevPOC = input.color(color.new(#F23645, 50), "pPOC", group = grpPS, inline = "ps")
prevStyle = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = grpPS, inline = "ps2")
prevW = input.int(1, "Width", minval = 1, maxval = 5, group = grpPS, inline = "ps2")
prevLbl = input.bool(false, "Show price labels", group = grpPS)
grpN = "Naked POCs"
showNaked = input.bool(false, "Show naked POCs", group = grpN,
tooltip = "POCs of past sessions that price has not traded back through. Removed once touched.")
maxNaked = input.int(5, "Max naked POCs shown", minval = 1, maxval = 20, group = grpN)
cNaked = input.color(color.new(#FF9800, 30), "Color", group = grpN, inline = "n")
nakedStyle = input.string("Dotted", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpN, inline = "n")
nakedW = input.int(1, "Width", minval = 1, maxval = 5, group = grpN, inline = "n")
nakedLbl = input.bool(false, "Show price labels", group = grpN)
grpLV = "Low-volume nodes (LVN)"
showLVN = input.bool(false, "Show LVN zones", group = grpLV,
tooltip = "Thin areas inside the current profile where volume is low relative to the POC. Profile tails at the very top/bottom are ignored.")
lvnPct = input.float(25, "Threshold (% of POC volume)", minval = 1, maxval = 90, group = grpLV) / 100
lvnSmooth = input.int(2, "Smoothing (rows each side)", minval = 0, maxval = 10, group = grpLV,
tooltip = "Averages neighbouring rows so single thin rows don't create noise.")
maxLVN = input.int(5, "Max zones", minval = 1, maxval = 15, group = grpLV)
lvnFrom = input.string("Range start", "Shade from", options = ["Range start", "Histogram only"], group = grpLV)
cLVN = input.color(color.new(#FFEB3B, 85), "Zone color", group = grpLV, inline = "lv")
lvnBorder = input.bool(false, "Border", group = grpLV, inline = "lv")
styleOf(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
// ───────────── Timeframe handling (works on any chart TF) ─────────────
tfSec = timeframe.in_seconds()
autoTF() =>
tfSec < 60 ? timeframe.period : // seconds charts: use the chart bars themselves
tfSec <= 3600 ? "1" : // 1m – 1h -> 1-minute intrabars
tfSec <= 14400 ? "5" : // up to 4h -> 5-minute
tfSec <= 86400 ? "15" : // daily -> 15-minute
tfSec <= 604800 ? "60" : // weekly -> 1-hour
"240" // monthly -> 4-hour
ltf = ltfIn == "" ? autoTF() : ltfIn
// Session reset period scales with the chart
resetTF = tfSec < 86400 ? "D" : tfSec < 604800 ? "W" : tfSec < 2592000 ? "M" : "12M"
[hA, lA, vA] = request.security_lower_tf(syminfo.tickerid, ltf, [high, low, volume])
// ───────────── Collect intrabars in range ─────────────
var array<float> bH = array.new<float>()
var array<float> bL = array.new<float>()
var array<float> bV = array.new<float>()
var array<int> bI = array.new<int>()
clearAll() =>
bH.clear()
bL.clear()
bV.clear()
bI.clear()
if mode == "Current session" and timeframe.change(resetTF)
clearAll()
inRange = switch mode
"Visible range" => time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
"From anchor time" => time >= anchorT
=> true
if inRange
n = hA.size()
if n > 0
for i = 0 to n - 1
bH.push(hA.get(i))
bL.push(lA.get(i))
bV.push(nz(vA.get(i)))
bI.push(bar_index)
else
// No intrabar data for this bar (older history) -> fall back to the chart bar
bH.push(high)
bL.push(low)
bV.push(nz(volume))
bI.push(bar_index)
if mode == "Last N bars"
while bI.size() > 0 and bI.first() <= bar_index - lookback
bH.shift()
bL.shift()
bV.shift()
bI.shift()
// Safety cap (Pine arrays max 100k elements)
while bI.size() > 95000
bH.shift()
bL.shift()
bV.shift()
bI.shift()
// ───────────── Session profiles for previous-session levels & naked POCs (addition) ─────────────
sessProfile(array<float> H, array<float> L, array<float> V) =>
float rPoc = na
float rVah = na
float rVal = na
if H.size() > 0
float sTop = H.max()
float sBot = L.min()
float sStep = (sTop - sBot) / rows
if sStep > 0
sv = array.new<float>(rows, 0.0)
for i = 0 to H.size() - 1
h = H.get(i)
l = L.get(i)
v = V.get(i)
if v > 0
if h <= l
idx = math.min(rows - 1, math.floor((l - sBot) / sStep))
sv.set(idx, sv.get(idx) + v)
else
r0 = math.max(0, math.floor((l - sBot) / sStep))
r1 = math.min(rows - 1, math.floor((h - sBot) / sStep))
for r = r0 to r1
rb = sBot + r * sStep
ov = math.min(h, rb + sStep) - math.max(l, rb)
if ov > 0
sv.set(r, sv.get(r) + v * ov / (h - l))
if sv.max() > 0
pIdx = sv.indexof(sv.max())
tgt = sv.sum() * vaPct
u = pIdx
d = pIdx
a = sv.get(pIdx)
while a < tgt and (u < rows - 1 or d > 0)
vU = u < rows - 1 ? sv.get(u + 1) : -1.0
vD = d > 0 ? sv.get(d - 1) : -1.0
if vU >= vD
u += 1
a += vU
else
d -= 1
a += vD
rPoc := math.round_to_mintick(sBot + (pIdx + 0.5) * sStep)
rVah := math.round_to_mintick(sBot + (u + 1) * sStep)
rVal := math.round_to_mintick(sBot + d * sStep)
[rPoc, rVah, rVal]
needSess = showPrev or showNaked
newSess = timeframe.change(resetTF)
var array<float> sH = array.new<float>()
var array<float> sL = array.new<float>()
var array<float> sV = array.new<float>()
var int sStart = na
var bool sawReset = false
var float pPOC = na
var float pVAH = na
var float pVAL = na
var int pStart = na
var array<float> nakedP = array.new<float>()
var array<int> nakedX = array.new<int>()
if needSess
if newSess
// Close out the finished session (skip the first, partial one on the chart)
if sawReset and sH.size() > 0
[sp, sh, sl] = sessProfile(sH, sL, sV)
if not na(sp)
pPOC := sp
pVAH := sh
pVAL := sl
pStart := sStart
nakedP.push(sp)
nakedX.push(sStart)
if nakedP.size() > 50
nakedP.shift()
nakedX.shift()
sawReset := true
sH.clear()
sL.clear()
sV.clear()
sStart := bar_index
if na(sStart)
sStart := bar_index
n2 = hA.size()
if n2 > 0
for i = 0 to n2 - 1
sH.push(hA.get(i))
sL.push(lA.get(i))
sV.push(nz(vA.get(i)))
else
sH.push(high)
sL.push(low)
sV.push(nz(volume))
while sH.size() > 95000
sH.shift()
sL.shift()
sV.shift()
// Remove POCs that price has traded through
if nakedP.size() > 0
for k = nakedP.size() - 1 to 0
p = nakedP.get(k)
if low <= p and high >= p
nakedP.remove(k)
nakedX.remove(k)
// ───────────── Build profile + draw ─────────────
var array<box> hist = array.new<box>()
var array<line> lvls = array.new<line>()
var array<label> lbls = array.new<label>()
var array<box> lvnBoxes = array.new<box>()
if barstate.islast
for b in hist
b.delete()
for l in lvls
l.delete()
for t in lbls
t.delete()
for b in lvnBoxes
b.delete()
hist.clear()
lvls.clear()
lbls.clear()
lvnBoxes.clear()
if bH.size() > 0
float top = bH.max()
float bot = bL.min()
float step = (top - bot) / rows
if step > 0
vol = array.new<float>(rows, 0.0)
// Spread each intrabar's volume across the rows it traded through
for i = 0 to bH.size() - 1
h = bH.get(i)
l = bL.get(i)
v = bV.get(i)
if v > 0
if h <= l
idx = math.min(rows - 1, math.floor((l - bot) / step))
vol.set(idx, vol.get(idx) + v)
else
r0 = math.max(0, math.floor((l - bot) / step))
r1 = math.min(rows - 1, math.floor((h - bot) / step))
for r = r0 to r1
rb = bot + r * step
ov = math.min(h, rb + step) - math.max(l, rb)
if ov > 0
vol.set(r, vol.get(r) + v * ov / (h - l))
// POC
maxV = vol.max()
pocIdx = vol.indexof(maxV)
// Value area: expand from POC adding the heavier neighbour
target = vol.sum() * vaPct
up = pocIdx
dn = pocIdx
acc = vol.get(pocIdx)
while acc < target and (up < rows - 1 or dn > 0)
vUp = up < rows - 1 ? vol.get(up + 1) : -1.0
vDn = dn > 0 ? vol.get(dn - 1) : -1.0
if vUp >= vDn
up += 1
acc += vUp
else
dn -= 1
acc += vDn
vah = math.round_to_mintick(bot + (up + 1) * step)
val = math.round_to_mintick(bot + dn * step)
poc = math.round_to_mintick(bot + (pocIdx + 0.5) * step)
xRight = bar_index + hOffset + histWidth
x1 = startAt == "Range start" ? bI.first() : bar_index
ext = extRight ? extend.right : extend.none
// Histogram rows, anchored on the right, growing left
if showHist and maxV > 0
for r = 0 to rows - 1
v = vol.get(r)
if v > 0
len = math.max(1, math.round(v / maxV * histWidth))
c = r == pocIdx ? cPOCrow : (r >= dn and r <= up) ? cVArow : cOutRow
rb = bot + r * step
hist.push(box.new(xRight - len, rb + step * 0.85, xRight, rb + step * 0.15,
border_color = c, border_width = 1, bgcolor = c))
// Level lines
lvls.push(line.new(x1, vah, xRight, vah, color = cVA, width = lw, extend = ext))
lvls.push(line.new(x1, poc, xRight, poc, color = cPOC, width = lw, extend = ext))
lvls.push(line.new(x1, val, xRight, val, color = cVA, width = lw, extend = ext))
if showLbl
lbls.push(label.new(xRight, vah, "VAH " + str.tostring(vah, format.mintick), style = label.style_label_left, color = color.new(cVA, 85), textcolor = cVA, size = size.small))
lbls.push(label.new(xRight, poc, "POC " + str.tostring(poc, format.mintick), style = label.style_label_left, color = color.new(cPOC, 85), textcolor = cPOC, size = size.small))
lbls.push(label.new(xRight, val, "VAL " + str.tostring(val, format.mintick), style = label.style_label_left, color = color.new(cVA, 85), textcolor = cVA, size = size.small))
// ───── LVN zones (addition) ─────
if showLVN and maxV > 0
sm = array.new<float>(rows, 0.0)
for r = 0 to rows - 1
float s = 0.0
int cnt = 0
for j = math.max(0, r - lvnSmooth) to math.min(rows - 1, r + lvnSmooth)
s += vol.get(j)
cnt += 1
sm.set(r, s / cnt)
thr = sm.max() * lvnPct
lx1 = lvnFrom == "Range start" ? bI.first() : xRight - histWidth
int lvI = 0
int found = 0
while lvI < rows and found < maxLVN
if sm.get(lvI) < thr
int lvJ = lvI
while lvJ + 1 < rows and sm.get(lvJ + 1) < thr
lvJ += 1
// Only zones with heavier volume on both sides (ignores profile tails)
if lvI > 0 and lvJ < rows - 1
lvnBoxes.push(box.new(lx1, bot + (lvJ + 1) * step, xRight, bot + lvI * step,
border_color = cLVN, border_width = lvnBorder ? 1 : 0, bgcolor = cLVN))
found += 1
lvI := lvJ + 1
else
lvI += 1
// ───────────── Previous session & naked POC drawing (addition) ─────────────
var array<line> xLines = array.new<line>()
var array<label> xLbls = array.new<label>()
if barstate.islast
for l in xLines
l.delete()
for t in xLbls
t.delete()
xLines.clear()
xLbls.clear()
xR = bar_index + hOffset + histWidth
if showPrev and not na(pPOC)
px1 = math.max(pStart, bar_index - 4999)
pst = styleOf(prevStyle)
xLines.push(line.new(px1, pVAH, xR, pVAH, color = cPrevVA, width = prevW, style = pst))
xLines.push(line.new(px1, pPOC, xR, pPOC, color = cPrevPOC, width = prevW, style = pst))
xLines.push(line.new(px1, pVAL, xR, pVAL, color = cPrevVA, width = prevW, style = pst))
if prevLbl
xLbls.push(label.new(xR, pVAH, "pVAH " + str.tostring(pVAH, format.mintick), style = label.style_label_left, color = color.new(cPrevVA, 85), textcolor = cPrevVA, size = size.small))
xLbls.push(label.new(xR, pPOC, "pPOC " + str.tostring(pPOC, format.mintick), style = label.style_label_left, color = color.new(cPrevPOC, 85), textcolor = cPrevPOC, size = size.small))
xLbls.push(label.new(xR, pVAL, "pVAL " + str.tostring(pVAL, format.mintick), style = label.style_label_left, color = color.new(cPrevVA, 85), textcolor = cPrevVA, size = size.small))
if showNaked and nakedP.size() > 0
nst = styleOf(nakedStyle)
int drawn = 0
for k = nakedP.size() - 1 to 0
if drawn >= maxNaked
break
np = nakedP.get(k)
nx = nakedX.get(k)
// Skip if it's the previous session POC already drawn above
if showPrev and nx == pStart
continue
xLines.push(line.new(math.max(nx, bar_index - 4999), np, xR, np, color = cNaked, width = nakedW, style = nst))
if nakedLbl
xLbls.push(label.new(xR, np, "nPOC " + str.tostring(np, format.mintick), style = label.style_label_left, color = color.new(cNaked, 85), textcolor = cNaked, size = size.small))
drawn += 1