EMA Ribbon 8/21/55 — Pine Script v5 strategy
This script computes three exponential moving averages on close and enters long only when they are stacked in order. It exits the position when the fast average crosses back under the medium one.
Full code (free to copy)
//@version=5
strategy("EMA Ribbon", overlay=true)
ema1 = ta.ema(close, 8)
ema2 = ta.ema(close, 21)
ema3 = ta.ema(close, 55)
if (ema1 > ema2 and ema2 > ema3)
strategy.entry("Long", strategy.long)
if (ema1 < ema2)
strategy.close("Long")How the script works
The first lines compute three exponential moving averages on close: ta.ema(close, 8), ta.ema(close, 21) and ta.ema(close, 55), stored in ema1, ema2 and ema3. The script runs on every bar, so each value updates at the close of that bar. These three series are the only inputs of the rule: no volume, no other indicator and no higher timeframe data is read.
The entry block tests ema1 > ema2 and ema2 > ema3, which means the fast average sits above the medium one, and the medium above the slow one. When both comparisons are true on a bar, the script calls strategy.entry with the id Long and direction strategy.long. The order is filled at the next bar open by default, and the default pyramiding setting keeps a single long position.
The exit block checks a single condition: ema1 < ema2. As soon as the fast EMA drops below the medium EMA, strategy.close is called with the id Long and the position is closed. The test does not require ema2 to be above ema3, so the exit can fire even while the three averages remain ordered. The script defines no stop loss, no take profit and no short side.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
ema1 | ta.ema(close, 8) | Fast exponential moving average on close, the first series compared in both blocks. |
ema2 | ta.ema(close, 21) | Medium exponential moving average, it must sit below ema1 to allow the entry and above ema1 to allow the exit. |
ema3 | ta.ema(close, 55) | Slow exponential moving average, it only takes part in the entry filter. |
entry condition | ema1 > ema2 and ema2 > ema3 | Both comparisons must be true on the same bar before an order is sent. |
entry order | strategy.entry("Long", strategy.long) | Submits a long market order with the id Long while the alignment holds. |
exit condition | ema1 < ema2 | Closes the open long as soon as the fast average crosses below the medium one. |
exit order | strategy.close("Long") | Closes the position attached to the id Long, at the next bar open by default. |
overlay | true | The strategy is drawn on the price chart instead of a separate pane. |
stop loss and take profit | none | No risk exit is coded; the only exit is the EMA condition. |
short side | none | No short entry is coded, the strategy is long only. |
Known limits of this rule
- Everything is evaluated on bar close, so a move that reverses before the bar ends is not seen and the entries and exits land after the turn rather than at it.
- In a range the three averages flatten and cross repeatedly, which produces entries near the middle of the range followed by quick exits once the fast average slips back.
- Commissions, spread and slippage are not modelled in the script and no stop loss is defined, so a raw backtest reads better than a funded account would.
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 reads close and the ta.ema function, so it can be applied to crypto pairs, forex or indices on any timeframe. Behaviour differs by market, 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?
The script has no stop: the only exit is ema1 < ema2. You can add strategy.exit("Exit", "Long", stop=...) after the entry call, or extend the existing exit block with a second closing condition.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.