EMA Crossover Strategy in Pine Script v6: Full Code with RSI Filter, Stop Loss & Alerts
A complete, copy-paste-ready EMA crossover strategy written in Pine Script v6. Includes RSI confirmation filter, ATR-based stop loss, take profit, and TradingView alert conditions.
What This Strategy Does
This Pine Script strategy enters long when a fast EMA crosses above a slow EMA and RSI is not overbought. It enters short when the fast EMA crosses below the slow EMA and RSI is not oversold. Exits use an ATR-based stop loss and a fixed 2:1 reward-to-risk take profit.
All parameters are exposed as inputs so you can tune them directly in TradingView's Settings panel without editing code.
- Works on any market: crypto, stocks, forex, futures
- Works on any timeframe: scalping (5m) to position trading (Daily+)
- RSI filter reduces false crossover signals in choppy markets
- ATR stop loss adapts to current volatility
- Built-in alerts — get notified on TradingView or via webhooks
The Complete Pine Script v6 Code
Copy the code below into TradingView's Pine Script editor (open any chart → Pine Script Editor → New → paste). Click 'Add to chart' to activate.
//@version=6
strategy(
title = "EMA Crossover + RSI Filter",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
commission_type = strategy.commission.percent,
commission_value = 0.05,
slippage = 2
)
// ── Inputs ───────────────────────────────────────────────────────────────────
fastLen = input.int(9, "Fast EMA Length", minval = 1)
slowLen = input.int(21, "Slow EMA Length", minval = 1)
rsiLen = input.int(14, "RSI Length", minval = 1)
rsiOB = input.int(70, "RSI Overbought", minval = 50, maxval = 100)
rsiOS = input.int(30, "RSI Oversold", minval = 0, maxval = 50)
atrLen = input.int(14, "ATR Length", minval = 1)
atrMult = input.float(1.5, "ATR Stop Multiplier", step = 0.1, minval = 0.5)
rrRatio = input.float(2.0, "Risk:Reward Ratio", step = 0.1, minval = 0.5)
// ── Indicators ───────────────────────────────────────────────────────────────
fastEma = ta.ema(close, fastLen)
slowEma = ta.ema(close, slowLen)
rsiVal = ta.rsi(close, rsiLen)
atrVal = ta.atr(atrLen)
// ── Plots ────────────────────────────────────────────────────────────────────
plot(fastEma, "Fast EMA", color = color.new(color.blue, 0), linewidth = 1)
plot(slowEma, "Slow EMA", color = color.new(color.orange, 0), linewidth = 2)
// ── Entry Conditions ─────────────────────────────────────────────────────────
longCond = ta.crossover(fastEma, slowEma) and rsiVal < rsiOB
shortCond = ta.crossunder(fastEma, slowEma) and rsiVal > rsiOS
// ── Entries ──────────────────────────────────────────────────────────────────
if longCond
strategy.entry("Long", strategy.long)
if shortCond
strategy.entry("Short", strategy.short)
// ── Exits: ATR Stop + R:R Take Profit ────────────────────────────────────────
longStop = strategy.position_avg_price - atrVal * atrMult
longTP = strategy.position_avg_price + atrVal * atrMult * rrRatio
shortStop = strategy.position_avg_price + atrVal * atrMult
shortTP = strategy.position_avg_price - atrVal * atrMult * rrRatio
strategy.exit("Long Exit", "Long", stop = longStop, limit = longTP)
strategy.exit("Short Exit", "Short", stop = shortStop, limit = shortTP)
// ── Alerts ───────────────────────────────────────────────────────────────────
alertcondition(longCond, "Long Signal", "EMA Crossover Long — {{ticker}} {{interval}}")
alertcondition(shortCond, "Short Signal", "EMA Crossover Short — {{ticker}} {{interval}}")How to Set Up TradingView Alerts
After adding the strategy to your chart, right-click any bar and select 'Add alert'. In the Condition dropdown, choose this script and then either 'Long Signal' or 'Short Signal'.
For webhook notifications (to a trading bot, Telegram, or Slack), enter your endpoint URL in the Webhook URL field. TradingView will POST the alert message to your URL every time a signal fires.
Tip
Set the alert to 'Once Per Bar Close' to avoid re-triggering on intra-bar price movements. This ensures you only get one alert per confirmed candle signal.
Recommended Settings by Market
These parameter combinations are starting points — always backtest on your specific market before live use.
- Crypto (BTC/ETH) 4H: Fast 9 / Slow 21 / RSI 14 / ATR 1.5× / R:R 2.0
- Crypto (altcoins) 1H: Fast 12 / Slow 26 / RSI 14 / ATR 2.0× / R:R 1.5
- US Stocks Daily: Fast 20 / Slow 50 / RSI 14 / ATR 1.0× / R:R 2.5
- Forex (majors) 1H: Fast 8 / Slow 21 / RSI 14 / ATR 1.5× / R:R 2.0
- Nifty/BankNifty 15m: Fast 9 / Slow 21 / RSI 14 / ATR 1.5× / R:R 1.5
Want a Custom Version? Use TradePilot
The above strategy is a solid starting template, but every trader has unique requirements. TradePilot can generate a custom Pine Script v6 strategy built around your specific indicators, timeframe, market, and risk rules — in plain English, no coding required.
Try it free at tradepilot.co.in or install the Chrome extension to generate strategies directly inside TradingView.
TradePilot Team
Traders and engineers building the fastest way to go from idea to live Pine Script strategy, right inside TradingView.