RSI + EMA Combo — Pine Script v5 strategy
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
| Parameter | Value | Role |
|---|---|---|
RSI length | 14 | Second argument of ta.rsi(close, 14); number of bars used to build rsiVal. |
EMA length | 50 | Second argument of ta.ema(close, 50); smoothing length used to build ema50. |
RSI entry level | 30 | Level passed to ta.crossover(rsiVal, 30); the entry needs the RSI to move from at or below 30 to above 30. |
Trend filter | close > ema50 | Second half of the entry condition; the close must be strictly above the EMA on the same bar. |
RSI exit level | 70 | First half of the exit condition; rsiVal > 70 closes the position. |
EMA exit condition | close < ema50 | Second half of the exit condition, joined to the RSI level with the or operator. |
overlay | true | Argument of strategy(); the script is drawn over the price chart. |
Entry order | strategy.entry("Long", strategy.long) | Opens one long position under the order id Long. |
Exit order | strategy.close("Long") | Closes the position with the same id; no stop or limit price is passed. |
Commission and slippage | not set | strategy() declares neither commission_type nor slippage, so both stay at their default. |
Quantity | not set | strategy.entry has no qty argument, so the default order size of the strategy is used. |
Version | v5 | The code declares //@version=5 and uses the ta.* functions of that version. |
Known limits of this rule
- The rule only goes long, so it ignores down moves that appear while no position is open, and it cannot profit from a falling market.
- The RSI cross and the EMA filter are momentum conditions, so in a sideways range the RSI can cross the level several times in a few bars and produce repeated entries and exits.
- Commission, slippage, order size, stop loss and take profit are not configured in the code, so the chart output does not reflect trading costs or risk per trade.
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.
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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.