N-Bar Clock Audit

ZILLIONAIRE010 · study · 130 行 · 点赞 10 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("N-Bar Clock Audit", overlay = false, max_bars_back = 5000, precision = 3)
 
// Original educational timing diagnostic for ZILLIONAIRE010.
// A close-to-close N-bar horizon contains N bar durations and N transitions.
// elapsed = scheduled bar durations + forward gaps - timestamp overlaps.
// A time gap is NOT evidence of missing trades, a feed fault, or an outage.
// No prices, orders, external series, future data, or trading signals are used.
 
int length = input.int(20, "Close-to-close horizon (N bars)", minval = 1, maxval = 2000, tooltip = "Measures from the close timestamp N bars ago to the latest confirmed close. Needs N + 1 chart bars. This differs from the open-to-close span of N candles.")
bool showTable = input.bool(true, "Show latest confirmed window")
string corner = input.string("Top right", "Table corner", options = ["Top right", "Top left", "Bottom right", "Bottom left"])
 
if barstate.isfirst and (not chart.is_standard or not (timeframe.isseconds or timeframe.isminutes))
    runtime.error("Use standard time-based seconds or minute/hour charts. Daily, weekly, monthly, tick, and synthetic charts are outside this audit's fixed-duration baseline.")
 
float nominalBarMs = timeframe.in_seconds() * 1000.0
float nominalWindowMs = length * nominalBarMs
bool validPair = not na(time_close) and not na(time_close[1]) and time_close > time
float durationMs = validPair ? float(time_close - time) : 0.0
float transitionMs = validPair ? float(time - time_close[1]) : 0.0
float gapMs = math.max(transitionMs, 0.0)
float overlapMs = math.max(-transitionMs, 0.0)
 
// Every rolling function runs on every execution. Invalid timestamps count as
// invalid, rather than being skipped by an na-ignoring rolling function.
float durations = math.sum(durationMs, length)
float gaps = math.sum(gapMs, length)
float overlaps = math.sum(overlapMs, length)
float gapCount = math.sum(gapMs > 0 ? 1.0 : 0.0, length)
float overlapCount = math.sum(overlapMs > 0 ? 1.0 : 0.0, length)
float shortCount = math.sum(validPair and durationMs < nominalBarMs ? 1.0 : 0.0, length)
float longCount = math.sum(validPair and durationMs > nominalBarMs ? 1.0 : 0.0, length)
float invalidCount = math.sum(validPair ? 0.0 : 1.0, length)
float largestGap = ta.highest(gapMs, length)
float elapsedMs = float(time_close - time_close[length])
bool fullWindow = bar_index >= length
bool ready = fullWindow and invalidCount == 0 and not na(elapsedMs) and elapsedMs > 0
float ratio = ready ? elapsedMs / nominalWindowMs : na
float residualMs = ready ? elapsedMs - (durations + gaps - overlaps) : na
bool closedWindow = barstate.isconfirmed and ready
 
// The live bar is intentionally blank. Its scheduled closing timestamp does
// not mean it has closed. The table retains the last committed window.
plot(closedWindow ? ratio : na, "Elapsed / nominal N-bar time", color = color.teal, linewidth = 2, style = plot.style_linebr)
hline(1.0, "Nominal N x timeframe", color = color.gray, linestyle = hline.style_dashed)
plot(closedWindow ? elapsedMs / 60000.0 : na, "Elapsed minutes", display = display.data_window)
plot(closedWindow ? durations / 60000.0 : na, "Scheduled bar minutes", display = display.data_window)
plot(closedWindow ? gaps / 60000.0 : na, "Forward gap minutes", display = display.data_window)
plot(closedWindow ? overlaps / 60000.0 : na, "Timestamp overlap minutes", display = display.data_window)
plot(closedWindow ? gapCount : na, "Forward gap count", display = display.data_window)
plot(closedWindow ? largestGap / 60000.0 : na, "Largest forward gap minutes", display = display.data_window)
plot(closedWindow ? shortCount : na, "Shorter-than-nominal bars", display = display.data_window)
plot(closedWindow ? longCount : na, "Longer-than-nominal bars", display = display.data_window)
plot(closedWindow ? overlapCount : na, "Timestamp overlap count", display = display.data_window)
plot(closedWindow ? residualMs : na, "Decomposition residual milliseconds", display = display.data_window)
 
var float lastRatio = na
var float lastElapsed = na
var float lastDuration = na
var float lastGaps = na
var float lastOverlaps = na
var float lastGapCount = na
var float lastLargest = na
var float lastShort = na
var float lastLong = na
var float lastOverlapCount = na
var float lastResidual = na
var int startClose = na
var int endClose = na
var bool lastReady = false
var bool lastFull = false
if barstate.isconfirmed
    lastReady := ready
    lastFull := fullWindow
    lastRatio := ratio
    lastElapsed := ready ? elapsedMs : na
    lastDuration := ready ? durations : na
    lastGaps := ready ? gaps : na
    lastOverlaps := ready ? overlaps : na
    lastGapCount := ready ? gapCount : na
    lastLargest := ready ? largestGap : na
    lastShort := ready ? shortCount : na
    lastLong := ready ? longCount : na
    lastOverlapCount := ready ? overlapCount : na
    lastResidual := residualMs
    startClose := ready ? time_close[length] : na
    endClose := ready ? time_close : na
 
f_minutes(float milliseconds) =>
    na(milliseconds) ? "n/a" : str.tostring(milliseconds / 60000.0, "#,##0.###") + " min"
 
f_count(float count) =>
    na(count) ? "n/a" : str.tostring(count, "0")
 
f_row(table t, int row, string label, string value, color bg) =>
    table.cell(t, 0, row, label, text_color = chart.fg_color, bgcolor = bg, text_size = size.small, text_halign = text.align_left)
    table.cell(t, 1, row, value, text_color = chart.fg_color, bgcolor = bg, text_size = size.small, text_halign = text.align_right)
 
string tablePosition = switch corner
    "Top left" => position.top_left
    "Bottom right" => position.bottom_right
    "Bottom left" => position.bottom_left
    => position.top_right
var table audit = table.new(tablePosition, 2, 15, border_width = 1, border_color = color.new(color.gray, 70))
if barstate.isfirst
    table.merge_cells(audit, 0, 0, 1, 0)
    table.merge_cells(audit, 0, 12, 1, 12)
    table.merge_cells(audit, 0, 13, 1, 13)
    table.merge_cells(audit, 0, 14, 1, 14)
 
if barstate.islast and showTable
    color bg = color.new(chart.bg_color, 5)
    color accent = color.new(color.teal, 82)
    string stateText = lastReady ? "Latest confirmed window" : lastFull ? "Invalid timestamp window" : "Waiting for N + 1 bars"
    table.cell(audit, 0, 0, "N-Bar Clock Audit | N = " + str.tostring(length), text_color = chart.fg_color, bgcolor = accent, text_size = size.small)
    f_row(audit, 1, "Elapsed / nominal", lastReady ? str.tostring(lastRatio, "0.000") + "x" : "n/a", accent)
    f_row(audit, 2, "Elapsed close-to-close", f_minutes(lastElapsed), bg)
    f_row(audit, 3, "Nominal N x timeframe", f_minutes(nominalWindowMs), bg)
    f_row(audit, 4, "Scheduled bar durations", f_minutes(lastDuration), bg)
    f_row(audit, 5, "Forward gaps (+)", f_minutes(lastGaps) + " | " + f_count(lastGapCount), bg)
    f_row(audit, 6, "Timestamp overlaps (-)", f_minutes(lastOverlaps) + " | " + f_count(lastOverlapCount), bg)
    f_row(audit, 7, "Largest forward gap", f_minutes(lastLargest), bg)
    f_row(audit, 8, "Short / long bars", f_count(lastShort) + " / " + f_count(lastLong), bg)
    f_row(audit, 9, "Start close (UTC)", lastReady ? str.format_time(startClose, "yyyy-MM-dd HH:mm:ss", "UTC") : "n/a", bg)
    f_row(audit, 10, "End close (UTC)", lastReady ? str.format_time(endClose, "yyyy-MM-dd HH:mm:ss", "UTC") : "n/a", bg)
    f_row(audit, 11, "Identity residual", lastReady ? str.tostring(lastResidual, "0.###") + " ms" : "n/a", bg)
    table.cell(audit, 0, 12, stateText + " | Live bar excluded", text_color = chart.fg_color, bgcolor = bg, text_size = size.small)
    table.cell(audit, 0, 13, "Durations are scheduled time, not traded activity", text_color = chart.fg_color, bgcolor = bg, text_size = size.small)
    table.cell(audit, 0, 14, "Gaps may be normal closures; no outage inference", text_color = chart.fg_color, bgcolor = bg, text_size = size.small)

← 返回列表