Gap & 5m ORB Scorecard

enjoyelan · study · 229 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
// Gap & 5-min ORB Scorecard — TradingView port of the SPY 0DTE dashboard strip (#150).
//
// Marks, per regular session (09:30–16:00 ET):
//   • GAP        today's open vs the prior regular-session close (premarket price before 09:30)
//   • 5m OR      the 09:30–09:35 high/low, boxed for the rest of the session
//   • 1st 5m     the first 5-minute candle that CLOSES above/below the range (the direction marker)
//   • GAP FILL   the prior close as a magenta line + unfilled zone; time it was first touched,
//                and how far price ran past it
// History lines are the 251-session SPY 5-min study (Oct 2025–Oct 2026) from the dashboard.
//
// Use a 1-minute or 5-minute chart (the 5m candle closes have to line up with chart bars).
// Pure price data — no server, works on TradingView desktop and web.
indicator("Gap & 5m ORB Scorecard", "Gap/ORB", overlay = true, max_boxes_count = 60, max_lines_count = 60, max_labels_count = 200)
 
TZ = "America/New_York"
 
daysToShow  = input.int(1, "Sessions to draw", minval = 1, maxval = 10)
showOR      = input.bool(true, "Opening-range box")
showGap     = input.bool(true, "Gap-fill line + zone")
showBreak   = input.bool(true, "1st 5m close marker")
showTable   = input.bool(true, "Scorecard strip")
showHistory = input.bool(true, "History lines (SPY 251-session study)")
tablePos    = input.string("Top center", "Strip position", options = ["Top center", "Top right", "Bottom center", "Bottom right"])
cOR   = input.color(#00e5ff, "Opening range")
cGap  = input.color(#ff3df2, "Gap fill")
cUp   = input.color(#00ff6a, "Close above range")
cDn   = input.color(#ff2d4a, "Close below range")
 
// ── Session timing ───────────────────────────────────────────────────
tfSec   = timeframe.in_seconds()
tfOK    = not timeframe.isdwm and tfSec <= 300 and 300 % tfSec == 0
inRth   = not na(time(timeframe.period, "0930-1600", TZ))
inOR    = not na(time(timeframe.period, "0930-0935", TZ))
rthStart = inRth and not inRth[1]
etMin(t) => hour(t, TZ) * 60 + minute(t, TZ)
closeMin = etMin(time_close) - 570            // minutes after 09:30 at this bar's close
nowMin   = etMin(time)
 
// Prior regular-session close (non-repainting daily idiom; RTH-only ticker so extended
// hours never leak into it). In today's premarket this is still yesterday's 16:00 close.
pc = request.security(ticker.modify(syminfo.tickerid, session.regular), "D", close[1], lookahead = barmerge.lookahead_on)
 
// ── Per-session state ────────────────────────────────────────────────
var float dayPC   = na
var float dayOpen = na
var float orHi    = na
var float orLo    = na
var float ext     = na
var bool  gapUp   = false
var bool  filled  = false
var int   fillT   = na
var int   brkDir  = 0
var int   brkT    = na
var float brkC    = na
var int   dayNum  = 0
 
var box   orBox  = na
var box   gapBox = na
var line  pcLine = na
var label orLbl  = na
var label gapLbl = na
 
// drawings of older sessions, deleted once more than daysToShow sessions exist
var array<box>   oldBoxes  = array.new<box>()
var array<line>  oldLines  = array.new<line>()
var array<label> oldLabels = array.new<label>()
var array<int>   oldBoxDay = array.new<int>()
var array<int>   oldLineDay = array.new<int>()
var array<int>   oldLblDay = array.new<int>()
 
keepBox(box b) =>
    if not na(b)
        oldBoxes.push(b)
        oldBoxDay.push(dayNum)
keepLine(line l) =>
    if not na(l)
        oldLines.push(l)
        oldLineDay.push(dayNum)
keepLabel(label l) =>
    if not na(l)
        oldLabels.push(l)
        oldLblDay.push(dayNum)
 
if tfOK and rthStart
    // archive yesterday's objects, then prune beyond daysToShow
    keepBox(orBox), keepBox(gapBox), keepLine(pcLine), keepLabel(orLbl), keepLabel(gapLbl)
    dayNum += 1
    while oldBoxDay.size() > 0 and oldBoxDay.get(0) <= dayNum - daysToShow
        oldBoxes.shift().delete()
        oldBoxDay.shift()
    while oldLineDay.size() > 0 and oldLineDay.get(0) <= dayNum - daysToShow
        oldLines.shift().delete()
        oldLineDay.shift()
    while oldLblDay.size() > 0 and oldLblDay.get(0) <= dayNum - daysToShow
        oldLabels.shift().delete()
        oldLblDay.shift()
 
    dayPC   := pc
    dayOpen := open
    gapUp   := open > pc
    orHi    := high
    orLo    := low
    ext     := open
    filled  := false
    fillT   := na
    brkDir  := 0
    brkT    := na
    brkC    := na
    orBox  := showOR ? box.new(bar_index, high, bar_index + 1, low, border_color = cOR, border_width = 1, bgcolor = color.new(cOR, 88)) : na
    orLbl  := showOR ? label.new(bar_index, high, "", style = label.style_label_lower_left, color = color.new(color.black, 100), textcolor = cOR, size = size.small) : na
    pcLine := showGap and not na(pc) ? line.new(bar_index, pc, bar_index + 1, pc, color = cGap, style = line.style_dashed, width = 2) : na
    gapBox := showGap and not na(pc) ? box.new(bar_index, math.max(pc, open), bar_index + 1, math.min(pc, open), border_color = color.new(cGap, 100), bgcolor = color.new(cGap, 84)) : na
    gapLbl := showGap and not na(pc) ? label.new(bar_index + 1, pc, "", style = gapUp ? label.style_label_upper_left : label.style_label_lower_left, color = color.new(color.black, 100), textcolor = cGap, size = size.small) : na
 
gapPct = dayPC > 0 ? dayOpen / dayPC - 1 : na
bigGap = not na(gapPct) and math.abs(gapPct) >= 0.0015
 
if tfOK and inRth and not na(dayOpen)
    // opening range
    if inOR
        orHi := math.max(orHi, high)
        orLo := math.min(orLo, low)
    // session extreme on the fill side, and first touch of the prior close
    ext := gapUp ? math.min(ext, low) : math.max(ext, high)
    if bigGap and not filled and (gapUp ? low <= dayPC : high >= dayPC)
        filled := true
        fillT  := time
        if showGap
            keepLabel(label.new(bar_index, dayPC, "", style = label.style_circle, color = cGap, size = size.tiny))
            keepLabel(label.new(bar_index, dayPC, "GAP FILLED " + str.format_time(time, "HH:mm", TZ), style = gapUp ? label.style_label_upper_left : label.style_label_lower_left, color = color.new(color.black, 100), textcolor = cGap, size = size.small))
    // first 5-minute candle CLOSE outside the range (on bar close only — no repaint)
    if brkDir == 0 and barstate.isconfirmed and closeMin >= 10 and closeMin % 5 == 0
        if close > orHi
            brkDir := 1
        else if close < orLo
            brkDir := -1
        if brkDir != 0
            brkT := time_close
            brkC := close
            if showBreak
                keepLabel(label.new(bar_index, brkDir == 1 ? low : high,
                     (brkDir == 1 ? "▲ 1st 5m close ABOVE " : "▼ 1st 5m close BELOW ") + str.format_time(time_close, "HH:mm", TZ),
                     style = brkDir == 1 ? label.style_label_up : label.style_label_down,
                     color = brkDir == 1 ? cUp : cDn, textcolor = brkDir == 1 ? color.black : color.white, size = size.small))
    // stretch today's drawings to the current bar
    if not na(orBox)
        orBox.set_top(orHi), orBox.set_bottom(orLo), orBox.set_right(bar_index)
        orLbl.set_y(orHi)
        orLbl.set_text("5m OR " + str.tostring(orLo, format.mintick) + "–" + str.tostring(orHi, format.mintick) + " ($" + str.tostring(orHi - orLo, "#.##") + ")")
    if not na(pcLine)
        pcLine.set_x2(bar_index)
        gapLbl.set_x(bar_index + 1)
        if filled or not bigGap                     // zone stops growing once filled
            gapLbl.set_text(bigGap ? "" : "prior close " + str.tostring(dayPC, format.mintick))
        else
            gapBox.set_right(bar_index)
            gapBox.set_top(math.max(dayPC, ext))
            gapBox.set_bottom(math.min(dayPC, ext))
            togo = gapUp ? close - dayPC : dayPC - close
            gapLbl.set_text("GAP FILL " + str.tostring(dayPC, format.mintick) + (gapUp ? "  ▼ $" : "  ▲ $") + str.tostring(math.max(0, togo), "#.##") + " to go")
 
// ── Scorecard strip ──────────────────────────────────────────────────
pos = tablePos == "Top right" ? position.top_right : tablePos == "Bottom center" ? position.bottom_center : tablePos == "Bottom right" ? position.bottom_right : position.top_center
var table strip = table.new(pos, 4, 3, border_width = 2, border_color = color.new(color.black, 100))
 
chip(int col, string txt, color bg, color fg) =>
    table.cell(strip, col, 0, txt, bgcolor = bg, text_color = fg, text_size = size.small, text_font_family = font.family_monospace)
 
fmtT(int t) => str.format_time(t, "HH:mm", TZ)
pct(float x) => (x >= 0 ? "+" : "") + str.tostring(x * 100, "#.##") + "%"
 
if barstate.islast and showTable
    strip.clear(0, 0, 3, 2)
    if not tfOK
        chip(0, "Gap/ORB: use a 1m or 5m chart", color.gray, color.black)
    else
        // premarket (extended-hours chart, before today's 09:30): live gap vs yesterday's close
        pre = not inRth and nowMin < 570 and nowMin >= 240 and not na(pc)
        g   = pre ? close / pc - 1 : gapPct
        refPC = pre ? pc : dayPC
        refOpen = pre ? close : dayOpen
        up = g > 0
        if not na(g)
            chip(0, "GAP " + (up ? "▲ " : "▼ ") + pct(g) + (pre ? " premarket" : "") + "  " + str.tostring(refOpen, format.mintick) + " vs close " + str.tostring(refPC, format.mintick), #ffd400, color.black)
            if pre or (inRth and closeMin < 5)
                chip(1, "opening range forms 09:30–09:35", cOR, color.black)
            else
                chip(1, "5m OR " + str.tostring(orLo, format.mintick) + "–" + str.tostring(orHi, format.mintick) + " ($" + str.tostring(orHi - orLo, "#.##") + ")", cOR, color.black)
                if brkDir != 0
                    chip(2, "1st 5m CLOSE " + (brkDir == 1 ? "▲ ABOVE" : "▼ BELOW") + " the range " + fmtT(brkT), brkDir == 1 ? cUp : cDn, brkDir == 1 ? color.black : color.white)
                else
                    chip(2, "no 5m close outside the range yet", #e6edf3, color.black)
                if math.abs(g) < 0.0015
                    chip(3, "GAP TOO SMALL", #9ba6b3, color.black)
                else if filled
                    past = up ? refPC - ext : ext - refPC
                    chip(3, "GAP FILLED " + fmtT(fillT) + (past > 0.05 ? " · " + (up ? "low " : "high ") + str.tostring(ext, format.mintick) + " ($" + str.tostring(past, "#.##") + " past)" : ""), #39ff14, color.black)
                else
                    togo = math.max(0, up ? close - refPC : refPC - close)
                    chip(3, "NOT FILLED · $" + str.tostring(togo, "#.##") + " (" + str.tostring(togo / refPC * 100, "#.##") + "%) to the prior close", cGap, color.black)
            if showHistory
                // fill odds by gap size: n, % filled by 10:30 / 12:00 / close
                string gl = na
                int n = 0
                int a = 0
                int b = 0
                int c = 0
                if g >= 0.008
                    gl := "≥+0.8%", n := 19, a := 5, b := 11, c := 16
                else if g >= 0.004
                    gl := "+0.4..0.8%", n := 38, a := 18, b := 29, c := 39
                else if g >= 0.0015
                    gl := "+0.15..0.4%", n := 53, a := 36, b := 51, c := 58
                else if g > -0.0015
                    gl := "flat (±0.15%)", n := 64, a := 83, b := 83, c := 84
                else if g > -0.004
                    gl := "−0.15..−0.4%", n := 38, a := 39, b := 42, c := 55
                else
                    gl := "≤−0.4%", n := 39, a := 8, b := 23, c := 31
                table.cell(strip, 0, 1, "HISTORY gap " + gl + ": fills " + str.tostring(a) + "% by 10:30, " + str.tostring(b) + "% by noon, " + str.tostring(c) + "% by the close (n=" + str.tostring(n) + ", SPY)", text_color = #ffe97a, bgcolor = color.new(color.black, 30), text_size = size.small, text_halign = text.align_left)
                strip.merge_cells(0, 1, 3, 1)
                if math.abs(g) >= 0.003 and brkDir != 0 and not pre
                    toward = (up and brkDir == -1) or (not up and brkDir == 1)
                    int n2 = up ? (toward ? 31 : 44) : (toward ? 31 : 24)
                    int noon = up ? (toward ? 42 : 11) : (toward ? 35 : 8)
                    int cl = up ? (toward ? 52 : 16) : (toward ? 45 : 25)
                    table.cell(strip, 0, 2, "HISTORY after a " + (up ? "gap-up" : "gap-down") + " with the 1st 5m close " + (toward ? "TOWARD" : "AWAY from") + " the fill: fills " + str.tostring(noon) + "% by noon, " + str.tostring(cl) + "% by the close (n=" + str.tostring(n2) + ", gaps ≥0.3%)", text_color = #ffe97a, bgcolor = color.new(color.black, 30), text_size = size.small, text_halign = text.align_left)
                    strip.merge_cells(0, 2, 3, 2)

← 返回列表