Skip to main content

EMA Crossover 9/21 — Pine Script v5 strategy

TRENDEMAPine Script v5

This strategy computes a nine-period EMA and a twenty-one-period EMA of the close, then opens a long position when the fast one crosses above the slow one. It closes that long position when the fast EMA crosses back below the slow EMA.

Full code (free to copy)

//@version=5
strategy("EMA Crossover 9/21", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if (ta.crossover(fast, slow))
    strategy.entry("Long", strategy.long)
if (ta.crossunder(fast, slow))
    strategy.close("Long")

How the script works

The script opens with strategy("EMA Crossover 9/21", overlay=true), which names the strategy and draws it directly on the price chart. It then computes fast = ta.ema(close, 9), a nine-period exponential moving average of the close, and slow = ta.ema(close, 21), the twenty-one-period version of the same average. Both series are recalculated on every bar and read the close price only.

The entry rule is a single condition: ta.crossover(fast, slow). That function is true only on the bar where the fast EMA moves from below the slow EMA to above it. When it is true, strategy.entry("Long", strategy.long) opens a long position tagged with the identifier "Long". No trend filter, volume filter or volatility condition is applied before that call.

The exit rule is the opposite condition: ta.crossunder(fast, slow), true on the bar where the fast EMA crosses back below the slow EMA. The script then calls strategy.close("Long"), which closes the long position carrying that identifier. There is no stop loss, take profit or trailing stop, so the opposite cross is the only exit, and a position open at the end of the test period stays open.

Parameters in the code

ParameterValueRole
fast length9Period of the fast average in fast = ta.ema(close, 9).
slow length21Period of the slow average in slow = ta.ema(close, 21).
sourceclosePrice series fed to both ta.ema calls; it is hardcoded, not an input.
overlaytrueSet in the strategy() declaration so the strategy is drawn on the price chart.
strategy titleEMA Crossover 9/21First argument of strategy(), the name the platform displays for this script.
entry identifierLongString given to strategy.entry and reused later by strategy.close.
entry directionstrategy.longSecond argument of strategy.entry, opens the position on the long side.
exit callstrategy.close("Long")Closes the whole Long position on the opposite cross.
order sizenot set (Pine default)strategy.entry receives no qty argument, so the default order size of the platform is used.

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 prices and EMAs, so it can be applied to any symbol the platform offers. Whether the crossover suits a given market has to be checked, and you can replay it on a period you have never seen in Strategy Arena Lab, which is free.

How do I add a stop loss?

Add a stop argument to strategy.entry, or call strategy.exit with a stop price after the entry. The current script contains neither, so only the opposite cross closes the position.

Other free Pine Script v5 strategies

RSI Mean ReversionEMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumSuperTrendStochastic RSIATR BreakoutVWAP BounceRSI + EMA Combo
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.