Opening Range Scalp (ORS) - NQ with ES VWAP Filter

johnmontojo04 · study · 194 行 · 点赞 13 · TradingView 原页

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

Pine Script

//@version=6
// Opening Range Scalp (ORS) — NQ opening-range retest with an ES VWAP filter
// Open-source. Built for NQ / MNQ futures on the 5-minute chart, New York session.
indicator("Opening Range Scalp (ORS) · NQ + ES VWAP filter", "ORS", overlay = true, max_boxes_count = 300, max_lines_count = 300, max_labels_count = 300)
 
// ───────── Inputs ─────────
gS = "Setup"
orEnd   = input.int(945,  "Opening range ends (NY time HHMM, starts 9:30)", group = gS)
fromHM  = input.int(945,  "Entries from (NY time HHMM)", group = gS)
toHM    = input.int(1100, "Entries until (NY time HHMM)", group = gS)
maxDay  = input.int(1,    "Max trades a day", minval = 1, group = gS)
gR = "Risk"
rr      = input.float(0.5, "Target (R multiple of the stop)", step = 0.05, group = gR)
tMin    = input.int(45,    "Time stop (minutes)", minval = 5, group = gR)
minStop = input.float(6,   "Skip if stop is smaller than (points)", group = gR)
maxStop = input.float(50,  "Skip if stop is larger than (points)", group = gR)
bufAtr  = input.float(0.1, "Stop buffer (x ATR 14)", step = 0.05, group = gR)
gF = "Confirmation"
esF     = input.bool(true, "Require ES on the same side of its session VWAP", group = gF)
esSym   = input.symbol("CME_MINI:ES1!", "Confirmation symbol", group = gF)
gV = "Display"
showOR  = input.bool(true, "Shade the opening range", group = gV)
showWin = input.bool(true, "Shade the entry window", group = gV)
showTbl = input.bool(true, "Show results table", group = gV)
tblPos  = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gV)
 
// ───────── Session helpers (New York time) ─────────
TZ = "America/New_York"
hm(t) => hour(t, TZ) * 100 + minute(t, TZ)
int  nowHM    = hm(time)
bool rth      = nowHM >= 930 and nowHM < 1600
bool rthStart = rth and not (nz(hm(time[1]), 0) >= 930 and nz(hm(time[1]), 0) < 1600)
 
// Session VWAP that resets at 9:30 NY (used on the confirmation symbol)
rthVwap() =>
    int  h  = hour(time, "America/New_York") * 100 + minute(time, "America/New_York")
    int  h1 = nz(hour(time[1], "America/New_York") * 100 + minute(time[1], "America/New_York"), 0)
    bool r  = h >= 930 and h < 1600
    bool st = r and not (h1 >= 930 and h1 < 1600)
    var float pv = 0.0
    var float vv = 0.0
    if st
        pv := 0.0
        vv := 0.0
    if r
        pv += hlc3 * nz(volume, 1)
        vv += nz(volume, 1)
    vv > 0 ? pv / vv : na
 
[esC, esVw] = request.security(esSym, timeframe.period, [close, rthVwap()], lookahead = barmerge.lookahead_off)
float cATR = ta.atr(14)
float lo3  = ta.lowest(low, 3)
float hi3  = ta.highest(high, 3)
 
// ───────── Opening range ─────────
var float orH = na
var float orL = na
var bool  orUp = false
var bool  orDn = false
var int   tradesToday = 0
var box   orBox = na
if rthStart
    orH := na
    orL := na
    orUp := false
    orDn := false
    tradesToday := 0
    orBox := na
if nowHM >= 930 and nowHM < orEnd
    orH := math.max(nz(orH, high), high)
    orL := math.min(nz(orL, low), low)
    if showOR
        if na(orBox)
            orBox := box.new(bar_index, orH, bar_index, orL, border_color = color.new(#29b6f6, 40), bgcolor = color.new(#29b6f6, 85))
        else
            box.set_top(orBox, orH)
            box.set_bottom(orBox, orL)
            box.set_right(orBox, bar_index)
// first close outside the range arms that side
if nowHM >= orEnd and not na(orH)
    orUp := orUp or close > orH
    orDn := orDn or close < orL
 
// ───────── Signals: break, then retest that closes back outside ─────────
bool sigL = orUp and low <= orH and close > orH and close > open and (not esF or esC > esVw)
bool sigS = orDn and high >= orL and close < orL and close < open and (not esF or esC < esVw)
 
// ───────── Trade tracking and results ─────────
var int   tDir = 0
var float tEn = na
var float tSL = na
var float tTP = na
var float tR = na
var int   tTime = na
var int   nT = 0
var int   nW = 0
var int   nL = 0
var int   nX = 0
var float sumR = 0.0
 
if tDir != 0
    bool hitSL = tDir == 1 ? low <= tSL : high >= tSL
    bool hitTP = tDir == 1 ? high >= tTP : low <= tTP
    if hitSL            // if both are touched in one bar, count the stop (conservative)
        nL += 1
        sumR -= 1.0
        label.new(bar_index, tSL, "−1R", style = tDir == 1 ? label.style_label_up : label.style_label_down, color = color.new(#ef5350, 10), textcolor = color.white, size = size.tiny)
        tDir := 0
    else if hitTP
        nW += 1
        sumR += rr
        label.new(bar_index, tTP, "+" + str.tostring(rr) + "R", style = tDir == 1 ? label.style_label_down : label.style_label_up, color = color.new(#26a69a, 10), textcolor = color.white, size = size.tiny)
        tDir := 0
    else if time - tTime >= tMin * 60000 or nowHM >= 1555
        float rTime = (close - tEn) * tDir / tR
        nX += 1
        sumR += rTime
        if rTime > 0
            nW += 1
        else
            nL += 1
        label.new(bar_index, close, "time " + str.tostring(rTime, "+#.##;−#.##") + "R", style = label.style_label_left, color = color.new(#ff9800, 15), textcolor = color.white, size = size.tiny)
        tDir := 0
 
bool inWin = nowHM >= fromHM and nowHM < toHM
bool canT  = tDir == 0 and inWin and tradesToday < maxDay and barstate.isconfirmed
float buf  = bufAtr * cATR
 
f_draw(int dir, float en, float sl, float tp) =>
    int x2 = bar_index + 9
    box.new(bar_index, math.max(en, tp), x2, math.min(en, tp), border_color = color.new(#26a69a, 30), bgcolor = color.new(#26a69a, 78))
    box.new(bar_index, math.max(en, sl), x2, math.min(en, sl), border_color = color.new(#ef5350, 30), bgcolor = color.new(#ef5350, 78))
    label.new(bar_index, dir == 1 ? low : high, (dir == 1 ? "ORS LONG" : "ORS SHORT") + "\nSL " + str.tostring(sl, format.mintick) + "  TP " + str.tostring(tp, format.mintick), style = dir == 1 ? label.style_label_up : label.style_label_down, color = dir == 1 ? #26a69a : #ef5350, textcolor = color.white, size = size.small)
 
bool longNow  = false
bool shortNow = false
if canT and sigL
    float sl = math.round_to_mintick(math.min(lo3, orH) - buf)
    float r  = close - sl
    if r >= minStop and r <= maxStop
        float tp = math.round_to_mintick(close + rr * r)
        f_draw(1, close, sl, tp)
        tradesToday += 1
        nT += 1
        tDir := 1
        tEn := close
        tSL := sl
        tTP := tp
        tR := r
        tTime := time
        longNow := true
else if canT and sigS
    float sl = math.round_to_mintick(math.max(hi3, orL) + buf)
    float r  = sl - close
    if r >= minStop and r <= maxStop
        float tp = math.round_to_mintick(close - rr * r)
        f_draw(-1, close, sl, tp)
        tradesToday += 1
        nT += 1
        tDir := -1
        tEn := close
        tSL := sl
        tTP := tp
        tR := r
        tTime := time
        shortNow := true
 
// ───────── Plots ─────────
plot(rth and nowHM >= orEnd ? orH : na, "OR high", color.new(#29b6f6, 0), style = plot.style_linebr, linewidth = 2)
plot(rth and nowHM >= orEnd ? orL : na, "OR low",  color.new(#29b6f6, 0), style = plot.style_linebr, linewidth = 2)
plot(rth and nowHM >= orEnd ? (orH + orL) / 2 : na, "OR mid", color.new(#29b6f6, 60), style = plot.style_linebr, linewidth = 1)
bgcolor(showWin and inWin ? color.new(#5c6bc0, 93) : na, title = "Entry window")
 
alertcondition(longNow,  "ORS long",  "ORS long on {{ticker}} at {{close}}")
alertcondition(shortNow, "ORS short", "ORS short on {{ticker}} at {{close}}")
 
// ───────── Results table (chart history, no fees or slippage) ─────────
var table tb = table.new(tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left, 2, 7, bgcolor = color.new(#131722, 10), border_color = color.new(#29b6f6, 60), border_width = 1, frame_color = color.new(#29b6f6, 30), frame_width = 1)
if showTbl and barstate.islast
    int done = nW + nL
    table.cell(tb, 0, 0, "ORS results", text_color = #29b6f6, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 0, "chart history", text_color = color.gray, text_size = size.tiny)
    table.cell(tb, 0, 1, "Trades", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 1, str.tostring(done), text_color = color.white, text_size = size.small)
    table.cell(tb, 0, 2, "Win rate", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 2, done > 0 ? str.tostring(100.0 * nW / done, "#.#") + "%" : "—", text_color = #26a69a, text_size = size.small)
    table.cell(tb, 0, 3, "Wins / losses", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 3, str.tostring(nW) + " / " + str.tostring(nL), text_color = color.white, text_size = size.small)
    table.cell(tb, 0, 4, "Time-stop exits", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 4, str.tostring(nX), text_color = #ff9800, text_size = size.small)
    table.cell(tb, 0, 5, "Net result", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 5, str.tostring(sumR, "+#.##;−#.##") + " R", text_color = sumR >= 0 ? #26a69a : #ef5350, text_size = size.small)
    table.cell(tb, 0, 6, "Target / stop", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(tb, 1, 6, str.tostring(rr) + "R / 1R", text_color = color.gray, text_size = size.small)

← 返回列表