EMA Crossover 9/21 — Pine Script v5 strategy
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
| Parameter | Value | Role |
|---|---|---|
fast length | 9 | Period of the fast average in fast = ta.ema(close, 9). |
slow length | 21 | Period of the slow average in slow = ta.ema(close, 21). |
source | close | Price series fed to both ta.ema calls; it is hardcoded, not an input. |
overlay | true | Set in the strategy() declaration so the strategy is drawn on the price chart. |
strategy title | EMA Crossover 9/21 | First argument of strategy(), the name the platform displays for this script. |
entry identifier | Long | String given to strategy.entry and reused later by strategy.close. |
entry direction | strategy.long | Second argument of strategy.entry, opens the position on the long side. |
exit call | strategy.close("Long") | Closes the whole Long position on the opposite cross. |
order size | not set (Pine default) | strategy.entry receives no qty argument, so the default order size of the platform is used. |
Known limits of this rule
- Exponential moving averages are lagging, so an entry arrives after part of a move has already happened, and in a range the two lines cross again and again, producing whipsaw entries and exits.
- The code has no stop loss and no take profit: a position is closed only by the opposite cross, so an adverse move is carried until that cross appears.
- Commissions, spread and slippage are not modelled in the script, and the fixed lengths are not adapted to the instrument or timeframe being traded.
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 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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.