Statistical Reversion Probability [Dots3Red]

Dots3Red · study · 192 行 · 点赞 306 · 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
// © Dots3Red
// TradingView: https://www.tradingview.com/u/Dots3Red/
 
//@version=6
indicator("Statistical Reversion Probability [Dots3Red]",
          shorttitle = "Reversion Prob [D3R]",
          overlay    = false,
          max_labels_count = 500)
 
// COLORS
C_TXT   = color.new(#f8fafc, 0)
C_DIM    = color.new(#94a3b8, 0)
C_BG     = color.new(#131722, 5)
C_BORD   = color.new(#334155, 0)
C_BULL   = color.new(#00f0ff, 0)
C_BEAR   = color.new(#ff00aa, 0)
C_GOOD   = color.new(#00c896, 0)
C_BAD    = color.new(#ff4466, 0)
 
 
// INPUTS
GRP_STAT = "📐 Statistics"
GRP_ENG  = "📊 Probability Engine"
GRP_VIS  = "🎨 Visualization"
GRP_HUD  = "🖥️ Dashboard"
 
z_len     = input.int(50, "Mean / Stdev Length", minval=10, group=GRP_STAT,
            tooltip="Lookback used to compute the rolling mean and standard deviation that the z-score is measured against.")
max_z     = input.float(3.0, "Max Z-Score (clamp)", minval=1.0, step=0.5, group=GRP_STAT,
            tooltip="Z-score readings beyond this are clamped, so one freak spike doesn't create an empty, useless bucket.")
bucket_w  = input.float(0.5, "Bucket Width", minval=0.1, step=0.1, group=GRP_STAT,
            tooltip="Width of each z-score bucket. Narrower buckets are more precise but take longer to fill with samples.")
 
horizon       = input.int(10, "Outcome Horizon (bars)", minval=1, maxval=200, group=GRP_ENG,
                tooltip="How many bars ahead reversion is checked, and the window each bucket's probability is graded on.")
min_bucket_n  = input.int(20, "Min Samples per Bucket", minval=5, group=GRP_ENG,
                tooltip="A bucket's probability is only shown once it has been observed this many times.")
min_conf      = input.float(60, "Min Confidence to Signal (%)", minval=50, maxval=95, step=1, group=GRP_ENG,
                tooltip="A label only prints when the current bucket's measured reversion rate is at least this lopsided.")
 
z_col        = input.color(#f8fafc, "Z-Score Line Color", group=GRP_VIS)
z_width      = input.int(2, "Z-Score Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_bg      = input.bool(true, "Background Tint by Confidence", group=GRP_VIS)
show_labels  = input.bool(true, "Show Signal Labels (on price chart)", group=GRP_VIS)
lbl_gap      = input.int(8, "Min Bars Between Labels", minval=1, maxval=200, group=GRP_VIS,
               tooltip="After a label prints, no new label (or alert) can fire until this many bars have passed. Stops clusters of labels while z-score stays stretched. Set to 1 to label every qualifying bar.")
show_live    = input.bool(true, "Show Live Label on Forming Bar", group=GRP_VIS,
               tooltip="Shows a provisional label on the current, still-forming bar. It can appear and disappear until the bar closes. Confirmed labels never change.")
 
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos  = input.string("Top Right", "Position",
           options=["Top Right","Top Left","Bottom Right","Bottom Left"], group=GRP_HUD)
 
 
// Z-SCORE 
float basis = ta.sma(close, z_len)
float dev   = ta.stdev(close, z_len)
float z     = dev != 0 ? (close - basis) / dev : 0.0
float z_c   = math.max(-max_z, math.min(max_z, z))
 
 
// BUCKETING 
int n_buckets = int(math.round((2 * max_z) / bucket_w)) + 1
 
f_bucket(float zv) =>
    int idx = int(math.floor((zv + max_z) / bucket_w))
    math.max(0, math.min(n_buckets - 1, idx))
 
var float[] bucket_count = array.new<float>(n_buckets, 0.0)
var float[] bucket_hits  = array.new<float>(n_buckets, 0.0)
 
int cur_bucket = f_bucket(z_c)
 
 
var float total_count = 0.0
var float total_hits  = 0.0
 
if barstate.isconfirmed and bar_index >= horizon and not na(z_c[horizon])
    float past_z    = z_c[horizon]
    int   past_bkt  = f_bucket(past_z)
    float past_px   = close[horizon]
    float change    = close - past_px
    bool  has_dir   = past_z != 0
    bool  reverted  = past_z > 0 ? change < 0 : change > 0
    if has_dir
        array.set(bucket_count, past_bkt, array.get(bucket_count, past_bkt) + 1.0)
        total_count += 1.0
        if reverted
            array.set(bucket_hits, past_bkt, array.get(bucket_hits, past_bkt) + 1.0)
            total_hits += 1.0
 
 
// CURRENT SIGNAL 
float cur_n    = array.get(bucket_count, cur_bucket)
float cur_hits = array.get(bucket_hits, cur_bucket)
bool  enough_n = cur_n >= min_bucket_n
float cur_prob = enough_n ? cur_hits / cur_n * 100.0 : na
 
// direction implied by current z: positive z = stretched up = expect reversion DOWN; negative z = expect UP
int   dir      = z_c > 0 ? -1 : z_c < 0 ? 1 : 0
bool  signal   = enough_n and dir != 0 and cur_prob >= min_conf
 
color sig_col  = dir > 0 ? C_BULL : C_BEAR
 
 
// VISUALS 
plot(z_c, "Z-Score", color=z_col, linewidth=z_width)
hline(0, "Mean", color=color.new(C_DIM, 60), linestyle=hline.style_dashed)
hline(max_z,  "Upper Clamp", color=color.new(C_BEAR, 75), linestyle=hline.style_dotted)
hline(-max_z, "Lower Clamp", color=color.new(C_BULL, 75), linestyle=hline.style_dotted)
 
bgcolor(show_bg and signal ? color.new(sig_col, 82) : na)
 
var label live_lbl = na
if not na(live_lbl)
    label.delete(live_lbl)
    live_lbl := na
 
f_lbl_txt(int d, float p) =>
    (d > 0 ? "▲ " : "▼ ") + str.tostring(math.round(p)) + "%"
 
 
var int last_lbl_bar = na
bool gap_ok = na(last_lbl_bar) or (bar_index - last_lbl_bar >= lbl_gap)
bool fire   = signal and gap_ok
 
if show_labels and fire
    float ly = dir > 0 ? low : high
    lst = dir > 0 ? label.style_label_up : label.style_label_down
    if barstate.isconfirmed
        label.new(bar_index, ly, f_lbl_txt(dir, cur_prob),
                  style = lst, color = color.new(#000000, 100), textcolor = sig_col,
                  size = size.small, force_overlay = true)
    else if show_live and barstate.islast
        live_lbl := label.new(bar_index, ly, f_lbl_txt(dir, cur_prob) + " (live)",
                  style = lst, color = color.new(#000000, 100), textcolor = color.new(sig_col, 35),
                  size = size.small, force_overlay = true)
 
 
if fire and barstate.isconfirmed
    last_lbl_bar := bar_index
 
 
// DASHBOARD
f_hud_pos(string s) =>
    switch s
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        =>                position.bottom_left
 
var table hud = table.new(f_hud_pos(hud_pos), 2, 6,
                           bgcolor=C_BG, border_color=C_BORD,
                           border_width=1, frame_color=C_BORD, frame_width=2)
 
if show_hud and barstate.islast
    table.cell(hud, 0, 0, "REVERSION PROB [D3R]", text_color=C_TXT,
               bgcolor=color.new(#1e293b, 0), text_size=11, text_halign=text.align_center)
    table.merge_cells(hud, 0, 0, 1, 0)
 
    table.cell(hud, 0, 1, "Z-Score", text_color=C_DIM, bgcolor=color.new(#1e293b, 40), text_size=11)
    table.cell(hud, 1, 1, str.tostring(z, "#.##"),
               text_color = z > 0 ? C_BEAR : z < 0 ? C_BULL : C_TXT,
               bgcolor=color.new(#1e293b, 40), text_size=11, text_halign=text.align_center)
 
    table.cell(hud, 0, 2, "Bucket Samples", text_color=C_DIM, bgcolor=color.new(#0f172a, 40), text_size=11)
    table.cell(hud, 1, 2, str.tostring(int(cur_n)) + (enough_n ? "" : " (min " + str.tostring(min_bucket_n) + ")"),
               text_color=C_TXT, bgcolor=color.new(#0f172a, 40), text_size=11, text_halign=text.align_center)
 
    string dir_txt = dir > 0 ? "Reverts Up" : dir < 0 ? "Reverts Down" : "—"
    table.cell(hud, 0, 3, "Reversion Prob", text_color=C_DIM, bgcolor=color.new(#1e293b, 40), text_size=11,
               tooltip="Measured rate, over this bucket's own history, that price moved the expected direction within " + str.tostring(horizon) + " bars.")
    table.cell(hud, 1, 3, not enough_n ? "Collecting…" : dir_txt + "  " + str.tostring(math.round(cur_prob)) + "%",
               text_color = not enough_n ? C_DIM : (cur_prob >= min_conf ? C_GOOD : C_DIM),
               bgcolor=color.new(#1e293b, 40), text_size=11, text_halign=text.align_center)
 
    float overall = total_count > 0 ? total_hits / total_count * 100.0 : na
    table.cell(hud, 0, 4, "Overall Accuracy", text_color=C_DIM, bgcolor=color.new(#0f172a, 40), text_size=11,
               tooltip="Across every bucket and every graded prediction on this chart.")
    table.cell(hud, 1, 4, na(overall) ? "—" : str.tostring(math.round(overall)) + "% (n=" + str.tostring(int(total_count)) + ")",
               text_color=na(overall) ? C_DIM : overall >= 50 ? C_GOOD : C_BAD,
               bgcolor=color.new(#0f172a, 40), text_size=11, text_halign=text.align_center)
 
    table.cell(hud, 0, 5, "Horizon", text_color=C_DIM, bgcolor=color.new(#1e293b, 40), text_size=11)
    table.cell(hud, 1, 5, str.tostring(horizon) + " bars",
               text_color=C_TXT, bgcolor=color.new(#1e293b, 40), text_size=11, text_halign=text.align_center)
 
 
// ALERTS
alertcondition(fire and dir > 0, "Reversion Probability — Bullish", "D3R Reversion Prob: high-confidence upward reversion signal")
alertcondition(fire and dir < 0, "Reversion Probability — Bearish", "D3R Reversion Prob: high-confidence downward reversion signal")

← 返回列表