本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =============================================================================
// DIFFERENT VOLUME PROFILE ANATOMIES (1 D-shape | 2 B-shape | 3 P-shape | 4 Double Zone)
// - Live mode : builds a real volume profile from the chart's bars, finds POC / VAH / VAL,
// smooths it, looks for a 2nd peak and the POC position, then CLASSIFIES the
// shape (D / B / P / Double Zone) and draws histogram + arrows + captions.
// - Gallery : generates 4 synthetic profiles from gaussian mixtures, runs the SAME classifier
// and SAME renderer on them, then builds a candle path from the profile levels
// (POC / VAH / VAL / 2nd peak), so you get the 2x2 poster from the image.
//
// Gallery sits to the right of the last bar -> scroll/zoom out the chart to see it.
// =============================================================================
indicator('Volume Profile Anatomies (D / B / P / Double Zone)', shorttitle = 'VP Anatomies', overlay = true, max_bars_back = 1000, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ------------------------------- INPUTS --------------------------------------
liveOn = input.bool(true, 'Live Detection', group = 'General')
galleryOn = input.bool(true, 'Show Gallery (1-4)', group = 'General')
lookback = input.int(100, 'Profile Lookback (bars)', minval = 20, maxval = 1000, group = 'Live Profile')
rowsIn = input.int(30, 'Profile Rows', minval = 10, maxval = 60, group = 'Live Profile')
wLive = input.int(25, 'Histogram Width (bars)', minval = 8, maxval = 80, group = 'Live Profile')
placeIn = input.string('Right of last bar', 'Histogram Placement', options = ['Right of last bar', 'Left of range'], group = 'Live Profile')
vaPct = input.float(70.0, 'Value Area %', minval = 50.0, maxval = 95.0, group = 'Live Profile') / 100.0
showTable = input.bool(true, 'Show diagnostics table', group = 'Live Profile')
smoothN = input.int(2, 'Smoothing passes (classifier)', minval = 0, maxval = 5, group = 'Classifier')
secondRatio = input.float(0.60, '2nd peak min size vs POC', minval = 0.2, maxval = 1.0, step = 0.05, group = 'Classifier')
valleyRatio = input.float(0.70, 'Valley max size vs 2nd peak', minval = 0.1, maxval = 1.0, step = 0.05, group = 'Classifier')
posHi = input.float(0.66, 'P-shape if POC position above', minval = 0.5, maxval = 0.95, step = 0.01, group = 'Classifier')
posLo = input.float(0.34, 'B-shape if POC position below', minval = 0.05, maxval = 0.5, step = 0.01, group = 'Classifier')
galOffset = input.int(50, 'Gallery X offset from last bar', minval = 5, maxval = 250, group = 'Gallery')
galBgOn = input.bool(true, 'Gallery panel background', group = 'Gallery')
galBg = input.color(color.white, 'Panel background', group = 'Gallery')
galTxt = input.color(color.black, 'Gallery text colour', group = 'Gallery')
colUp = input.color(#1aa88a, 'Volume above POC', group = 'Colours')
colDn = input.color(#e5383b, 'Volume below POC', group = 'Colours')
colPoc = input.color(#f4b400, 'POC row', group = 'Colours')
colLine = input.color(#222222, 'Dashed levels', group = 'Colours')
// --- Gate flags (Gallery can be switched off independently) ---
showLive = liveOn
showGallery = galleryOn and liveOn ? true : galleryOn // gallery is independent of live
// (the line above keeps gallery available whether or not live detection is on;
// set galleryOn = false to disable the 2x2 poster entirely)
// ------------------------------ DRAWING STORE --------------------------------
var array<box> gBox = array.new<box>()
var array<line> gLine = array.new<line>()
var array<label> gLbl = array.new<label>()
var table tbl = table.new(position.top_right, 2, 6, bgcolor = color.new(color.white, 8), border_color = color.gray, border_width = 1, frame_color = color.gray, frame_width = 1)
f_clear() =>
while array.size(gBox) > 0
box.delete(array.pop(gBox))
while array.size(gLine) > 0
line.delete(array.pop(gLine))
while array.size(gLbl) > 0
label.delete(array.pop(gLbl))
f_bx(int l, float t, int r, float b, color bg, color bc) =>
array.push(gBox, box.new(left = l, top = t, right = r, bottom = b, border_color = bc, bgcolor = bg, xloc = xloc.bar_index))
f_lnS(int x1, float y1, int x2, float y2, color c, int w) =>
array.push(gLine, line.new(x1, y1, x2, y2, xloc.bar_index, extend.none, c, line.style_solid, w))
f_lnD(int x1, float y1, int x2, float y2, color c, int w) =>
array.push(gLine, line.new(x1, y1, x2, y2, xloc.bar_index, extend.none, c, line.style_dashed, w))
f_lnA(int x1, float y1, int x2, float y2, color c, int w) =>
array.push(gLine, line.new(x1, y1, x2, y2, xloc.bar_index, extend.none, c, line.style_arrow_right, w))
f_lnB(int x1, float y1, int x2, float y2, color c, int w) =>
array.push(gLine, line.new(x1, y1, x2, y2, xloc.bar_index, extend.none, c, line.style_arrow_both, w))
// plain text (no background)
f_txt(int x, float y, string s, color tc) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_none, tc, size.small))
f_txtT(int x, float y, string s, color tc) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_none, tc, size.tiny))
f_txtL(int x, float y, string s, color tc) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_none, tc, size.large))
// tag with white background (readable on any chart theme), text to the right of the point
f_tag(int x, float y, string s, color tc) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 10), label.style_label_left, tc, size.small))
// big title above a point
f_title(int x, float y, string s) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 5), label.style_label_down, color.black, size.large))
// tag centered below a point
f_cap(int x, float y, string s, color tc) =>
array.push(gLbl, label.new(x, y, s, xloc.bar_index, yloc.price, color.new(color.white, 5), label.style_label_up, tc, size.small))
// ------------------------------ NAMES / TEXT ---------------------------------
f_name(int k) =>
k == 1 ? 'D-SHAPED PROFILE' : k == 2 ? 'B-SHAPED PROFILE' : k == 3 ? 'P-SHAPED PROFILE' : 'DOUBLE ZONE PROFILE'
f_badge(int k) =>
k == 1 ? '①' : k == 2 ? '②' : k == 3 ? '③' : '④'
f_market(int k, bool up) =>
k == 1 ? 'BALANCED MARKET NORMAL VOLATILITY' : k == 2 ? 'BALANCED MARKET STRONG SHORT TERM VOLATILITY' : k == 3 ? up ? 'BULL RUN MOMENTUM' : 'BEAR RUN MOMENTUM' : 'BALANCED MARKET CONSOLIDATION'
f_mom(int k, bool up) =>
string strength = k == 1 ? 'NORMAL' : k == 4 ? 'WEAK' : 'STRONG'
strength + (up ? ' BULLISH MOMENTUM ↑' : ' BEARISH MOMENTUM ↓')
// ------------------------------ PROFILE MATHS --------------------------------
// Value area: expand from POC row, always adding the larger neighbour, until pct of volume is covered
f_valueArea(array<float> v, int poc, float pct) =>
int n = array.size(v)
float total = array.sum(v)
float acc = array.get(v, poc)
int up = poc
int dn = poc
while acc < total * pct and (up < n - 1 or dn > 0)
float upRaw = array.get(v, math.min(up + 1, n - 1))
float dnRaw = array.get(v, math.max(dn - 1, 0))
float upV = up < n - 1 ? upRaw : -1.0
float dnV = dn > 0 ? dnRaw : -1.0
if upV >= dnV
up := up + 1
acc := acc + upV
acc
else
dn := dn - 1
acc := acc + dnV
acc
[up, dn]
// Shape classifier: returns [kind, pocIndex(smoothed), secondPeakIndex]
// kind 1 = D, 2 = B (POC low), 3 = P (POC high), 4 = Double Zone (bimodal)
f_classify(array<float> v) =>
int n = array.size(v)
array<float> s = array.copy(v)
for p = 1 to smoothN by 1
array<float> t = array.copy(s)
for i = 0 to n - 1 by 1
float a = array.get(t, math.max(i - 1, 0))
float b = array.get(t, i)
float c = array.get(t, math.min(i + 1, n - 1))
array.set(s, i, (a + 2.0 * b + c) / 4.0)
float pocV = array.max(s)
int poc = array.indexof(s, pocV)
int minSep = math.max(3, math.round(n * 0.2))
int p2 = -1
float p2V = 0.0
for i = 0 to n - 1 by 1
if math.abs(i - poc) >= minSep
float cv = array.get(s, i)
float lv = array.get(s, math.max(i - 1, 0))
float rv = array.get(s, math.min(i + 1, n - 1))
bool isPeak = (i == 0 or cv >= lv) and (i == n - 1 or cv >= rv)
if isPeak and cv > p2V
p2V := cv
p2 := i
p2
int kind = 0
if p2 >= 0 and p2V >= secondRatio * pocV
int a = math.min(poc, p2)
int b = math.max(poc, p2)
float valley = pocV
for j = a to b by 1
valley := math.min(valley, array.get(s, j))
valley
if valley <= valleyRatio * p2V
kind := 4
kind
if kind == 0
float pos = n > 1 ? poc / (n - 1.0) : 0.5
kind := pos > posHi ? 3 : pos < posLo ? 2 : 1
kind
[kind, poc, p2]
// Histogram renderer (same for live data and synthetic gallery data)
// Green above POC, red below POC, POC row highlighted, value area opaque, outside faded
f_drawProfile(array<float> v, float lowP, float stepP, int x0, int wBars) =>
int n = array.size(v)
float vmax = math.max(array.max(v), 0.0000001)
int pocI = array.indexof(v, array.max(v))
[upI, dnI] = f_valueArea(v, pocI, vaPct)
for r = 0 to n - 1 by 1
float vv = array.get(v, r)
int w = math.max(1, math.round(wBars * vv / vmax))
bool inVA = r >= dnI and r <= upI
color base = r > pocI ? colUp : r < pocI ? colDn : colPoc
f_bx(x0, lowP + (r + 1) * stepP - stepP * 0.08, x0 + w, lowP + r * stepP + stepP * 0.08, color.new(base, inVA ? 12 : 55), na)
[lowP + (pocI + 0.5) * stepP, lowP + (upI + 1) * stepP, lowP + dnI * stepP, pocI]
// ------------------------------ GALLERY GENERATORS ---------------------------
f_gauss(float t, float m, float s) =>
math.exp(-0.5 * math.pow((t - m) / s, 2))
// synthetic volume at normalised price t (0 = low, 1 = high) for each anatomy
f_synth(int kind, float t) =>
float v = 0.0
if kind == 1
v := f_gauss(t, 0.50, 0.17)
v
else if kind == 2
v := f_gauss(t, 0.28, 0.12) + 0.30 * f_gauss(t, 0.62, 0.22)
v
else if kind == 3
v := f_gauss(t, 0.76, 0.10) + 0.28 * f_gauss(t, 0.45, 0.22)
v
else
v := f_gauss(t, 0.72, 0.08) + 0.90 * f_gauss(t, 0.27, 0.08) + 0.12
v
v + 0.04
// consolidation phase of the generated price path
f_phaseB(int kind, float w, float pocT, float vahT, float valT, float lowT, float highT) =>
float osc = math.sin(w * 2.0 * math.pi * 1.5)
float rng = math.max(vahT - valT, 0.10)
float t = pocT
if kind == 1
t := pocT + osc * rng * 0.5
t
else if kind == 2
t := pocT + (osc > 0 ? osc * 0.35 : osc * 0.9) * rng
t
else if kind == 3
t := pocT + osc * rng * 0.4
t
else
t := lowT + (highT - lowT) * (0.5 + 0.5 * math.sin(w * 2.0 * math.pi * 1.25 - math.pi / 2.0))
t
math.min(t, 0.93)
// full path target: trend leg -> consolidation around profile levels -> breakout
f_target(int kind, float u, float pocT, float vahT, float valT, float lowT, float highT) =>
float base = kind == 3 ? valT : kind == 4 ? lowT : pocT
float t = 0.0
if u < 0.28
t := 0.05 + (base - 0.05) * (u / 0.28)
t
else if u < 0.80
t := f_phaseB(kind, (u - 0.28) / 0.52, pocT, vahT, valT, lowT, highT)
t
else
float tb = f_phaseB(kind, 1.0, pocT, vahT, valT, lowT, highT)
t := tb + (0.97 - tb) * ((u - 0.80) / 0.20)
t
t
// one gallery panel
f_panel(int kind, int px, float pTop, float pH, int pW) =>
int rowsG = 22
int profW = 14
int nC = 18
int sp = 3
float yt = pTop - 0.16 * pH
float yb = pTop - pH + 0.20 * pH
float H = yt - yb
float stepG = H / rowsG
int axisX = px + 2
int profX0 = px + 3
int cx0 = profX0 + profW + 4
// panel background
if galBgOn
f_bx(px, pTop, px + pW, pTop - pH, galBg, color.new(color.gray, 60))
// synthetic profile generated from maths, then classified by the real classifier
array<float> v = array.new_float(rowsG, 0.0)
for r = 0 to rowsG - 1 by 1
array.set(v, r, f_synth(kind, (r + 0.5) / rowsG))
[kDet, pkI, p2I] = f_classify(v)
[pocP, vahP, valP, pocI] = f_drawProfile(v, yb, stepG, profX0, profW)
// title + axes + arrows
f_txtL(px + math.round(pW / 2.0), pTop - 0.06 * pH, f_badge(kind) + ' ' + f_name(kind) + (kDet == kind ? ' ✔' : ' ✖'), galTxt)
f_lnB(axisX, yb, axisX, yt, galTxt, 1)
f_txtT(px + 4, pTop - 0.11 * pH, 'PRICE', galTxt)
f_txtT(px + 13, pTop - 0.11 * pH, 'HIGH PRICE', galTxt)
f_txtT(px + 6, yb - 0.01 * pH, 'LOW PRICE', galTxt)
f_lnA(profX0, yb - 0.03 * pH, profX0 + profW, yb - 0.03 * pH, galTxt, 1)
f_txtT(profX0 + math.round(profW / 2.0), yb - 0.07 * pH, 'VOLUME', galTxt)
// POC (solid) + value area (dashed) lines through the candle area
int xEnd = px + pW - 2
f_lnS(profX0 + profW, pocP, xEnd, pocP, galTxt, 1)
f_lnD(profX0, vahP, xEnd, vahP, colLine, 2)
f_lnD(profX0, valP, xEnd, valP, colLine, 2)
f_txtT(profX0 + profW + 2, pocP, 'POC', galTxt)
// levels from the computed profile (normalised 0..1)
float pocT = (pocP - yb) / H
float vahT = (vahP - yb) / H
float valT = (valP - yb) / H
float p1T = (pkI + 0.5) / rowsG
float p2T = p2I >= 0 ? (p2I + 0.5) / rowsG : p1T
float lowT = math.min(p1T, p2T)
float highT = math.max(p1T, p2T)
// candle path generated from the levels
float prevC = yb + 0.03 * H
for i = 0 to nC - 1 by 1
float u = i / (nC - 1.0)
float t = f_target(kind, u, pocT, vahT, valT, lowT, highT)
t := t + 0.025 * math.sin(i * 2.7 + kind)
t := math.max(0.0, math.min(1.0, t))
float cl = yb + t * H
float op = prevC
float wk = 0.02 * H * (1.0 + math.abs(math.sin(i * 1.7 + kind)))
float hiC = math.max(op, cl) + wk
float loC = math.min(op, cl) - wk * 0.8
int x = cx0 + i * sp + 1
color cc = cl >= op ? colUp : colDn
f_lnS(x, loC, x, hiC, cc, 1)
f_bx(x - 1, math.max(op, cl), x + 1, math.min(op, cl), cc, cc)
prevC := cl
prevC
// momentum arrows (leg 1 into the range, leg 2 on the breakout)
float baseT = kind == 3 ? valT : kind == 4 ? lowT : pocT
float tb = f_phaseB(kind, 1.0, pocT, vahT, valT, lowT, highT)
f_lnA(cx0 - 3, yb + 0.03 * H, cx0 + math.round(0.28 * (nC - 1) * sp) - 3, yb + baseT * H + 0.04 * H, colUp, 2)
f_lnA(cx0 + math.round(0.80 * (nC - 1) * sp) + 4, yb + tb * H, cx0 + (nC - 1) * sp + 4, yb + 0.97 * H, colUp, 2)
// captions
string cap1 = kind == 1 ? 'BALANCED MARKET NORMAL\nVOLATILITY' : kind == 2 ? 'BALANCED MARKET\nSTRONG SHORT TERM\nVOLATILITY' : kind == 3 ? 'BULL RUN MOMENTUM' : 'BALANCED MARKET\nCONSOLIDATION'
f_txtT(px + math.round(pW * 0.27), pTop - pH + 0.08 * pH, cap1, galTxt)
f_txtT(px + math.round(pW * 0.72), pTop - pH + 0.08 * pH, f_mom(kind, true), colUp)
// ------------------------------ SERIES-LEVEL CALCS ---------------------------
float hiP = ta.highest(high, lookback)
float loP = ta.lowest(low, lookback)
float gHi = ta.highest(high, 200)
float gLo = ta.lowest(low, 200)
// ------------------------------ MAIN (last bar only) -------------------------
if barstate.islast
f_clear()
// ======================= LIVE DETECTION =======================
if showLive and not na(hiP) and bar_index > 25
int lb = math.min(lookback, bar_index + 1)
float stp = math.max((hiP - loP) / rowsIn, syminfo.mintick)
// 1) build the volume profile: spread each bar's volume across the rows it overlaps
array<float> vols = array.new_float(rowsIn, 0.0)
for i = 0 to lb - 1 by 1
float bh = high[i]
float bl = low[i]
float bv = nz(volume[i], 0.0)
if bv <= 0
bv := 1.0
bv
int r0 = math.max(0, math.min(rowsIn - 1, math.floor((bl - loP) / stp)))
int r1 = math.max(0, math.min(rowsIn - 1, math.floor((bh - loP) / stp)))
if bh - bl <= 0
array.set(vols, r0, array.get(vols, r0) + bv)
else
for r = r0 to r1 by 1
float rl = loP + r * stp
float ru = rl + stp
float ov = math.max(0.0, math.min(bh, ru) - math.max(bl, rl))
array.set(vols, r, array.get(vols, r) + bv * ov / (bh - bl))
// 2) classify the shape from the data
[kind, pkI, p2I] = f_classify(vols)
float cStart = close[lb - 1]
bool up = close >= cStart
// 3) render
bool right = placeIn == 'Right of last bar'
int startX = bar_index - lb + 1
int x0 = right ? bar_index + 3 : startX
[pocP, vahP, valP, pocI] = f_drawProfile(vols, loP, stp, x0, wLive)
int endX = right ? x0 + wLive : bar_index + 3
f_lnS(startX, pocP, endX, pocP, colLine, 1)
f_lnD(startX, vahP, endX, vahP, colLine, 2)
f_lnD(startX, valP, endX, valP, colLine, 2)
f_tag(endX + 1, pocP, 'POC ' + str.tostring(pocP, format.mintick), color.black)
f_tag(endX + 1, vahP, 'VAH', color.black)
f_tag(endX + 1, valP, 'VAL', color.black)
// price axis + volume axis arrows
f_lnB(x0 - 1, loP, x0 - 1, hiP, color.gray, 1)
f_lnA(x0, loP - stp * 0.8, x0 + wLive, loP - stp * 0.8, color.gray, 1)
f_tag(x0 + wLive + 1, loP - stp * 0.8, 'VOLUME', color.black)
f_tag(x0 + wLive + 1, hiP, 'HIGH PRICE', color.black)
f_tag(x0 + wLive + 1, loP, 'LOW PRICE', color.black)
// trend arrow along the move (green up / red down)
float shift = (up ? -1.0 : 1.0) * stp * 2.0
f_lnA(startX, cStart + shift, bar_index, close + shift, up ? colUp : colDn, 3)
// title + captions
f_title(x0 + math.round(wLive / 2.0), hiP + stp * 3, f_badge(kind) + ' ' + f_name(kind))
f_cap(x0 + math.round(wLive / 2.0), loP - stp * 2, f_market(kind, up), color.black)
f_cap(x0 + math.round(wLive / 2.0), loP - stp * 6, f_mom(kind, up), up ? colUp : colDn)
// diagnostics
if showTable
float posPct = rowsIn > 1 ? pkI / (rowsIn - 1.0) * 100.0 : 50.0
float vaW = (vahP - valP) / (hiP - loP) * 100.0
table.cell(tbl, 0, 0, 'Detected', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 0, f_name(kind), text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 1, 'POC position', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 1, str.tostring(posPct, '#.#') + '% of range', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 2, '2nd peak', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 2, p2I >= 0 ? str.tostring((p2I + 0.5) / rowsIn * 100.0, '#.#') + '%' : 'none', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 3, 'Value area width', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 3, str.tostring(vaW, '#.#') + '% of range', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 4, 'Bias', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 4, f_mom(kind, up), text_color = up ? colUp : colDn, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 5, 'Lookback / rows', text_color = color.black, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 5, str.tostring(lb) + ' / ' + str.tostring(rowsIn), text_color = color.black, text_size = size.small, text_halign = text.align_left)
// ======================= GALLERY 1-2-3-4 =======================
if showGallery and not na(gHi) and bar_index > 25
int pW = 82
int gapX = 6
float Hg = gHi - gLo
float gapY = 0.06 * Hg
float pH = (Hg - gapY) / 2.0
int gx0 = bar_index + galOffset
for k = 1 to 4 by 1
int col = (k - 1) % 2
int rw = math.floor((k - 1) / 2.0)
f_panel(k, gx0 + col * (pW + gapX), gHi - rw * (pH + gapY), pH, pW)