本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// FUTURES VALUE CONFLUENCE ON CFD CHARTS
// Static value: POC / VAH / VAL of the PREVIOUS session's futures volume profile.
// Dynamic value: the current session's futures VWAP with standard-deviation bands.
// The script compares the two on every bar: where the VWAP (optionally a band)
// sits within a tolerance of a static level it marks a confluence zone, it measures the room from
// price to the next value reference above and below, and it reports whether
// today's VWAP trades above, inside or below yesterday's value area.
// Both use the same session and the same REAL futures volume (price = CFD,
// weight = futures, placed per intrabar). CFD tick volume is never used.
// Descriptive tool: it shows where value sits, it does not signal trades.
indicator("Futures Value Confluence — CFD Charts", shorttitle = "Fut Value Conf", overlay = true,
max_boxes_count = 50, max_labels_count = 50)
// ---- session ----
sessMode = input.string("Auto (cash hours)", "Session", options = ["Auto (cash hours)", "Custom"], group = "Session",
tooltip = "Auto: FDAX 09:00-17:30 Berlin, FTSE 08:00-16:30 London, NQ/ES/YM 09:30-16:00 New York. Unknown symbols use the custom session below. Static value = the last completed session, dynamic value = the running one.")
sessCustom = input.session("0900-1730", "…custom session", group = "Session",
tooltip = "The session needs a break between days (a 24h session without a gap would never reset).")
tzCustom = input.string("Europe/Berlin", "…custom timezone", group = "Session")
// ---- volume ----
volMode = input.string("Futures symbol", "Volume source", options = ["Chart (CFD)", "Futures symbol"], group = "Volume",
tooltip = "Futures = real exchange volume of the matching contract. Chart = this chart's own volume (use on a native futures chart).")
autoFut = input.bool(true, "Auto-detect futures symbol", group = "Volume")
futManual = input.symbol("EUREX:FDAX1!", "…manual otherwise", group = "Volume")
precIn = input.string("Auto", "Intrabar precision", options = ["Auto", "1 min", "5 min", "15 min", "Off (chart bars)"], group = "Volume",
tooltip = "Volume is placed at the high/low of these sub-bars instead of the whole chart bar. Auto: 1 min up to 15m charts, 5 min up to 1h, 15 min above. Falls back to chart bars where no intrabar history exists.")
useChart = volMode == "Chart (CFD)"
useFut = not useChart
// ---- value levels ----
vaPct = input.float(70, "Value area %", minval = 50, maxval = 90, step = 5, group = "Value levels")
rowsIn = input.int(80, "Profile rows", minval = 20, maxval = 400, group = "Value levels")
sd1 = input.float(1.0, "VWAP band 1 (σ)", minval = 0.1, step = 0.1, group = "Value levels")
sd2 = input.float(2.0, "VWAP band 2 (σ)", minval = 0.1, step = 0.1, group = "Value levels")
// ---- confluence ----
tolPct = input.float(2.5, "Tolerance (% of daily ATR)", minval = 0.5, maxval = 50, step = 0.5, group = "Confluence",
tooltip = "A static and a dynamic level count as confluent when they are at most this far apart. Daily ATR(14) of the chart symbol, from completed days only. On DAX 2024-25, 2.5% marked a zone on about 10% of session bars, 5% on about 18%.")
incBands = input.bool(false, "Also compare with the VWAP bands", group = "Confluence",
tooltip = "Off: only the VWAP is compared with POC / VAH / VAL. On: the bands count too. With bands, some pair is almost always close (on DAX 2024-25 about 40% of bars at 2.5% tolerance), so zones lose meaning.")
showZ = input.bool(true, "Draw confluence zones", group = "Confluence")
// ---- display ----
showBands = input.bool(true, "Show VWAP bands", group = "Display")
showTab = input.bool(true, "Show value table", group = "Display")
stCol = input.color(color.blue, "Static value (VAH / VAL)", group = "Display")
pocCol = input.color(color.red, "Static POC", group = "Display")
dynCol = input.color(color.orange, "Dynamic value (VWAP)", group = "Display")
bandCol = input.color(color.gray, "VWAP bands", group = "Display")
zoneCol = input.color(color.purple, "Confluence zones", group = "Display")
// ---- auto-detection: chart symbol -> matching futures contract ----
f_detectFut() =>
s = str.lower(syminfo.ticker) + " " + str.lower(syminfo.prefix) + " " + str.lower(syminfo.description)
isNQ = str.contains(s, "nas") or str.contains(s, "ndx") or str.contains(s, "us100") or str.contains(s, "ustec") or str.contains(s, "tech100") or str.contains(s, "nq1") or str.contains(s, "nq!")
isYM = str.contains(s, "dow") or str.contains(s, "us30") or str.contains(s, "dji") or str.contains(s, "ym1") or str.contains(s, "ym!") or str.contains(s, "wall street")
isDAX = str.contains(s, "dax") or str.contains(s, "ger40") or str.contains(s, "de40") or str.contains(s, "german")
isFTSE = str.contains(s, "ftse") or str.contains(s, "uk100") or str.contains(s, "ukx")
isES = str.contains(s, "us500") or str.contains(s, "sp500") or str.contains(s, "spx") or str.contains(s, "s&p") or str.contains(s, "es1") or str.contains(s, "es!")
isDAX ? "EUREX:FDAX1!" : isFTSE ? "ICEEUR:Z1!" : isNQ ? "CME_MINI:NQ1!" :
isYM ? "CBOT_MINI:YM1!" : isES ? "CME_MINI:ES1!" : syminfo.tickerid
futSym = autoFut ? f_detectFut() : futManual
f_root(string sym) =>
parts = str.split(sym, ":")
r = array.get(parts, array.size(parts) - 1)
str.replace_all(str.replace_all(r, "1!", ""), "!", "")
futRoot = f_root(futSym)
// ---- session per future (cash hours) ----
isEU = str.contains(futSym, "FDAX") or str.contains(futSym, "FESX")
isUK = str.contains(futSym, "ICEEUR:Z")
isUS = str.contains(futSym, "NQ") or str.contains(futSym, "ES1") or str.contains(futSym, "YM") or str.contains(futSym, "RTY")
useAuto = sessMode == "Auto (cash hours)" and (isEU or isUK or isUS)
sessStr = useAuto ? (isEU ? "0900-1730" : isUK ? "0800-1630" : "0930-1600") : sessCustom
tzStr = useAuto ? (isEU ? "Europe/Berlin" : isUK ? "Europe/London" : "America/New_York") : tzCustom
// ---- intrabar timeframe (never higher than the chart) ----
chartSec = timeframe.in_seconds()
autoTf = chartSec <= 60 ? timeframe.period : chartSec <= 900 ? "1" : chartSec <= 3600 ? "5" : "15"
reqTf = precIn == "Auto" ? autoTf : precIn == "1 min" ? "1" : precIn == "5 min" ? "5" : precIn == "15 min" ? "15" : timeframe.period
ltf = timeframe.in_seconds(reqTf) > chartSec ? timeframe.period : reqTf
[cT, cH, cL, cV] = request.security_lower_tf(syminfo.tickerid, ltf, [time, high, low, volume])
[fT, fV] = request.security_lower_tf(futSym, ltf, [time, volume], ignore_invalid_symbol = true)
// Chart-bar fallback; gaps_on returns na where the future had no bar.
futVolBar = request.security(futSym, timeframe.period, volume, gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
// Daily ATR of the chart symbol from completed days only ([1] + lookahead_on = no repaint).
atrD = request.security(syminfo.tickerid, "D", ta.atr(14)[1], lookahead = barmerge.lookahead_on)
tol = nz(atrD) * tolPct / 100.0
// ---- session gate from the chart's own timestamp ----
inS = not na(time(timeframe.period, sessStr, tzStr))
gapNs = not na(time[1]) and time - time[1] > 2 * 3600000
newS = inS and (not inS[1] or gapNs)
endS = not inS and inS[1]
// ---- per-session state ----
MAX_E = 20000
var array<float> eLo = array.new_float()
var array<float> eHi = array.new_float()
var array<float> eV = array.new_float()
var bool sOpen = false
var int sBar = na
var float sumV = 0.0
var float sumPV = 0.0
var float sumP2V = 0.0
var int nIntra = 0
var int nMiss = 0
var int nCoarse = 0
// static value = profile of the last completed session
var float stPoc = na
var float stVah = na
var float stVal = na
var int nDone = 0
f_push(float lo, float hi, float v) =>
if array.size(eV) < MAX_E
array.push(eLo, lo)
array.push(eHi, hi)
array.push(eV, v)
// the VWAP always gets the entry, even beyond the profile cap
mid = (lo + hi) / 2.0
[v, v * mid, v * mid * mid]
// Profile of the collected session entries: [POC, VAH, VAL].
f_profile() =>
nE = array.size(eV)
float poc = na
float vah = na
float val = na
if nE > 0
rHi = array.max(eHi)
rLo = array.min(eLo)
rowW = (rHi - rLo) / rowsIn
nRows = rowsIn
if rowW <= 0
rowW := syminfo.mintick
nRows := 1
vols = array.new_float(nRows, 0.0)
for i = 0 to nE - 1
loK = math.max(0, math.min(nRows - 1, int(math.floor((array.get(eLo, i) - rLo) / rowW))))
hiK = math.max(0, math.min(nRows - 1, int(math.floor((array.get(eHi, i) - rLo) / rowW))))
share = array.get(eV, i) / (hiK - loK + 1)
for k = loK to hiK
array.set(vols, k, array.get(vols, k) + share)
total = array.sum(vols)
pocI = array.indexof(vols, array.max(vols))
vaVol = array.get(vols, pocI)
loI = pocI
hiI = pocI
target = total * vaPct / 100.0
while vaVol < target and (loI > 0 or hiI < nRows - 1)
upV = hiI < nRows - 1 ? array.get(vols, hiI + 1) : -1.0
dnV = loI > 0 ? array.get(vols, loI - 1) : -1.0
if upV >= dnV
hiI += 1
vaVol += math.max(upV, 0.0)
else
loI -= 1
vaVol += math.max(dnV, 0.0)
poc := rLo + (pocI + 0.5) * rowW
vah := rLo + (hiI + 1) * rowW
val := rLo + loI * rowW
[poc, vah, val]
// ---- session close: the finished profile becomes the static value ----
if (endS or newS) and sOpen
[p, h, l] = f_profile()
if not na(p)
stPoc := p
stVah := h
stVal := l
nDone += 1
sOpen := false
// ---- session start: reset ----
if newS
array.clear(eLo)
array.clear(eHi)
array.clear(eV)
sOpen := true
sBar := bar_index
sumV := 0.0
sumPV := 0.0
sumP2V := 0.0
nIntra := 0
nMiss := 0
nCoarse := 0
// ---- collect futures volume entries of the running session ----
if inS and sOpen
nC = array.size(cT)
if useFut
nF = array.size(fT)
if nC > 0 and nF > 0
j = 0
for i = 0 to nF - 1
v = array.get(fV, i)
if not na(v) and v > 0
t = array.get(fT, i)
while j < nC and array.get(cT, j) < t
j += 1
matched = j < nC and array.get(cT, j) == t
[a, b, c] = f_push(matched ? array.get(cL, j) : low, matched ? array.get(cH, j) : high, v)
sumV += a
sumPV += b
sumP2V += c
nIntra += 1
if not matched
nMiss += 1
else if not na(futVolBar) and futVolBar > 0
[a, b, c] = f_push(low, high, futVolBar)
sumV += a
sumPV += b
sumP2V += c
nCoarse += 1
else
if nC > 0
for i = 0 to nC - 1
v = array.get(cV, i)
if not na(v) and v > 0
[a, b, c] = f_push(array.get(cL, i), array.get(cH, i), v)
sumV += a
sumPV += b
sumP2V += c
nIntra += 1
else if not na(volume) and volume > 0
[a, b, c] = f_push(low, high, volume)
sumV += a
sumPV += b
sumP2V += c
nCoarse += 1
// ---- dynamic value: futures VWAP + bands of the running session ----
live = inS and sOpen and sumV > 0
vwap = live ? sumPV / sumV : na
dev = live ? math.sqrt(math.max(sumP2V / sumV - vwap * vwap, 0.0)) : na
u1 = vwap + sd1 * dev
l1 = vwap - sd1 * dev
u2 = vwap + sd2 * dev
l2 = vwap - sd2 * dev
// static lines break where a new profile takes over
stBreak = ta.change(stPoc) != 0
plot(stBreak ? na : stPoc, "Static POC", pocCol, 2, plot.style_linebr)
plot(stBreak ? na : stVah, "Static VAH", stCol, 1, plot.style_linebr)
plot(stBreak ? na : stVal, "Static VAL", stCol, 1, plot.style_linebr)
plot(vwap, "VWAP", dynCol, 2, plot.style_linebr)
plot(showBands ? u1 : na, "VWAP +band 1", color.new(bandCol, 30), 1, plot.style_linebr)
plot(showBands ? l1 : na, "VWAP -band 1", color.new(bandCol, 30), 1, plot.style_linebr)
plot(showBands ? u2 : na, "VWAP +band 2", color.new(bandCol, 55), 1, plot.style_linebr)
plot(showBands ? l2 : na, "VWAP -band 2", color.new(bandCol, 55), 1, plot.style_linebr)
// ---- confluence: every static level against every dynamic level ----
sNames = array.from("POC", "VAH", "VAL")
sVals = array.from(stPoc, stVah, stVal)
dNames = array.from("VWAP", "+" + str.tostring(sd1) + "σ", "-" + str.tostring(sd1) + "σ", "+" + str.tostring(sd2) + "σ", "-" + str.tostring(sd2) + "σ")
dVals = array.from(vwap, u1, l1, u2, l2)
zTop = array.new_float()
zBot = array.new_float()
zTxt = array.new_string()
inZone = false
if live and not na(stPoc) and tol > 0
for i = 0 to 2
sv = array.get(sVals, i)
for k = 0 to (incBands ? 4 : 0)
dv = array.get(dVals, k)
if math.abs(sv - dv) <= tol
top = math.max(sv, dv) + tol / 4
bot = math.min(sv, dv) - tol / 4
array.push(zTop, top)
array.push(zBot, bot)
array.push(zTxt, array.get(sNames, i) + " ≈ " + array.get(dNames, k))
if high >= bot and low <= top
inZone := true
alertcondition(inZone and not inZone[1], "Price enters a confluence zone",
"{{ticker}}: price entered a zone where static and dynamic futures value meet")
// ---- room: nearest value reference above and below price ----
refN = array.concat(array.copy(sNames), array.copy(dNames))
refV = array.concat(array.copy(sVals), array.copy(dVals))
float upD = na
float dnD = na
string upN = ""
string dnN = ""
for i = 0 to array.size(refV) - 1
r = array.get(refV, i)
if not na(r)
if r > close and (na(upD) or r - close < upD)
upD := r - close
upN := array.get(refN, i)
if r < close and (na(dnD) or close - r < dnD)
dnD := close - r
dnN := array.get(refN, i)
// ---- draw zones + table on the last bar ----
var box[] zBoxes = array.new_box()
var label[] zLabs = array.new_label()
var table tab = table.new(position.top_right, 1, 6, bgcolor = color.new(color.black, 25), border_width = 0)
f_atr(float d) => atrD > 0 ? " (" + str.tostring(100.0 * d / atrD, "#") + "% ATR)" : ""
if barstate.islast
for b in zBoxes
box.delete(b)
array.clear(zBoxes)
for lb in zLabs
label.delete(lb)
array.clear(zLabs)
if showZ and array.size(zTop) > 0
for i = 0 to array.size(zTop) - 1
array.push(zBoxes, box.new(nz(sBar, bar_index), array.get(zTop, i), bar_index + 5, array.get(zBot, i),
border_color = color.new(zoneCol, 40), bgcolor = color.new(zoneCol, 82)))
array.push(zLabs, label.new(bar_index + 5, (array.get(zTop, i) + array.get(zBot, i)) / 2, array.get(zTxt, i),
style = label.style_label_left, color = color.new(zoneCol, 20), textcolor = color.white, size = size.small))
if showTab
hasVol = useChart or nIntra + nCoarse > 0
srcTxt = useChart ? "Chart volume" : hasVol ? "Futures volume: " + futRoot + " ✓" : "⚠ Futures volume: " + futRoot + " — no data this session"
srcTxt += " · " + sessStr + " " + tzStr + (useAuto ? "" : " (custom)")
relTxt = na(stPoc) ? "Static value: waiting for the first complete session" :
not live ? "Session closed: static = the session just finished" :
vwap > stVah ? "VWAP above prior value area: value migrating up" :
vwap < stVal ? "VWAP below prior value area: value migrating down" :
"VWAP inside prior value area: overlapping value"
zTxtRow = live and dev > 0 ? "Price vs VWAP: " + (close >= vwap ? "+" : "") + str.tostring((close - vwap) / dev, "#.##") + "σ" : "Price vs VWAP: session not running"
upTxt = na(upD) ? "Room up: no reference above" : "Room up: " + str.tostring(upD, format.mintick) + " to " + upN + f_atr(upD)
dnTxt = na(dnD) ? "Room down: no reference below" : "Room down: " + str.tostring(dnD, format.mintick) + " to " + dnN + f_atr(dnD)
cTxt = str.tostring(array.size(zTop)) + " confluence zone" + (array.size(zTop) == 1 ? "" : "s") + " · tolerance " + str.tostring(tol, format.mintick)
table.cell(tab, 0, 0, srcTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tab, 0, 1, relTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tab, 0, 2, zTxtRow, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tab, 0, 3, upTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tab, 0, 4, dnTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tab, 0, 5, cTxt, text_color = color.new(color.white, 30), text_size = size.small, text_halign = text.align_left)