Opening Range Breakout - ORB Statistics [Dots3Red]

Dots3Red · study · 221 行 · 点赞 580 · TradingView 原页

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

Pine Script

//@version=6
indicator("Opening Range Breakout - ORB Statistics [Dots3Red]",
          shorttitle = "ORB Stats [D3R]",
          overlay    = true,
          max_labels_count = 500)
 
// Inputs
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_GOOD  = color.new(#00c896, 0)
C_BAD   = color.new(#ff4466, 0)
C_AMBER = color.new(#ffb700, 0)
C_UP    = color.new(#00f0ff, 0)
C_DN    = color.new(#ff00aa, 0)
 
GRP_OR  = "⏱️ Opening Range"
GRP_ENG = "🎯 Breakout Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
 
or_min    = input.int(30, "Opening Range (minutes)", minval=1, maxval=240, group=GRP_OR,
            tooltip="The high and low of the first N minutes of each session form the opening range.")
use_sess  = input.bool(false, "Use Custom Session Start", group=GRP_OR,
            tooltip="Off: the range starts at the beginning of the chart's trading day (for futures that is the overnight open, for crypto 00:00 UTC). On: the range starts at the beginning of the session below, in exchange time, e.g. 0930-1600 for the US cash open.")
sess      = input.session("0930-1600", "Session", group=GRP_OR)
tgt_mult  = input.float(1.0, "Target (× Range Size)", minval=0.1, step=0.1, group=GRP_ENG,
            tooltip="Success = price travels this multiple of the opening range beyond the broken side before hitting the stop.")
stop_mode = input.string("Opposite Side", "Stop", options=["Opposite Side", "Midpoint"], group=GRP_ENG,
            tooltip="Where a breakout is considered failed: the other side of the range, or the middle of it.")
min_n     = input.int(10, "Min Breakouts to Show Rates", minval=3, group=GRP_ENG)
 
col_or    = input.color(#ffb700, "Range Lines", group=GRP_VIS)
w_or      = input.int(2, "Range Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_fill = input.bool(true, "Fill Range", group=GRP_VIS)
fill_tr   = input.int(90, "Fill Transparency", minval=50, maxval=100, group=GRP_VIS)
show_tgt  = input.bool(true, "Show Target Levels", group=GRP_VIS)
col_tgt   = input.color(#64748b, "Target Color", group=GRP_VIS)
show_lbl  = input.bool(true, "Breakout Labels", group=GRP_VIS)
col_bull  = input.color(#00f0ff, "Bullish Label Color", group=GRP_VIS)
col_bear  = input.color(#ff00aa, "Bearish Label Color", group=GRP_VIS)
 
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)
 
int  or_ms = or_min * 60000
bool in_sess = not na(time(timeframe.period, sess))
bool chg_d   = timeframe.change("D")
bool nd      = use_sess ? in_sess and not in_sess[1] : chg_d
bool tf_ok   = timeframe.isintraday and timeframe.in_seconds() <= or_min * 60 and (or_min * 60) % timeframe.in_seconds() == 0
 
var int   sess_t     = na
var float or_hi      = na
var float or_lo      = na
var bool  broke      = false
var bool  day_had_or = false
 
var int   p_dir = 0
var int   p_bar = na
var float p_tgt = na
var float p_stp = na
 
var int b_n = 0
var int b_w = 0
var int b_s = 0
var int s_n = 0
var int s_w = 0
var int s_s = 0
var int days_n  = 0
var int days_nb = 0
 
bool new_bull = false
bool new_bear = false
 
if barstate.isconfirmed and nd
    if p_dir != 0
        if p_dir > 0
            b_n += 1
        else
            s_n += 1
        p_dir := 0
    if day_had_or and tf_ok
        days_n += 1
        if not broke
            days_nb += 1
 
if nd
    sess_t     := time
    or_hi      := high
    or_lo      := low
    broke      := false
    day_had_or := false
else if not na(sess_t) and time - sess_t < or_ms
    or_hi := math.max(or_hi, high)
    or_lo := math.min(or_lo, low)
 
bool  in_win  = not na(sess_t) and time - sess_t < or_ms
bool  or_done = not na(sess_t) and not in_win
float or_rng  = or_hi - or_lo
float or_mid  = (or_hi + or_lo) / 2
 
if or_done
    day_had_or := true
 
if barstate.isconfirmed and not nd and p_dir != 0 and bar_index > p_bar
    bool hit_t = p_dir > 0 ? high >= p_tgt : low <= p_tgt
    bool hit_s = p_dir > 0 ? low <= p_stp  : high >= p_stp
    if hit_t or hit_s
        if p_dir > 0
            b_n += 1
            if hit_t and not hit_s
                b_w += 1
            else
                b_s += 1
        else
            s_n += 1
            if hit_t and not hit_s
                s_w += 1
            else
                s_s += 1
        p_dir := 0
 
if barstate.isconfirmed and tf_ok and or_done and not broke and or_rng > 0 and (not use_sess or in_sess)
    if close > or_hi
        broke    := true
        new_bull := true
        p_dir    := 1
        p_bar    := bar_index
        p_tgt    := or_hi + tgt_mult * or_rng
        p_stp    := stop_mode == "Midpoint" ? or_mid : or_lo
    else if close < or_lo
        broke    := true
        new_bear := true
        p_dir    := -1
        p_bar    := bar_index
        p_tgt    := or_lo - tgt_mult * or_rng
        p_stp    := stop_mode == "Midpoint" ? or_mid : or_hi
 
f_pct(int part, int total) =>
    total >= min_n ? str.tostring(math.round(part / float(total) * 100.0)) + "%" : "…"
 
// 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), 4, 7,
                          bgcolor=C_BG, border_color=C_BORD,
                          border_width=1, frame_color=C_BORD, frame_width=2)
 
if show_hud and barstate.islast
    color bgA = color.new(#1e293b, 40)
    color bgB = color.new(#0f172a, 40)
 
    table.cell(hud, 0, 0, "ORB STATS [D3R] · " + str.tostring(or_min) + "m", text_color=C_TXT,
               bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 0, 0, 3, 0)
 
    table.cell(hud, 0, 1, "Direction",  text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 1, "Breakouts",  text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, 1, "Hit Target", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of first breakouts that reached " + str.tostring(tgt_mult) + "× the range size beyond the broken side before the stop. Breakouts still open at the session end count in neither column.")
    table.cell(hud, 3, 1, "Stopped",    text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of first breakouts that hit the stop (" + stop_mode + ") before the target.")
 
    table.cell(hud, 0, 2, "▲ Bullish", text_color=C_UP, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 2, str.tostring(b_n), text_color=C_TXT, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, 2, f_pct(b_w, b_n), text_color = b_n >= min_n ? C_GOOD : C_DIM, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 3, 2, f_pct(b_s, b_n), text_color = b_n >= min_n ? C_BAD : C_DIM, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
 
    table.cell(hud, 0, 3, "▼ Bearish", text_color=C_DN, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 3, str.tostring(s_n), text_color=C_TXT, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, 3, f_pct(s_w, s_n), text_color = s_n >= min_n ? C_GOOD : C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 3, 3, f_pct(s_s, s_n), text_color = s_n >= min_n ? C_BAD : C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
 
    table.cell(hud, 0, 4, "No-Breakout Days", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 4, days_n >= min_n ? str.tostring(math.round(days_nb / float(days_n) * 100.0)) + "% of " + str.tostring(days_n) + " days" : "…",
               text_color=C_TXT, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 4, 3, 4)
 
    string status = in_win ? "Building range" : not or_done ? "—" : not broke ? "Waiting for breakout" : p_dir > 0 ? "Broke ▲ (open)" : p_dir < 0 ? "Broke ▼ (open)" : "Breakout resolved"
    table.cell(hud, 0, 5, "Today", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 5, status + (na(or_rng) ? "" : " · range " + str.tostring(or_rng, format.mintick)),
               text_color=C_AMBER, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 5, 3, 5)
 
    table.cell(hud, 0, 6, tf_ok ? "Target / Stop" : "Chart timeframe too high", text_color = tf_ok ? C_DIM : C_BAD, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 6, tf_ok ? str.tostring(tgt_mult) + "× range / " + stop_mode : "Use a chart that divides " + str.tostring(or_min) + "m evenly",
               text_color = tf_ok ? C_TXT : C_BAD, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 6, 3, 6)
 
// Visual
color c_or  = nd ? na : col_or
color c_tgt = nd ? na : col_tgt
 
p_hi = plot(tf_ok and not na(sess_t) ? or_hi : na, "OR High", color=c_or, linewidth=w_or, style=plot.style_linebr)
p_lo = plot(tf_ok and not na(sess_t) ? or_lo : na, "OR Low",  color=c_or, linewidth=w_or, style=plot.style_linebr)
fill(p_hi, p_lo, color = show_fill and not nd ? color.new(col_or, fill_tr) : na, title="OR Fill")
 
plot(show_tgt and tf_ok and or_done ? or_hi + tgt_mult * or_rng : na, "Bull Target", color=c_tgt, linewidth=1, style=plot.style_cross)
plot(show_tgt and tf_ok and or_done ? or_lo - tgt_mult * or_rng : na, "Bear Target", color=c_tgt, linewidth=1, style=plot.style_cross)
 
f_rate_txt(int w, int n) =>
    n >= min_n ? " " + str.tostring(math.round(w / float(n) * 100.0)) + "%" : ""
 
if show_lbl and new_bull
    label.new(bar_index, low, "ORB ▲" + f_rate_txt(b_w, b_n),
              style=label.style_label_up, color=color.new(#000000, 100), textcolor=col_bull, size=size.small)
 
if show_lbl and new_bear
    label.new(bar_index, high, "ORB ▼" + f_rate_txt(s_w, s_n),
              style=label.style_label_down, color=color.new(#000000, 100), textcolor=col_bear, size=size.small)
 
// Alert
alertcondition(new_bull, "Bullish ORB", "D3R ORB Stats: first close above the opening range on {{ticker}}")
alertcondition(new_bear, "Bearish ORB", "D3R ORB Stats: first close below the opening range on {{ticker}}")

← 返回列表