Multi-Rolling VWAP

mochains · study · 629 行 · 点赞 17 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// Original Rolling VWAP © TradingView. Adapted and extended for MOXBT.
// MOXBT Multi-RVWAP v1.7.1 — configurable deviation status panel position.
//@version=6
indicator("MOXBT Multi-Rolling VWAP", "MOXBT Multi-RVWAP", overlay = true, max_labels_count = 50, max_polylines_count = 32)
 
import PineCoders/ConditionalAverages/2 as pc
 
// Chart-synthetic OHLC prices are not a suitable default input for volume-weighted
// price levels. Use a standard bar/candle/line chart with actual OHLC values.
if not chart.is_standard
    runtime.error("MOXBT Multi-RVWAP: use standard candles/bars/lines with market OHLC, not synthetic chart prices.")
if timeframe.isticks
    runtime.error("MOXBT Multi-RVWAP: use a time-based chart. Tick bars do not provide a consistent elapsed chart timeframe for automatic deviations.")
 
// Rolling elapsed-time windows. These are NOT calendar-period anchored VWAPs.
const int MS_HOUR = 60 * 60 * 1000
const int MS_DAY = MS_HOUR * 24
const int MS_WEEK = MS_DAY * 7
const int MS_MONTH = 2629800000 // 30.4375 days (approx. rolling month)
const int MS_90D = 90 * MS_DAY   // exact elapsed 90 days
const int MS_365D = 365 * MS_DAY // exact elapsed 365 days
 
// ── GENERAL
const string G_GENERAL = "01 · General"
float src = input.source(hlc3, "Price source", group = G_GENERAL)
bool autoHide = input.bool(true, "Auto-hide windows <= chart timeframe", group = G_GENERAL,
     tooltip = "On a 1D chart, hides 1D and shorter RVWAPs; on a 3D chart, hides 3D and shorter RVWAPs. Disable for full manual control.")
bool requireFull = input.bool(true, "Require elapsed window history", group = G_GENERAL,
     tooltip = "Waits until elapsed time since the first positive-volume source candle reaches the selected window. This confirms age of history, not completeness of trading observations inside it. Long RVWAPs can be unavailable on short loaded datasets.")
int minBars = input.int(1, "Minimum bars kept in time window", minval = 1, maxval = 100, group = G_GENERAL,
     tooltip = "Minimum retained chart candles in the rolling library. Keeping 1 can exceptionally retain a candle beyond the elapsed window across a long closure or missing-data gap; 90D/365D are nominal elapsed spans, not absolute hard cutoffs in this case.")
int minObsForBands = input.int(5, "Minimum positive-volume bars for bands", minval = 2, maxval = 100, group = G_GENERAL,
     tooltip = "Minimum count of valid positive-volume chart candles in the rolling window. This is a data-quality gate, not statistical proof of band reliability.")
float minEffectiveObs = input.float(3.0, "Minimum effective observations for bands", minval = 1.0, maxval = 100.0, step = 0.5, group = G_GENERAL,
     tooltip = "Effective sample size = (sum volume)^2 / sum(volume squared). If one candle dominates volume, effective observations fall below the raw candle count. Recommended 3.0 or higher.")
 
// ── DEVIATION CONTROLS
const string G_BANDS = "02 · Standard deviations"
// Auto mode changes the *deviation source*, not the main RVWAP visibility map.
// It selects the requested trading-oriented rolling window as chart TF increases.
string devMode = input.string("Auto (chart timeframe)", "Deviation display", group = G_BANDS,
     options = ["Auto (chart timeframe)", "Off", "Selected RVWAP", "1D only", "1W only", "1D + 1W", "All visible"],
     tooltip = "Auto: up to 1H chart → 1D; above 1H to 4H → 3D; above 4H to 12H → 1W; above 12H to 1D → 1M; above 1D to 1W → 90D; above 1W to 1M → 365D; above 1M → Off. Intermediate chart intervals follow the nearest range. Quality gates and global/local switches still apply.")
string selectedDev = input.string("1D", "Selected RVWAP (when mode is Selected)", group = G_BANDS,
     options = ["1H", "4H", "1D", "3D", "1W", "1M", "90D", "365D"])
// 90D is manually off by default; Auto can display it when selected.
bool autoShowRequired = input.bool(true, "Auto-display selected RVWAP centerline", group = G_BANDS,
     tooltip = "In AUTO mode, display the selected RVWAP and its bands even when the individual Show toggle is off. OFF keeps each Show toggle as a strict master switch. Does not override timeframe filtering or data-quality checks.")
bool globalS1 = input.bool(true, "Show ±1σ", inline = "levels", group = G_BANDS)
bool globalS2 = input.bool(true, "±2σ", inline = "levels", group = G_BANDS)
bool globalS3 = input.bool(false, "±3σ", inline = "levels", group = G_BANDS)
bool globalS4 = input.bool(false, "±4σ", inline = "levels", group = G_BANDS)
float mult1 = input.float(1.0, "Level 1 multiplier", minval = 0.1, step = 0.25, group = G_BANDS)
float mult2 = input.float(2.0, "Level 2 multiplier", minval = 0.1, step = 0.25, group = G_BANDS)
float mult3 = input.float(3.0, "Level 3 multiplier", minval = 0.1, step = 0.25, group = G_BANDS)
float mult4 = input.float(4.0, "Level 4 multiplier", minval = 0.1, step = 0.25, group = G_BANDS,
     tooltip = "Fourth, extreme deviation. Independent of levels 1–3; disabled by default. It uses recent-history drawing objects to stay within Pine's 64 plot-count limit.")
int l4History = input.int(1200, "±4σ recent history (bars)", minval = 100, maxval = 4000, step = 100, group = G_BANDS,
     tooltip = "Only L4 uses bounded recent-history polylines; L1–L3 and RVWAP centerlines remain full-history plots. Maximum 4000 bars per source. A total-point budget may reduce this limit when multiple 4σ sources are active; the status panel shows the effective cap. Up to 16 polylines.")
bool showDevStatus = input.bool(true, "Show active deviation status", group = G_BANDS,
     tooltip = "Compact status: selected rolling band window, enabled multipliers, sample count and READY/hidden reason. When 4σ is enabled, its bounded history is noted. Does not consume plot counts.")
string statusPosition = input.string(position.bottom_center, "Status panel position", group = G_BANDS,
     options = [position.bottom_center, position.bottom_left, position.bottom_right, position.top_right],
     tooltip = "Move the deviation status away from TradingView chart controls. Bottom Center is the default; Bottom Left is an alternative.")
bool markActiveTags = input.bool(true, "Mark active band source with σ", group = G_BANDS,
     tooltip = "Adds a sigma mark to the label of each RVWAP with a displayed deviation band. Turn off for label-only tags.")
 
// ── RIGHT-SIDE TAGS
const string G_TAGS = "03 · Right-side labels"
bool showTags = input.bool(true, "Show RVWAP tags", group = G_TAGS)
bool tagShowPrice = input.bool(false, "Include price in tag", group = G_TAGS)
int tagOffset = input.int(3, "Offset from last bar (bars)", minval = 1, maxval = 100, group = G_TAGS)
string tagSizeInput = input.string("Tiny", "Tag size", options = ["Tiny", "Small", "Normal"], group = G_TAGS)
int tagBgTransparency = input.int(88, "Tag background transparency", minval = 0, maxval = 100, group = G_TAGS)
string tagSize = switch tagSizeInput
    "Small" => size.small
    "Normal" => size.normal
    => size.tiny
 
// ── WINDOW OPTIONS
// Main-line colors are editable and also color the right-side tags.
// L1–L3 bands use static subdued presets to retain the 64 plot-count budget.
// L4 drawings use a matching, adjustable RVWAP line/tag color. L4 is not in Style;
// change the matching source color under its window's Inputs group.
 
const string G_1H = "1H · 60 minutes"
bool on_1h = input.bool(false, "Show 1H", inline = "main", group = G_1H)
color col_1h = input.color(#8F9CAB, "Line / tag", inline = "main", group = G_1H, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_1h = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_1H)
bool tag_1h = input.bool(true, "Tag", inline = "bands", group = G_1H)
bool b1_1h = input.bool(true, "1σ", inline = "bands", group = G_1H)
bool b2_1h = input.bool(true, "2σ", inline = "bands", group = G_1H)
bool b3_1h = input.bool(true, "3σ", inline = "bands", group = G_1H)
bool b4_1h = input.bool(true, "4σ", inline = "bands", group = G_1H)
const color bandCol_1h = #8F9CAB
 
const string G_4H = "4H · 240 minutes"
bool on_4h = input.bool(false, "Show 4H", inline = "main", group = G_4H)
color col_4h = input.color(#768B97, "Line / tag", inline = "main", group = G_4H, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_4h = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_4H)
bool tag_4h = input.bool(true, "Tag", inline = "bands", group = G_4H)
bool b1_4h = input.bool(true, "1σ", inline = "bands", group = G_4H)
bool b2_4h = input.bool(true, "2σ", inline = "bands", group = G_4H)
bool b3_4h = input.bool(true, "3σ", inline = "bands", group = G_4H)
bool b4_4h = input.bool(true, "4σ", inline = "bands", group = G_4H)
const color bandCol_4h = #768B97
 
const string G_1D = "1D · 24 hours"
bool on_1d = input.bool(true, "Show 1D", inline = "main", group = G_1D)
color col_1d = input.color(#D7A64B, "Line / tag", inline = "main", group = G_1D, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_1d = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_1D)
bool tag_1d = input.bool(true, "Tag", inline = "bands", group = G_1D)
bool b1_1d = input.bool(true, "1σ", inline = "bands", group = G_1D)
bool b2_1d = input.bool(true, "2σ", inline = "bands", group = G_1D)
bool b3_1d = input.bool(true, "3σ", inline = "bands", group = G_1D)
bool b4_1d = input.bool(true, "4σ", inline = "bands", group = G_1D)
const color bandCol_1d = #D7A64B
 
const string G_3D = "3D · 72 hours"
bool on_3d = input.bool(true, "Show 3D", inline = "main", group = G_3D)
color col_3d = input.color(#B48B57, "Line / tag", inline = "main", group = G_3D, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_3d = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_3D)
bool tag_3d = input.bool(true, "Tag", inline = "bands", group = G_3D)
bool b1_3d = input.bool(true, "1σ", inline = "bands", group = G_3D)
bool b2_3d = input.bool(true, "2σ", inline = "bands", group = G_3D)
bool b3_3d = input.bool(true, "3σ", inline = "bands", group = G_3D)
bool b4_3d = input.bool(true, "4σ", inline = "bands", group = G_3D)
const color bandCol_3d = #B48B57
 
const string G_1W = "1W · 7 days"
bool on_1w = input.bool(true, "Show 1W", inline = "main", group = G_1W)
color col_1w = input.color(#4EA59A, "Line / tag", inline = "main", group = G_1W, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_1w = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_1W)
bool tag_1w = input.bool(true, "Tag", inline = "bands", group = G_1W)
bool b1_1w = input.bool(true, "1σ", inline = "bands", group = G_1W)
bool b2_1w = input.bool(true, "2σ", inline = "bands", group = G_1W)
bool b3_1w = input.bool(true, "3σ", inline = "bands", group = G_1W)
bool b4_1w = input.bool(true, "4σ", inline = "bands", group = G_1W)
const color bandCol_1w = #4EA59A
 
const string G_1M = "1M · approx. 30.44 days"
bool on_1m = input.bool(true, "Show 1M", inline = "main", group = G_1M)
color col_1m = input.color(#7795B6, "Line / tag", inline = "main", group = G_1M, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_1m = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_1M)
bool tag_1m = input.bool(true, "Tag", inline = "bands", group = G_1M)
bool b1_1m = input.bool(true, "1σ", inline = "bands", group = G_1M)
bool b2_1m = input.bool(true, "2σ", inline = "bands", group = G_1M)
bool b3_1m = input.bool(true, "3σ", inline = "bands", group = G_1M)
bool b4_1m = input.bool(true, "4σ", inline = "bands", group = G_1M)
const color bandCol_1m = #7795B6
 
const string G_90D = "90D · exact 90 days"
bool on_90d = input.bool(false, "Show 90D", inline = "main", group = G_90D, tooltip = "Manual visibility. When Auto-display selected RVWAP is enabled, AUTO mode can show the needed 90D centerline and deviations even if this is off.")
color col_90d = input.color(#A0A8B4, "Line / tag", inline = "main", group = G_90D, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_90d = input.int(2, "Width", minval = 1, maxval = 5, inline = "main", group = G_90D)
bool tag_90d = input.bool(true, "Tag", inline = "bands", group = G_90D)
bool b1_90d = input.bool(true, "1σ", inline = "bands", group = G_90D)
bool b2_90d = input.bool(true, "2σ", inline = "bands", group = G_90D)
bool b3_90d = input.bool(true, "3σ", inline = "bands", group = G_90D)
bool b4_90d = input.bool(true, "4σ", inline = "bands", group = G_90D)
const color bandCol_90d = #A0A8B4
 
const string G_365D = "1Y · exact 365 rolling days"
bool on_365d = input.bool(true, "Show 1Y (365D)", inline = "main", group = G_365D)
color col_365d = input.color(#CED4DA, "Line / tag", inline = "main", group = G_365D, tooltip = "Changes main line and right-side tag. Deviation colors can be adjusted under Style.")
int width_365d = input.int(3, "Width", minval = 1, maxval = 5, inline = "main", group = G_365D)
bool tag_365d = input.bool(true, "Tag", inline = "bands", group = G_365D)
bool b1_365d = input.bool(true, "1σ", inline = "bands", group = G_365D)
bool b2_365d = input.bool(true, "2σ", inline = "bands", group = G_365D)
bool b3_365d = input.bool(true, "3σ", inline = "bands", group = G_365D)
bool b4_365d = input.bool(true, "4σ", inline = "bands", group = G_365D)
const color bandCol_365d = #CED4DA
 
// ── ROLLING CALCULATION
// Price and volume are sampled from CHART candles. This is NOT tick VWAP.
// All eight instances have isolated rolling state in PineCoders/ConditionalAverages/2.
// Bad/zero-volume observations do not contribute; they cannot poison the sums.
// Variance is a volume-weighted population second central moment, translated
// around the fixed first valid price for numerical stability. No Bessel adjustment.
var int firstValidBarTime = na
bool validBar = not na(src) and not na(volume) and volume > 0
// Fixed translation origin greatly reduces subtracting two large squared prices.
// It is set only once per script run and never recalibrated mid-window.
var float priceOrigin = na
if na(firstValidBarTime) and validBar
    firstValidBarTime := time
    priceOrigin := src
float totalChartVolume = ta.cum(validBar ? volume : 0.0)
if barstate.islast and totalChartVolume <= 0
    runtime.error("MOXBT Multi-RVWAP needs positive volume and a valid price series.")
 
// `computeBands` is fixed during a script execution (chart timeframe + user input).
// Compute just the two VWAP sums for centerline-only windows. The three extra
// aggregation states (squared price, squared volume, count) are created only
// for windows selected by the deviation display mode. All enabled states are
// updated on every chart bar, as required by the PineCoders rolling library.
f_rvwap(simple int windowMs, bool enabled, bool computeBands) =>
    float mean = na
    float sigma = na
    float observations = na
    float effectiveN = na
    bool fullReady = false
    if enabled
        float v = validBar ? volume : 0.0
        float delta = validBar and not na(priceOrigin) ? src - priceOrigin : 0.0
        // Translation-invariant moments: E[P] = origin + E[delta],
        // Var[P] = E[delta²] - E[delta]². This improves precision when
        // P is large relative to the within-window price dispersion.
        float sumDeltaV = pc.totalForTimeWhen(delta * v, windowMs, true, minBars)
        float sumV = pc.totalForTimeWhen(v, windowMs, true, minBars)
        float deltaMean = sumV > 0 ? sumDeltaV / sumV : na
        fullReady := not requireFull or (not na(firstValidBarTime) and time - firstValidBarTime >= windowMs)
        if fullReady and not na(deltaMean)
            mean := priceOrigin + deltaMean
        if computeBands
            float sumDelta2V = pc.totalForTimeWhen(delta * delta * v, windowMs, true, minBars)
            float sumV2 = pc.totalForTimeWhen(v * v, windowMs, true, minBars)
            observations := pc.totalForTimeWhen(v > 0 ? 1.0 : 0.0, windowMs, true, minBars)
            effectiveN := sumV > 0 and sumV2 > 0 ? sumV * sumV / sumV2 : na
            if fullReady and not na(deltaMean) and observations >= minObsForBands and effectiveN >= minEffectiveObs
                // Volume-weighted population dispersion; no Bessel correction.
                float variance = sumDelta2V / sumV - deltaMean * deltaMean
                sigma := math.sqrt(math.max(0.0, variance))
    [mean, sigma, observations, effectiveN, fullReady]
 
// Chart timeframe -> deviation window (constant for this execution).
// If the chart timeframe changes, TradingView restarts the indicator on that chart,
// so the auto target and all calculations switch to the new timeframe.
int chartSeconds = timeframe.in_seconds()
string autoDevWindow = switch
    chartSeconds <= timeframe.in_seconds("60") => "1D"
    chartSeconds <= timeframe.in_seconds("240") => "3D"
    chartSeconds <= timeframe.in_seconds("720") => "1W"
    chartSeconds <= timeframe.in_seconds("1D") => "1M"
    chartSeconds <= timeframe.in_seconds("1W") => "90D"
    chartSeconds <= timeframe.in_seconds("1M") => "365D"
    => "Off"
 
// AUTO source promotion is explicit and configurable. Only Auto mode may override
// a window's normal Show toggle; Selected and other manual modes never do.
bool allowAutoSource = devMode == "Auto (chart timeframe)" and autoShowRequired
f_autoRequired(string windowName) =>
    allowAutoSource and autoDevWindow == windowName
 
// Decide which RVWAP receives the deviation bands.
f_devEnabled(string windowName) =>
    switch devMode
        "Auto (chart timeframe)" => windowName == autoDevWindow
        "Selected RVWAP" => windowName == selectedDev
        "1D only" => windowName == "1D"
        "1W only" => windowName == "1W"
        "1D + 1W" => windowName == "1D" or windowName == "1W"
        "All visible" => true
        => false
 
bool active_1h = (on_1h or f_autoRequired("1H")) and (not autoHide or chartSeconds < timeframe.in_seconds("60"))
bool dev_1h = active_1h and f_devEnabled("1H")
[rv_1h, sd_1h, n_1h, ne_1h, full_1h] = f_rvwap(MS_HOUR, active_1h, dev_1h)
 
bool active_4h = (on_4h or f_autoRequired("4H")) and (not autoHide or chartSeconds < timeframe.in_seconds("240"))
bool dev_4h = active_4h and f_devEnabled("4H")
[rv_4h, sd_4h, n_4h, ne_4h, full_4h] = f_rvwap(4 * MS_HOUR, active_4h, dev_4h)
 
bool active_1d = (on_1d or f_autoRequired("1D")) and (not autoHide or chartSeconds < timeframe.in_seconds("1D"))
bool dev_1d = active_1d and f_devEnabled("1D")
[rv_1d, sd_1d, n_1d, ne_1d, full_1d] = f_rvwap(MS_DAY, active_1d, dev_1d)
 
bool active_3d = (on_3d or f_autoRequired("3D")) and (not autoHide or chartSeconds < timeframe.in_seconds("3D"))
bool dev_3d = active_3d and f_devEnabled("3D")
[rv_3d, sd_3d, n_3d, ne_3d, full_3d] = f_rvwap(3 * MS_DAY, active_3d, dev_3d)
 
bool active_1w = (on_1w or f_autoRequired("1W")) and (not autoHide or chartSeconds < timeframe.in_seconds("1W"))
bool dev_1w = active_1w and f_devEnabled("1W")
[rv_1w, sd_1w, n_1w, ne_1w, full_1w] = f_rvwap(MS_WEEK, active_1w, dev_1w)
 
bool active_1m = (on_1m or f_autoRequired("1M")) and (not autoHide or chartSeconds < timeframe.in_seconds("1M"))
bool dev_1m = active_1m and f_devEnabled("1M")
[rv_1m, sd_1m, n_1m, ne_1m, full_1m] = f_rvwap(MS_MONTH, active_1m, dev_1m)
 
// 90D is intentionally optional in manual mode, promoted automatically on 2D–1W charts.
bool active_90d = (on_90d or f_autoRequired("90D")) and (not autoHide or chartSeconds < int(MS_90D / 1000))
bool dev_90d = active_90d and f_devEnabled("90D")
[rv_90d, sd_90d, n_90d, ne_90d, full_90d] = f_rvwap(MS_90D, active_90d, dev_90d)
 
bool active_365d = (on_365d or f_autoRequired("365D")) and (not autoHide or chartSeconds < int(MS_365D / 1000))
bool dev_365d = active_365d and f_devEnabled("365D")
[rv_365d, sd_365d, n_365d, ne_365d, full_365d] = f_rvwap(MS_365D, active_365d, dev_365d)
 
// ── PLOTS
// 8 centerlines + (8 × 6) band lines = 56 plot() calls; linebr prevents
// misleading bridging through unavailable (na) values.
// Color of centerline is input-qualified, bands use const-qualified colors.
// Plot-count classification follows TradingView's documented rules;
// final compilation/runtime plot count must still be checked in Pine Editor.
// Labels and the diagnostic table consume no plot counts.
// Deviation bands are intentionally faint; there are no filled clouds.
// Main line colors are set in Inputs; band styling remains in Style.
 
// 1H
plot(rv_1h, "1H RVWAP", color = col_1h, linewidth = width_1h, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_1h and globalS1 and b1_1h ? rv_1h + sd_1h * mult1 : na, "1H +L1", color = color.new(bandCol_1h, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1h and globalS1 and b1_1h ? rv_1h - sd_1h * mult1 : na, "1H -L1", color = color.new(bandCol_1h, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1h and globalS2 and b2_1h ? rv_1h + sd_1h * mult2 : na, "1H +L2", color = color.new(bandCol_1h, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1h and globalS2 and b2_1h ? rv_1h - sd_1h * mult2 : na, "1H -L2", color = color.new(bandCol_1h, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1h and globalS3 and b3_1h ? rv_1h + sd_1h * mult3 : na, "1H +L3", color = color.new(bandCol_1h, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1h and globalS3 and b3_1h ? rv_1h - sd_1h * mult3 : na, "1H -L3", color = color.new(bandCol_1h, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 4H
plot(rv_4h, "4H RVWAP", color = col_4h, linewidth = width_4h, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_4h and globalS1 and b1_4h ? rv_4h + sd_4h * mult1 : na, "4H +L1", color = color.new(bandCol_4h, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_4h and globalS1 and b1_4h ? rv_4h - sd_4h * mult1 : na, "4H -L1", color = color.new(bandCol_4h, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_4h and globalS2 and b2_4h ? rv_4h + sd_4h * mult2 : na, "4H +L2", color = color.new(bandCol_4h, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_4h and globalS2 and b2_4h ? rv_4h - sd_4h * mult2 : na, "4H -L2", color = color.new(bandCol_4h, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_4h and globalS3 and b3_4h ? rv_4h + sd_4h * mult3 : na, "4H +L3", color = color.new(bandCol_4h, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_4h and globalS3 and b3_4h ? rv_4h - sd_4h * mult3 : na, "4H -L3", color = color.new(bandCol_4h, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 1D
plot(rv_1d, "1D RVWAP", color = col_1d, linewidth = width_1d, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_1d and globalS1 and b1_1d ? rv_1d + sd_1d * mult1 : na, "1D +L1", color = color.new(bandCol_1d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1d and globalS1 and b1_1d ? rv_1d - sd_1d * mult1 : na, "1D -L1", color = color.new(bandCol_1d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1d and globalS2 and b2_1d ? rv_1d + sd_1d * mult2 : na, "1D +L2", color = color.new(bandCol_1d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1d and globalS2 and b2_1d ? rv_1d - sd_1d * mult2 : na, "1D -L2", color = color.new(bandCol_1d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1d and globalS3 and b3_1d ? rv_1d + sd_1d * mult3 : na, "1D +L3", color = color.new(bandCol_1d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1d and globalS3 and b3_1d ? rv_1d - sd_1d * mult3 : na, "1D -L3", color = color.new(bandCol_1d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 3D
plot(rv_3d, "3D RVWAP", color = col_3d, linewidth = width_3d, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_3d and globalS1 and b1_3d ? rv_3d + sd_3d * mult1 : na, "3D +L1", color = color.new(bandCol_3d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_3d and globalS1 and b1_3d ? rv_3d - sd_3d * mult1 : na, "3D -L1", color = color.new(bandCol_3d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_3d and globalS2 and b2_3d ? rv_3d + sd_3d * mult2 : na, "3D +L2", color = color.new(bandCol_3d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_3d and globalS2 and b2_3d ? rv_3d - sd_3d * mult2 : na, "3D -L2", color = color.new(bandCol_3d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_3d and globalS3 and b3_3d ? rv_3d + sd_3d * mult3 : na, "3D +L3", color = color.new(bandCol_3d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_3d and globalS3 and b3_3d ? rv_3d - sd_3d * mult3 : na, "3D -L3", color = color.new(bandCol_3d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 1W
plot(rv_1w, "1W RVWAP", color = col_1w, linewidth = width_1w, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_1w and globalS1 and b1_1w ? rv_1w + sd_1w * mult1 : na, "1W +L1", color = color.new(bandCol_1w, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1w and globalS1 and b1_1w ? rv_1w - sd_1w * mult1 : na, "1W -L1", color = color.new(bandCol_1w, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1w and globalS2 and b2_1w ? rv_1w + sd_1w * mult2 : na, "1W +L2", color = color.new(bandCol_1w, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1w and globalS2 and b2_1w ? rv_1w - sd_1w * mult2 : na, "1W -L2", color = color.new(bandCol_1w, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1w and globalS3 and b3_1w ? rv_1w + sd_1w * mult3 : na, "1W +L3", color = color.new(bandCol_1w, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1w and globalS3 and b3_1w ? rv_1w - sd_1w * mult3 : na, "1W -L3", color = color.new(bandCol_1w, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 1M
plot(rv_1m, "1M RVWAP", color = col_1m, linewidth = width_1m, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_1m and globalS1 and b1_1m ? rv_1m + sd_1m * mult1 : na, "1M +L1", color = color.new(bandCol_1m, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1m and globalS1 and b1_1m ? rv_1m - sd_1m * mult1 : na, "1M -L1", color = color.new(bandCol_1m, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1m and globalS2 and b2_1m ? rv_1m + sd_1m * mult2 : na, "1M +L2", color = color.new(bandCol_1m, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1m and globalS2 and b2_1m ? rv_1m - sd_1m * mult2 : na, "1M -L2", color = color.new(bandCol_1m, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_1m and globalS3 and b3_1m ? rv_1m + sd_1m * mult3 : na, "1M +L3", color = color.new(bandCol_1m, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_1m and globalS3 and b3_1m ? rv_1m - sd_1m * mult3 : na, "1M -L3", color = color.new(bandCol_1m, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 90D
plot(rv_90d, "90D RVWAP", color = col_90d, linewidth = width_90d, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_90d and globalS1 and b1_90d ? rv_90d + sd_90d * mult1 : na, "90D +L1", color = color.new(bandCol_90d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_90d and globalS1 and b1_90d ? rv_90d - sd_90d * mult1 : na, "90D -L1", color = color.new(bandCol_90d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_90d and globalS2 and b2_90d ? rv_90d + sd_90d * mult2 : na, "90D +L2", color = color.new(bandCol_90d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_90d and globalS2 and b2_90d ? rv_90d - sd_90d * mult2 : na, "90D -L2", color = color.new(bandCol_90d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_90d and globalS3 and b3_90d ? rv_90d + sd_90d * mult3 : na, "90D +L3", color = color.new(bandCol_90d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_90d and globalS3 and b3_90d ? rv_90d - sd_90d * mult3 : na, "90D -L3", color = color.new(bandCol_90d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// 365D
plot(rv_365d, "1Y (365D) RVWAP", color = col_365d, linewidth = width_365d, style = plot.style_linebr, linestyle = plot.linestyle_solid)
plot(dev_365d and globalS1 and b1_365d ? rv_365d + sd_365d * mult1 : na, "365D +L1", color = color.new(bandCol_365d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_365d and globalS1 and b1_365d ? rv_365d - sd_365d * mult1 : na, "365D -L1", color = color.new(bandCol_365d, 73), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_365d and globalS2 and b2_365d ? rv_365d + sd_365d * mult2 : na, "365D +L2", color = color.new(bandCol_365d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_365d and globalS2 and b2_365d ? rv_365d - sd_365d * mult2 : na, "365D -L2", color = color.new(bandCol_365d, 64), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dashed)
plot(dev_365d and globalS3 and b3_365d ? rv_365d + sd_365d * mult3 : na, "365D +L3", color = color.new(bandCol_365d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
plot(dev_365d and globalS3 and b3_365d ? rv_365d - sd_365d * mult3 : na, "365D -L3", color = color.new(bandCol_365d, 81), linewidth = 1, style = plot.style_linebr, linestyle = plot.linestyle_dotted)
 
// ── FOURTH DEVIATION RENDERER (PLOT-BUDGET SAFE)
// Each of the eight written calls below maintains independent persistent point arrays.
// L4 is a *bounded recent-history overlay* (not a full-history plot). This is an
// explicit tradeoff: plotting all 8 × 4 × 2 bands plus 8 centerlines would exceed
// Pine's 64 plot-count limit. The existing L1/L2/L3 remain native full-history plots.
//
// Only append valid points; reset on an invalid point to prevent bridging a gap.
// Keep ≤ l4History points per side and rebuild at the last bar. Drawing objects
// do not consume plot counts; the overlay uses at most 16 simultaneous polylines.
// Using var, not varip, respects Pine's normal realtime rollback behavior.
f_drawL4(bool enabled, float basis, float sigma, float k, color sourceColor, int maxPts) =>
    var array<chart.point> hiPoints = array.new<chart.point>()
    var array<chart.point> loPoints = array.new<chart.point>()
    var polyline hiDrawing = na
    var polyline loDrawing = na
    bool pointValid = enabled and not na(basis) and not na(sigma)
    if enabled
        if pointValid
            array.push(hiPoints, chart.point.from_index(bar_index, basis + k * sigma))
            array.push(loPoints, chart.point.from_index(bar_index, basis - k * sigma))
            if array.size(hiPoints) > maxPts
                array.shift(hiPoints)
                array.shift(loPoints)
        else
            // A missing observation starts a new continuous segment.
            array.clear(hiPoints)
            array.clear(loPoints)
    if barstate.islast
        if not na(hiDrawing)
            polyline.delete(hiDrawing)
            hiDrawing := na
        if not na(loDrawing)
            polyline.delete(loDrawing)
            loDrawing := na
        if enabled and array.size(hiPoints) >= 2
            color faint = color.new(sourceColor, 62)
            hiDrawing := polyline.new(hiPoints, curved = false, closed = false,
                 xloc = xloc.bar_index, line_color = faint,
                 line_style = line.style_dotted, line_width = 1)
            loDrawing := polyline.new(loPoints, curved = false, closed = false,
                 xloc = xloc.bar_index, line_color = faint,
                 line_style = line.style_dotted, line_width = 1)
    pointValid and array.size(hiPoints) >= 2
 
// Global L4 point budget: at most 8,000 upper and 8,000 lower points.
// In all-visible mode the per-source history limit is reduced automatically.
int activeL4Sources = (dev_1h and globalS4 and b4_1h ? 1 : 0) + (dev_4h and globalS4 and b4_4h ? 1 : 0) + (dev_1d and globalS4 and b4_1d ? 1 : 0) + (dev_3d and globalS4 and b4_3d ? 1 : 0) + (dev_1w and globalS4 and b4_1w ? 1 : 0) + (dev_1m and globalS4 and b4_1m ? 1 : 0) + (dev_90d and globalS4 and b4_90d ? 1 : 0) + (dev_365d and globalS4 and b4_365d ? 1 : 0)
int effectiveL4History = math.min(l4History, int(8000 / math.max(1, activeL4Sources)))
 
// L4 controls are independent from L1/L2/L3. These function calls execute on
// *every bar*, even when disabled, to maintain stable per-call history/state.
bool drawnL4_1h = f_drawL4(dev_1h and globalS4 and b4_1h, rv_1h, sd_1h, mult4, col_1h, effectiveL4History)
bool drawnL4_4h = f_drawL4(dev_4h and globalS4 and b4_4h, rv_4h, sd_4h, mult4, col_4h, effectiveL4History)
bool drawnL4_1d = f_drawL4(dev_1d and globalS4 and b4_1d, rv_1d, sd_1d, mult4, col_1d, effectiveL4History)
bool drawnL4_3d = f_drawL4(dev_3d and globalS4 and b4_3d, rv_3d, sd_3d, mult4, col_3d, effectiveL4History)
bool drawnL4_1w = f_drawL4(dev_1w and globalS4 and b4_1w, rv_1w, sd_1w, mult4, col_1w, effectiveL4History)
bool drawnL4_1m = f_drawL4(dev_1m and globalS4 and b4_1m, rv_1m, sd_1m, mult4, col_1m, effectiveL4History)
bool drawnL4_90d = f_drawL4(dev_90d and globalS4 and b4_90d, rv_90d, sd_90d, mult4, col_90d, effectiveL4History)
bool drawnL4_365d = f_drawL4(dev_365d and globalS4 and b4_365d, rv_365d, sd_365d, mult4, col_365d, effectiveL4History)
 
// Flag active sources only when at least one actual upper/lower band is displayed.
bool actualBand_1h = dev_1h and not na(sd_1h) and ((globalS1 and b1_1h) or (globalS2 and b2_1h) or (globalS3 and b3_1h) or drawnL4_1h)
bool actualBand_4h = dev_4h and not na(sd_4h) and ((globalS1 and b1_4h) or (globalS2 and b2_4h) or (globalS3 and b3_4h) or drawnL4_4h)
bool actualBand_1d = dev_1d and not na(sd_1d) and ((globalS1 and b1_1d) or (globalS2 and b2_1d) or (globalS3 and b3_1d) or drawnL4_1d)
bool actualBand_3d = dev_3d and not na(sd_3d) and ((globalS1 and b1_3d) or (globalS2 and b2_3d) or (globalS3 and b3_3d) or drawnL4_3d)
bool actualBand_1w = dev_1w and not na(sd_1w) and ((globalS1 and b1_1w) or (globalS2 and b2_1w) or (globalS3 and b3_1w) or drawnL4_1w)
bool actualBand_1m = dev_1m and not na(sd_1m) and ((globalS1 and b1_1m) or (globalS2 and b2_1m) or (globalS3 and b3_1m) or drawnL4_1m)
bool actualBand_90d = dev_90d and not na(sd_90d) and ((globalS1 and b1_90d) or (globalS2 and b2_90d) or (globalS3 and b3_90d) or drawnL4_90d)
bool actualBand_365d = dev_365d and not na(sd_365d) and ((globalS1 and b1_365d) or (globalS2 and b2_365d) or (globalS3 and b3_365d) or drawnL4_365d)
 
// Track all *actually visible* deviation sources. Useful when the user chooses
// "1D + 1W" or "All visible" instead of a single selected window.
int actualBandCount = (actualBand_1h ? 1 : 0) + (actualBand_4h ? 1 : 0) + (actualBand_1d ? 1 : 0) + (actualBand_3d ? 1 : 0) + (actualBand_1w ? 1 : 0) + (actualBand_1m ? 1 : 0) + (actualBand_90d ? 1 : 0) + (actualBand_365d ? 1 : 0)
string actualBandSources = (actualBand_1h ? "1H " : "") + (actualBand_4h ? "4H " : "") + (actualBand_1d ? "1D " : "") + (actualBand_3d ? "3D " : "") + (actualBand_1w ? "1W " : "") + (actualBand_1m ? "1M " : "") + (actualBand_90d ? "90D " : "") + (actualBand_365d ? "365D" : "")
 
// ── PERSISTENT RIGHT-EDGE TAGS
// Only one label per window, updated on the last bar; no label accumulation.
f_updateTag(label current, bool enabled, float price, string caption, color txtColor) =>
    label tag = current
    if barstate.islast
        if enabled and not na(price)
            string txt = tagShowPrice ? caption + "  " + str.tostring(price, format.mintick) : caption
            color bg = color.new(chart.bg_color, tagBgTransparency)
            if na(tag)
                tag := label.new(bar_index + tagOffset, price, txt,
                     xloc = xloc.bar_index, yloc = yloc.price,
                     style = label.style_label_left, color = bg,
                     textcolor = txtColor, size = tagSize)
            else
                label.set_xy(tag, bar_index + tagOffset, price)
                label.set_text(tag, txt)
                label.set_textcolor(tag, txtColor)
                label.set_color(tag, bg)
                label.set_size(tag, tagSize)
        else
            if not na(tag)
                label.delete(tag)
                tag := na
    tag
 
var label lbl_1h = na
var label lbl_4h = na
var label lbl_1d = na
var label lbl_3d = na
var label lbl_1w = na
var label lbl_1m = na
var label lbl_90d = na
var label lbl_365d = na
lbl_1h := f_updateTag(lbl_1h, showTags and tag_1h and active_1h, rv_1h, "1H" + (markActiveTags and actualBand_1h ? " σ" : ""), col_1h)
lbl_4h := f_updateTag(lbl_4h, showTags and tag_4h and active_4h, rv_4h, "4H" + (markActiveTags and actualBand_4h ? " σ" : ""), col_4h)
lbl_1d := f_updateTag(lbl_1d, showTags and tag_1d and active_1d, rv_1d, "1D" + (markActiveTags and actualBand_1d ? " σ" : ""), col_1d)
lbl_3d := f_updateTag(lbl_3d, showTags and tag_3d and active_3d, rv_3d, "3D" + (markActiveTags and actualBand_3d ? " σ" : ""), col_3d)
lbl_1w := f_updateTag(lbl_1w, showTags and tag_1w and active_1w, rv_1w, "1W" + (markActiveTags and actualBand_1w ? " σ" : ""), col_1w)
lbl_1m := f_updateTag(lbl_1m, showTags and tag_1m and active_1m, rv_1m, "1M" + (markActiveTags and actualBand_1m ? " σ" : ""), col_1m)
lbl_90d := f_updateTag(lbl_90d, showTags and tag_90d and active_90d, rv_90d, "90D" + (markActiveTags and actualBand_90d ? " σ" : ""), col_90d)
lbl_365d := f_updateTag(lbl_365d, showTags and tag_365d and active_365d, rv_365d, "1Y" + (markActiveTags and actualBand_365d ? " σ" : ""), col_365d)
 
// ── OPTIONAL DIAGNOSTIC — FOR AUDITING BAND ELIGIBILITY
// Exposes source and data-quality information without creating more plots.
string diagnosticTarget = switch devMode
    "Auto (chart timeframe)" => autoDevWindow
    "Selected RVWAP" => selectedDev
    "1D only" => "1D"
    "1W only" => "1W"
    "Off" => "Off"
    => "Multiple"
 
bool diagnosticEnabled = switch diagnosticTarget
    "1H" => active_1h
    "4H" => active_4h
    "1D" => active_1d
    "3D" => active_3d
    "1W" => active_1w
    "1M" => active_1m
    "90D" => active_90d
    "365D" => active_365d
    => false
 
float diagnosticRVWAP = switch diagnosticTarget
    "1H" => rv_1h
    "4H" => rv_4h
    "1D" => rv_1d
    "3D" => rv_3d
    "1W" => rv_1w
    "1M" => rv_1m
    "90D" => rv_90d
    "365D" => rv_365d
    => na
 
float diagnosticSigma = switch diagnosticTarget
    "1H" => sd_1h
    "4H" => sd_4h
    "1D" => sd_1d
    "3D" => sd_3d
    "1W" => sd_1w
    "1M" => sd_1m
    "90D" => sd_90d
    "365D" => sd_365d
    => na
 
bool diagnosticFull = switch diagnosticTarget
    "1H" => full_1h
    "4H" => full_4h
    "1D" => full_1d
    "3D" => full_3d
    "1W" => full_1w
    "1M" => full_1m
    "90D" => full_90d
    "365D" => full_365d
    => false
 
float diagnosticCount = switch diagnosticTarget
    "1H" => n_1h
    "4H" => n_4h
    "1D" => n_1d
    "3D" => n_3d
    "1W" => n_1w
    "1M" => n_1m
    "90D" => n_90d
    "365D" => n_365d
    => na
 
float diagnosticEffectiveN = switch diagnosticTarget
    "1H" => ne_1h
    "4H" => ne_4h
    "1D" => ne_1d
    "3D" => ne_3d
    "1W" => ne_1w
    "1M" => ne_1m
    "90D" => ne_90d
    "365D" => ne_365d
    => na
 
// Status reflects both global and per-window level switches.
bool diagnosticL1 = switch diagnosticTarget
    "1H" => globalS1 and b1_1h
    "4H" => globalS1 and b1_4h
    "1D" => globalS1 and b1_1d
    "3D" => globalS1 and b1_3d
    "1W" => globalS1 and b1_1w
    "1M" => globalS1 and b1_1m
    "90D" => globalS1 and b1_90d
    "365D" => globalS1 and b1_365d
    => false
bool diagnosticL2 = switch diagnosticTarget
    "1H" => globalS2 and b2_1h
    "4H" => globalS2 and b2_4h
    "1D" => globalS2 and b2_1d
    "3D" => globalS2 and b2_3d
    "1W" => globalS2 and b2_1w
    "1M" => globalS2 and b2_1m
    "90D" => globalS2 and b2_90d
    "365D" => globalS2 and b2_365d
    => false
bool diagnosticL3 = switch diagnosticTarget
    "1H" => globalS3 and b3_1h
    "4H" => globalS3 and b3_4h
    "1D" => globalS3 and b3_1d
    "3D" => globalS3 and b3_3d
    "1W" => globalS3 and b3_1w
    "1M" => globalS3 and b3_1m
    "90D" => globalS3 and b3_90d
    "365D" => globalS3 and b3_365d
    => false
bool diagnosticL4 = switch diagnosticTarget
    "1H" => globalS4 and b4_1h
    "4H" => globalS4 and b4_4h
    "1D" => globalS4 and b4_1d
    "3D" => globalS4 and b4_3d
    "1W" => globalS4 and b4_1w
    "1M" => globalS4 and b4_1m
    "90D" => globalS4 and b4_90d
    "365D" => globalS4 and b4_365d
    => false
bool diagnosticL4Drawn = switch diagnosticTarget
    "1H" => drawnL4_1h
    "4H" => drawnL4_4h
    "1D" => drawnL4_1d
    "3D" => drawnL4_3d
    "1W" => drawnL4_1w
    "1M" => drawnL4_1m
    "90D" => drawnL4_90d
    "365D" => drawnL4_365d
    => false
bool anySelectedLevel = diagnosticL1 or diagnosticL2 or diagnosticL3 or diagnosticL4
string diagnosticState = diagnosticTarget == "Off" ? "OFF" : diagnosticTarget == "Multiple" ? "MULTI" : not diagnosticEnabled ? "SOURCE HIDDEN" : not anySelectedLevel ? "LEVELS OFF" : not diagnosticFull ? "WAIT HISTORY" : na(diagnosticRVWAP) ? "NO VOLUME" : na(diagnosticSigma) ? "LOW SAMPLE" : diagnosticL4 and not (diagnosticL1 or diagnosticL2 or diagnosticL3) and not diagnosticL4Drawn ? "L4 BUILDING" : "READY"
string enabledLevels = (diagnosticL1 ? "±" + str.tostring(mult1, "#.##") + "σ  " : "") + (diagnosticL2 ? "±" + str.tostring(mult2, "#.##") + "σ  " : "") + (diagnosticL3 ? "±" + str.tostring(mult3, "#.##") + "σ  " : "") + (diagnosticL4 ? "±" + str.tostring(mult4, "#.##") + "σ" : "")
string modeCaption = devMode == "Auto (chart timeframe)" ? "AUTO" : devMode == "Selected RVWAP" ? "SELECTED" : "MANUAL"
string chartTFText = timeframe.isintraday ? (chartSeconds >= 3600 and chartSeconds % 3600 == 0 ? str.tostring(int(chartSeconds / 3600)) + "H" : chartSeconds >= 60 and chartSeconds % 60 == 0 ? str.tostring(int(chartSeconds / 60)) + "m" : timeframe.period) : timeframe.period
string diagnosticHeader = "STD  " + modeCaption + "  " + chartTFText + " → " + diagnosticTarget
string diagnosticDetail = (diagnosticTarget == "Multiple" ? (actualBandCount > 0 ? str.tostring(actualBandCount) + " ACTIVE: " + actualBandSources : "NO ACTIVE BANDS") : diagnosticState + (diagnosticState == "READY" ? "  " + enabledLevels : "") + (diagnosticTarget != "Off" ? " | N=" + (na(diagnosticCount) ? "—" : str.tostring(diagnosticCount, "#")) + " / Eff=" + (na(diagnosticEffectiveN) ? "—" : str.tostring(diagnosticEffectiveN, "#.0")) : "")) + (globalS4 and devMode != "Off" ? " | 4σ last " + str.tostring(l4History) + " bars" : "")
var table devTable = table.new(statusPosition, 1, 2)
if barstate.islast
    color panelBg = showDevStatus ? color.new(chart.bg_color, 12) : color.new(chart.bg_color, 100)
    color panelText = showDevStatus ? chart.fg_color : color.new(chart.fg_color, 100)
    table.cell(devTable, 0, 0, showDevStatus ? diagnosticHeader : "", text_color = panelText, bgcolor = panelBg, text_size = size.tiny)
    table.cell(devTable, 0, 1, showDevStatus ? diagnosticDetail : "", text_color = panelText, bgcolor = panelBg, text_size = size.tiny)

← 返回列表