Skip to content
Docs
Open Portal
Copy manually

Your browser did not allow direct clipboard access. Select and copy the text below.

Add a confirmed higher-timeframe filter

Use a completed 15-minute EMA on a 1-minute chart, understand its delay, and test the guard against an invalid timeframe.

You will add an orange 15-minute EMA to a 1-minute chart, using only a completed higher-timeframe value. A signal will require a confirmed upward cross of the blue chart EMA and a close above that orange value.

Use this lesson when your rule needs another timeframe. You do not need a higher-timeframe filter to use PineConnector.

Before replacing a script, pause its actual saved alerts in TradingView and inspect any open broker positions. Keep this exercise separate from a connected trading script: it contains informational text only, and Enable practice alerts starts off.

Use BITSTAMP:BTCUSD, a 1-minute interval and standard Candles. Open Pine Editor and choose Create new → Indicator so your connected script remains separately identifiable. Copy the complete example below, replace the starter code, and save it as My higher-timeframe practice.

Pine Editor script menu with Create new and Indicator highlighted
Enlarge screenshot
Open the script-name menu, choose Create new, then Indicator.

Open the script-name menu, choose Create new, then Indicator.

Pine Editor script menu with Create new and Indicator highlighted
Complete confirmed higher-timeframe examplePine v6
TradingView → Pine Editor → new practice indicator Pine Script
//@version=6
indicator("Pine practice: confirmed higher timeframe", overlay = true)
int length = input.int(20, "EMA length", minval = 1)
string higherTf = input.timeframe("15", "Higher timeframe")
bool enablePracticeAlerts = input.bool(false, "Enable practice alerts")
if timeframe.in_seconds(higherTf) <= timeframe.in_seconds()
runtime.error("Higher timeframe must be greater than the chart timeframe.")
float chartEma = ta.ema(close, length)
// Keep [1] and lookahead_on together: request the PREVIOUS completed HTF value.
float confirmedHigherEma = request.security(syminfo.tickerid, higherTf,
ta.ema(close, length)[1], lookahead = barmerge.lookahead_on)
bool crossedUp = ta.crossover(close, chartEma)
bool entrySignal = crossedUp and barstate.isconfirmed and close > confirmedHigherEma
string message = "HTF practice | " + syminfo.ticker + " | entry | HTF=" + higherTf
plot(chartEma, title = "Chart EMA", color = color.blue, linewidth = 2)
plot(confirmedHigherEma, title = "Last confirmed higher-timeframe EMA", color = color.orange,
linewidth = 2, style = plot.style_stepline)
plotshape(entrySignal, title = "Confirmed higher-timeframe entry", style = shape.triangleup,
location = location.belowbar, color = color.green, size = size.small, text = "HTF entry")
var table preview = table.new(position.top_right, 1, 4, border_width = 1)
if barstate.islast
table.cell(preview, 0, 0, "Confirmed higher-timeframe filter", bgcolor = color.blue, text_color = color.white)
table.cell(preview, 0, 1, "Chart: " + timeframe.period + " | Higher: " + higherTf, bgcolor = color.white, text_color = color.black)
table.cell(preview, 0, 2, "Last confirmed HTF EMA: " + str.tostring(confirmedHigherEma, format.mintick), bgcolor = color.white, text_color = color.black)
table.cell(preview, 0, 3, "Message preview: " + message, bgcolor = color.white, text_color = color.black)
// Informational messages only. Existing alert snapshots need separate pausing.
if enablePracticeAlerts and entrySignal
alert(message, alert.freq_once_per_bar_close)
1

Add the indicator and check its inputs

Save with the cloud-shaped control, then click the triangle-shaped Add to chart button. Open the settings gear for Pine practice: confirmed higher timeframe.

In Inputs, use EMA length = 20 and Higher timeframe = 15 minutes. Keep Enable practice alerts unchecked and click Ok.

What you should see

The chart uses 1 minute; this indicator's Higher timeframe is 15 minutes and Enable practice alerts is unchecked.

Higher timeframe is set to 15 minutes and Enable practice alerts is unchecked.
Enlarge screenshot
On a 1-minute chart, choose 15 minutes for Higher timeframe (1). Keep practice alerts off (2).

On a 1-minute chart, choose 15 minutes for Higher timeframe (1). Keep practice alerts off (2).

Higher timeframe is set to 15 minutes and Enable practice alerts is unchecked.
2

Compare the two EMA lines

Inspect the blue line and orange stepped line. Read the on-chart box to check the selected chart and higher timeframes. Find an HTF entry marker and compare its candle’s close with both lines.

A missing marker can be correct: an upward cross still has to pass the orange-line filter. The message preview does not mean an alert was sent.

What you should see

The blue EMA follows the chart's bars. The orange line holds a completed higher-timeframe value between its updates.

Blue chart EMA and orange stepped confirmed 15-minute EMA beneath a qualifying HTF entry marker.
Enlarge screenshot
The blue EMA uses chart candles (1). The orange line holds the last completed higher-timeframe value in steps (2). This marked entry also passes that filter (3).

The blue EMA uses chart candles (1). The orange line holds the last completed higher-timeframe value in steps (2). This marked entry also passes that filter (3).

Blue chart EMA and orange stepped confirmed 15-minute EMA beneath a qualifying HTF entry marker.

The blue calculation uses 20 chart bars. The orange calculation uses 20 higher-timeframe bars. Changing the timeframe changes the data behind that calculation, even though both lengths say 20.

Keep the two parts of the request together

Section titled “Keep the two parts of the request together”

Find this request in the code:

Request the previous completed higher-timeframe EMAExcerpt. Keep the [1] offset and lookahead setting together.
Read in your existing practice script Pine Script
// Keep [1] and lookahead_on together: request the PREVIOUS completed HTF value.
float confirmedHigherEma = request.security(syminfo.tickerid, higherTf,
ta.ema(close, length)[1], lookahead = barmerge.lookahead_on)

Inside this request, [1] selects the previous higher-timeframe value. Paired with barmerge.lookahead_on, it makes that completed value available consistently from the start of the next higher-timeframe period. Removing the offset while keeping lookahead_on can leak future values into historical results. TradingView’s higher-timeframe guidance

A one-minute candle closing does not confirm an unfinished fifteen-minute candle. Waiting for the chart’s close alone is insufficient when another timeframe supplies unconfirmed data.

The tradeoff is delay: this example deliberately uses the last completed higher-timeframe EMA, not the value still forming. Its orange line updates when another completed higher-timeframe value becomes available. It does not promise that other scripts, data revisions or broker outcomes cannot differ.

3

Choose the same timeframe deliberately

Keep the chart on 1 minute. Open this indicator’s Settings → Inputs, change Higher timeframe to 1 minute, then click Ok.

This is a deliberate invalid-input exercise. Open the script’s error indicator to read the message.

What you should see

The script reports that Higher timeframe must be greater than the chart timeframe.

TradingView reports that Higher timeframe must be greater than the chart timeframe.
Enlarge screenshot
This is the intended guard: a 1-minute higher-timeframe selection cannot be used on a 1-minute chart. Restore 15 minutes in Inputs.

This is the intended guard: a 1-minute higher-timeframe selection cannot be used on a 1-minute chart. Restore 15 minutes in Inputs.

TradingView reports that Higher timeframe must be greater than the chart timeframe.

The code compares the intervals as durations. It rejects an equal or lower timeframe instead of silently applying a higher-timeframe recipe to a different case. This is a runtime guard: the source can compile while the selected inputs are unsuitable.

4

Restore 15 minutes

Reopen Inputs, set Higher timeframe back to 15 minutes, and click Ok. Compare the chart with the valid example above.

Leave Enable practice alerts off. Do not remove the guard to make the error disappear.

What you should see

The guard error clears and the blue and orange lines return.

Question: a one-minute candle has closed, but the current fifteen-minute candle is still forming. Can this script use that unfinished fifteen-minute EMA as its orange value?

Show the answer

No. It requests the previous completed higher-timeframe EMA. The chart’s close does not complete the separate fifteen-minute candle.

Question: would removing [1] make this a faster version of the same trustworthy historical test?

Show the answer

No. With lookahead_on still present, the historical request can use higher-timeframe information before it was available. Keep the pair together and understand the delay it introduces.

Try: set Higher timeframe to 30 minutes, predict which line should change, then restore 15 minutes. The orange calculation changes; the blue 20-bar chart EMA remains on the 1-minute chart.