Opening Range Levels

ORBMap · study · 200 行 · 点赞 11 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
// ═══════════════════════════════════════════════════════════════════════════════
//  Opening Range Levels — Lite  (v1.3)
//  Publish as: PUBLIC · OPEN-SOURCE (free). Never mention a paid script in the
//  description, chart, or code — TradingView rules forbid it.
//
//  Session opening range plus the structural levels intraday traders reference:
//  range high/low/middle, 1× range moves, session VWAP, prior day high/low/close,
//  overnight high/low. Every level is named at the right edge with its exact
//  price; labels stack so they never overlap. Line colors are set in the Style
//  tab. A small box reads the range as a % of the prior daily ATR.
//  No signals. Non-repainting.
// ═══════════════════════════════════════════════════════════════════════════════
indicator("Opening Range Levels — Lite", shorttitle = "OR Levels", overlay = true, max_labels_count = 100)
 
sessIn    = input.session("0930-1600", "Regular session")
tzIn      = input.string("America/New_York", "Session timezone")
orMin     = input.int(15, "Opening range (minutes)", minval = 1, maxval = 120)
atrLen    = input.int(14, "Daily ATR length", minval = 2)
narrowPct = input.float(20.0, "Narrow open ≤ % of daily ATR")
widePct   = input.float(40.0, "Wide open ≥ % of daily ATR")
lvScope   = input.string("Today only", "Show levels for", options = ["Today only", "All sessions"])
showExt   = input.bool(true, "1× range moves")
showVW    = input.bool(true, "Session VWAP")
showPrev  = input.bool(true, "Prior day high/low/close")
showON    = input.bool(true, "Overnight high/low")
showLbl   = input.bool(true, "Right-edge labels")
lblOff    = input.int(6, "Label offset (bars)", minval = 2, maxval = 50)
lblGap    = input.float(5.0, "Label spacing (% of visible price range)", minval = 1.0, maxval = 12.0, step = 0.5)
showInfo  = input.bool(true, "Show info box")
grpC = "Label colors (line colors: Style tab)"
lcOR   = input.color(#2563eb, "Range", group = grpC, inline = "l")
lcVW   = input.color(#9333ea, "VWAP", group = grpC, inline = "l")
lcPrev = input.color(#64748b, "Prior day", group = grpC, inline = "l")
lcON   = input.color(#0891b2, "Overnight", group = grpC, inline = "l")
 
C_OR   = #2563eb
C_VW   = #9333ea
C_PREV = #64748b
C_ON   = #0891b2
DISP   = display.pane + display.data_window
 
tfSec   = timeframe.in_seconds()
tfOK    = timeframe.isintraday and tfSec <= orMin * 60 and (orMin * 60) % tfSec == 0
inSess  = not na(time(timeframe.period, sessIn, tzIn))
dKey    = year(time, tzIn) * 10000 + month(time, tzIn) * 100 + dayofmonth(time, tzIn)
lastKey = year(last_bar_time, tzIn) * 10000 + month(last_bar_time, tzIn) * 100 + dayofmonth(last_bar_time, tzIn)
isFirst = inSess and (not inSess[1] or dKey != dKey[1])
inScope = lvScope == "All sessions" or dKey == lastKey
sessEnded = not inSess and inSess[1]
var float visHi = na
var float visLo = na
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
    visHi := na(visHi) ? high : math.max(visHi, high)
    visLo := na(visLo) ? low : math.min(visLo, low)
 
dATR = request.security(syminfo.tickerid, "D", ta.atr(atrLen)[1], lookahead = barmerge.lookahead_on)
 
var float sHi = na
var float sLo = na
var float sCl = na
var float pHi = na
var float pLo = na
var float pCl = na
var float onHiB = na
var float onLoB = na
var float onHi = na
var float onLo = na
var float cPV = 0.0
var float cV = 0.0
var int   sStart = na
var float orHi = na
var float orLo = na
var bool  orDone = false
 
if not inSess
    onHiB := na(onHiB) ? high : math.max(onHiB, high)
    onLoB := na(onLoB) ? low : math.min(onLoB, low)
 
if isFirst
    pHi := sHi
    pLo := sLo
    pCl := sCl
    sHi := high
    sLo := low
    onHi := onHiB
    onLo := onLoB
    onHiB := na
    onLoB := na
    cPV := 0.0
    cV := 0.0
    sStart := time
    orHi := na
    orLo := na
    orDone := false
else if inSess
    sHi := math.max(sHi, high)
    sLo := math.min(sLo, low)
if inSess
    sCl := close
    cPV += hlc3 * nz(volume)
    cV += nz(volume)
 
orEndMs = na(sStart) ? na : sStart + orMin * 60000
inOR = inSess and not na(orEndMs) and time < orEndMs
if inOR
    orHi := na(orHi) ? high : math.max(orHi, high)
    orLo := na(orLo) ? low : math.min(orLo, low)
if inOR and time_close >= orEndMs
    orDone := true
 
orRng = orHi - orLo
orMid = (orHi + orLo) / 2
orPct = not na(dATR) and dATR > 0 and orDone ? orRng / dATR * 100 : na
sVWAP = inSess and cV > 0 ? cPV / cV : na
// "Prior day" = last completed RTH session; overnight = current overnight when outside RTH
dPH  = inSess ? pHi : sHi
dPL  = inSess ? pLo : sLo
dPC  = inSess ? pCl : sCl
dONH = inSess ? onHi : onHiB
dONL = inSess ? onLo : onLoB
 
// Lines — one named swatch per line in the Style tab
f_show(float x, bool cond) => cond and not isFirst and not sessEnded and inScope ? x : na
show = tfOK and inSess and not na(orHi)
p1 = plot(f_show(orHi, show), "Range high", C_OR, 2, plot.style_linebr, display = DISP)
p2 = plot(f_show(orLo, show), "Range low", C_OR, 2, plot.style_linebr, display = DISP)
fill(p1, p2, color.new(C_OR, 90), "Range fill")
plot(f_show(orMid, show and orDone), "Range middle", color.new(C_OR, 45), 1, plot.style_linebr, display = DISP)
plot(f_show(orHi + orRng, show and orDone and showExt), "1× range up", color.new(C_OR, 65), 1, plot.style_linebr, display = DISP)
plot(f_show(orLo - orRng, show and orDone and showExt), "1× range down", color.new(C_OR, 65), 1, plot.style_linebr, display = DISP)
plot(f_show(sVWAP, tfOK and showVW), "VWAP", C_VW, 2, plot.style_linebr, display = DISP)
plot(f_show(dPH, tfOK and showPrev), "Prior day high", C_PREV, 1, plot.style_linebr, display = DISP)
plot(f_show(dPL, tfOK and showPrev), "Prior day low", C_PREV, 1, plot.style_linebr, display = DISP)
plot(f_show(dPC, tfOK and showPrev), "Prior day close", color.new(C_PREV, 45), 1, plot.style_linebr, display = DISP)
plot(f_show(dONH, tfOK and showON), "Overnight high", C_ON, 1, plot.style_linebr, display = DISP)
plot(f_show(dONL, tfOK and showON), "Overnight low", C_ON, 1, plot.style_linebr, display = DISP)
 
// Right-edge labels, stacked so they never overlap
f_add(array<float> ps, array<string> ns, array<color> cs, float p, string n, color col) =>
    if not na(p)
        ps.push(p)
        ns.push(n)
        cs.push(col)
 
var array<label> lvLabels = array.new<label>()
if barstate.islast
    for lb in lvLabels
        label.delete(lb)
    lvLabels.clear()
    if showLbl and tfOK and (not na(orHi) or not inSess)
        ps = array.new<float>()
        ns = array.new<string>()
        cs = array.new<color>()
        if inSess
            f_add(ps, ns, cs, orHi, "Range high", lcOR)
            f_add(ps, ns, cs, orLo, "Range low", lcOR)
        if inSess and orDone
            f_add(ps, ns, cs, orMid, "Range middle", lcOR)
            if showExt
                f_add(ps, ns, cs, orHi + orRng, "1× range up", lcOR)
                f_add(ps, ns, cs, orLo - orRng, "1× range down", lcOR)
        if showVW
            f_add(ps, ns, cs, sVWAP, "VWAP", lcVW)
        if showPrev
            f_add(ps, ns, cs, dPH, "Prior day high", lcPrev)
            f_add(ps, ns, cs, dPL, "Prior day low", lcPrev)
            f_add(ps, ns, cs, dPC, "Prior day close", lcPrev)
        if showON
            f_add(ps, ns, cs, dONH, "Overnight high", lcON)
            f_add(ps, ns, cs, dONL, "Overnight low", lcON)
        if ps.size() > 0
            idx = ps.sort_indices(order.descending)
            top = math.max(ps.max(), nz(visHi, ps.max()))
            bot = math.min(ps.min(), nz(visLo, ps.min()))
            gap = (top - bot) * lblGap / 100
            xL  = bar_index + lblOff
            float prevY = na
            for k in idx
                p = ps.get(k)
                y = na(prevY) ? p : math.min(p, prevY - gap)
                prevY := y
                lvLabels.push(label.new(xL, y, ns.get(k) + "  " + str.tostring(p, format.mintick), xloc.bar_index, yloc.price,
                     color.new(cs.get(k), 5), label.style_label_left, color.white, size.small))
 
// Info box (compact)
var table tb = table.new(position.top_right, 2, 3, color.new(#0f172a, 8), color.new(#334155, 0), 1, color.new(#334155, 0), 0)
if showInfo and barstate.islast
    cKey = color.new(#94a3b8, 0)
    cVal = color.new(#f1f5f9, 0)
    dayType = na(orPct) ? "—" : orPct <= narrowPct ? "NARROW" : orPct >= widePct ? "WIDE" : "NORMAL"
    meaning = dayType == "NARROW" ? "quiet open, room to move" : dayType == "WIDE" ? "big open, much of day's range used" : dayType == "NORMAL" ? "typical open" : ""
    status = not tfOK ? "Chart TF must divide " + str.tostring(orMin) + "m" : na(dATR) ? "Loading daily data" : not inSess ? "Closed" : inOR ? "Building range" : "Range set"
    table.cell(tb, 0, 0, "OPENING RANGE", text_color = cVal, text_size = size.small, text_halign = text.align_left, bgcolor = color.new(#1e293b, 0))
    table.cell(tb, 1, 0, status, text_color = not tfOK ? color.new(#f59e0b, 0) : cKey, text_size = size.small, text_halign = text.align_left, bgcolor = color.new(#1e293b, 0))
    table.cell(tb, 0, 1, "Range", text_color = cKey, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 1, na(orHi) ? "—" : str.tostring(orLo, format.mintick) + " – " + str.tostring(orHi, format.mintick) + " (" + str.tostring(orRng, format.mintick) + ")", text_color = cVal, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 0, 2, "Open", text_color = cKey, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 2, na(orPct) ? "—" : dayType + " " + str.tostring(orPct, "#") + "% ATR · " + meaning, text_color = dayType == "WIDE" ? color.new(#f59e0b, 0) : cVal, text_size = size.small, text_halign = text.align_left)

← 返回列表