本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wonderfulRiceafb80
//@version=6
// SPY 5m signals: EMA9 / EMA21 / VWAP stack + ADX > 20
// LONG (backtested, recommended): EMA9 > EMA21 > VWAP, ADX > 20
// SHORT (optional, OFF by default - lost money in 2024-26 backtests): EMA9 < EMA21 < VWAP, ADX > 20
// Filters: 15m trend vs 15m EMA50, entries 9:45-15:00 ET, optional daily SMA50 trend
// Exits: stop = 1.5 ATR, target = 2R (3 ATR), flat by 15:55 ET. One trade at a time.
indicator("EMA9/21 VWAP ADX Signals", overlay=true, max_labels_count=500)
grpF = "Filters"
adxMin = input.float(20, "ADX minimum", group=grpF)
useHTF = input.bool(true, "15m trend filter (15m close vs 15m EMA50)", group=grpF)
useTime = input.bool(true, "Entries only 9:45-15:00 ET", group=grpF)
useDaily = input.bool(false, "Daily trend filter (prior close vs daily SMA50)", group=grpF)
grpR = "Risk"
atrMult = input.float(1.5, "Stop = ATR x", group=grpR)
rr = input.float(2.0, "Target = R multiple", group=grpR)
grpS = "Signals"
showLong = input.bool(true, "Show BUY (long) signals", group=grpS)
showShort = input.bool(false, "Show SELL SHORT signals (not profitable in backtest)", group=grpS)
showEMAs = input.bool(true, "Show EMA9 / EMA21 / VWAP", group=grpS)
// ── Indicators ───────────────────────────────────────────────
e9 = ta.ema(close, 9)
e21 = ta.ema(close, 21)
vw = ta.vwap(hlc3)
[dip, dim, adx] = ta.dmi(14, 14)
atr = ta.atr(14)
inRTH = not na(time(timeframe.period, "0930-1600", "America/New_York"))
hm = hour(time, "America/New_York") * 100 + minute(time, "America/New_York")
eodBar = hm >= 1555
timeOK = not useTime or (hm >= 945 and hm < 1500)
htfC = request.security(syminfo.tickerid, "15", close[1], lookahead=barmerge.lookahead_on)
htfE = request.security(syminfo.tickerid, "15", ta.ema(close, 50)[1], lookahead=barmerge.lookahead_on)
dC = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
dS = request.security(syminfo.tickerid, "D", ta.sma(close, 50)[1], lookahead=barmerge.lookahead_on)
// ── Setups ───────────────────────────────────────────────────
bullStack = e9 > e21 and e9 > vw and e21 > vw and adx > adxMin
bearStack = e9 < e21 and e9 < vw and e21 < vw and adx > adxMin
longSetup = showLong and bullStack and not bullStack[1] and inRTH and not eodBar and timeOK
longSetup := longSetup and (not useHTF or htfC > htfE) and (not useDaily or dC > dS)
shortSetup = showShort and bearStack and not bearStack[1] and inRTH and not eodBar and timeOK
shortSetup := shortSetup and (not useHTF or htfC < htfE) and (not useDaily or dC < dS)
// ── Trade tracking: pos = 1 long, -1 short, 0 flat ───────────
var int pos = 0
var float entryPx = na
var float stopPx = na
var float tgtPx = na
exitHit = false
exitType = ""
exitSide = 0
if pos == 1
if low <= stopPx // stop & target in same bar -> assume stop
exitHit := true, exitType := "STOP"
else if high >= tgtPx
exitHit := true, exitType := "TARGET"
else if eodBar
exitHit := true, exitType := "TIME EXIT"
else if pos == -1
if high >= stopPx
exitHit := true, exitType := "STOP"
else if low <= tgtPx
exitHit := true, exitType := "TARGET"
else if eodBar
exitHit := true, exitType := "TIME EXIT"
if exitHit
exitSide := pos
pos := 0
buySignal = longSetup and pos == 0 and not exitHit
shortSignal = shortSetup and pos == 0 and not exitHit and not buySignal
if buySignal
pos := 1
entryPx := close
stopPx := close - atrMult * atr
tgtPx := close + rr * atrMult * atr
if shortSignal
pos := -1
entryPx := close
stopPx := close + atrMult * atr
tgtPx := close - rr * atrMult * atr
// ── Plots ────────────────────────────────────────────────────
plot(showEMAs ? e9 : na, "EMA9", color.blue)
plot(showEMAs ? e21 : na, "EMA21", color.black)
plot(showEMAs ? vw : na, "VWAP", color.fuchsia)
showLvls = pos != 0 or exitHit
plot(showLvls ? entryPx : na, "Entry", color.gray, 1, plot.style_linebr)
plot(showLvls ? stopPx : na, "Stop", color.red, 2, plot.style_linebr)
plot(showLvls ? tgtPx : na, "Target", color.green, 2, plot.style_linebr)
fmt(x) => str.tostring(x, format.mintick)
if buySignal
label.new(bar_index, low, "BUY\n" + fmt(entryPx) + "\nSL " + fmt(stopPx) + "\nTP " + fmt(tgtPx),
style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)
if shortSignal
label.new(bar_index, high, "SELL SHORT\n" + fmt(entryPx) + "\nSL " + fmt(stopPx) + "\nTP " + fmt(tgtPx),
style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
if exitHit
exitColor = exitType == "TARGET" ? color.green : exitType == "STOP" ? color.red : color.gray
exitText = (exitSide == 1 ? "SELL - " : "COVER - ") + exitType
label.new(bar_index, exitSide == 1 ? high : low, exitText,
style=exitSide == 1 ? label.style_label_down : label.style_label_up,
color=exitColor, textcolor=color.white, size=size.tiny)
// ── Alerts ───────────────────────────────────────────────────
// For prices in the message: create the alert with "Any alert() function call".
if buySignal
alert("SPY BUY @ " + fmt(entryPx) + " | Stop " + fmt(stopPx) + " | Target " + fmt(tgtPx), alert.freq_once_per_bar_close)
if shortSignal
alert("SPY SELL SHORT @ " + fmt(entryPx) + " | Stop " + fmt(stopPx) + " | Target " + fmt(tgtPx), alert.freq_once_per_bar_close)
if exitHit
alert(exitSide == 1 ? "SPY " + exitType + " - SELL to close long" : "SPY " + exitType + " - BUY TO COVER short", alert.freq_once_per_bar_close)
// Fixed-text alerts (alternative)
alertcondition(buySignal, "Buy signal", "SPY long setup: BUY")
alertcondition(shortSignal, "Sell short signal", "SPY short setup: SELL SHORT")
alertcondition(exitHit and exitSide == 1, "Exit long", "SPY: SELL to close long (stop, target or time)")
alertcondition(exitHit and exitSide == -1, "Cover short", "SPY: BUY TO COVER short (stop, target or time)")