Daily Opening Range For Scalping

writetoinvest · study · 566 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("OpeningRange For Scalping", overlay = true, max_lines_count = 50, max_labels_count = 50)
 
// ─────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────
tzInput     = input.string("America/New_York", "Timezone")
 
rthSession  = input.session("0930-1600:23456", "Regular NY Session")
pmSession   = input.session("0400-0930:23456", "Pre-Market Session")
orSession   = input.session("0930-1000:23456", "Opening Range Session")
 
showPD      = input.bool(true, "Show Previous Regular Session High/Low")
showDBY     = input.bool(true, "Show Day Before Yesterday High/Low")
showPM      = input.bool(true, "Show Pre-Market High/Low")
showOR      = input.bool(true, "Show Opening Range High/Low")
showLabels  = input.bool(true, "Show Labels")
 
pdColor     = input.color(color.rgb(30, 144, 255), "Previous Day Color")
dbyColor    = input.color(color.rgb(255, 105, 180), "Day Before Yesterday Color")
pmColor     = input.color(color.rgb(255, 165, 0), "Pre-Market Color")
orColor     = input.color(color.rgb(50, 205, 50), "Opening Range Color")
 
// ── ATR inputs ────────────────────────────────
showATR     = input.bool(true, "Show Open ± Daily ATR", group = "Daily ATR Levels")
atrLength   = input.int(14, "Daily ATR Length", group = "Daily ATR Levels", minval = 1)
atrMult     = input.float(1.0, "ATR Multiplier", group = "Daily ATR Levels", minval = 0.1, step = 0.1)
atrColor    = input.color(color.rgb(220, 80, 220), "ATR Level Color", group = "Daily ATR Levels")
 
lineWidth   = input.int(2, "Line Width", minval = 1, maxval = 5)
lineStyleIn = input.string("Solid", "Line Style", options = ["Solid", "Dashed", "Dotted"])
 
lineStyle = lineStyleIn == "Dashed" ? line.style_dashed :
     lineStyleIn == "Dotted" ? line.style_dotted :
     line.style_solid
 
// ─────────────────────────────────────────────
// Daily ATR
// ─────────────────────────────────────────────
dailyAtr = request.security(
     syminfo.tickerid,
     "D",
     ta.atr(atrLength),
     lookahead = barmerge.lookahead_off)
 
// ─────────────────────────────────────────────
// NY timestamps
// ─────────────────────────────────────────────
nyYear  = year(time, tzInput)
nyMonth = month(time, tzInput)
nyDay   = dayofmonth(time, tzInput)
 
marketOpenTs  = timestamp(tzInput, nyYear, nyMonth, nyDay, 9, 30)
marketCloseTs = timestamp(tzInput, nyYear, nyMonth, nyDay, 16, 0)
marketMidTs   = timestamp(tzInput, nyYear, nyMonth, nyDay, 12, 45)
 
newNyDay = na(time[1]) or
     nyDay != dayofmonth(time[1], tzInput) or
     nyMonth != month(time[1], tzInput) or
     nyYear != year(time[1], tzInput)
 
// ─────────────────────────────────────────────
// Session detection
// ─────────────────────────────────────────────
inRTH = not na(time(timeframe.period, rthSession, tzInput))
inPM  = not na(time(timeframe.period, pmSession, tzInput))
inOR  = not na(time(timeframe.period, orSession, tzInput))
 
rthStart = inRTH and not inRTH[1]
rthEnd   = not inRTH and inRTH[1]
 
// ─────────────────────────────────────────────
// Track RTH High / Low
//
// prevRth = yesterday
// prev2Rth = day before yesterday
// ─────────────────────────────────────────────
var float currentRthHigh = na
var float currentRthLow  = na
 
var float prevRthHigh  = na
var float prevRthLow   = na
 
var float prev2RthHigh = na
var float prev2RthLow  = na
 
if rthStart
    currentRthHigh := high
    currentRthLow  := low
else if inRTH
    currentRthHigh := na(currentRthHigh) ? high : math.max(currentRthHigh, high)
    currentRthLow  := na(currentRthLow) ? low : math.min(currentRthLow, low)
 
// When RTH ends:
// Yesterday becomes Day Before Yesterday
// Today's completed session becomes Previous Day
if rthEnd
    prev2RthHigh := prevRthHigh
    prev2RthLow  := prevRthLow
 
    prevRthHigh := currentRthHigh
    prevRthLow  := currentRthLow
 
// ─────────────────────────────────────────────
// Track Pre-Market + Opening Range
// ─────────────────────────────────────────────
var float pmHigh = na
var float pmLow  = na
var float orHigh = na
var float orLow  = na
 
if newNyDay
    pmHigh := na
    pmLow  := na
    orHigh := na
    orLow  := na
 
if inPM
    pmHigh := na(pmHigh) ? high : math.max(pmHigh, high)
    pmLow  := na(pmLow) ? low : math.min(pmLow, low)
 
if inOR
    orHigh := na(orHigh) ? high : math.max(orHigh, high)
    orLow  := na(orLow) ? low : math.min(orLow, low)
 
// ─────────────────────────────────────────────
// Capture 9:30 Open + Freeze Daily ATR
// ─────────────────────────────────────────────
var float nyOpenPrice  = na
var float atrAtOpen    = na
var float openPlusAtr  = na
var float openMinusAtr = na
 
if newNyDay
    nyOpenPrice  := na
    atrAtOpen    := na
    openPlusAtr  := na
    openMinusAtr := na
 
if rthStart
    nyOpenPrice  := open
    atrAtOpen    := dailyAtr
    openPlusAtr  := nyOpenPrice + atrMult * atrAtOpen
    openMinusAtr := nyOpenPrice - atrMult * atrAtOpen
 
// ─────────────────────────────────────────────
// Drawing Objects
// ─────────────────────────────────────────────
 
// Previous Day
var line pdhLine = na
var line pdlLine = na
 
// Day Before Yesterday
var line dbyhLine = na
var line dbylLine = na
 
// PM / OR / ATR
var line pmhLine = na
var line pmlLine = na
var line orhLine = na
var line orlLine = na
var line opaLine = na
var line omaLine = na
 
// Labels
var label pdhLabel = na
var label pdlLabel = na
 
var label dbyhLabel = na
var label dbylLabel = na
 
var label pmhLabel = na
var label pmlLabel = na
var label orhLabel = na
var label orlLabel = na
 
var label opaLabel = na
var label omaLabel = na
 
var label opaMidLabel = na
var label omaMidLabel = na
 
// ─────────────────────────────────────────────
// Helper Functions
// ─────────────────────────────────────────────
f_deleteLine(line ln) =>
    if not na(ln)
        line.delete(ln)
 
f_deleteLabel(label lb) =>
    if not na(lb)
        label.delete(lb)
 
// ─────────────────────────────────────────────
// Re-create Objects Each NY Day
// ─────────────────────────────────────────────
if newNyDay or na(pdhLine)
 
    // Delete old lines
    f_deleteLine(pdhLine)
    f_deleteLine(pdlLine)
 
    f_deleteLine(dbyhLine)
    f_deleteLine(dbylLine)
 
    f_deleteLine(pmhLine)
    f_deleteLine(pmlLine)
    f_deleteLine(orhLine)
    f_deleteLine(orlLine)
 
    f_deleteLine(opaLine)
    f_deleteLine(omaLine)
 
    // Delete old labels
    f_deleteLabel(pdhLabel)
    f_deleteLabel(pdlLabel)
 
    f_deleteLabel(dbyhLabel)
    f_deleteLabel(dbylLabel)
 
    f_deleteLabel(pmhLabel)
    f_deleteLabel(pmlLabel)
    f_deleteLabel(orhLabel)
    f_deleteLabel(orlLabel)
 
    f_deleteLabel(opaLabel)
    f_deleteLabel(omaLabel)
 
    f_deleteLabel(opaMidLabel)
    f_deleteLabel(omaMidLabel)
 
    // ── Create Previous Day Lines ──
    pdhLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(pdColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    pdlLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(pdColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    // ── Create Day Before Yesterday Lines ──
    dbyhLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(dbyColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    dbylLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(dbyColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    // ── PM Lines ──
    pmhLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(pmColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    pmlLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(pmColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    // ── Opening Range Lines ──
    orhLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(orColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    orlLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(orColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    // ── ATR Lines ──
    opaLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(atrColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    omaLine := line.new(
         marketOpenTs, close,
         marketCloseTs, close,
         xloc = xloc.bar_time,
         color = color.new(atrColor, 100),
         width = lineWidth,
         style = lineStyle)
 
    // ── Previous Day Labels ──
    pdhLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = pdColor,
         size = size.small)
 
    pdlLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = pdColor,
         size = size.small)
 
    // ── Day Before Yesterday Labels ──
    dbyhLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = dbyColor,
         size = size.small)
 
    dbylLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = dbyColor,
         size = size.small)
 
    // ── PM Labels ──
    pmhLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = pmColor,
         size = size.small)
 
    pmlLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = pmColor,
         size = size.small)
 
    // ── OR Labels ──
    orhLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = orColor,
         size = size.small)
 
    orlLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = orColor,
         size = size.small)
 
    // ── ATR Right Labels ──
    opaLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = atrColor,
         size = size.small)
 
    omaLabel := label.new(
         marketCloseTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_left,
         color = color.new(color.black, 100),
         textcolor = atrColor,
         size = size.small)
 
    // ── ATR Mid Labels ──
    opaMidLabel := label.new(
         marketMidTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_center,
         color = color.new(atrColor, 15),
         textcolor = color.white,
         size = size.normal)
 
    omaMidLabel := label.new(
         marketMidTs, close, "",
         xloc = xloc.bar_time,
         yloc = yloc.price,
         style = label.style_label_center,
         color = color.new(atrColor, 15),
         textcolor = color.white,
         size = size.normal)
 
// ─────────────────────────────────────────────
// Generic Level Update
// ─────────────────────────────────────────────
f_updateLevel(line ln, label lb, float price, string txt, color col, bool visible) =>
    if visible and not na(price)
        line.set_xy1(ln, marketOpenTs, price)
        line.set_xy2(ln, marketCloseTs, price)
        line.set_color(ln, col)
        line.set_width(ln, lineWidth)
        line.set_style(ln, lineStyle)
 
        label.set_x(lb, marketCloseTs)
        label.set_y(lb, price)
        label.set_text(lb, showLabels ? txt : "")
        label.set_textcolor(lb, col)
        label.set_color(lb, color.new(color.black, 100))
    else
        line.set_color(ln, color.new(col, 100))
        label.set_text(lb, "")
 
// ─────────────────────────────────────────────
// ATR Mid Label
// ─────────────────────────────────────────────
f_updateMidLabel(label lb, float price, string prefix, bool visible) =>
    if visible and not na(price)
 
        decimals = math.round(
             math.log(1 / syminfo.mintick) /
             math.log(10))
 
        priceStr = str.tostring(
             math.round(price, decimals))
 
        labelTxt = prefix + "\n" + priceStr
 
        label.set_x(lb, marketMidTs)
        label.set_y(lb, price)
        label.set_text(lb, labelTxt)
        label.set_color(lb, color.new(atrColor, 15))
        label.set_textcolor(lb, color.white)
 
    else
        label.set_text(lb, "")
        label.set_color(lb, color.new(atrColor, 100))
 
// ─────────────────────────────────────────────
// Apply Updates
// ─────────────────────────────────────────────
 
// Previous Day
f_updateLevel(
     pdhLine, pdhLabel,
     prevRthHigh,
     "PDH",
     pdColor,
     showPD)
 
f_updateLevel(
     pdlLine, pdlLabel,
     prevRthLow,
     "PDL",
     pdColor,
     showPD)
 
// Day Before Yesterday — PINK
f_updateLevel(
     dbyhLine, dbyhLabel,
     prev2RthHigh,
     "DBYH",
     dbyColor,
     showDBY)
 
f_updateLevel(
     dbylLine, dbylLabel,
     prev2RthLow,
     "DBYL",
     dbyColor,
     showDBY)
 
// Pre-Market
f_updateLevel(
     pmhLine, pmhLabel,
     pmHigh,
     "PMH",
     pmColor,
     showPM)
 
f_updateLevel(
     pmlLine, pmlLabel,
     pmLow,
     "PML",
     pmColor,
     showPM)
 
// Opening Range
f_updateLevel(
     orhLine, orhLabel,
     orHigh,
     "ORH",
     orColor,
     showOR)
 
f_updateLevel(
     orlLine, orlLabel,
     orLow,
     "ORL",
     orColor,
     showOR)
 
// ATR
f_updateLevel(
     opaLine, opaLabel,
     openPlusAtr,
     "Open+ATR",
     atrColor,
     showATR)
 
f_updateLevel(
     omaLine, omaLabel,
     openMinusAtr,
     "Open-ATR",
     atrColor,
     showATR)
 
f_updateMidLabel(
     opaMidLabel,
     openPlusAtr,
     "Open+ATR",
     showATR)
 
f_updateMidLabel(
     omaMidLabel,
     openMinusAtr,
     "Open-ATR",
     showATR)

← 返回列表