Trend FollowingBeginnerEMARSI

EMA Crossover Strategy

The EMA crossover strategy enters long when a fast EMA crosses above a slow EMA, and short when it crosses below. An RSI filter prevents entries in overbought/oversold conditions, reducing false signals in ranging markets. Stop loss and take profit are defined as ATR multiples so they adapt to current volatility.

Best for: Trending markets — crypto, indices, forex majorsTimeframes: 1H, 4H, Daily

Pine Script v6 — Complete Strategy Code

//@version=6
ema-crossover-pine-script.pine
//@version=6
strategy("EMA Crossover + RSI Filter", overlay=true,
     default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// ── Inputs ──
int   fastLen = input.int(defval=9,    title="Fast EMA Length", minval=1,   group="MA Settings")
int   slowLen = input.int(defval=21,   title="Slow EMA Length", minval=1,   group="MA Settings")
int   rsiLen  = input.int(defval=14,   title="RSI Length",      minval=1,   group="Filter")
int   rsiLow  = input.int(defval=40,   title="RSI Min (Long)",  minval=1,   group="Filter")
int   rsiHigh = input.int(defval=60,   title="RSI Max (Long)",  minval=1,   group="Filter")
int   atrLen  = input.int(defval=14,   title="ATR Length",      minval=1,   group="Risk")
float slMult  = input.float(defval=1.5, title="SL ATR Mult",   minval=0.1, group="Risk", step=0.1)
float tpMult  = input.float(defval=3.0, title="TP ATR Mult",   minval=0.1, group="Risk", step=0.1)

// ── Calculations ──
float fastEma = ta.ema(close, fastLen)
float slowEma = ta.ema(close, slowLen)
float rsi     = ta.rsi(close, rsiLen)
float atr     = ta.atr(atrLen)

// ── Conditions ──
bool longCond  = ta.crossover(fastEma, slowEma)  and rsi > rsiLow  and rsi < rsiHigh
bool shortCond = ta.crossunder(fastEma, slowEma) and rsi > rsiLow  and rsi < rsiHigh

// ── Visuals ──
plot(fastEma, "Fast EMA", color.new(color.aqua,   0), linewidth=2)
plot(slowEma, "Slow EMA", color.new(color.orange, 0), linewidth=2)
plotshape(longCond,  style=shape.triangleup,   location=location.belowbar, color=color.new(color.green, 0), size=size.small)
plotshape(shortCond, style=shape.triangledown, location=location.abovebar, color=color.new(color.red,   0), size=size.small)

// ── Orders ──
if longCond
    strategy.entry("Long", strategy.long)
    strategy.exit("XL", from_entry="Long",
         stop=close - atr * slMult, limit=close + atr * tpMult)
if shortCond
    strategy.entry("Short", strategy.short)
    strategy.exit("XS", from_entry="Short",
         stop=close + atr * slMult, limit=close - atr * tpMult)

// ── Alerts ──
alertcondition(longCond,  title="EMA Long",  message="EMA crossover long — {{ticker}} @ {{close}}")
alertcondition(shortCond, title="EMA Short", message="EMA crossover short — {{ticker}} @ {{close}}")

Copy and paste into TradingView Pine Editor, or generate a custom variation with AI.

How it works

Strategy logic, step by step

  1. 1

    Fast EMA (default 9) and slow EMA (default 21) are calculated on close.

  2. 2

    Long entry: fast EMA crosses above slow EMA AND RSI is between 40 and 60 (not overbought/oversold).

  3. 3

    Short entry: fast EMA crosses below slow EMA AND RSI is between 40 and 60.

  4. 4

    Stop loss: 1.5× ATR(14) below entry for longs, above entry for shorts.

  5. 5

    Take profit: 3× ATR(14) in the profit direction (2:1 R:R).

  6. 6

    Alerts fire on both entry conditions.

Parameters

Configurable inputs

ParameterDefault
Fast EMA Length9
Slow EMA Length21
RSI Length14
SL ATR Mult1.5
TP ATR Mult3.0

Ask the TradePilot copilot to backtest this and sweep the inputs to find the best combination for your asset and timeframe.

FAQ

EMA Crossover Strategy questions

What timeframe works best for EMA crossover?

1H and 4H tend to produce the cleanest signals on trending assets. Daily is good for swing trading. Avoid M1/M5 — too much noise for EMA crossover.

Why add an RSI filter to EMA crossover?

EMA crossover generates many false signals in ranging markets. The RSI filter between 40–60 ensures entries happen in neutral momentum — not when price is already extended.

How do I optimize the EMA periods?

Use TradePilot's Strategy Optimizer to test Fast EMA 5–15 and Slow EMA 15–50 on your specific asset. Rank by Profit Factor rather than Net Profit for more robust results.

Can I make this long-only?

Yes — open TradePilot chat, paste the script or describe the change: "make it long only". The inline editor will remove the short entry logic.

More strategies

Other Pine Script strategies

Want a custom version of this strategy?

Describe your modifications in plain English — different indicators, different risk parameters, long-only, multi-timeframe filter — and TradePilot generates the full Pine Script v6 code in seconds.