Pine Script higher-timeframe trend: see the bigger chart on any chart
We check the bigger chart before every trade, and trade with it. This script does that check for you, on whatever chart you're looking at, using only daily candles that have already closed.
Why a bigger-chart filter?
A 15-minute chart shows the waves. The daily chart shows the tide.
A buy setup on the 15-minute chart is swimming with the tide when the daily trend is up, and against it when the daily trend is down. Our method is simple about this: check the bigger chart first, and trade with it. More in multiple timeframe analysis.
Switching charts back and forth is easy to skip. A small script puts the answer on the chart you trade from.
This page builds on Pine Script request.security, which explains how bigger-timeframe data works and why it can repaint. Read that first if the terms are new.
What "trend up" means here
We need a rule a script can check. This one uses the daily chart and a 50-day EMA (an average that leans toward recent days):
- Up: the last finished daily close is above the 50-day average, and the average is rising.
- Down: the close is below the average, and the average is falling.
- Unclear: anything else.
"Unclear" is an answer too. It often means sit tight.
The script
//@version=6
indicator("Bigger-chart trend", overlay=true)
tf = input.timeframe("D", "Bigger timeframe")
len = input.int(50, "Average length", minval=2)
if timeframe.in_seconds(tf) <= timeframe.in_seconds(timeframe.period)
runtime.error("Pick a bigger timeframe than the chart's.")
[bigClose, bigAvg, bigAvgPrev] = request.security(syminfo.tickerid, tf, [close[1], ta.ema(close, len)[1], ta.ema(close, len)[2]], lookahead=barmerge.lookahead_on)
trendUp = bigClose > bigAvg and bigAvg > bigAvgPrev
trendDown = bigClose < bigAvg and bigAvg < bigAvgPrev
bgcolor(trendUp ? color.new(color.green, 92) : trendDown ? color.new(color.red, 92) : na)
plot(bigAvg, "Bigger average", color=color.purple, linewidth=2)
var label tag = label.new(bar_index, close, "", style=label.style_label_left, textcolor=color.white)
if barstate.islast
word = trendUp ? "Up" : trendDown ? "Down" : "Unclear"
label.set_xy(tag, bar_index + 2, close)
label.set_text(tag, tf + " trend: " + word)
label.set_color(tag, trendUp ? color.green : trendDown ? color.red : color.gray)
Every line, in plain words
- Line 1 says this is Pine Script version 6.
- Line 2 names the script and puts it on the price chart.
- Line 3 makes the bigger timeframe a setting. "D" is daily.
- Line 4 makes the average length a setting.
- Lines 5 and 6 stop the script with a clear message if the "bigger" timeframe isn't bigger. timeframe.in_seconds turns a timeframe into a number of seconds, so they can be compared.
- Line 7 does the main work. It asks the daily chart for 3 values at once, in square brackets: yesterday's close, yesterday's average, and the day before's average. [1] means one daily candle back. [2] means two.
- Line 8 is true when the trend is up by our rule.
- Line 9 is true when it's down.
- Line 10 shades the background faint green for up, faint red for down, and leaves it clear when unclear.
- Line 11 draws the daily average as a thick purple line. On a small chart it looks like steps, because it changes once a day.
- Line 12 makes one label, once. var keeps it.
- Lines 13 to 17 update it on the newest candle: move it to the right of price, write "D trend: Up" (or Down or Unclear), and color it.
Why [1] with lookahead_on?
This is the part that keeps the script honest.
Line 7 only uses finished daily candles: [1] and [2]. lookahead_on lines each finished value up with the start of the next day, instead of a day late.
Together, they mean: what you see live is what history shows later. Nothing changes after it's drawn.
The cost is that the trend is always one day behind. On a daily trend check, that's a fair trade. A full explanation is in Pine Script request.security.
Never use lookahead_on without [1]. Past candles would see the day's final close before the day was over. The chart would look brilliant and be wrong.
Picking the bigger timeframe
A common habit is about 4 to 6 times bigger than the chart you trade:
| Your chart | A bigger chart to check |
|---|---|
| 5 minutes | 30 minutes or 1 hour |
| 15 minutes | 1 hour or 4 hours |
| 1 hour | 4 hours or daily |
| Daily | Weekly |
Too close, and the "bigger" trend is just noise. Too far, and it changes so rarely it says little.
A better trend rule
An average is a simple way to read direction. It's also late. A more direct read is the one we teach: higher highs and higher lows for up, lower highs and lower lows for down. See higher highs and higher lows and how to identify a trend.
You can code that too, with turning points from Pine Script swing highs and lows, and pull it from the bigger timeframe the same way.
Use it as a filter, not a trigger
The bigger-chart trend answers one question: which way should I be looking? It doesn't say when to act.
- Trend up: only look for buy setups on your chart.
- Trend down: only look for sell setups.
- Unclear: wait, or trade smaller.
Combine it with a setup rule, like a candle body closing past a level. Pair it with the crossover in Pine Script moving average crossover, or put the answer in a Pine Script table with your other checks.
Then every trade still needs a stop from the chart and a size from the math.
Try it today
- Paste the script into the Pine Editor on a computer. Save and add it to a 15-minute chart.
- Scroll back a few weeks. Notice the shading changes only at the start of a day.
- Change the timeframe setting to "W" (weekly) and look again. Fewer changes.
- On bar replay, step through a week. Check that the color never changes after it's drawn.
Unclear is useful. Count how many days were "Unclear". Those are days the bigger chart wasn't giving a clear direction. Many traders do less on those days.
Learning from a real script
Our course includes the full source code of the StructureX chart tool to learn from and customize, for example adding your own bigger-chart filter. It's source code, not a ready-made trading bot.
Common mistakes
- lookahead_on without [1]. The past peeks at the future.
- A "bigger" timeframe that isn't bigger. Lines 5 and 6 catch this.
- Treating the filter as a buy signal. It only tells you which way to look.
- Forgetting it's a day behind. It's honest, but it's late by design.
Questions
How do I show the daily trend on an intraday chart in Pine Script?
Use request.security() with the daily timeframe and values offset by [1], with lookahead=barmerge.lookahead_on. Then compare the finished daily close to a finished daily average.
Does a higher timeframe filter repaint in Pine Script?
It can. With plain values and lookahead off, today's value moves until the day closes. Using [1] with lookahead_on uses only finished candles, so it doesn't change after it's drawn.
Can request.security return more than one value?
Yes. Put the values in square brackets, like [close[1], ta.ema(close, 50)[1]], and receive them as a group on the left side.
What higher timeframe should I use?
A common habit is 4 to 6 times your chart: 1 hour for a 15-minute chart, daily for a 1-hour chart.
Want the next step? We'll walk you through it.
Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.
Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytimeOr see the whole method, step by step:
See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)One chart idea a day, in plain English. Follow along: