本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZILLIONAIRE010
//@version=6
indicator("Drawdown Recovery Censoring Audit", shorttitle="Recovery Audit", overlay=false, precision=2)
// Educational chart-price accounting, not portfolio returns or recovery forecasts.
// Duration = consecutive confirmed closes below the segment's running close high.
// The first underwater close counts; the recovery close does not.
// Invalid (missing or nonpositive) closes break continuity. Completed episodes
// remain in pooled statistics; an interrupted active episode is discarded and counted.
if not chart.is_standard
runtime.error("Use a standard chart. This audit does not support synthetic/nonstandard bars.")
var float peak = na
var float trough = na
var float currentDepth = na
var float worstDepth = na
var int activeBars = 0
var int completed = 0
var float completedSum = 0.0
var int completedMax = 0
var int invalidBars = 0
var int interrupted = 0
var int firstValidTime = na
var int segmentStartTime = na
var int lastConfirmedTime = na
var int validBars = 0
var bool lastValid = false
if barstate.isconfirmed
lastConfirmedTime := time
lastValid := not na(close) and close > 0
if not lastValid
invalidBars += 1
if activeBars > 0
interrupted += 1
peak := na
trough := na
currentDepth := na
worstDepth := na
activeBars := 0
segmentStartTime := na
else
validBars += 1
if na(firstValidTime)
firstValidTime := time
if na(peak)
peak := close
segmentStartTime := time
currentDepth := 0.0
worstDepth := 0.0
else if close < peak
activeBars += 1
trough := na(trough) ? close : math.min(trough, close)
currentDepth := 100.0 * (peak - close) / peak
worstDepth := 100.0 * (peak - trough) / peak
else
if activeBars > 0
completed += 1
completedSum += activeBars
completedMax := math.max(completedMax, activeBars)
peak := close
trough := na
activeBars := 0
currentDepth := 0.0
worstDepth := 0.0
float completedMean = completed > 0 ? completedSum / completed : na
float completedMaxValue = completed > 0 ? float(completedMax) : na
bool showConfirmed = barstate.isconfirmed
bool showValid = showConfirmed and lastValid
bool beyondCompleted = activeBars > 0 and completed > 0 and activeBars > completedMax
plot(showValid ? activeBars : na, "Current underwater closes", color=beyondCompleted ? color.orange : color.blue, style=plot.style_columns)
plot(showConfirmed ? completedMean : na, "Completed-only mean underwater closes", color=color.teal, linewidth=2, style=plot.style_linebr)
plot(showConfirmed ? completedMaxValue : na, "Completed-only max underwater closes", color=color.purple, linewidth=2, style=plot.style_linebr)
// Exportable audit fields. Forming-bar output is deliberately blank.
plot(showConfirmed ? completed : na, "Completed episodes", display=display.data_window)
plot(showConfirmed ? completedSum : na, "Completed underwater-close sum", display=display.data_window)
plot(showValid ? (activeBars > 0 ? 1 : 0) : na, "Open episode (0 or 1)", display=display.data_window)
plot(showValid ? currentDepth : na, "Current close depth (%)", display=display.data_window)
plot(showValid ? worstDepth : na, "Open episode worst close depth (%)", display=display.data_window)
plot(showValid ? peak : na, "Segment running close high", display=display.data_window)
plot(showConfirmed ? validBars : na, "Valid confirmed closes", display=display.data_window)
plot(showConfirmed ? invalidBars : na, "Invalid closes / continuity breaks", display=display.data_window)
plot(showConfirmed ? interrupted : na, "Interrupted episodes excluded", display=display.data_window)
plot(showConfirmed ? (lastValid ? 1 : 0) : na, "Valid close (0 or 1)", display=display.data_window)
numberOrNA(float value) =>
na(value) ? "N/A" : str.tostring(value, "#.##")
dateOrNA(int value) =>
na(value) ? "N/A" : str.format_time(value, "yyyy-MM-dd HH:mm", "UTC")
var table audit = table.new(position.top_right, 2, 12, border_width=1)
if barstate.islast
color cellBg = color.rgb(20, 23, 31)
color textFg = color.rgb(238, 241, 247)
table.cell(audit, 0, 0, "RECOVERY AUDIT", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 0, "Confirmed closes only", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 1, "First valid / segment (UTC)", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 1, dateOrNA(firstValidTime) + "\n" + dateOrNA(segmentStartTime), bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 2, "Last confirmed bar (UTC)", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 2, dateOrNA(lastConfirmedTime), bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 3, "Completed episodes", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 3, str.tostring(completed), bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 4, "Completed mean / max", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 4, numberOrNA(completedMean) + " / " + numberOrNA(completedMaxValue), bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 5, "Open episodes", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 5, lastValid ? (activeBars > 0 ? "1" : "0") : "N/A: invalid close", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 6, "Open count so far", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 6, activeBars > 0 ? str.tostring(activeBars) + " (lower bound)" : "N/A", bgcolor=cellBg, text_color=beyondCompleted ? color.orange : textFg, text_size=size.small)
table.cell(audit, 0, 7, "Open depth: now / worst", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 7, activeBars > 0 ? numberOrNA(currentDepth) + "% / " + numberOrNA(worstDepth) + "%" : "N/A", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 8, "Invalid / interrupted", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 8, str.tostring(invalidBars) + " / " + str.tostring(interrupted), bgcolor=cellBg, text_color=invalidBars > 0 ? color.orange : textFg, text_size=size.small)
table.cell(audit, 0, 9, "Count convention", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 9, "Underwater closes only;\nrecovery close excluded", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 10, "Scope", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 10, "Loaded history; valid segments pooled.\nBars, not calendar time.", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 0, 11, "Caution", bgcolor=cellBg, text_color=textFg, text_size=size.small)
table.cell(audit, 1, 11, "Open episode excluded from completed stats.\nRecovery is not assured or predicted.", bgcolor=cellBg, text_color=textFg, text_size=size.small)