Concept 33 / 52·Trend·Moving averages
‹ Moving Average RibbonMulti-Timeframe Trend Filter ›

Moving Average Slope in Pine Script

EMA slope · MA angle · slope filter · moving average angle

The change in a moving average over a few bars, measured in ATR units, so a trend only counts when the average is climbing or falling faster than a threshold.

Reads best on: Any liquid market, from 15 minute charts to weekly, because the ATR normalisation makes the threshold portable. It shines on daily charts of trending instruments and as a permission filter on intraday charts.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

Moving average slope measures how fast an average is rising or falling. Instead of asking whether price is above the line, it asks whether the line itself is going anywhere. The library's version takes a 50 period EMA, subtracts its value 5 bars ago, and divides by the 14 period ATR so the result is in units of normal bar range rather than in price. A slope above 0.5 ATR is an uptrend, below −0.5 a downtrend, and the zone between is flat. It belongs to the moving averages family and is used mostly as a filter.

The measure works because the slope of an average is the cleanest available definition of a trend. A rising 50 period EMA means the average price of the last 50 bars is higher than it was 5 bars ago, which cannot happen without a persistent drift. Expressing it in ATR units makes the threshold portable: 0.5 ATR of drift over 5 bars means the same thing on a currency pair and on a crypto coin, and on a 5 minute chart and a daily. A raw price slope would need a different threshold for every instrument.

There is no single author; the idea is as old as moving averages, and the ATR normalisation follows the practice of measuring everything in units of volatility. Angles in degrees, which some platforms show, depend on the chart's scale and are not comparable. The library's implementation plots the slope as columns in a pane, green above the threshold, red below the negative threshold and grey between, with dashed threshold lines and a dotted zero line. It marks Long when the slope crosses above the threshold and Short when it crosses below the negative threshold.

On the chart the columns behave like a slow momentum reading. In a trend they stay one colour for a long run and grow as the move accelerates; as the trend matures they shrink towards the threshold while price still makes new highs, which is an early warning. In a range the columns sit grey around zero and the average is flat, which is the state the filter exists to detect. Crossings of the threshold arrive a few bars after the trend visibly begins, and the flat zone means a fading trend turns grey before it turns the other colour.

02

Reading the chart

The slope is a number with a threshold, so reading it is about the zone it is in and the direction it is moving.

  1. Choose the average and the lookback. A 50 period EMA over 5 bars is the library's default; a 20 over 3 gives a faster read, a 200 over 10 a regime read.
  2. Look at the zone. Green columns above the threshold mean an uptrend strong enough for trend rules; red below the negative threshold a downtrend; grey between means no trend on this length.
  3. Note the direction of the columns within the zone. Columns growing away from zero show acceleration; columns shrinking towards the threshold show a trend losing pace even while price holds.
  4. Treat the threshold crossing as the event. The library's script marks Long when the slope crosses above the threshold and Short when it crosses below the negative one, on confirmed bars only.
  5. Compare the slope with price. New highs in price with a lower slope peak is a divergence of drift, and it often precedes the return to grey.
  6. Use it with an entry tool. The slope sets whether trend rules apply and in which direction; a cross, a pullback or an oscillator trigger sets the timing.
03

The maths

The slope is the change in the EMA over the lookback, divided by the ATR so it reads in units of normal bar range.

EMAt=α Ct+(1−α) EMAt−1\mathrm{EMA}_t = \alpha \, C_t + (1 - \alpha) \, \mathrm{EMA}_{t-1}
α=2n+1\alpha = \frac{2}{n + 1}
Slopet=EMAt−EMAt−kATRt(14)\mathrm{Slope}_t = \frac{\mathrm{EMA}_t - \mathrm{EMA}_{t-k}}{\mathrm{ATR}_t(14)}
Longt={1Slopet−1≤θ∧Slopet>θ0otherwise\mathrm{Long}_t = \begin{cases} 1 & \mathrm{Slope}_{t-1} \le \theta \land \mathrm{Slope}_t > \theta \\ 0 & \text{otherwise} \end{cases}
Shortt={1Slopet−1≥−θ∧Slopet<−θ0otherwise\mathrm{Short}_t = \begin{cases} 1 & \mathrm{Slope}_{t-1} \ge -\theta \land \mathrm{Slope}_t < -\theta \\ 0 & \text{otherwise} \end{cases}
CtC_tthe close of bar t (the source)
nnthe EMA length, 50 by default
α\alphathe EMA smoothing factor
EMAt\mathrm{EMA}_tthe exponential average at bar t
kkthe slope lookback in bars, 5 by default
ATRt(14)\mathrm{ATR}_t(14)the 14 period average true range at bar t
Slopet\mathrm{Slope}_tthe slope in ATR units
θ\thetathe threshold, 0.5 ATR units by default

The ATR length is fixed at 14 in the library's script; the EMA length, the lookback and the threshold are the inputs.

If the ATR is 0, as on a flat synthetic series, the slope is set to 0 rather than left undefined.

A slope in degrees depends on the pixel scale of the chart and cannot be compared across instruments or zoom levels; ATR units can.

The same measure applies to any average: an SMA slope is smoother and later, an HMA slope faster and noisier.

04

Inputs that matter

InputTypicalWhat changing it does
EMA length50A shorter average has a steeper, noisier slope that crosses the threshold often; a longer one moves slowly and marks only sustained trends.
Slope lookback5 barsA longer lookback smooths the reading and delays it; a shorter one reacts within a bar or 2 and flickers around the threshold.
Threshold0.5 ATR unitsRaising it demands a steeper trend before a signal, which cuts whipsaw and skips gentle trends; lowering it lets shallow drifts count as trends.
05

In practice

  • As a trend filter: the filter keeps long entries from any other tool only while the slope is above the threshold, and shorts only below the negative one. This is the main use and removes most range trades.
  • As a no-trade zone: the grey band is a range regime, where reversion rules apply and trend rules stand down.
  • As an early exit: a slope shrinking towards the threshold while price still holds its highs is the trend losing drift; exit rules tighten stops or take a partial exit before the colour changes.
  • As a strength ranking: compare slopes across a watchlist in ATR units to find the instruments trending hardest, a comparison raw price slopes cannot make fairly.
  • As a standalone rule: the library's script marks Long and Short on the threshold crossings, which makes a slow, low-frequency trend system for higher timeframes.
  • As a confirmation for crosses: a 9/21 cross that occurs while the 50 period slope is already past the threshold is a continuation; the same cross in the grey zone is a coin flip.
06

Where it goes wrong

  • Measuring the slope in price or degrees. Both change with the instrument and the chart scale; use ATR units so one threshold works everywhere.
  • Trading the threshold crossing as a timing signal on low timeframes. It lags the start of the move by several bars; use it to permit trades and something faster to time them.
  • Setting the threshold too low. At 0.1 ATR nearly every drift counts as a trend and the filter stops filtering; 0.5 is a fair start and 1.0 is strict.
  • Ignoring the grey zone. A trader who only looks for green and red misses that the flat state is the most useful output of the tool.
07

Compared with

ADX & DMI. ADX measures trend strength without direction, from the balance of up and down moves, and needs the DI lines for the side. The slope gives strength and direction in one number and is easier to reason about, but it depends on the chosen average.

Hull Moving Average. The HMA's colour flips on any change of direction of a fast, lag-corrected line. The slope filter measures how steep an ordinary EMA is and stays grey until the steepness passes a threshold, so it is slower and far less flickery.

Rate of Change. ROC is the percentage change of price itself over n bars, which is noisy and not volatility-adjusted. The slope applies the same idea to a smoothed line and divides by ATR, which turns a momentum reading into a trend regime reading.

Moving Average. A plain average is read by which side price is on, which is a level test. The slope ignores the level and reads only the direction and pace of the line, so it filters sideways markets that a level test would still call an uptrend.

08

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.

Open Moving Average Slope on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • Why measure the slope in ATR units?

    So one threshold works on every instrument and timeframe. A 50 point rise in an average means nothing on its own; a rise of half a normal bar's range over 5 bars means the same thing on an index, a currency pair and a coin. Degrees depend on the chart scale and are not comparable at all.

  • What threshold should I use?

    0.5 ATR units over a 5 bar lookback is the library's default and a fair middle. Raise it towards 1.0 if you only want strong trends and can accept missing gentle ones; lower it towards 0.3 on higher timeframes, where averages move more slowly relative to the ATR.

  • Is the slope filter a complete rule by itself?

    It can be, and the library's script marks Long and Short on the threshold crossings. On daily and weekly charts that makes a slow trend system with few trades. On intraday charts the crossings lag too much to time entries, so the slope is better used to permit trades from a faster tool.

  • How is this different from ADX?

    ADX measures how one-sided the recent bar ranges are, with no direction, and DI lines supply the side. The slope reads the drift of an average directly and carries its sign. In practice they agree in strong trends; the slope goes flat sooner in fading trends, and ADX is less tied to one average length.

  • What does a shrinking slope with rising prices mean?

    That the drift is slowing while the level still climbs. The average needs steady gains to keep its slope, and a series of smaller pushes lets the columns fall towards the threshold. It is the trend version of momentum divergence and usually precedes the grey zone, which is why it is a good place to tighten stops.

  • Does the slope indicator repaint?

    No. The EMA, the ATR and the difference are all computed on closed bars, and the threshold crossings are marked on confirmed bars only. The current bar's column moves while the bar forms, so a crossing seen mid-bar is not a signal until the bar closes.