Moving Average Ribbon in Pine Script
Three or more moving averages of rising length drawn together, so their order, spacing and fanning show the trend's direction, maturity and compression at a glance.
Definition
A moving average ribbon is a set of averages of increasing length drawn on the same chart, 3 in the library's version (8, 21 and 55 period EMAs) and up to a dozen in some displays. The lines are read as a group rather than one at a time. When they are stacked in order, fastest on top in an uptrend or fastest at the bottom in a downtrend, the trend is aligned across horizons. When they braid, the horizons disagree and the market is in transition. It belongs to the moving averages family and extends the 2 line crossover to a whole band.
The ribbon works because different lengths answer different questions. The 8 period line tracks the last few sessions, the 21 the last month of an intraday chart, the 55 the longer swing. A trend that is strong across all of them stacks the lines and spreads them apart as momentum builds. As the trend tires, the fast lines fold back towards the slow ones and the ribbon compresses. That compression is the visible form of a range or a pause, and the next fan-out shows which way it resolved.
Popular sets are 8, 21 and 55 or 5, 8, 13, 21, 34, 55; the Fibonacci lengths are a convention rather than a discovery. The library's implementation draws 3 EMAs with 2 fills: the fast-to-mid band and the mid-to-slow band are green while the lines are stacked bullishly, red while stacked bearishly, and grey while mixed. It marks Long on the bar the bullish stack first forms and Short on the bar the bearish stack first forms, with an optional alert on each alignment.
On the chart a mature trend shows a wide ribbon of one colour with price riding the outer edge and pullbacks bouncing off the fast or mid line. A transition shows the ribbon narrowing to a thread and turning grey as the lines cross each other in sequence. Because the 3 lines have to sort themselves fully, the alignment signal arrives later than a single crossover and fires far less often; the trade-off is that the mixed state, which a crossover cannot express, is where most losing crossover trades happen.
Reading the chart
A ribbon is read by its shape: its order, its width and how price sits against it.
- Check the order of the lines. Fast above mid above slow is a bullish stack; fast below mid below slow is bearish; anything else is mixed and the library's fill turns grey.
- Measure the width. A fanning ribbon with the lines separating shows a trend gaining momentum; a ribbon narrowing to a thread shows a trend losing it or a range forming.
- Watch where price trades relative to the band. In a strong trend price rides outside the fast line and pullbacks stop at the fast or mid line; a close through the slow line is a warning.
- Wait for full alignment before calling a new trend. The library's script only marks Long or Short on the bar all 3 lines sort themselves, which is later than a single cross but far cleaner.
- Read compression as preparation. When the lines squeeze together and turn grey, the market is deciding; the first fan-out after a squeeze is the strongest alignment reading.
- Confirm with the higher timeframe. A bullish stack on a 15 minute chart inside a bearish daily ribbon is a counter-trend bounce and deserves a smaller size and a faster exit.
The maths
Each line is an exponential average of the close at its own length, and the state is the ordering of the 3 values on the bar.
Adding lines does not change the logic: a 6 line ribbon is aligned when every adjacent pair is in order, and the width is the distance between the outermost lines.
Width can be measured in ATR units or as a percentage of price to compare compression across instruments, which is how a squeeze scan on ribbons is built.
Simple averages give a smoother, later ribbon; exponential averages are the convention because the fast lines are meant to react.
The library's script evaluates alignment on confirmed bars only, so a stack that forms and breaks inside one bar never prints a mark.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Fast length | 8 | A shorter fast line hugs price and breaks the stack on small pullbacks; a longer one keeps the stack intact through noise at the cost of a later signal. |
| Mid length | 21 | The mid line is the pullback level in a trend; moving it changes where dips are expected to hold. |
| Slow length | 55 | A longer slow line demands a bigger move before alignment and produces fewer, larger regimes. |
In practice
- As a trend-state display: keep the ribbon on every chart and read the colour before anything else. Green marks the long side, red the short side, grey a wait.
- As an alignment entry: the library's script marks Long on the first bar of a bullish stack. The rule enters there or on the first pullback to the mid line after it, with a stop under the slow line.
- As a compression scan: search a watchlist for ribbons narrower than they have been for 20 bars. A narrow, grey ribbon is a pending decision, and the fan-out is the trade.
- As a trailing exit: the rule holds a trend trade while the stack holds and exits when the fast line closes back inside the ribbon or the stack breaks. The trade ends when the trend does.
- As a filter for faster triggers: the filter passes a 9/21 cross or an oscillator trigger only in the direction of the ribbon's colour, which removes counter-trend entries mechanically.
- As a maturity gauge: a ribbon at its widest for months is a stretched trend, which is where scale-out rules apply rather than adding.
Where it goes wrong
- Reading a single line of the ribbon. The information is in the order and width of the group; one line crossing another is not an alignment signal.
- Trading the grey state. Mixed lines mean the horizons disagree and most signals there fail; wait for the stack or trade the range with other tools.
- Chasing the first alignment far from the ribbon. If price is already several ATR above the fast line at the signal, the entry is late; wait for the pullback.
- Confusing compression with reversal. A narrow ribbon says a decision is coming, not which way; let the fan-out choose the side.
Compared with
Moving Average Crossover. A crossover has 2 states, fast above or below slow, and signals on every cross. The ribbon adds a third, mixed state and signals only on full alignment, so it fires less often and shows the transitions a crossover hides.
Ichimoku Cloud. The cloud is also a band read by colour and thickness, but it is built from midpoints of ranges and shifted forward in time. A ribbon is plain averages on the current bar, easier to tune and with no displacement.
Volatility Squeeze. A squeeze measures compression with Bollinger Bands inside Keltner Channels and says nothing about direction. Ribbon compression is the same idea read from averages, and its fan-out shows the direction as the lines sort themselves.
Moving Average. One average gives a level and a slope on one horizon. A ribbon gives the relationship between 3 horizons, which is what tells you whether a pullback is a dip in a trend or the start of a transition.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
How many lines should a ribbon have?
Three is enough to read order, width and the mixed state, which is why the library uses 8, 21 and 55. More lines make the fan prettier and the compression easier to see, but they add nothing to the logic and slow the alignment signal, because every adjacent pair has to sort itself.
Why are the lengths 8, 21 and 55?
They are Fibonacci numbers, and that is a convention rather than a property of markets. What matters is the spacing: each line about 2.5 times the one before, so the 3 horizons are distinct. 5, 10 and 20 or 10, 20 and 50 read the same way on the chart.
What does it mean when the ribbon goes grey?
That the 3 averages are not in order: the fast line has crossed back through the mid line, or the mid through the slow. The horizons disagree, which is the state of a range or a reversal in progress. Most losing trend trades are taken in the grey state, so the simplest rule is to wait.
Is the ribbon better than a single crossover?
It is slower and cleaner. A 9/21 cross fires on every wobble and gives the earliest entry; the ribbon waits for 3 lines to align and skips most of the whipsaw. Traders who want early entries use the cross and the ribbon's colour as the filter, which combines the strengths of both.
How do I trade ribbon compression?
As a pending breakout. When the lines squeeze together and turn grey, mark the range and wait for the first bar on which the ribbon fans out and the stack forms, which is the library's Long or Short mark. The rule enters in that direction with the stop beyond the far side of the compressed ribbon.
Does the ribbon repaint?
No. Each line is an average of closed bars and the alignment is checked on confirmed bars only, so a mark never appears and then vanishes. The current bar's lines move while the bar forms, which is why the library's script waits for the close before it marks a stack.