How we check our EMA 9/21 targets against Pine Script

A target is only useful if it is computed the same way every time. How we rebuilt a popular EMA 9/21 target method, what broke along the way, and how we test it.

·6 min read

Draft for review — not published yet

What the method does

The EMA 9/21 target method follows two exponential moving averages of the close: a fast one over 9 bars and a slow one over 21. When the fast average crosses above the slow one, it projects two reference levels above the price, T1 and T2. When it crosses below, it projects two below.

The distance comes from the instrument’s own history. Looking back 501 bars from the crossover, the method compares two things: the average close at past crossovers, and the most extreme close that held on the right side of both averages. Half of that distance, added to the crossover’s opening price, is T1; all of it is T2.

It started as an open-source Pine Script indicator, and many traders check their charts against it. So our version has one job: give the same targets, to the cent, on the same bars.

Why “close enough” is not enough

Two implementations can both look right and still disagree. In August, our targets for one stock with a short trading history were about 1.3% away from the reference. The two downside targets matched exactly; the two upside ones did not.

The cause was the warm-up. In Pine, a 21-bar EMA does not exist until there are 21 bars, and it starts from a simple average of those first closes. Our first version started both averages on the very first bar. For a few bars they were little more than copies of the first prices, and they “crossed” each other before either meant anything. Those phantom crossovers went into the average and moved the targets.

With seven years of history the effect was a rounding error. On a stock with one year of history it was large. The numbers were plausible and consistent with themselves. Only an outside reference caught the problem, and that is why we test against one.

The rules we follow now

  • Same warm-up. No EMA value until the period is complete, seeded with a simple average, as Pine’s ta.ema does.
  • Same crossovers. A cross needs both averages to exist on both bars.
  • Same window. 501 bars back from the crossover, on every timeframe.
  • Confirmed bars only. A crossover on a bar that is still forming does not move the targets.
  • Enough history. Each timeframe gets enough bars to fill the window: years of daily bars, and longer weekly and monthly series where the data exists.
  • One engine. The lines drawn on the chart and the crossovers come from the same arrays. One formula per indicator, used everywhere it appears.

How we test it

We take the same bars the reference uses, run the reference logic and ours side by side, and compare every target. On 4-hour bars for NVDA, AAPL, SPY, TSLA and BTC, the deviation is 0.00%.

The limits we know about:

  • Different price feeds. In crypto, one chart may show a composite index and another a single exchange. Targets then differ by a fraction of a percent: the method is the same, the prices are not.
  • Shorter history. Where our history starts later than the reference’s, long-horizon monthly targets cannot match.

The same discipline applies to the rest of the terminal. In an August 2026 audit we recomputed 151 indicator values (moving averages, RSI, ATR, Bollinger bands, ADX, pivots and more) with an independent implementation written from scratch. All 151 matched to rounding.

What a target is, and what it isn’t

A target is a reference level computed from history. It tells you how far price ran after similar crossovers in the past. It does not say this one will, and it is not a recommendation to buy or sell. TREDIA shows targets next to the trend, volume profile and the other levels on the chart, so you can weigh them against everything else.

For research and education. Not investment advice.

More method notes

Every number in TREDIA carries its source, its date and its method.

esc
Request early access

Request early access

TREDIA is in private beta. Leave your email and we will send your invitation as new seats open.

What best describes you?

We use your email to manage your access request, as described in our Privacy Notice.

Check your inbox

We sent you a link to confirm your email. Once confirmed, you are on the list.