8am Sniper

tylerjhelen94 · study · 247 行 · 点赞 27 · TradingView 原页

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

Pine Script

//@version=6
// 8am Sniper
// • Marks the high, low and dotted midline of the 8:00–9:00 ET hourly candle
// • BO / RC: BUY/SELL when a bar closes OUT of or back INTO the range with volume above its MA
// • Reversal Alert (buy) / Reversal Sell: 15-minute doji that ends a trend → BUY when price breaks the doji HIGH (after a downtrend)
//                                       → SELL when price breaks the doji LOW (after an uptrend)
//   Reversal signals fire only in the NY session (09:30–16:00 ET by default)
// • Previous-day high / low / midline, labeled, drawn across the whole session (18:00 open → 17:00 close ET)
// • Faint outlines around the Asia, London and New York sessions
indicator("8am Sniper", shorttitle = "8am Sniper", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
 
const string TZ = "America/New_York"
 
// ───────────── Range inputs ─────────────
grpR = "8AM Range"
rangeSess = input.session("0800-0900", "Range candle (ET)", group = grpR)
dispSess  = input.session("0800-1600", "Draw lines until (ET)", group = grpR)
hlColor   = input.color(color.new(color.teal, 0), "High / Low color", group = grpR)
hlWidth   = input.int(2, "High / Low width", minval = 1, maxval = 4, group = grpR)
midColor  = input.color(color.new(color.teal, 30), "Midline color", group = grpR)
shade8am  = input.bool(true, "Shade the 8AM candle", group = grpR)
 
// ───────────── Range signal inputs ─────────────
grpS = "Range Signals"
tradeSess = input.session("0930-1600", "Signal window (ET)", group = grpS)
useBO     = input.bool(true, "Breakout signals (BO)", group = grpS)
useRC     = input.bool(true, "Reclaim signals (RC)", group = grpS)
volLen    = input.int(20, "Volume MA length", minval = 1, group = grpS)
 
// ───────────── Doji reversal inputs ─────────────
grpD = "Doji Reversal"
useDJ      = input.bool(true, "Reversal Alert / Sell signals", group = grpD)
dojiTF     = input.timeframe("15", "Doji timeframe", group = grpD)
dojiPct    = input.float(10.0, "Doji = body ≤ this % of the candle's range", minval = 1, maxval = 50, step = 1, group = grpD)
trendLen   = input.int(3, "Trend before doji (candles)", minval = 1, group = grpD, tooltip = "Downtrend: the candle before the doji is red and closes below the close this many candles earlier. Uptrend is the mirror.")
revSess    = input.session("0930-1600", "Reversal session (ET)", group = grpD, tooltip = "Reversal Alert / Sell setups and signals only during this session (NY session by default).")
validBars  = input.int(2, "Setup stays valid for (doji-timeframe candles)", minval = 1, group = grpD)
trigClose  = input.bool(true, "Require a 1m CLOSE beyond the doji (off = wick break)", group = grpD)
showTrig   = input.bool(true, "Draw doji trigger lines", group = grpD)
djBuyCol   = input.color(color.new(#2962FF, 0), "Reversal Alert color", group = grpD)
djSellCol  = input.color(color.new(color.purple, 0), "Reversal Sell color", group = grpD)
 
// ───────────── Previous-day level inputs ─────────────
grpA = "Previous Day Levels"
showPD    = input.bool(true, "Draw PDH / PDL / midline", group = grpA)
pdCol     = input.color(color.new(color.orange, 0), "PDH / PDL color", group = grpA)
pdMidCol  = input.color(color.new(color.orange, 30), "PD midline color", group = grpA)
 
// ───────────── Session outline inputs ─────────────
grpO = "Session Outlines"
showAsia   = input.bool(true, "Asia", group = grpO, inline = "as")
asiaSess   = input.session("1800-0300", "", group = grpO, inline = "as")
asiaCol    = input.color(color.new(#E91E63, 0), "", group = grpO, inline = "as")
showLon    = input.bool(true, "London", group = grpO, inline = "lo")
lonSess    = input.session("0400-0800", "", group = grpO, inline = "lo")
lonCol     = input.color(color.new(#2196F3, 0), "", group = grpO, inline = "lo")
showNY     = input.bool(true, "New York", group = grpO, inline = "ny")
nySess     = input.session("0930-1600", "", group = grpO, inline = "ny")
nyCol      = input.color(color.new(#FF9800, 0), "", group = grpO, inline = "ny")
sessFade   = input.int(75, "Outline faintness (0 = solid, 100 = invisible)", minval = 0, maxval = 100, group = grpO)
showSessLb = input.bool(true, "Show session names", group = grpO)
 
if timeframe.in_seconds() > 3600
    runtime.error("8am Sniper: use a chart timeframe of 1 hour or lower.")
if useDJ and timeframe.in_seconds() > timeframe.in_seconds(dojiTF)
    runtime.error("8am Sniper: chart timeframe must be at or below the doji timeframe (use 1 minute).")
 
// ───────────── Sessions ─────────────
inRange = not na(time(timeframe.period, rangeSess, TZ))
inDisp  = not na(time(timeframe.period, dispSess, TZ))
inTrade = not na(time(timeframe.period, tradeSess, TZ))
inRev   = not na(time(timeframe.period, revSess, TZ))
 
// ───────────── 8AM range + lines ─────────────
var float rngHi   = na
var float rngLo   = na
var bool  rngDone = false
var line  hiLn    = na
var line  loLn    = na
var line  midLn   = na
 
if inRange and not inRange[1]
    rngHi := high
    rngLo := low
    rngDone := false
    mid = (rngHi + rngLo) / 2
    hiLn  := line.new(bar_index, rngHi, bar_index, rngHi, color = hlColor, width = hlWidth, force_overlay = true)
    loLn  := line.new(bar_index, rngLo, bar_index, rngLo, color = hlColor, width = hlWidth, force_overlay = true)
    midLn := line.new(bar_index, mid, bar_index, mid, color = midColor, style = line.style_dotted, width = 1, force_overlay = true)
else if inRange
    rngHi := math.max(rngHi, high)
    rngLo := math.min(rngLo, low)
    mid = (rngHi + rngLo) / 2
    line.set_y1(hiLn, rngHi)
    line.set_y2(hiLn, rngHi)
    line.set_y1(loLn, rngLo)
    line.set_y2(loLn, rngLo)
    line.set_y1(midLn, mid)
    line.set_y2(midLn, mid)
 
if not inRange and inRange[1]
    rngDone := true
 
if inDisp and not na(hiLn)
    line.set_x2(hiLn, bar_index)
    line.set_x2(loLn, bar_index)
    line.set_x2(midLn, bar_index)
 
bgcolor(shade8am and inRange ? color.new(color.teal, 90) : na, title = "8AM candle", force_overlay = true)
 
// ───────────── Volume ─────────────
volMA = ta.sma(volume, volLen)
volOK = volume > volMA
 
// ───────────── Range signals (volume above MA) ─────────────
ok = rngDone and inTrade and volOK
 
boBuy  = useBO and ok and close > rngHi and close[1] <= rngHi
boSell = useBO and ok and close < rngLo and close[1] >= rngLo
rcSell = useRC and ok and close[1] > rngHi and close <= rngHi and close >= rngLo
rcBuy  = useRC and ok and close[1] < rngLo and close >= rngLo and close <= rngHi
 
// ───────────── 15m doji reversal ─────────────
// Evaluates the LAST CLOSED doji-timeframe candle (no repainting)
f_doji() =>
    body   = math.abs(close[1] - open[1])
    rng    = high[1] - low[1]
    isDoji = rng > 0 and body <= rng * dojiPct / 100
    bull   = isDoji and close[2] < open[2] and close[2] < close[2 + trendLen]
    bear   = isDoji and close[2] > open[2] and close[2] > close[2 + trendLen]
    [bull, bear, high[1], low[1], time[1]]
 
[dBull, dBear, dHigh, dLow, dTime] = request.security(syminfo.tickerid, dojiTF, f_doji(), lookahead = barmerge.lookahead_on)
 
tfMs    = timeframe.in_seconds(dojiTF) * 1000
newDoji = useDJ and inRev and not na(dTime) and dTime != dTime[1]
 
var float bullTrig = na
var float bullInv  = na
var int   bullExp  = na
var line  bullLn   = na
var float bearTrig = na
var float bearInv  = na
var int   bearExp  = na
var line  bearLn   = na
 
if newDoji and dBull
    bullTrig := dHigh
    bullInv  := dLow
    bullExp  := dTime + tfMs * (1 + validBars)
    if showTrig
        bullLn := line.new(dTime, dHigh, time, dHigh, xloc = xloc.bar_time, color = djBuyCol, width = 2, force_overlay = true)
if newDoji and dBear
    bearTrig := dLow
    bearInv  := dHigh
    bearExp  := dTime + tfMs * (1 + validBars)
    if showTrig
        bearLn := line.new(dTime, dLow, time, dLow, xloc = xloc.bar_time, color = djSellCol, width = 2, force_overlay = true)
 
 
bullBreak = trigClose ? close > bullTrig : high > bullTrig
bearBreak = trigClose ? close < bearTrig : low < bearTrig
 
djBuy  = useDJ and not na(bullTrig) and inRev and time < bullExp and bullBreak
djSell = useDJ and not na(bearTrig) and inRev and time < bearExp and bearBreak
 
if not na(bullTrig)
    if showTrig and not na(bullLn)
        line.set_x2(bullLn, time)
    if djBuy or time >= bullExp or close < bullInv
        bullTrig := na
if not na(bearTrig)
    if showTrig and not na(bearLn)
        line.set_x2(bearLn, time)
    if djSell or time >= bearExp or close > bearInv
        bearTrig := na
 
// ───────────── Previous-day high / low / midline ─────────────
// Previous day = prior daily candle (CME day 18:00–17:00 ET); no repainting
[pdh, pdl] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead = barmerge.lookahead_on)
pdMid  = (pdh + pdl) / 2
newDay = timeframe.change("D")
 
var line  pdhLn = na
var line  pdlLn = na
var line  pdmLn = na
var label pdhLb = na
var label pdlLb = na
var label pdmLb = na
 
// Lines span the full current session: from the 18:00 open to the session close (17:00 ET),
// drawn out ahead of price right away
if newDay and showPD and not na(pdh)
    t1 = time
    t2 = time_close("D")
    pdhLn := line.new(t1, pdh, t2, pdh, xloc = xloc.bar_time, color = pdCol, width = 2, force_overlay = true)
    pdlLn := line.new(t1, pdl, t2, pdl, xloc = xloc.bar_time, color = pdCol, width = 2, force_overlay = true)
    pdmLn := line.new(t1, pdMid, t2, pdMid, xloc = xloc.bar_time, color = pdMidCol, style = line.style_dotted, width = 1, force_overlay = true)
    pdhLb := label.new(t2, pdh, "PDH", xloc = xloc.bar_time, style = label.style_label_left, color = color.new(color.black, 100), textcolor = pdCol, size = size.small, force_overlay = true)
    pdlLb := label.new(t2, pdl, "PDL", xloc = xloc.bar_time, style = label.style_label_left, color = color.new(color.black, 100), textcolor = pdCol, size = size.small, force_overlay = true)
    pdmLb := label.new(t2, pdMid, "PD Mid", xloc = xloc.bar_time, style = label.style_label_left, color = color.new(color.black, 100), textcolor = pdMidCol, size = size.small, force_overlay = true)
 
// ───────────── Session outlines ─────────────
// Faint box around each session's high / low, growing as the session trades
f_session(bool show, string sess, string name, color col) =>
    inS = not na(time(timeframe.period, sess, TZ))
    var box   bx = na
    var label lb = na
    var float hi = na
    var float lo = na
    faint = color.new(col, sessFade)
    if show and inS and not inS[1]
        hi := high
        lo := low
        bx := box.new(bar_index, hi, bar_index, lo, border_color = faint, border_width = 1, bgcolor = color.new(col, 100), force_overlay = true)
        if showSessLb
            lb := label.new(bar_index, hi, name, style = label.style_label_lower_left, color = color.new(color.black, 100), textcolor = faint, size = size.tiny, force_overlay = true)
    else if show and inS and not na(bx)
        hi := math.max(hi, high)
        lo := math.min(lo, low)
        box.set_top(bx, hi)
        box.set_bottom(bx, lo)
        box.set_right(bx, bar_index)
        if not na(lb)
            label.set_y(lb, hi)
    inS
 
f_session(showAsia, asiaSess, "Asia", asiaCol)
f_session(showLon, lonSess, "London", lonCol)
f_session(showNY, nySess, "New York", nyCol)
 
// ───────────── Price-chart signals ─────────────
plotshape(boBuy,  "Breakout BUY",  shape.triangleup,   location.belowbar, color.lime,   size = size.small, text = "BUY\nBO",  textcolor = color.lime,  force_overlay = true)
plotshape(boSell, "Breakout SELL", shape.triangledown, location.abovebar, color.red,    size = size.small, text = "SELL\nBO", textcolor = color.red,   force_overlay = true)
plotshape(rcBuy,  "Reclaim BUY",   shape.labelup,      location.belowbar, color.aqua,   size = size.small, text = "BUY\nRC",  textcolor = color.black, force_overlay = true)
plotshape(rcSell, "Reclaim SELL",  shape.labeldown,    location.abovebar, color.orange, size = size.small, text = "SELL\nRC", textcolor = color.black, force_overlay = true)
plotshape(djBuy,  "Reversal Alert", shape.labelup,      location.belowbar, djBuyCol,     size = size.small, text = "Reversal\nAlert",  textcolor = color.white, force_overlay = true)
plotshape(djSell, "Reversal Sell", shape.labeldown,    location.abovebar, djSellCol,    size = size.small, text = "Reversal\nSell", textcolor = color.white, force_overlay = true)
 
// ───────────── Alerts ─────────────
alertcondition(boBuy,  "Breakout BUY",  "8am Sniper breakout BUY on {{ticker}} @ {{close}}")
alertcondition(boSell, "Breakout SELL", "8am Sniper breakout SELL on {{ticker}} @ {{close}}")
alertcondition(rcBuy,  "Reclaim BUY",   "8am Sniper reclaim BUY on {{ticker}} @ {{close}}")
alertcondition(rcSell, "Reclaim SELL",  "8am Sniper reclaim SELL on {{ticker}} @ {{close}}")
alertcondition(djBuy,  "Reversal Alert", "8am Sniper Reversal Alert on {{ticker}} @ {{close}}")
alertcondition(djSell, "Reversal Sell", "8am Sniper Reversal Sell on {{ticker}} @ {{close}}")
alertcondition(boBuy or boSell or rcBuy or rcSell or djBuy or djSell, "Any signal", "8am Sniper signal on {{ticker}} @ {{close}}")

← 返回列表