EMA (9,20) & VWAP [stock screener]

jaspreet1998 · strategy · 61 行 · 点赞 33 · TradingView 原页

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

Pine Script

//@version=6
strategy("9EMA + VWAP x 20EMA (Daily)", overlay=true,
         default_qty_type=strategy.percent_of_equity, default_qty_value=100,
         initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05)
 
// ── Inputs ──────────────────────────────────────────────
fastLen   = input.int(9,  "Fast EMA length", minval=1)
slowLen   = input.int(20, "Slow EMA length", minval=1)
vwapLen   = input.int(20, "Rolling VWAP length (bars)", minval=1,
     tooltip="On a daily chart the built-in session VWAP resets every bar, so a rolling VWAP over N days is used instead.")
allowShort = input.bool(true, "Trade shorts (off = sell signal only exits longs)")
 
slMode  = input.string("ATR", "Stop-loss type", options=["ATR", "Entry candle low/high"], group="Stop loss",
     tooltip="ATR: stop = entry candle close -/+ multiplier x ATR (ATR value taken at the entry candle).\nEntry candle low/high: long stop = low of entry candle, short stop = high of entry candle.")
atrLen  = input.int(14, "ATR length", minval=1, group="Stop loss")
atrMult = input.float(2.0, "ATR multiplier", minval=0.1, step=0.1, group="Stop loss")
 
// ── Calculations ────────────────────────────────────────
ema9  = ta.ema(close, fastLen)
ema20 = ta.ema(close, slowLen)
vwap  = math.sum(hlc3 * volume, vwapLen) / math.sum(volume, vwapLen)
 
// Both lines above/below the 20 EMA AND close confirms
buyState  = ema9 > ema20 and vwap > ema20 and close > ema20
sellState = ema9 < ema20 and vwap < ema20 and close < ema20
 
// Signal fires on the bar the full condition first becomes true
buySignal  = buyState  and not buyState[1]
sellSignal = sellState and not sellState[1]
 
// ── Orders (daily close, no repainting) ─────────────────
atrVal = ta.atr(atrLen)
var float longStop  = na
var float shortStop = na
 
if buySignal
    longStop := slMode == "ATR" ? close - atrMult * atrVal : low
    strategy.entry("Long", strategy.long)
if sellSignal
    shortStop := slMode == "ATR" ? close + atrMult * atrVal : high
    if allowShort
        strategy.entry("Short", strategy.short)
    else
        strategy.close("Long")
 
// Stop is fixed at the entry candle and does not trail
if buySignal or strategy.position_size > 0
    strategy.exit("Long SL", "Long", stop=longStop)
if (sellSignal and allowShort) or strategy.position_size < 0
    strategy.exit("Short SL", "Short", stop=shortStop)
 
// ── Plots ───────────────────────────────────────────────
plot(ema9,  "EMA 9",  color=color.new(color.orange, 0), linewidth=1)
plot(ema20, "EMA 20", color=color.new(color.blue, 0),   linewidth=2)
plot(vwap,  "VWAP",   color=color.new(color.purple, 0), linewidth=1)
 
plot(strategy.position_size > 0 ? longStop  : na, "Long stop",  color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? shortStop : na, "Short stop", color=color.red, style=plot.style_linebr, linewidth=1)
 
plotshape(buySignal, "Buy",  shape.labelup,   location.belowbar, color.green, text="BUY",  textcolor=color.white, size=size.small)
plotshape(sellSignal, "Sell", shape.labeldown, location.abovebar, color.red,   text="SELL", textcolor=color.white, size=size.small)

← 返回列表