本页源码来自 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.
// Copyright (c) 2026 ZILLIONAIRE010
//@version=6
indicator("Volatility Concentration Audit", overlay=false, precision=2, max_bars_back=501)
// An original educational diagnostic of the composition of close-to-close
// squared log returns. The inverse-concentration formula is standard math;
// it measures equivalent contributors, not independent observations.
int length = input.int(50, "Confirmed returns in window", minval=5, maxval=500, tooltip="Uses exactly N consecutive chart returns and therefore N+1 positive closes. A missing or nonpositive close invalidates the window. Default 50 is illustrative, not optimized.")
bool showTable = input.bool(true, "Show audit table")
string tableCorner = input.string("Top right", "Table position", options=["Top right", "Top left", "Bottom right", "Bottom left"])
bool supported = chart.is_standard and (timeframe.isseconds or timeframe.isminutes or timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly)
bool validPrice = not na(close) and not na(close[1]) and close > 0 and close[1] > 0
// The difference of logs avoids overflow from forming a price ratio.
float logReturn = validPrice ? math.log(close) - math.log(close[1]) : na
var float largestShare = na
var float effectiveBars = na
var float effectiveFraction = na
var float fullRms = na
var float excludedRms = na
var float retainedRms = na
var float largestSignedReturn = na
var int largestAge = na
var int sampleCount = 0
var int sampleClose = na
var string status = "Waiting for confirmed data"
// No request.security, future references, retrospective labels or intrabar
// accumulation. Persistent values change only on a confirmed chart bar.
if barstate.isconfirmed
largestShare := na
effectiveBars := na
effectiveFraction := na
fullRms := na
excludedRms := na
retainedRms := na
largestSignedReturn := na
largestAge := na
sampleCount := 0
sampleClose := time_close
status := supported ? "Insufficient valid returns" : "Use standard time-based chart"
if supported
float maxAbs = 0.0
int maxIndex = 0
// Scan newest to oldest; strict > gives ties to the most recent bar.
for i = 0 to length - 1
float r = logReturn[i]
if not na(r)
sampleCount += 1
if math.abs(r) > maxAbs
maxAbs := math.abs(r)
maxIndex := i
if sampleCount == length
if maxAbs == 0.0
fullRms := 0.0
excludedRms := 0.0
status := "All returns zero; shares undefined"
else
// Normalize before squaring to keep the sums well conditioned.
// Sum survivors directly, not S-M, to avoid cancellation.
float sumEnergy = 0.0
float sumEnergySquared = 0.0
float survivorEnergy = 0.0
for i = 0 to length - 1
float relativeReturn = logReturn[i] / maxAbs
float energy = relativeReturn * relativeReturn
sumEnergy += energy
sumEnergySquared += energy * energy
if i != maxIndex
survivorEnergy += energy
largestShare := 100.0 / sumEnergy
effectiveBars := math.min(length, math.max(1.0, sumEnergy * sumEnergy / sumEnergySquared))
effectiveFraction := 100.0 * effectiveBars / length
fullRms := 100.0 * maxAbs * math.sqrt(sumEnergy / length)
excludedRms := 100.0 * maxAbs * math.sqrt(survivorEnergy / (length - 1))
retainedRms := 100.0 * math.sqrt((survivorEnergy / sumEnergy) * length / (length - 1))
largestSignedReturn := 100.0 * logReturn[maxIndex]
largestAge := maxIndex
status := "Confirmed window"
// Do not display a provisional point on the open bar. The table retains the
// latest confirmed sample and explicitly timestamps it in the exchange zone.
bool plotReady = supported and barstate.isconfirmed
plot(plotReady ? largestShare : na, "Largest squared-return share (%)", color=color.orange, linewidth=2, style=plot.style_linebr)
plot(plotReady ? effectiveFraction : na, "Effective contributors / N (%)", color=color.aqua, linewidth=2, style=plot.style_linebr)
hline(0, "Zero", color=color.new(color.gray, 70))
hline(100, "100 percent", color=color.new(color.gray, 70))
plot(plotReady ? effectiveBars : na, "Effective contributing bars", display=display.data_window)
plot(plotReady ? fullRms : na, "Full RMS log return (% per bar)", display=display.data_window)
plot(plotReady ? excludedRms : na, "Ex-largest RMS log return (% per bar)", display=display.data_window)
plot(plotReady ? retainedRms : na, "RMS retained without largest (%)", display=display.data_window)
plot(plotReady ? largestSignedReturn : na, "Excluded log return (%)", display=display.data_window)
plot(plotReady ? largestAge : na, "Excluded return age (bars)", display=display.data_window)
formatNumber(float value) =>
na(value) ? "n/a" : str.tostring(value, "#.####")
formatPercent(float value) =>
na(value) ? "n/a" : str.tostring(value, "#.##") + "%"
string panelPosition = switch tableCorner
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
var table panel = table.new(panelPosition, 2, 11, bgcolor=color.new(color.black, 12), frame_color=color.new(color.gray, 50), frame_width=1)
writeRow(int row, string label, string value, color valueColor) =>
table.cell(panel, 0, row, label, text_color=color.silver, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, row, value, text_color=valueColor, text_size=size.small, text_halign=text.align_right)
if barstate.islast
if showTable
writeRow(0, "VOLATILITY COMPOSITION", status, color.white)
writeRow(1, "Valid returns / N", str.tostring(sampleCount) + " / " + str.tostring(length), color.white)
writeRow(2, "Largest energy share", formatPercent(largestShare), color.orange)
writeRow(3, "Effective contributors", formatNumber(effectiveBars) + " / " + str.tostring(length), color.aqua)
writeRow(4, "Full RMS (% log / bar)", formatNumber(fullRms), color.white)
writeRow(5, "Ex-largest RMS (% log / bar)", formatNumber(excludedRms), color.white)
writeRow(6, "RMS retained", formatPercent(retainedRms), color.white)
writeRow(7, "Excluded log return", formatPercent(largestSignedReturn), color.white)
writeRow(8, "Age from sample close (bars)", na(largestAge) ? "n/a" : str.tostring(largestAge), color.white)
writeRow(9, "Sample close (exchange zone)", na(sampleClose) ? "n/a" : str.format_time(sampleClose, "yyyy-MM-dd HH:mm", syminfo.timezone), color.white)
writeRow(10, "Descriptive only", "No forecast / no trade signal", color.silver)
else
table.clear(panel, 0, 0, 1, 10)