本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// FUTURES VOLUME SESSION LINES ON CFD CHARTS
// A line chart of the chart symbol's price, one line per trading session
// (Asia / Europe / US), drawn only where the matching futures contract really
// traded and, optionally, only where its volume beats the usual volume at
// that time of day. Line opacity follows that relative volume: strong where
// participation was high, faint where it was thin, a gap where the future did
// not trade at all. Bubbles in three sizes mark bars with above-normal volume.
// Hide the candles in the chart settings and the chart becomes a price path
// of real participation instead of a candle chart.
// Relative volume compares each bar with the average futures volume of the
// SAME clock time over the last N days (the open and close spikes are compared
// with earlier opens and closes, not with the quiet midday).
// CFD tick volume is never used.
indicator("Futures Volume Session Lines — CFD Charts", shorttitle = "Fut Vol Lines", overlay = true)
// ---- sessions ----
tzIn = input.string("Europe/Berlin", "Session timezone", group = "Sessions")
useAs = input.bool(true, "Asia", inline = "as", group = "Sessions")
sessAs = input.session("0100-0800", "", inline = "as", group = "Sessions")
colAs = input.color(color.new(#f2a900, 0), "", inline = "as", group = "Sessions")
useEu = input.bool(true, "Europe", inline = "eu", group = "Sessions")
sessEu = input.session("0800-1530", "", inline = "eu", group = "Sessions")
colEu = input.color(color.new(#2962ff, 0), "", inline = "eu", group = "Sessions")
useUs = input.bool(true, "US", inline = "us", group = "Sessions")
sessUs = input.session("1530-2200", "", inline = "us", group = "Sessions")
colUs = input.color(color.new(#e91e63, 0), "", inline = "us", group = "Sessions")
useOt = input.bool(false, "Other hours", inline = "ot", group = "Sessions",
tooltip = "Bars outside the three sessions where the future still traded. Where sessions overlap, the later one wins (US over Europe over Asia).")
colOt = input.color(color.new(color.gray, 0), "", inline = "ot", group = "Sessions")
// ---- volume filter ----
volMode = input.string("Futures symbol", "Volume source", options = ["Chart (CFD)", "Futures symbol"], group = "Volume filter",
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 filter")
futManual = input.symbol("EUREX:FDAX1!", "…manual otherwise", group = "Volume filter")
minRatio = input.float(0.0, "Show bars from (x normal volume, 0 = all)", minval = 0, step = 0.1, group = "Volume filter",
tooltip = "Normal = average futures volume at the same clock time over the lookback days. 0 shows every bar on which the future traded. 1.0 shows only bars with at least normal volume.")
lookback = input.int(20, "Lookback (days)", minval = 3, maxval = 60, group = "Volume filter")
belowMode = input.string("Hide", "Bars below the filter", options = ["Hide", "Dim"], group = "Volume filter")
useChart = volMode == "Chart (CFD)"
// ---- line ----
src = input.source(close, "Price", group = "Line")
lineW = input.int(2, "Line width", minval = 1, maxval = 4, group = "Line")
bubS = input.float(1.3, "Volume bubbles from (x normal): S", minval = 0, step = 0.1, inline = "bub", group = "Line")
bubM = input.float(2.0, "M", minval = 0, step = 0.1, inline = "bub", group = "Line")
bubL = input.float(3.0, "L", minval = 0, step = 0.1, inline = "bub", group = "Line",
tooltip = "Bubbles on the line where the bar's futures volume reaches this multiple of the same-time average: small, medium, large. Lower values show more bubbles. S = 0 turns bubbles off. To hide the chart's own candles, open Chart settings > Symbol and untick Body, Borders and Wick (Pine cannot hide the main series).")
// ---- 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)
// gaps_on: a chart bar without a futures bar (e.g. overnight hours of a 24h
// CFD) returns na instead of repeating the last futures volume.
futVol = request.security(futSym, timeframe.period, volume, gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
vol = useChart ? volume : futVol
traded = not na(vol) and vol > 0
// ---- session of this bar (later session wins on overlap) ----
inAs = useAs and not na(time(timeframe.period, sessAs, tzIn))
inEu = useEu and not na(time(timeframe.period, sessEu, tzIn))
inUs = useUs and not na(time(timeframe.period, sessUs, tzIn))
sid = inUs ? 3 : inEu ? 2 : inAs ? 1 : 0
// ---- relative volume against the same clock time of earlier days ----
// Only confirmed bars enter the baseline, and a bar is compared with the
// average BEFORE its own volume is added (no lookahead).
type Bucket
array<float> v
var map<int, Bucket> hist = map.new<int, Bucket>()
key = hour(time, tzIn) * 60 + minute(time, tzIn)
if not map.contains(hist, key)
map.put(hist, key, Bucket.new(array.new_float()))
arr = map.get(hist, key).v
nBase = array.size(arr)
avgV = nBase > 0 ? array.avg(arr) : na
relV = traded and nBase >= 3 and avgV > 0 ? vol / avgV : na
if traded and barstate.isconfirmed
array.push(arr, vol)
if array.size(arr) > lookback
array.shift(arr)
warm = traded and na(relV)
passes = traded and (minRatio <= 0 or warm or relV >= minRatio)
dimmed = traded and not passes and belowMode == "Dim"
shown = passes or dimmed
// opacity: 0x normal -> faint, 1x -> medium, 2x normal and more -> solid
f_col(color c) =>
t = dimmed ? 85.0 : na(relV) ? 20.0 : 60.0 - math.min(relV, 2.0) / 2.0 * 60.0
color.new(c, t)
plot(shown and sid == 1 ? src : na, "Asia", f_col(colAs), lineW, plot.style_linebr)
plot(shown and sid == 2 ? src : na, "Europe", f_col(colEu), lineW, plot.style_linebr)
plot(shown and sid == 3 ? src : na, "US", f_col(colUs), lineW, plot.style_linebr)
plot(shown and sid == 0 and useOt ? src : na, "Other hours", f_col(colOt), lineW, plot.style_linebr)
sCol = sid == 3 ? colUs : sid == 2 ? colEu : sid == 1 ? colAs : colOt
// bubble size by relative volume: largest tier reached wins
bubOk = bubS > 0 and passes and not na(relV) and (sid > 0 or useOt)
tier = not bubOk ? 0 : relV >= bubL ? 3 : relV >= bubM ? 2 : relV >= bubS ? 1 : 0
bCol = color.new(sCol, 15)
plot(tier == 1 ? src : na, "Bubble S", bCol, lineW + 2, plot.style_circles)
plot(tier == 2 ? src : na, "Bubble M", bCol, lineW + 5, plot.style_circles)
plot(tier == 3 ? src : na, "Bubble L", bCol, lineW + 9, plot.style_circles)
// ---- session volume shares of the current day ----
dayId = timestamp(tzIn, year(time, tzIn), month(time, tzIn), dayofmonth(time, tzIn), 0, 0)
newDay = ta.change(dayId) != 0
var float vAs = 0.0
var float vEu = 0.0
var float vUs = 0.0
var float vOt = 0.0
if newDay
vAs := 0.0
vEu := 0.0
vUs := 0.0
vOt := 0.0
if traded
if sid == 1
vAs += vol
else if sid == 2
vEu += vol
else if sid == 3
vUs += vol
else
vOt += vol
f_pct(float v, float tot) => tot > 0 ? str.tostring(100.0 * v / tot, "#") + "%" : "–"
var table st = table.new(position.top_right, 1, 4, bgcolor = color.new(color.black, 25), border_width = 0)
if barstate.islast
vTot = vAs + vEu + vUs + vOt
srcTxt = useChart ? "Chart volume" : vTot > 0 ? "Futures volume: " + futRoot + " ✓" : "⚠ Futures volume: " + futRoot + " — no data today"
filtTxt = minRatio > 0 ? "Filter: ≥ " + str.tostring(minRatio, "#.##") + "x same-time volume (" + str.tostring(lookback) + " d)" : "Filter: off (every bar with futures volume)"
shareTxt = "Today: Asia " + f_pct(vAs, vTot) + " · Europe " + f_pct(vEu, vTot) + " · US " + f_pct(vUs, vTot) + (vOt > 0 ? " · other " + f_pct(vOt, vTot) : "")
baseTxt = nBase < 3 ? "Building the time-of-day baseline: " + str.tostring(nBase) + "/3 days" : "Baseline: " + str.tostring(nBase) + " days at this clock time"
table.cell(st, 0, 0, srcTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(st, 0, 1, filtTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(st, 0, 2, shareTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(st, 0, 3, baseTxt, text_color = color.new(color.white, 30), text_size = size.small, text_halign = text.align_left)