Multi-Timeframe Trend Filter in Pine Script
A moving average from a higher timeframe drawn on a lower chart, so every intraday decision is checked against the direction of the daily or weekly trend before it is taken.
Definition
A multi-timeframe trend filter reads the trend on a higher timeframe and shows it on the chart you trade from. The usual form is a daily or weekly moving average drawn as a stepped line on a 5 minute to 1 hour chart. Trades are allowed only on the side of that line: longs above the higher-timeframe average, shorts below. It belongs to the trend filters family, and it is the mechanical form of the old advice to trade with the larger trend.
The filter works because trends on a higher timeframe last longer and carry more money than the swings on a lower one. An intraday setup against the daily trend is fighting flows it cannot see on its own chart; the same setup with the daily trend behind it has the larger move as a tailwind. The filter does not improve the entry itself. It removes the half of the entries that were always low quality, which raises the average trade without adding a single new entry.
There is no single author; the practice of top-down analysis is as old as charting. The common choices are the daily 50 or 200 period EMA on intraday charts and the weekly 20 on daily charts. The library's implementation requests the previous completed higher-timeframe value of an EMA (daily, 50 by default), draws it as a step line coloured by which side price is on, tints the background the same way, and marks Long or Short when the close crosses the line on a confirmed bar.
On the chart the line moves in steps: it holds one value for the whole of each higher-timeframe bar and jumps when that bar completes. Price can spend a whole session on one side of it, which is the point; the filter changes state a few times a month, not a few times a day. Because the script uses the last completed higher-timeframe bar rather than the forming one, the line never redraws its history, at the cost of lagging the live higher-timeframe average by one of its bars.
Reading the chart
The filter is a line and a side, so reading it is about choosing the right higher timeframe and respecting what the side means.
- Pick the higher timeframe as 4 to 6 steps above the chart. Daily for 15 minute and 1 hour charts, weekly for daily charts, 4 hour for 5 minute charts. Too close and it adds nothing; too far and it never changes.
- Pick the average. A 50 period EMA on the higher timeframe is the library's default; the 200 gives a slower regime line, the 20 a faster one that flips more often.
- Read the side. Price above the line means the higher-timeframe trend is up and only longs are permitted; below it, only shorts. The library's background tint shows the state at a glance.
- Accept the steps. The line jumps once per higher-timeframe bar and stays flat between; a step that lands above price after a daily close changes the permitted side for the whole next day.
- Treat crosses as regime changes, not entries. The script marks Long and Short when the close crosses the line, and those marks are best read as permission switching sides.
- Combine it with an entry tool on the chart timeframe. A 9/21 cross, a pullback to VWAP or an oscillator signal in the permitted direction is the trade; the filter alone is not.
The maths
The script computes an EMA on the higher timeframe, takes its value on the last completed bar of that timeframe, and holds it on every chart bar until the next one completes.
Requesting the current higher-timeframe bar's value would make the line move during the day and redraw when the bar closed. The library's script requests the previous completed bar with lookahead off, so the plotted history is exactly what a live chart showed.
The cost of that choice is one higher-timeframe bar of lag: on a 15 minute chart the daily filter reflects yesterday's close, not today's forming bar.
Any average or indicator can be used the same way; a higher-timeframe SuperTrend or ADX makes a filter with the same step behaviour.
The chart timeframe must be lower than the filter timeframe. Requesting a daily value on a weekly chart returns something meaningless.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Higher timeframe | D (W on daily charts) | A closer timeframe flips the permitted side more often and adds less; a more distant one holds its side for weeks and forbids whole sessions of trading. |
| EMA length | 50 | A shorter average follows the higher-timeframe swings and switches sides sooner; the 200 marks the regime and switches a few times a year. |
In practice
- As a permission filter: the filter allows long entries from any intraday tool only while price is above the higher-timeframe average, and shorts only below it. This is the main use and cuts most counter-trend trades.
- As a background regime: the library's tint colours the whole chart by the higher trend, so a glance shows which playbook applies before a single setup is judged.
- As a dynamic level: the stepped daily 50 on a 15 minute chart is a level intraday traders react to, and a first test of it in the direction of the trend is a pullback entry.
- As a session bias: read the side at the open and keep it for the session, because the line only steps once a day. Bias flips mid-session are rare and worth noticing.
- As a regime alert: the script's Long and Short marks fire on the cross of the filter, which alerts a watchlist to a change of higher-timeframe side without watching each chart.
- As one of 2 filters: a daily 50 and a weekly 20 on the same chart, with trades taken only when both agree, gives a strict top-down system for swing entries.
Where it goes wrong
- Choosing a timeframe too close to the chart. A 1 hour average on a 15 minute chart flips constantly and filters nothing; go 4 to 6 steps up.
- Reading the forming higher-timeframe bar. A filter built on the live daily value redraws at the close and shows a history no one could have traded; use the previous completed bar, as the library's script does.
- Treating the cross as the entry. Price crossing a daily average on a 5 minute chart is a permission change; the entry comes from the chart-timeframe tool in the new direction.
- Applying the filter on a chart above its timeframe. A daily filter on a weekly chart has no meaning, and the script will not produce anything useful.
Compared with
Moving Average. A moving average on the chart's own timeframe smooths the swings you are trading; a higher-timeframe average smooths a horizon 4 to 6 steps up and steps once per higher bar. The first times entries, the second decides whether to take them.
Golden Cross & Death Cross. The golden cross is a regime label on the daily chart itself, crossing a few times a decade. The higher-timeframe filter brings a regime line onto the intraday chart and switches sides monthly, which is a working filter rather than a headline.
SuperTrend. SuperTrend on the chart timeframe is a trailing line that flips with price; the same tool requested from a higher timeframe is a filter with the same step behaviour as the EMA version. Either works; the EMA is the simpler reference.
ADX & DMI. ADX filters by trend strength on the same chart and says nothing about the larger trend. The multi-timeframe filter says only which way the larger trend points; the two are often combined, strength on the chart and direction from above.
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
Which higher timeframe should I use?
About 4 to 6 steps above the chart: daily for 15 minute and 1 hour charts, weekly for daily charts, 4 hour for 5 minute charts. Closer and the filter flips too often to help; further and it holds one side for months and forbids most sessions. The library's default is a daily 50 period EMA.
Does a higher-timeframe indicator repaint?
It does if it reads the forming higher bar, because that value moves during the day and settles at the close. The library's script asks for the previous completed higher-timeframe bar with lookahead off, so the line steps once per higher bar and its history never changes. The cost is one higher bar of lag.
Why does the line move in steps?
Because it carries one value per higher-timeframe bar. On a 15 minute chart the daily EMA has a single value for the whole session and jumps when the daily bar completes, so the line is flat for 26 bars and steps once. That is correct behaviour, not a drawing error.
Is the filter cross an entry on its own?
Rarely on its own. The cross tells you the permitted side has changed, and the library's script marks it so you can alert on it. The entry comes from a tool on the chart timeframe in the new direction, such as a pullback or a fast cross, because the filter cross itself lags the turn.
Can I use 2 higher timeframes?
Yes, and it is a common strict setup: a daily 50 and a weekly 20 on an hourly chart, with trades only when price is on the same side of both. Expect far fewer trades, each with both filters agreeing. Ask for a second filter on the built script, or run the template twice with different timeframes.
Does the filter help in a range?
Less than in a trend. When the higher timeframe is ranging, price crosses the average often and the permitted side flips with it, so the filter adds little and can hurt. Pair it with a strength measure such as ADX or the EMA slope, and a flat higher timeframe means no trades.