Market Open Prep Dashboard

dhills333 · study · 380 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("Market Open Prep Dashboard", shorttitle="Market Open", overlay=true)
 
// =====================================================================
// MARKET OPEN PREP DASHBOARD
// Premarket / market-open stats table: Daily ATR, ATR levels from the open,
// ATR used/left, Open, Prior Close, Gap, VIX, Globex H/L/Range, Opening Range
// (plus a London opening range on Currencies charts).
// Built for futures (CME Central Time). Use on an INTRADAY chart.
// =====================================================================
 
// ------------------------------ INPUTS --------------------------------
// ATR
grpATR      = "ATR"
atrLen      = input.int(14, "Daily ATR Length", minval=1, group=grpATR)
usedMode    = input.string("Net move from open", "ATR Used Method",
     options=["Net move from open", "Max extension from open", "Day range (High-Low)"],
     tooltip="Net move: |Current price - Open|\nMax extension: farthest price has traveled from the open (up or down)\nDay range: Day High - Day Low", group=grpATR)
preOpenRef  = input.bool(true, "Pre-open: use prior close as reference", group=grpATR,
     tooltip="Before the open, ATR levels / used / left are measured from the prior RTH close. After the open they switch to the opening price.")
 
// Sessions
grpSess     = "Sessions"
tz          = input.string("America/Chicago", "Timezone (CME Central)", group=grpSess)
preset      = input.string("Auto (by symbol)", "Market Preset",
     options=["Auto (by symbol)", "Equity Index", "Gold / Metals", "Crude / Energy", "Treasuries", "Currencies", "Grains", "Livestock", "Custom"],
     tooltip="Auto picks the preset from the chart's futures symbol (ES, NQ, GC, CL, NG, ZN, 6E, ZC, LE...). Sets the regular-session open/close in CME Central Time. Globex always starts at 1700 CT and ends at the RTH open.\n\nEquity Index: 0830-1500 (ES, NQ, YM, RTY)\nGold / Metals: 0720-1230 (GC, SI, HG)\nCrude / Energy: 0800-1330 (CL, NG, RB, HO)\nTreasuries: 0720-1400 (ZN, ZB)\nCurrencies: 0720-1400 (6E, 6J, 6B...)\nGrains: 0830-1320 (ZC, ZS, ZW)\nLivestock: 0830-1305 (LE, HE)", group=grpSess)
autoFallback = input.string("Equity Index", "Auto: preset if symbol not recognized",
     options=["Equity Index", "Gold / Metals", "Crude / Energy", "Treasuries", "Currencies", "Grains", "Livestock", "Custom"], group=grpSess)
customRTH   = input.string("0830-1500", "Custom RTH, Central Time (used only if preset = Custom)", group=grpSess)
lonSess     = input.string("0200-0215", "London opening range, Central Time (Currencies only)", group=grpSess,
     tooltip="Second opening range shown on Currencies charts, in addition to the US open range. Keep it 15 minutes long.")
 
// Map futures root symbol to a market preset
f_auto(string root, string fallback) =>
    string k = "|" + root + "|"
    str.contains("|ES|MES|NQ|MNQ|YM|MYM|RTY|M2K|EMD|", k) ? "Equity Index" :
     str.contains("|GC|MGC|SI|SIL|HG|MHG|PL|PA|QO|QI|QC|", k) ? "Gold / Metals" :
     str.contains("|CL|MCL|QM|NG|QG|RB|HO|BZ|", k) ? "Crude / Energy" :
     str.contains("|ZN|ZB|ZF|ZT|UB|TN|TWE|", k) ? "Treasuries" :
     str.contains("|6E|6J|6B|6A|6C|6S|6N|6M|M6E|M6A|M6B|E7|J7|", k) ? "Currencies" :
     str.contains("|ZC|ZS|ZW|ZM|ZL|KE|ZO|ZR|XC|XK|XW|", k) ? "Grains" :
     str.contains("|LE|HE|GF|", k) ? "Livestock" : fallback
 
f_rth(string p) =>
    switch p
        "Equity Index"   => "0830-1500"
        "Gold / Metals"  => "0720-1230"
        "Crude / Energy" => "0800-1330"
        "Treasuries"     => "0720-1400"
        "Currencies"     => "0720-1400"
        "Grains"         => "0830-1320"
        "Livestock"      => "0830-1305"
        => customRTH
 
string presetEff = preset == "Auto (by symbol)" ? f_auto(syminfo.root, autoFallback) : preset
string rthSess = f_rth(presetEff)
string gSess   = "1700-" + str.substring(rthSess, 0, 4)
orMin       = 15  // Opening range is fixed at 15 minutes (RTH open to open + 15m), independent of chart timeframe
 
// Table layout
grpTbl      = "Table Style"
tblPos      = input.string("Top Right", "Position",
     options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group=grpTbl)
tblSize     = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=grpTbl)
cBg         = input.color(color.new(#131722, 0), "Background", group=grpTbl)
bgTransp    = input.int(10, "Background Transparency", minval=0, maxval=100, group=grpTbl)
cHdrBg      = input.color(#2962FF, "Title Background", group=grpTbl)
cHdrTxt     = input.color(color.white, "Title Text", group=grpTbl)
cSecBg      = input.color(#363A45, "Section Header Background", group=grpTbl)
cSecTxt     = input.color(#B2B5BE, "Section Header Text", group=grpTbl)
cLabel      = input.color(#B2B5BE, "Label Text", group=grpTbl)
cValue      = input.color(color.white, "Value Text", group=grpTbl)
cPos        = input.color(#26A69A, "Positive (gap up) Text", group=grpTbl)
cNeg        = input.color(#EF5350, "Negative (gap down) Text", group=grpTbl)
cBorder     = input.color(#363A45, "Border Color", group=grpTbl)
borderW     = input.int(1, "Border Width", minval=0, maxval=5, group=grpTbl)
cFrame      = input.color(#787B86, "Frame Color", group=grpTbl)
frameW      = input.int(1, "Frame Width", minval=0, maxval=5, group=grpTbl)
 
// Sections to show
grpShow     = "Show Sections"
showATR     = input.bool(true, "ATR block", group=grpShow)
showOpen    = input.bool(true, "Open / Prior Close / Gap block", group=grpShow)
showGlobex  = input.bool(true, "Globex block", group=grpShow)
showOR      = input.bool(true, "Opening Range block", group=grpShow)
showVIX     = input.bool(true, "VIX block", group=grpShow)
showLon     = input.bool(true, "London opening range block (Currencies only)", group=grpShow)
 
// VIX
grpVIX      = "VIX"
vixSym      = input.symbol("CBOE:VIX", "VIX Symbol", group=grpVIX)
vixHi       = input.float(25.0, "Red at or above", step=0.5, group=grpVIX)
vixMid      = input.float(22.0, "Orange at or above", step=0.5, group=grpVIX)
cVixHi      = input.color(#EF5350, "Red (high VIX)", group=grpVIX)
cVixMid     = input.color(#FF9800, "Orange (elevated VIX)", group=grpVIX)
cVixUp      = input.color(#EF5350, "VIX rising (change text)", group=grpVIX)
cVixDn      = input.color(#26A69A, "VIX falling (change text)", group=grpVIX)
 
// Chart lines
grpLines    = "Chart Lines"
showAtrLn   = input.bool(true,  "ATR High / Low from open", group=grpLines)
showGlbLn   = input.bool(false, "Globex High / Low", group=grpLines)
showOrLn    = input.bool(true,  "Opening Range High / Low", group=grpLines)
cAtrHi      = input.color(#EF5350, "ATR High", group=grpLines)
cAtrLo      = input.color(#26A69A, "ATR Low", group=grpLines)
cGlb        = input.color(#FF9800, "Globex H/L", group=grpLines)
cOR         = input.color(#2962FF, "Opening Range H/L", group=grpLines)
showLonLn   = input.bool(true, "London Opening Range High / Low (Currencies only)", group=grpLines)
cLon        = input.color(#AB47BC, "London Opening Range H/L", group=grpLines)
 
// ------------------------------ HELPERS -------------------------------
f_pos(string s) =>
    switch s
        "Top Left"      => position.top_left
        "Top Center"    => position.top_center
        "Top Right"     => position.top_right
        "Middle Left"   => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right"  => position.middle_right
        "Bottom Left"   => position.bottom_left
        "Bottom Center" => position.bottom_center
        => position.bottom_right
 
f_size(string s) =>
    switch s
        "Tiny"  => size.tiny
        "Small" => size.small
        "Normal"=> size.normal
        "Large" => size.large
        => size.huge
 
fmtP(float v) => na(v) ? "—" : str.tostring(v, format.mintick)
fmtPct(float v) => na(v) ? "—" : str.tostring(v, "0.0") + "%"
fmtSgnPct(float v) => na(v) ? "—" : (v > 0 ? "+" : "") + str.tostring(v, "0.0") + "%"
 
// ------------------------------ DAILY ATR -----------------------------
// Prior completed day's ATR (does not repaint, known premarket)
float atrD = request.security(syminfo.tickerid, "D", ta.atr(atrLen)[1], lookahead = barmerge.lookahead_on)
 
// ------------------------------ VIX DATA ------------------------------
float vixNow     = request.security(vixSym, timeframe.period, close)
float vixBarOpen = request.security(vixSym, timeframe.period, open)
float vixPrev    = request.security(vixSym, "D", close[1], lookahead = barmerge.lookahead_on)
var float vixOpenPx = na
 
f_vixCol(float v) =>
    na(v) ? cValue : (v >= vixHi ? cVixHi : (v >= vixMid ? cVixMid : cValue))
 
f_chgTxt(float v, float base) =>
    float d = v - base
    na(d) or na(base) or base == 0 ? "—" : (d > 0 ? "▲ +" : (d < 0 ? "▼ " : "– ")) + str.tostring(d, "0.00") + " (" + (d > 0 ? "+" : "") + str.tostring(d / base * 100, "0.0") + "%)"
 
f_chgCol(float v, float base) =>
    float d = v - base
    na(d) ? cValue : (d > 0 ? cVixUp : (d < 0 ? cVixDn : cValue))
 
// ------------------------------ SESSION STATE -------------------------
bool inRTH = not na(time(timeframe.period, rthSess, tz))
bool inG   = not na(time(timeframe.period, gSess, tz))
bool newRTH = inRTH and not inRTH[1]
bool newG   = inG and not inG[1]
 
var float gHigh         = na
var float gLow          = na
var float lastRTHClose  = na
var float dayPriorClose = na
var float openPx        = na
var bool  rthStarted    = false
var float rthHigh       = na
var float rthLow        = na
var int   rthStartT     = na
// Opening range computed on 1-minute data so it is always exactly open to open+15m,
// no matter what timeframe the chart is on.
f_or() =>
    bool _inRTH = not na(time(timeframe.period, rthSess, tz))
    bool _newRTH = _inRTH and not _inRTH[1]
    var float _h = na
    var float _l = na
    var int _t0 = na
    if _newRTH
        _t0 := time
        _h := high
        _l := low
    else if _inRTH and not na(_t0) and time < _t0 + orMin * 60000
        _h := math.max(_h, high)
        _l := math.min(_l, low)
    [_h, _l]
 
[orHigh, orLow] = request.security(syminfo.tickerid, "1", f_or())
 
// Second opening range (London, Currencies charts), also computed on 1-minute data
f_orS(string sess) =>
    bool _in = not na(time(timeframe.period, sess, tz))
    bool _new = _in and not _in[1]
    bool _g = not na(time(timeframe.period, gSess, tz))
    bool _newG = _g and not _g[1]
    var float _h = na
    var float _l = na
    var bool _st = false
    var int _t0 = na
    if _newG
        _h := na
        _l := na
        _st := false
        _t0 := na
    if _new
        _h := high
        _l := low
        _st := true
        _t0 := time
    else if _in and _st
        _h := math.max(_h, high)
        _l := math.min(_l, low)
    [_h, _l, _st, _t0]
 
[lonHigh, lonLow, lonStarted, lonT0] = request.security(syminfo.tickerid, "1", f_orS(lonSess))
 
// New overnight session = new trading day: reset
if newG
    gHigh := high
    gLow := low
    rthStarted := false
    openPx := na
    vixOpenPx := na
    rthHigh := na
    rthLow := na
else if inG
    gHigh := na(gHigh) ? high : math.max(gHigh, high)
    gLow  := na(gLow) ? low : math.min(gLow, low)
 
// Regular session
if newRTH
    dayPriorClose := lastRTHClose
    openPx := open
    vixOpenPx := vixBarOpen
    rthStarted := true
    rthStartT := time
    rthHigh := high
    rthLow := low
else if inRTH and rthStarted
    rthHigh := math.max(rthHigh, high)
    rthLow := math.min(rthLow, low)
 
if inRTH
    lastRTHClose := close
 
// ------------------------------ CALCULATIONS --------------------------
float ref = rthStarted ? openPx : (preOpenRef ? lastRTHClose : na)
float curHi = rthStarted ? rthHigh : gHigh
float curLo = rthStarted ? rthLow : gLow
 
float atrHi = na(ref) or na(atrD) ? na : ref + atrD
float atrLo = na(ref) or na(atrD) ? na : ref - atrD
 
float usedPts = na
if not na(ref)
    usedPts := switch usedMode
        "Net move from open"      => math.abs(close - ref)
        "Max extension from open" => math.max(curHi - ref, ref - curLo)
        => curHi - curLo
 
float usedPct = na(usedPts) or na(atrD) ? na : usedPts / atrD * 100
float leftPct = na(usedPct) ? na : math.max(0, 100 - usedPct)
float leftPts = na(usedPts) or na(atrD) ? na : math.max(0, atrD - usedPts)
 
// Gap: real gap after the open, live (indicative) gap before the open
float gapPts    = rthStarted ? openPx - dayPriorClose : close - lastRTHClose
float gapBase   = rthStarted ? dayPriorClose : lastRTHClose
float gapPctPx  = na(gapPts) or na(gapBase) ? na : gapPts / gapBase * 100
float gapPctAtr = na(gapPts) or na(atrD) ? na : gapPts / atrD * 100
 
float gRange    = na(gHigh) or na(gLow) ? na : gHigh - gLow
float gRangePct = na(gRange) or na(atrD) ? na : gRange / atrD * 100
 
float orRange    = na(orHigh) or na(orLow) ? na : orHigh - orLow
float orRangePct = na(orRange) or na(atrD) ? na : orRange / atrD * 100
bool  orDone     = rthStarted and not na(rthStartT) and timenow >= rthStartT + orMin * 60000
 
bool  lonActive   = showLon and presetEff == "Currencies"
float lonRange    = na(lonHigh) or na(lonLow) ? na : lonHigh - lonLow
float lonRangePct = na(lonRange) or na(atrD) ? na : lonRange / atrD * 100
bool  lonDone     = lonStarted and not na(lonT0) and timenow >= lonT0 + orMin * 60000
 
// ------------------------------ PLOTS ---------------------------------
plot(showAtrLn ? atrHi : na, "ATR High", color=cAtrHi, style=plot.style_linebr, linewidth=1)
plot(showAtrLn ? atrLo : na, "ATR Low", color=cAtrLo, style=plot.style_linebr, linewidth=1)
plot(showGlbLn ? gHigh : na, "Globex High", color=cGlb, style=plot.style_linebr, linewidth=1)
plot(showGlbLn ? gLow : na, "Globex Low", color=cGlb, style=plot.style_linebr, linewidth=1)
plot(showOrLn and rthStarted ? orHigh : na, "OR High", color=cOR, style=plot.style_linebr, linewidth=1)
plot(showOrLn and rthStarted ? orLow : na, "OR Low", color=cOR, style=plot.style_linebr, linewidth=1)
plot(showLonLn and lonActive and lonStarted ? lonHigh : na, "London OR High", color=cLon, style=plot.style_linebr, linewidth=1)
plot(showLonLn and lonActive and lonStarted ? lonLow : na, "London OR Low", color=cLon, style=plot.style_linebr, linewidth=1)
 
// ------------------------------ TABLE ---------------------------------
var table t = table.new(f_pos(tblPos), 3, 30,
     bgcolor = color.new(cBg, bgTransp),
     frame_color = cFrame, frame_width = frameW,
     border_color = cBorder, border_width = borderW)
 
f_section(table tb, int r, string txt) =>
    table.cell(tb, 0, r, txt, text_color=cSecTxt, bgcolor=cSecBg, text_size=f_size(tblSize), text_halign=text.align_left)
    table.cell(tb, 1, r, "", bgcolor=cSecBg)
    table.cell(tb, 2, r, "", bgcolor=cSecBg)
    table.merge_cells(tb, 0, r, 2, r)
    r + 1
 
f_row(table tb, int r, string lbl, string v1, string v2, color c1) =>
    table.cell(tb, 0, r, lbl, text_color=cLabel, text_size=f_size(tblSize), text_halign=text.align_left)
    table.cell(tb, 1, r, v1, text_color=c1, text_size=f_size(tblSize), text_halign=text.align_right)
    table.cell(tb, 2, r, v2, text_color=cValue, text_size=f_size(tblSize), text_halign=text.align_right)
    r + 1
 
f_rowc(table tb, int r, string lbl, string v1, color c1, string v2, color c2) =>
    table.cell(tb, 0, r, lbl, text_color=cLabel, text_size=f_size(tblSize), text_halign=text.align_left)
    table.cell(tb, 1, r, v1, text_color=c1, text_size=f_size(tblSize), text_halign=text.align_right)
    table.cell(tb, 2, r, v2, text_color=c2, text_size=f_size(tblSize), text_halign=text.align_right)
    r + 1
 
if barstate.islast
    table.clear(t, 0, 0, 2, 29)
    int r = 0
 
    string status = rthStarted ? "RTH" : "PRE-OPEN"
    table.cell(t, 0, r, syminfo.ticker + "  |  " + presetEff + "  |  " + status, text_color=cHdrTxt, bgcolor=cHdrBg, text_size=f_size(tblSize), text_halign=text.align_left)
    table.cell(t, 1, r, "", bgcolor=cHdrBg)
    table.cell(t, 2, r, "", bgcolor=cHdrBg)
    table.merge_cells(t, 0, r, 2, r)
    r += 1
 
    if not timeframe.isintraday
        table.cell(t, 0, r, "Switch to an intraday chart", text_color=cNeg, text_size=f_size(tblSize), text_halign=text.align_left)
        table.cell(t, 1, r, "", bgcolor=color.new(cBg, bgTransp))
        table.cell(t, 2, r, "", bgcolor=color.new(cBg, bgTransp))
        table.merge_cells(t, 0, r, 2, r)
    else
        if showATR
            string refTxt = rthStarted ? "Open" : "Prior Close"
            r := f_section(t, r, "DAILY ATR (" + str.tostring(atrLen) + ")  ·  ref: " + refTxt)
            r := f_row(t, r, "Daily ATR", fmtP(atrD), "", cValue)
            r := f_row(t, r, "ATR High", fmtP(atrHi), "", cValue)
            r := f_row(t, r, "ATR Low", fmtP(atrLo), "", cValue)
            r := f_row(t, r, "ATR Used", fmtP(usedPts), fmtPct(usedPct), cValue)
            r := f_row(t, r, "ATR Left", fmtP(leftPts), fmtPct(leftPct), cValue)
 
        if showOpen
            r := f_section(t, r, "OPEN / GAP")
            r := f_row(t, r, "Open", rthStarted ? fmtP(openPx) : "Pending", "", cValue)
            r := f_row(t, r, "Prior Close", fmtP(rthStarted ? dayPriorClose : lastRTHClose), "", cValue)
            color gapCol = na(gapPts) ? cValue : (gapPts > 0 ? cPos : (gapPts < 0 ? cNeg : cValue))
            string gapTxt = na(gapPts) ? "—" : (gapPts > 0 ? "+" : "") + fmtP(gapPts) + " (" + (gapPctPx > 0 ? "+" : "") + str.tostring(gapPctPx, "0.00") + "%)"
            r := f_row(t, r, rthStarted ? "Gap" : "Gap (live)", gapTxt, fmtSgnPct(gapPctAtr), gapCol)
 
        if showVIX
            r := f_section(t, r, "VIX")
            r := f_rowc(t, r, "Prior Close", na(vixPrev) ? "—" : str.tostring(vixPrev, "0.00"), f_vixCol(vixPrev), "", cValue)
            if rthStarted
                r := f_rowc(t, r, "At Open", na(vixOpenPx) ? "—" : str.tostring(vixOpenPx, "0.00"), f_vixCol(vixOpenPx), f_chgTxt(vixOpenPx, vixPrev), f_chgCol(vixOpenPx, vixPrev))
                r := f_rowc(t, r, "Now", na(vixNow) ? "—" : str.tostring(vixNow, "0.00"), f_vixCol(vixNow), f_chgTxt(vixNow, vixOpenPx), f_chgCol(vixNow, vixOpenPx))
            else
                r := f_rowc(t, r, "At Open", "Pending", cValue, "", cValue)
                r := f_rowc(t, r, "Now", na(vixNow) ? "—" : str.tostring(vixNow, "0.00"), f_vixCol(vixNow), f_chgTxt(vixNow, vixPrev), f_chgCol(vixNow, vixPrev))
 
        if showGlobex
            r := f_section(t, r, "GLOBEX")
            r := f_row(t, r, "High", fmtP(gHigh), "", cValue)
            r := f_row(t, r, "Low", fmtP(gLow), "", cValue)
            r := f_row(t, r, "Range", fmtP(gRange), fmtPct(gRangePct), cValue)
 
        if lonActive
            r := f_section(t, r, "LONDON OPEN RANGE (" + lonSess + " CT)" + (lonStarted and not lonDone ? " · forming" : ""))
            r := f_row(t, r, "High", lonStarted ? fmtP(lonHigh) : "Pending", "", cValue)
            r := f_row(t, r, "Low", lonStarted ? fmtP(lonLow) : "Pending", "", cValue)
            r := f_row(t, r, "Range", lonStarted ? fmtP(lonRange) : "Pending", lonStarted ? fmtPct(lonRangePct) : "", cValue)
 
        if showOR
            r := f_section(t, r, (presetEff == "Currencies" ? "US " : "") + "OPENING RANGE (first 15m)" + (rthStarted and not orDone ? " · forming" : ""))
            r := f_row(t, r, "High", rthStarted ? fmtP(orHigh) : "Pending", "", cValue)
            r := f_row(t, r, "Low", rthStarted ? fmtP(orLow) : "Pending", "", cValue)
            r := f_row(t, r, "Range", rthStarted ? fmtP(orRange) : "Pending", rthStarted ? fmtPct(orRangePct) : "", cValue)

← 返回列表