本页源码来自 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/
// © ahmad1darwich
//@version=6
indicator("Nexus Profile Candles - Buy/Sell Volume Profile", shorttitle = "NX Prof", overlay = true, max_boxes_count = 500, max_lines_count = 200, max_labels_count = 200)
// Period volume profiles split into buy / sell (a proxy from where each bar closed in its range, not real delta).
// Finished periods are drawn to the right of price as a candle plus a two-sided profile. No volume = time at price.
// ---------- Inputs ----------
string GRP_P = "Profile"
string periodInput = input.string("Auto", "Period", options = ["Auto", "Day", "Week", "Month"], group = GRP_P, tooltip = "Auto: Day up to 15m, Week up to 4h, Month above.")
int rowsInput = input.int(10, "Rows", minval = 6, maxval = 40, group = GRP_P, tooltip = "Fewer rows keep the chart clean: each profile uses about 2 x rows boxes.")
float vaInput = input.float(70.0, "Value area (%)", minval = 50.0, maxval = 95.0, step = 1.0, group = GRP_P)
int profilesInput = input.int(1, "Profiles on the right", minval = 1, maxval = 5, group = GRP_P)
int halfWInput = input.int(8, "Half width (bars)", minval = 3, maxval = 20, group = GRP_P)
int gapInput = input.int(8, "Gap from last bar", minval = 3, maxval = 40, group = GRP_P)
int candleWInput = input.int(1, "Candle width (bars)", minval = 1, maxval = 3, group = GRP_P, tooltip = "Width of the period candle drawn beside each profile.")
int candleAlphaInput = input.int(45, "Candle opacity (transparency %)", minval = 0, maxval = 90, group = GRP_P, tooltip = "Higher = more transparent body.")
int boxesInput = input.int(2, "Period boxes on chart", minval = 1, maxval = 12, group = GRP_P, tooltip = "Newest N period boxes kept on the chart; older ones are deleted.")
bool showBoxesInput = input.bool(true, "Period boxes", group = GRP_P)
bool showPrevInput = input.bool(true, "Previous POC / VAH / VAL on chart", group = GRP_P)
bool showNakedInput = input.bool(true, "Naked POCs", group = GRP_P, tooltip = "A finished period's POC not traded through yet. It stays until price touches it.")
bool showSigInput = input.bool(true, "Value area re-entry signals", group = GRP_P, tooltip = "Close back above the previous VAL = long toward the POC. Close back below the previous VAH = short toward the POC. Once per direction per period.")
string GRP_C = "Colors"
string themeInput = input.string("Custom", "Theme", options = ["Custom", "Neon", "Classic", "Minimal", "Mono"], group = GRP_C, tooltip = "Custom uses the colors below. The other themes override bull / bear / buy / sell / POC colors.")
color bullUser = input.color(#26A69A, "Bull candle / box", group = GRP_C)
color bearUser = input.color(#EF5350, "Bear candle / box", group = GRP_C)
color buyUser = input.color(#5BB6AD, "Buy volume", group = GRP_C)
color sellUser = input.color(#F28B82, "Sell volume", group = GRP_C)
bool showBgInput = input.bool(true, "Light blue background behind profiles", group = GRP_C)
color bgUser = input.color(#4C84C9, "Background color", group = GRP_C)
color pocBoxUser = input.color(#D4B070, "POC row border", group = GRP_C)
pick(color custom, color neon, color classic, color minimal, color mono) =>
themeInput == "Custom" ? custom : themeInput == "Neon" ? neon : themeInput == "Classic" ? classic : themeInput == "Minimal" ? minimal : mono
color bullInput = pick(bullUser, #00E5A0, #089981, #26A69A, #D1D4DC)
color bearInput = pick(bearUser, #FF3D71, #F23645, #EF5350, #787B86)
color buyInput = pick(buyUser, #00C896, #26A69A, #4DB6AC, #B2B5BE)
color sellInput = pick(sellUser, #7C4DFF, #2962FF, #9598A1, #787B86)
color pocBoxInput = pick(pocBoxUser, #FFC400, #FF9800, #C9A227, #E0E3EB)
color pocLineInput = input.color(#8A8D9A, "POC line", group = GRP_C, inline = "pl")
int pocWidthInput = input.int(1, "", minval = 1, maxval = 4, group = GRP_C, inline = "pl")
string pocStyleInput = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = GRP_C, inline = "pl")
color vaLineInput = input.color(#8A8D9A, "VAH / VAL lines", group = GRP_C)
color textInput = input.color(#8A8D9A, "Text", group = GRP_C)
color panelInput = input.color(#1E222D, "Table background", group = GRP_C)
string GRP_D = "Dashboard"
bool showDashInput = input.bool(true, "Show table", group = GRP_D)
string dashPosInput = input.string("Bottom left", "Position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = GRP_D)
// ---------- Types & state ----------
type Period
float pOpen
float pClose
float pHigh
float pLow
int startBar
int endBar
array<float> bHi
array<float> bLo
array<float> bBuy
array<float> bSell
var array<Period> periods = array.new<Period>()
var Period cur = na
var array<float> nkPrice = array.new<float>()
var array<int> nkBar = array.new<int>()
var array<box> dBoxes = array.new<box>()
var array<line> dLines = array.new<line>()
var array<label> dLabels = array.new<label>()
var float prevPoc = na
var float prevVah = na
var float prevVal = na
var bool longDone = false
var bool shortDone = false
var bool sawVolume = false
var string lastSig = "-"
var int lastSigBar = na
int tfSec = timeframe.in_seconds()
string periodTf = periodInput == "Day" ? "D" : periodInput == "Week" ? "W" : periodInput == "Month" ? "M" : tfSec <= 900 ? "D" : tfSec <= 14400 ? "W" : "M"
string tfName = periodTf == "D" ? "1D" : periodTf == "W" ? "1W" : "1M"
bool newPeriod = timeframe.change(periodTf)
float atr = ta.atr(14)
if not na(volume) and volume > 0
sawVolume := true
string mode = sawVolume ? "Volume" : "TPO"
lineStyle(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
// Fills sells / buys per row from the bars of a period; returns [lowPrice, rowStep, pocRow, vaTopRow, vaBottomRow].
fillProfile(Period p, array<float> sells, array<float> buys, int rows) =>
float lo = p.pLow
float step = math.max((p.pHigh - p.pLow) / rows, syminfo.mintick)
for i = 0 to p.bHi.size() - 1
int r0 = math.max(0, math.min(rows - 1, int(math.floor((p.bLo.get(i) - lo) / step))))
int r1 = math.max(0, math.min(rows - 1, int(math.floor((p.bHi.get(i) - lo) / step))))
float n = r1 - r0 + 1
for r = r0 to r1
sells.set(r, sells.get(r) + p.bSell.get(i) / n)
buys.set(r, buys.get(r) + p.bBuy.get(i) / n)
int poc = 0
float total = 0.0
float mx = 0.0
for r = 0 to rows - 1
float v = sells.get(r) + buys.get(r)
total += v
if v > mx
mx := v
poc := r
float target = total * vaInput / 100.0
float acc = mx
int up = poc
int dn = poc
while acc < target and (up < rows - 1 or dn > 0)
float vu = up < rows - 1 ? sells.get(up + 1) + buys.get(up + 1) : -1.0
float vd = dn > 0 ? sells.get(dn - 1) + buys.get(dn - 1) : -1.0
if vu >= vd
up += 1
acc += vu
else
dn -= 1
acc += vd
[lo, step, poc, up, dn]
// ---------- Period handling ----------
bool nakedTouched = false
if nkPrice.size() > 0
for i = nkPrice.size() - 1 to 0
if bar_index > nkBar.get(i) and high >= nkPrice.get(i) and low <= nkPrice.get(i)
nakedTouched := true
nkPrice.remove(i)
nkBar.remove(i)
bool nakedNew = false
if newPeriod or na(cur)
if not na(cur)
cur.endBar := bar_index - 1
array<float> fs = array.new<float>(rowsInput, 0.0)
array<float> fb = array.new<float>(rowsInput, 0.0)
[fLo, fStep, fPoc, fUp, fDn] = fillProfile(cur, fs, fb, rowsInput)
prevPoc := fLo + fStep * (fPoc + 0.5)
prevVah := fLo + fStep * (fUp + 1)
prevVal := fLo + fStep * fDn
if showNakedInput
nakedNew := true
nkPrice.push(prevPoc)
nkBar.push(bar_index)
if nkPrice.size() > 5
nkPrice.shift()
nkBar.shift()
longDone := false
shortDone := false
cur := Period.new(pOpen = open, pClose = close, pHigh = high, pLow = low, startBar = bar_index, endBar = bar_index, bHi = array.new<float>(), bLo = array.new<float>(), bBuy = array.new<float>(), bSell = array.new<float>())
periods.push(cur)
if periods.size() > 14
periods.shift()
float rngBar = high - low
float buyShare = rngBar > 0 ? math.min(math.max((close - low) / rngBar, 0.0), 1.0) : 0.5
float w = sawVolume ? nz(volume) : 1.0
cur.pHigh := math.max(cur.pHigh, high)
cur.pLow := math.min(cur.pLow, low)
cur.pClose := close
cur.endBar := bar_index
cur.bHi.push(high)
cur.bLo.push(low)
cur.bBuy.push(w * buyShare)
cur.bSell.push(w * (1.0 - buyShare))
// ---------- Signals (confirmed bars, once per direction per period) ----------
bool crossUpVal = ta.crossover(close, prevVal)
bool crossDnVah = ta.crossunder(close, prevVah)
bool longSig = false
bool shortSig = false
if barstate.isconfirmed and not na(prevPoc) and not na(atr)
if crossUpVal and not longDone and prevVal < prevPoc
longDone := true
longSig := true
lastSig := "▲ Re-entry"
lastSigBar := bar_index
if showSigInput
label.new(bar_index, low - atr * 0.4, "▲ VA", style = label.style_label_up, color = bullInput, textcolor = color.white, size = size.small, tooltip = "Closed back above the previous VAL " + str.tostring(prevVal, format.mintick) + ". Target POC " + str.tostring(prevPoc, format.mintick) + ". Idea invalid below " + str.tostring(low - atr * 0.5, format.mintick) + ".")
if crossDnVah and not shortDone and prevVah > prevPoc
shortDone := true
shortSig := true
lastSig := "▼ Re-entry"
lastSigBar := bar_index
if showSigInput
label.new(bar_index, high + atr * 0.4, "▼ VA", style = label.style_label_down, color = bearInput, textcolor = color.white, size = size.small, tooltip = "Closed back below the previous VAH " + str.tostring(prevVah, format.mintick) + ". Target POC " + str.tostring(prevPoc, format.mintick) + ". Idea invalid above " + str.tostring(high + atr * 0.5, format.mintick) + ".")
// ---------- Drawing (last bar: everything is rebuilt, so nothing piles up) ----------
if barstate.islast and not na(cur) and periods.size() > 0
for b in dBoxes
b.delete()
for l in dLines
l.delete()
for t in dLabels
t.delete()
dBoxes.clear()
dLines.clear()
dLabels.clear()
color pocLs = pocLineInput
int nP = periods.size()
// Period boxes on the chart with an open-to-close line
if showBoxesInput
int firstB = math.max(0, nP - boxesInput)
for i = firstB to nP - 1
Period p = periods.get(i)
color c = p.pClose >= p.pOpen ? bullInput : bearInput
int xe = i == nP - 1 ? bar_index : p.endBar
int xs = math.max(p.startBar, bar_index - 4000)
int xe2 = math.max(xe, xs)
dBoxes.push(box.new(xs, p.pHigh, xe2, p.pLow, border_color = c, bgcolor = color.new(c, 88), border_width = 1, text = tfName, text_size = size.tiny, text_color = textInput, text_halign = text.align_left, text_valign = text.align_top))
dLines.push(line.new(xs, p.pOpen, xe2, p.pClose, color = c, width = 1))
// Previous period levels on the current period, naked POCs
if showPrevInput and not na(prevPoc) and nP >= 2
int x0 = math.max(cur.startBar, bar_index - 4000)
dLines.push(line.new(x0, prevPoc, bar_index + 3, prevPoc, color = pocLs, width = pocWidthInput, style = lineStyle(pocStyleInput)))
dLines.push(line.new(x0, prevVah, bar_index + 3, prevVah, color = color.new(vaLineInput, 30), width = 1, style = line.style_dashed))
dLines.push(line.new(x0, prevVal, bar_index + 3, prevVal, color = color.new(vaLineInput, 30), width = 1, style = line.style_dashed))
dLabels.push(label.new(bar_index + 3, prevPoc, "pPOC", style = label.style_none, textcolor = textInput, size = size.tiny))
dLabels.push(label.new(bar_index + 3, prevVah, "pVAH", style = label.style_none, textcolor = textInput, size = size.tiny))
dLabels.push(label.new(bar_index + 3, prevVal, "pVAL", style = label.style_none, textcolor = textInput, size = size.tiny))
if nkPrice.size() > 0
for i = 0 to nkPrice.size() - 1
float np = nkPrice.get(i)
if na(prevPoc) or np != prevPoc
dLines.push(line.new(math.max(nkBar.get(i), bar_index - 4000), np, bar_index + 3, np, color = color.new(pocBoxInput, 40), width = 1, style = line.style_dotted))
dLabels.push(label.new(bar_index + 3, np, "nPOC", style = label.style_none, textcolor = pocBoxInput, size = size.tiny))
// Profiles to the right: candle + sell (left) / buy (right) around a centre axis
int nShow = math.min(profilesInput, nP)
int cursor = bar_index + gapInput
// Light blue panel behind the profiles (created first so it sits underneath)
if showBgInput and nShow > 0
float bgTop = na
float bgBot = na
for k = nP - nShow to nP - 1
Period q = periods.get(k)
bgTop := na(bgTop) ? q.pHigh : math.max(bgTop, q.pHigh)
bgBot := na(bgBot) ? q.pLow : math.min(bgBot, q.pLow)
float pad = na(atr) ? 0.0 : atr * 0.3
int bgRight = bar_index + gapInput + nShow * (2 * halfWInput + 9)
dBoxes.push(box.new(bar_index + gapInput - 2, bgTop + pad, bgRight, bgBot - pad, border_color = color.new(bgUser, 70), bgcolor = color.new(bgUser, 88), border_width = 1))
for k = nP - nShow to nP - 1
Period p = periods.get(k)
color c = p.pClose >= p.pOpen ? bullInput : bearInput
array<float> sells = array.new<float>(rowsInput, 0.0)
array<float> buys = array.new<float>(rowsInput, 0.0)
[lo, step, poc, up, dn] = fillProfile(p, sells, buys, rowsInput)
int xc = cursor
int axis = xc + 4 + halfWInput
float mx = 0.0
for r = 0 to rowsInput - 1
mx := math.max(mx, math.max(sells.get(r), buys.get(r)))
// candle: wick, body and border share one color
int wickX = xc + int(candleWInput / 2)
dLines.push(line.new(wickX, p.pHigh, wickX, p.pLow, color = c, width = 1))
dBoxes.push(box.new(xc, math.max(p.pOpen, p.pClose), xc + candleWInput, math.min(p.pOpen, p.pClose), border_color = c, bgcolor = color.new(c, candleAlphaInput)))
for r = 0 to rowsInput - 1
float sv = sells.get(r)
float bv = buys.get(r)
bool inVa = r >= dn and r <= up
float yTop = lo + step * (r + 0.9)
float yBot = lo + step * (r + 0.1)
int lenS = sv > 0 and mx > 0 ? math.max(1, int(math.round(halfWInput * sv / mx))) : 0
int lenB = bv > 0 and mx > 0 ? math.max(1, int(math.round(halfWInput * bv / mx))) : 0
if lenS > 0
dBoxes.push(box.new(axis - lenS, yTop, axis, yBot, border_color = color.new(sellInput, 100), bgcolor = color.new(sellInput, inVa ? 30 : 65)))
if lenB > 0
dBoxes.push(box.new(axis, yTop, axis + lenB, yBot, border_color = color.new(buyInput, 100), bgcolor = color.new(buyInput, inVa ? 30 : 65)))
if r == poc
dBoxes.push(box.new(axis - math.max(lenS, 1), yTop, axis + math.max(lenB, 1), yBot, border_color = pocBoxInput, border_width = 2, bgcolor = na))
float pocP = lo + step * (poc + 0.5)
float vahP = lo + step * (up + 1)
float valP = lo + step * dn
int xe = axis + halfWInput + 1
dLines.push(line.new(xc, pocP, xe, pocP, color = pocLs, width = pocWidthInput, style = lineStyle(pocStyleInput)))
dLines.push(line.new(xc, vahP, xe, vahP, color = color.new(vaLineInput, 20), width = 1))
dLines.push(line.new(xc, valP, xe, valP, color = color.new(vaLineInput, 20), width = 1))
dLabels.push(label.new(xe, vahP, "VAH", style = label.style_label_left, color = color.new(color.gray, 100), textcolor = textInput, size = size.tiny))
dLabels.push(label.new(xe, valP, "VAL", style = label.style_label_left, color = color.new(color.gray, 100), textcolor = textInput, size = size.tiny))
dLabels.push(label.new(xe, pocP, "POC", style = label.style_label_left, color = color.new(color.gray, 100), textcolor = pocBoxInput, size = size.tiny))
if k == nP - nShow
dLabels.push(label.new(axis, p.pHigh, tfName, style = label.style_label_down, color = color.new(color.gray, 100), textcolor = textInput, size = size.small))
// Buy / sell totals of this period: percent out of 100 and contracts (bars when there is no volume)
float totB = p.bBuy.sum()
float totS = p.bSell.sum()
float totAll = totB + totS
int pctB = totAll > 0 ? int(math.round(totB / totAll * 100)) : 50
int pctS = 100 - pctB
string unitTxt = sawVolume ? "" : " bars"
string qtyB = str.tostring(totB, format.volume) + unitTxt
string qtyS = str.tostring(totS, format.volume) + unitTxt
dLabels.push(label.new(axis - math.max(1, int(halfWInput / 2)), p.pLow, "Sell " + str.tostring(pctS) + "%\n" + qtyS, style = label.style_label_up, color = color.new(sellInput, 100), textcolor = sellInput, size = size.tiny, tooltip = "Sell side of the period: " + str.tostring(pctS) + "% of 100, " + qtyS + (sawVolume ? " contracts (proxy split)" : " (time at price, no volume on this symbol)")))
dLabels.push(label.new(axis + math.max(1, int(halfWInput / 2)), p.pLow, "Buy " + str.tostring(pctB) + "%\n" + qtyB, style = label.style_label_up, color = color.new(buyInput, 100), textcolor = buyInput, size = size.tiny, tooltip = "Buy side of the period: " + str.tostring(pctB) + "% of 100, " + qtyB + (sawVolume ? " contracts (proxy split)" : " (time at price, no volume on this symbol)")))
cursor := axis + halfWInput + 5
// ---------- Dashboard ----------
dashPos = dashPosInput == "Top right" ? position.top_right : dashPosInput == "Bottom right" ? position.bottom_right : dashPosInput == "Top left" ? position.top_left : position.bottom_left
var table dash = table.new(dashPos, 2, 9, border_width = 1, border_color = color.new(#787B86, 85), frame_color = color.new(sellInput, 40), frame_width = 1)
row(int r, string k, string v, color vc) =>
dash.cell(0, r, k, text_color = textInput, bgcolor = color.new(panelInput, 8), text_size = size.small, text_halign = text.align_left)
dash.cell(1, r, v, text_color = vc, bgcolor = color.new(panelInput, 8), text_size = size.small, text_halign = text.align_right, text_font_family = font.family_monospace)
if showDashInput and barstate.islast and not na(cur)
float sB = cur.bBuy.sum()
float sS = cur.bSell.sum()
float delta = sB + sS > 0 ? (sB - sS) / (sB + sS) * 100 : 0.0
string where = na(prevVah) ? "-" : close > prevVah ? "Above value ▲" : close < prevVal ? "Below value ▼" : "In value ●"
color whereC = na(prevVah) ? textInput : close > prevVah ? bullInput : close < prevVal ? sellInput : pocBoxInput
dash.cell(0, 0, "NEXUS", text_color = color.white, bgcolor = color.new(sellInput, 15), text_size = size.small)
dash.cell(1, 0, "Profile " + tfName, text_color = color.white, bgcolor = color.new(sellInput, 15), text_size = size.small)
row(1, "Mode", mode + " (buy/sell proxy)", mode == "TPO" ? pocBoxInput : textInput)
row(2, "Prev POC", na(prevPoc) ? "-" : str.tostring(prevPoc, format.mintick), pocBoxInput)
row(3, "Prev VAH", na(prevVah) ? "-" : str.tostring(prevVah, format.mintick), textInput)
row(4, "Prev VAL", na(prevVal) ? "-" : str.tostring(prevVal, format.mintick), textInput)
row(5, "Price", where, whereC)
row(6, "Delta (period)", (delta >= 0 ? "+" : "") + str.tostring(delta, "#") + "%", delta >= 0 ? buyInput : sellInput)
row(7, "Last signal", na(lastSigBar) ? "-" : lastSig + " • " + str.tostring(bar_index - lastSigBar) + " bars", lastSig == "▲ Re-entry" ? bullInput : lastSig == "▼ Re-entry" ? bearInput : textInput)
float curTot = sB + sS
int curPctB = curTot > 0 ? int(math.round(sB / curTot * 100)) : 50
row(8, "Buy / Sell", str.tostring(curPctB) + "% " + str.tostring(sB, format.volume) + " / " + str.tostring(100 - curPctB) + "% " + str.tostring(sS, format.volume), curPctB >= 50 ? buyInput : sellInput)
// ---------- Alerts ----------
alertcondition(longSig, "Value area re-entry up", "Nexus Profile: closed back above previous VAL on {{ticker}} {{interval}} at {{close}}, target POC")
alertcondition(shortSig, "Value area re-entry down", "Nexus Profile: closed back below previous VAH on {{ticker}} {{interval}} at {{close}}, target POC")
alertcondition(nakedTouched, "Naked POC touched", "Nexus Profile: price touched a naked POC on {{ticker}} {{interval}} at {{close}}")
alertcondition(nakedNew, "New naked POC", "Nexus Profile: a period closed and left a new POC on {{ticker}} {{interval}}")