Range Boundary Retirement Audit

ZILLIONAIRE010 · study · 91 行 · 点赞 13 · TradingView 原页

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

Pine Script

//@version=6
// This source code is subject to the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Independently implemented for ZILLIONAIRE010.
indicator("Range Boundary Retirement Audit", shorttitle = "Boundary Retirement", overlay = true, max_bars_back = 1002, precision = 6)
 
// Each test compares the close with the range of PRIOR L bars, excluding the test bar.
// A retiring extreme can relax this threshold. This is not a claim that price did not move.
int length = input.int(20, "Prior-bar range length", minval = 1, maxval = 500)
int auditLength = input.int(100, "Audit window (confirmed test bars)", minval = 1, maxval = 500)
bool showBounds = input.bool(true, "Show prior-bar boundaries")
bool showMarkers = input.bool(true, "Show confirmed close-exceedance markers")
bool showTable = input.bool(true, "Show audit table")
string corner = input.string("Bottom right", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"])
 
bool standardTime = chart.is_standard and (timeframe.isseconds or timeframe.isminutes or timeframe.isdwm)
if barstate.isfirst and not standardTime
    runtime.error("Use standard time-based candles or bars. Synthetic, tick and range charts are not supported.")
 
bool validOHLC = not na(open) and not na(high) and not na(low) and not na(close) and high >= math.max(open, close) and low <= math.min(open, close) and high >= low
float upper = ta.highest(high[1], length)
float lower = ta.lowest(low[1], length)
float priorUpper = upper[1]
float priorLower = lower[1]
float validHistory = math.sum(validOHLC ? 1.0 : 0.0, length + 2)
bool ready = bar_index >= length + 1 and validHistory == length + 2
bool up = ready and close > upper
bool down = ready and close < lower
bool assistedUp = up and close <= priorUpper
bool assistedDown = down and close >= priorLower
// Net boundary movement only. Tied retiring extrema cause no contraction.
float upperRetirement = ready ? math.max(priorUpper - upper, 0.0) : na
float lowerRetirement = ready ? math.max(lower - priorLower, 0.0) : na
float upperExpansion = ready ? math.max(upper - priorUpper, 0.0) : na
float lowerExpansion = ready ? math.max(priorLower - lower, 0.0) : na
float eligibleCount = math.sum(ready ? 1.0 : 0.0, auditLength)
bool auditReady = bar_index >= length + auditLength and eligibleCount == auditLength
float upCount = math.sum(up ? 1.0 : 0.0, auditLength)
float downCount = math.sum(down ? 1.0 : 0.0, auditLength)
float assistedUpCount = math.sum(assistedUp ? 1.0 : 0.0, auditLength)
float assistedDownCount = math.sum(assistedDown ? 1.0 : 0.0, auditLength)
float contractionCount = math.sum(ready and (upperRetirement > 0 or lowerRetirement > 0) ? 1.0 : 0.0, auditLength)
bool confirmed = barstate.isconfirmed
 
plot(showBounds and confirmed and ready ? upper : na, "Prior L upper", color = color.new(color.blue, 25), style = plot.style_stepline)
plot(showBounds and confirmed and ready ? lower : na, "Prior L lower", color = color.new(color.blue, 25), style = plot.style_stepline)
plotshape(showMarkers and confirmed and assistedUp, "Retirement-assisted upper close", shape.diamond, location.abovebar, color.orange, size = size.small, text = "R", textcolor = color.orange, display = display.pane)
plotshape(showMarkers and confirmed and assistedDown, "Retirement-assisted lower close", shape.diamond, location.belowbar, color.orange, size = size.small, text = "R", textcolor = color.orange, display = display.pane)
plotshape(showMarkers and confirmed and up and not assistedUp, "Upper close exceeds both boundaries", shape.circle, location.abovebar, color.teal, size = size.tiny, display = display.pane)
plotshape(showMarkers and confirmed and down and not assistedDown, "Lower close exceeds both boundaries", shape.circle, location.belowbar, color.teal, size = size.tiny, display = display.pane)
// Independent exportable diagnostics. No intrabar preview, offsets or higher-timeframe requests.
plot(confirmed and ready ? upper : na, "Audit upper", display = display.data_window)
plot(confirmed and ready ? lower : na, "Audit lower", display = display.data_window)
plot(confirmed and ready ? priorUpper : na, "Previous upper", display = display.data_window)
plot(confirmed and ready ? priorLower : na, "Previous lower", display = display.data_window)
plot(confirmed ? upperRetirement : na, "Upper net retirement", display = display.data_window)
plot(confirmed ? lowerRetirement : na, "Lower net retirement", display = display.data_window)
plot(confirmed ? upperExpansion : na, "Upper net expansion", display = display.data_window)
plot(confirmed ? lowerExpansion : na, "Lower net expansion", display = display.data_window)
plot(confirmed and ready ? (up ? 1 : 0) : na, "Upper exceedance flag", display = display.data_window)
plot(confirmed and ready ? (down ? 1 : 0) : na, "Lower exceedance flag", display = display.data_window)
plot(confirmed and ready ? (assistedUp ? 1 : 0) : na, "Upper retirement-assisted flag", display = display.data_window)
plot(confirmed and ready ? (assistedDown ? 1 : 0) : na, "Lower retirement-assisted flag", display = display.data_window)
plot(confirmed and auditReady ? upCount : na, "Audit upper count", display = display.data_window)
plot(confirmed and auditReady ? downCount : na, "Audit lower count", display = display.data_window)
plot(confirmed and auditReady ? assistedUpCount : na, "Audit assisted upper count", display = display.data_window)
plot(confirmed and auditReady ? assistedDownCount : na, "Audit assisted lower count", display = display.data_window)
plot(confirmed and auditReady ? contractionCount : na, "Audit inward-boundary bars", display = display.data_window)
 
string positionValue = corner == "Top right" ? position.top_right : corner == "Bottom right" ? position.bottom_right : corner == "Top left" ? position.top_left : position.bottom_left
var table report = table.new(positionValue, 2, 12, border_width = 1)
f_row(int r, string label, string value, color valueColor) =>
    table.cell(report, 0, r, label, text_color = color.white, bgcolor = color.rgb(20, 24, 31), text_size = size.small)
    table.cell(report, 1, r, value, text_color = valueColor, bgcolor = color.rgb(20, 24, 31), text_size = size.small)
f_count(float value) => str.tostring(value, "0")
f_share(float part, float total) => total > 0 ? str.tostring(100 * part / total, "0.0") + "%" : "n/a: no exceedances"
// Only a completed bar can replace the table. It stays unchanged during the next forming bar.
if confirmed and showTable
    color fg = color.white
    string status = auditReady ? "Complete" : "Warming up / missing data"
    f_row(0, "BOUNDARY RETIREMENT", "L=" + str.tostring(length) + " | M=" + str.tostring(auditLength), fg)
    f_row(1, "Audit status", status, fg)
    f_row(2, "Upper close exceedances", auditReady ? f_count(upCount) : "n/a", fg)
    f_row(3, "Upper retirement-assisted", auditReady ? f_count(assistedUpCount) + " / " + f_share(assistedUpCount, upCount) : "n/a", color.orange)
    f_row(4, "Lower close exceedances", auditReady ? f_count(downCount) : "n/a", fg)
    f_row(5, "Lower retirement-assisted", auditReady ? f_count(assistedDownCount) + " / " + f_share(assistedDownCount, downCount) : "n/a", color.orange)
    f_row(6, "Bars with inward boundary move", auditReady ? f_count(contractionCount) + " / " + str.tostring(auditLength) : "n/a", fg)
    f_row(7, "Orange R", "Exceeds new, not previous boundary", color.orange)
    f_row(8, "Teal dot", "Exceeds both boundaries", color.teal)
    f_row(9, "Window end", str.format_time(time_close, "yyyy-MM-dd HH:mm", syminfo.timezone), fg)
    f_row(10, "Confirmed only", syminfo.timezone, fg)
    f_row(11, "Each qualifying bar counts", "Not trades, independence or forecasts", fg)

← 返回列表