Skip to main content

RSI + EMA Combo — Pine Script v5 strategy

MOMENTUMRSIEMAPine Script v5

This script combines the RSI and a 50-period EMA into a single long-only entry rule. It buys when the RSI crosses above 30 while price is still above the EMA, and closes the position when the RSI exceeds 70 or the close drops below the EMA.

Full code (free to copy)

//@version=5
strategy("RSI + EMA Combo", overlay=true)
rsiVal = ta.rsi(close, 14)
ema50 = ta.ema(close, 50)
if (ta.crossover(rsiVal, 30) and close > ema50)
    strategy.entry("Long", strategy.long)
if (rsiVal > 70 or close < ema50)
    strategy.close("Long")

How the script works

The script calls ta.rsi(close, 14) and ta.ema(close, 50) once per bar and stores the results in rsiVal and ema50. Those two series are the whole indicator layer: the RSI measures recent close changes over 14 bars, and the EMA smooths close over 50 bars. The strategy() declaration sets overlay=true so both are plotted on the price chart.

The single if block evaluates ta.crossover(rsiVal, 30) and close > ema50 together with the and operator. ta.crossover is true only on the bar where the RSI moves from at or below 30 to above 30, so a flat RSI reading above 30 never re-triggers it. Only when that cross and the EMA filter agree does strategy.entry("Long", strategy.long) open a long position.

The second if block uses or: it fires when rsiVal > 70 or close < ema50. Either condition alone is enough, and the test is evaluated on each bar while the position is open. When true, strategy.close("Long") sends a market order that closes the whole long position. No stop, target or trail is defined in the code, so the only exit is this rule.

Parameters in the code

ParameterValueRole
RSI length14Second argument of ta.rsi(close, 14); number of bars used to build rsiVal.
EMA length50Second argument of ta.ema(close, 50); smoothing length used to build ema50.
RSI entry level30Level passed to ta.crossover(rsiVal, 30); the entry needs the RSI to move from at or below 30 to above 30.
Trend filterclose > ema50Second half of the entry condition; the close must be strictly above the EMA on the same bar.
RSI exit level70First half of the exit condition; rsiVal > 70 closes the position.
EMA exit conditionclose < ema50Second half of the exit condition, joined to the RSI level with the or operator.
overlaytrueArgument of strategy(); the script is drawn over the price chart.
Entry orderstrategy.entry("Long", strategy.long)Opens one long position under the order id Long.
Exit orderstrategy.close("Long")Closes the position with the same id; no stop or limit price is passed.
Commission and slippagenot setstrategy() declares neither commission_type nor slippage, so both stay at their default.
Quantitynot setstrategy.entry has no qty argument, so the default order size of the strategy is used.
Versionv5The code declares //@version=5 and uses the ta.* functions of that version.

Known limits of this rule

Before you trust it: test it on data it never saw

A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.

Get the free Lab →

FAQ

Does it work on crypto?

The code only uses close, so it can run on crypto pairs as well as on any other symbol. The behaviour changes with the timeframe because the 14 and 50 lengths stay fixed.

How do I add a stop loss?

Add a strategy.exit call with a stop argument, or send a stop price with the exit order. You can then replay the modified rule on a period you have never seen in Strategy Arena Lab, which is free.

Other free Pine Script v5 strategies

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumSuperTrendStochastic RSIATR BreakoutVWAP Bounce
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.