本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Rolling Volume Profile x4", shorttitle = "RVP x4", overlay = true, max_boxes_count = 500, max_lines_count = 50, max_labels_count = 500, max_bars_back = 5000)
// ════════════════════════════════════════════════════════════════════
// WHAT THIS DOES
// Up to 4 rolling volume profiles in one indicator. Each profile is built
// from its own number of candles (you set it) and always ends with the
// current candle. On every new candle the oldest candle drops out, and the
// current (forming) candle is recalculated on every tick.
// Settings follow TradingView's Fixed Range Volume Profile (FRVP).
//
// HOW VOLUME IS SPREAD: each candle's volume is spread evenly over its
// high-low range and split into rows. A candle counts as Up volume when
// close >= open, Down volume when close < open. (FRVP uses lower-timeframe
// data, so row values will be close to FRVP but not identical.)
//
// ROW LIMIT: TradingView allows 500 boxes per indicator, shared by all
// profiles. Each row uses up to 2 boxes, so the row count is reduced
// automatically when more profiles are on (4 profiles → about 62 rows each).
// ════════════════════════════════════════════════════════════════════
const int MAX_ROWS = 240
const int MAX_BOXES = 500
// ───────────── Profiles ─────────────
string gP = "Profiles (on · candles · POC color)"
bool p1On = input.bool(true, "Profile 1", inline = "p1", group = gP)
int p1Len = input.int(100, "", minval = 2, maxval = 5000, inline = "p1", group = gP)
color p1Poc = input.color(color.black, "", inline = "p1", group = gP)
bool p2On = input.bool(false, "Profile 2", inline = "p2", group = gP)
int p2Len = input.int(200, "", minval = 2, maxval = 5000, inline = "p2", group = gP)
color p2Poc = input.color(color.blue, "", inline = "p2", group = gP)
bool p3On = input.bool(false, "Profile 3", inline = "p3", group = gP)
int p3Len = input.int(300, "", minval = 2, maxval = 5000, inline = "p3", group = gP)
color p3Poc = input.color(color.orange, "", inline = "p3", group = gP)
bool p4On = input.bool(false, "Profile 4", inline = "p4", group = gP)
int p4Len = input.int(500, "", minval = 2, maxval = 5000, inline = "p4", group = gP)
color p4Poc = input.color(color.purple, "", inline = "p4", group = gP)
// ───────────── Inputs (shared by all profiles) ─────────────
string gI = "Inputs"
string rowsLayout = input.string("Number of Rows", "Rows Layout", options = ["Number of Rows", "Ticks Per Row"], group = gI)
int rowSize = input.int(70, "Row Size", minval = 1, group = gI, tooltip = "Number of rows, or ticks per row, depending on Rows Layout. Reduced automatically when several profiles are on (500-box limit).")
string volMode = input.string("Up/Down", "Volume", options = ["Up/Down", "Total", "Delta"], group = gI)
float vaPct = input.float(70.0, "Value Area Volume", minval = 1.0, maxval = 100.0, group = gI)
bool extRight = input.bool(false, "Extend Right", group = gI, tooltip = "Extends the POC, VAH and VAL lines to the right.")
// ───────────── Style (shared by all profiles) ─────────────
string gS = "Style"
bool showProf = input.bool(true, "Volume profile", group = gS)
bool showVals = input.bool(false, "Values", inline = "val", group = gS)
color valCol = input.color(color.gray, "", inline = "val", group = gS)
int widthPct = input.int(30, "Width (% of the box)", minval = 1, maxval = 100, group = gS)
string placement = input.string("Left", "Placement", options = ["Left", "Right"], group = gS)
color upCol = input.color(color.new(#26c6da, 60), "Up Volume", group = gS)
color dnCol = input.color(color.new(#f06292, 60), "Down Volume", group = gS)
color vaUpCol = input.color(color.new(#26c6da, 25), "Value Area Up", group = gS)
color vaDnCol = input.color(color.new(#f06292, 25), "Value Area Down", group = gS)
bool showVAH = input.bool(false, "VAH", inline = "vah", group = gS)
color vahCol = input.color(color.gray, "", inline = "vah", group = gS)
int vahW = input.int(1, "", minval = 1, maxval = 4, inline = "vah", group = gS)
string vahSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "vah", group = gS)
bool showVAL = input.bool(false, "VAL", inline = "val2", group = gS)
color valLnCol = input.color(color.gray, "", inline = "val2", group = gS)
int valW = input.int(1, "", minval = 1, maxval = 4, inline = "val2", group = gS)
string valSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "val2", group = gS)
bool showPOC = input.bool(true, "POC (color set per profile)", inline = "poc", group = gS)
int pocW = input.int(2, "", minval = 1, maxval = 4, inline = "poc", group = gS)
string pocSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "poc", group = gS)
bool showDevPOC = input.bool(false, "Developing POC (profile POC color)", group = gS)
bool showDevVA = input.bool(false, "Developing VA", inline = "dva", group = gS)
color devVACol = input.color(color.new(#26c6da, 0), "", inline = "dva", group = gS)
int devBars = input.int(500, "Developing lines: candles back", minval = 1, maxval = 5000, group = gS, tooltip = "How many candles back the Developing POC / VA are drawn. Higher = slower to load.")
bool showHBox = input.bool(false, "Histogram Box", inline = "hb", group = gS)
color hBoxCol = input.color(color.new(color.gray, 92), "", inline = "hb", group = gS)
// ───────────── Row cap shared across profiles ─────────────
int nOn = (p1On ? 1 : 0) + (p2On ? 1 : 0) + (p3On ? 1 : 0) + (p4On ? 1 : 0)
int hBoxes = showHBox ? nOn : 0
int rowsCap = nOn > 0 ? math.max(1, math.min(MAX_ROWS, int(math.floor((MAX_BOXES - hBoxes) / (2.0 * nOn))))) : MAX_ROWS
// ───────────── Functions ─────────────
lineStyle(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
// Builds the profile for the last n candles (n >= 2).
// Returns up-volume and down-volume per row, the profile low, row height and row count.
calcProfile(int n, int cap) =>
float pHi = high
float pLo = low
for i = 1 to n - 1
pHi := math.max(pHi, high[i])
pLo := math.min(pLo, low[i])
float rngP = pHi - pLo
float step = 0.0
int rows = 1
if rowsLayout == "Number of Rows"
rows := math.min(rowSize, cap)
step := rngP / rows
else
step := rowSize * syminfo.mintick
rows := math.max(1, int(math.ceil(rngP / step)))
if rows > cap
rows := cap
step := rngP / rows
if step <= 0
step := syminfo.mintick
rows := 1
array<float> upV = array.new<float>(rows, 0.0)
array<float> dnV = array.new<float>(rows, 0.0)
for i = 0 to n - 1
float h = high[i]
float l = low[i]
float v = nz(volume[i])
bool isUp = close[i] >= open[i]
int r1 = math.max(0, math.min(rows - 1, int(math.floor((l - pLo) / step))))
int r2 = math.max(0, math.min(rows - 1, int(math.floor((h - pLo) / step))))
for r = r1 to r2
float share = v
if h > l
float rb = pLo + r * step
float rt = rb + step
share := v * math.max(0.0, math.min(h, rt) - math.max(l, rb)) / (h - l)
if isUp
upV.set(r, upV.get(r) + share)
else
dnV.set(r, dnV.get(r) + share)
[upV, dnV, pLo, step, rows]
// Point of control and value area (row indexes), based on total volume.
calcVA(array<float> tot, int rows) =>
float total = tot.sum()
int poc = tot.indexof(tot.max())
int lo = poc
int hi = poc
float acc = tot.get(poc)
float target = total * vaPct / 100.0
while acc < target and (lo > 0 or hi < rows - 1)
float above = hi < rows - 1 ? tot.get(hi + 1) : -1.0
float below = lo > 0 ? tot.get(lo - 1) : -1.0
if above >= below
hi += 1
acc += above
else
lo -= 1
acc += below
[poc, lo, hi]
// Calculates one profile; draws it on the last bar.
// Returns the developing POC, VAH and VAL for this bar (na when not calculated).
runProfile(bool on, int lookback, color pocCol, array<box> boxes, array<label> labs, array<line> lns) =>
float dPOC = na
float dVAH = na
float dVAL = na
int n = math.min(lookback, bar_index + 1)
bool dev = showDevPOC or showDevVA
if on and n >= 2 and (barstate.islast or (dev and bar_index >= last_bar_index - devBars))
[upV, dnV, pLo, step, rows] = calcProfile(n, rowsCap)
array<float> tot = array.new<float>(rows, 0.0)
for r = 0 to rows - 1
tot.set(r, upV.get(r) + dnV.get(r))
[poc, vaLo, vaHi] = calcVA(tot, rows)
dPOC := pLo + (poc + 0.5) * step
dVAH := pLo + (vaHi + 1) * step
dVAL := pLo + vaLo * step
if barstate.islast
int xL = bar_index - n + 1
int xR = bar_index
bool left = placement == "Left"
float maxLen = math.max(1.0, (xR - xL) * widthPct / 100.0)
float pHi = pLo + rows * step
float maxVal = 0.0
for r = 0 to rows - 1
float rv = volMode == "Delta" ? math.abs(upV.get(r) - dnV.get(r)) : tot.get(r)
maxVal := math.max(maxVal, rv)
if showHBox
boxes.push(box.new(xL, pHi, xR, pLo, border_color = color.new(hBoxCol, 50), bgcolor = hBoxCol))
if showProf and maxVal > 0
for r = 0 to rows - 1
float bot = pLo + r * step
float top = bot + step
bool inVA = r >= vaLo and r <= vaHi
color cU = inVA ? vaUpCol : upCol
color cD = inVA ? vaDnCol : dnCol
float u = upV.get(r)
float d = dnV.get(r)
// Segments measured from the anchor side: [a1, a2] first color, [a2, b2] second color
int a1 = 0
int a2 = 0
int b2 = 0
color c1 = cU
color c2 = cD
if volMode == "Up/Down"
a2 := int(math.round(u / maxVal * maxLen))
b2 := int(math.round((u + d) / maxVal * maxLen))
else if volMode == "Total"
a2 := int(math.round((u + d) / maxVal * maxLen))
b2 := a2
else
a2 := int(math.round(math.abs(u - d) / maxVal * maxLen))
b2 := a2
c1 := u >= d ? cU : cD
if a2 > a1
int x1 = left ? xL + a1 : xR - a2
int x2 = left ? xL + a2 : xR - a1
boxes.push(box.new(x1, top, x2, bot, border_color = c1, bgcolor = c1))
if b2 > a2
int x1 = left ? xL + a2 : xR - b2
int x2 = left ? xL + b2 : xR - a2
boxes.push(box.new(x1, top, x2, bot, border_color = c2, bgcolor = c2))
if showVals
float shown = volMode == "Delta" ? u - d : u + d
int xEnd = left ? xL + b2 : xR - b2
labs.push(label.new(xEnd, (top + bot) / 2, str.tostring(shown, format.volume), color = color.new(color.black, 100), textcolor = valCol, size = size.tiny, style = left ? label.style_label_left : label.style_label_right))
string ext = extRight ? extend.right : extend.none
if showPOC
lns.push(line.new(xL, dPOC, xR, dPOC, extend = ext, color = pocCol, width = pocW, style = lineStyle(pocSty)))
if showVAH
lns.push(line.new(xL, dVAH, xR, dVAH, extend = ext, color = vahCol, width = vahW, style = lineStyle(vahSty)))
if showVAL
lns.push(line.new(xL, dVAL, xR, dVAL, extend = ext, color = valLnCol, width = valW, style = lineStyle(valSty)))
[dPOC, dVAH, dVAL]
// ───────────── Calculation ─────────────
var array<box> boxes = array.new<box>()
var array<label> labs = array.new<label>()
var array<line> lns = array.new<line>()
// Remove the previous drawing, then each profile redraws for its current window
if barstate.islast
for b in boxes
b.delete()
boxes.clear()
for lb in labs
lb.delete()
labs.clear()
for ln in lns
ln.delete()
lns.clear()
[poc1, vah1, val1] = runProfile(p1On, p1Len, p1Poc, boxes, labs, lns)
[poc2, vah2, val2] = runProfile(p2On, p2Len, p2Poc, boxes, labs, lns)
[poc3, vah3, val3] = runProfile(p3On, p3Len, p3Poc, boxes, labs, lns)
[poc4, vah4, val4] = runProfile(p4On, p4Len, p4Poc, boxes, labs, lns)
// ───────────── Developing lines ─────────────
plot(showDevPOC ? poc1 : na, "Developing POC 1", p1Poc, 1, plot.style_linebr)
plot(showDevPOC ? poc2 : na, "Developing POC 2", p2Poc, 1, plot.style_linebr)
plot(showDevPOC ? poc3 : na, "Developing POC 3", p3Poc, 1, plot.style_linebr)
plot(showDevPOC ? poc4 : na, "Developing POC 4", p4Poc, 1, plot.style_linebr)
plot(showDevVA ? vah1 : na, "Developing VAH 1", devVACol, 1, plot.style_linebr)
plot(showDevVA ? val1 : na, "Developing VAL 1", devVACol, 1, plot.style_linebr)
plot(showDevVA ? vah2 : na, "Developing VAH 2", devVACol, 1, plot.style_linebr)
plot(showDevVA ? val2 : na, "Developing VAL 2", devVACol, 1, plot.style_linebr)
plot(showDevVA ? vah3 : na, "Developing VAH 3", devVACol, 1, plot.style_linebr)
plot(showDevVA ? val3 : na, "Developing VAL 3", devVACol, 1, plot.style_linebr)
plot(showDevVA ? vah4 : na, "Developing VAH 4", devVACol, 1, plot.style_linebr)
plot(showDevVA ? val4 : na, "Developing VAL 4", devVACol, 1, plot.style_linebr)
// Current values in the Data Window (usable in alerts)
plot(barstate.islast ? poc1 : na, "POC 1", display = display.data_window)
plot(barstate.islast ? vah1 : na, "VAH 1", display = display.data_window)
plot(barstate.islast ? val1 : na, "VAL 1", display = display.data_window)
plot(barstate.islast ? poc2 : na, "POC 2", display = display.data_window)
plot(barstate.islast ? vah2 : na, "VAH 2", display = display.data_window)
plot(barstate.islast ? val2 : na, "VAL 2", display = display.data_window)
plot(barstate.islast ? poc3 : na, "POC 3", display = display.data_window)
plot(barstate.islast ? vah3 : na, "VAH 3", display = display.data_window)
plot(barstate.islast ? val3 : na, "VAL 3", display = display.data_window)
plot(barstate.islast ? poc4 : na, "POC 4", display = display.data_window)
plot(barstate.islast ? vah4 : na, "VAH 4", display = display.data_window)
plot(barstate.islast ? val4 : na, "VAL 4", display = display.data_window)