Skip to main content

EMA Ribbon 8/21/55 — Pine Script v5 strategy

TRENDEMAPine Script v5

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

ParameterValueRole
ema1ta.ema(close, 8)Fast exponential moving average on close, the first series compared in both blocks.
ema2ta.ema(close, 21)Medium exponential moving average, it must sit below ema1 to allow the entry and above ema1 to allow the exit.
ema3ta.ema(close, 55)Slow exponential moving average, it only takes part in the entry filter.
entry conditionema1 > ema2 and ema2 > ema3Both comparisons must be true on the same bar before an order is sent.
entry orderstrategy.entry("Long", strategy.long)Submits a long market order with the id Long while the alignment holds.
exit conditionema1 < ema2Closes the open long as soon as the fast average crosses below the medium one.
exit orderstrategy.close("Long")Closes the position attached to the id Long, at the next bar open by default.
overlaytrueThe strategy is drawn on the price chart instead of a separate pane.
stop loss and take profitnoneNo risk exit is coded; the only exit is the EMA condition.
short sidenoneNo short entry is coded, the strategy is long only.

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 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

RSI Mean ReversionEMA Crossover 9/21Bollinger 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.