8am Sniper

tylerjhelen94 · study · 169 行 · 点赞 9 · 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 Buy/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 in every session (overnight, pre-market and RTH)
indicator("8am Sniper", shorttitle = "8am Sniper", overlay = true, max_lines_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 Buy / 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.")
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 Buy color", group = grpD)
djSellCol  = input.color(color.new(color.purple, 0), "Reversal Sell color", group = grpD)
 
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))
 
// ───────────── 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 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 time < bullExp and bullBreak
djSell = useDJ and not na(bearTrig) 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
 
// ───────────── 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 Buy",  shape.labelup,      location.belowbar, djBuyCol,     size = size.small, text = "Reversal\nBuy",  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 Buy",  "8am Sniper Reversal Buy 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}}")

← 返回列表