HTF trend filter
Filter a 15m chart through confirmed 1h and 4h EMA trend state with multiple subscriptions.
This example subscribes the same market at two higher timeframes and tints the chart only when price, the 1h EMA, and the 4h EMA all agree. It covers multiple data.ohlcv subscriptions and the confirmed-versus-forming distinction.

script "HTF Trend Filter"
data chart = subscribe(data.ohlcv)
data h1 = subscribe(data.ohlcv, timeframe: tf("1h"))
data h4 = subscribe(data.ohlcv, timeframe: tf("4h"))
input (
h1Len = input.int(21, title: "1h EMA Length", min: 2, max: 200)
h4Len = input.int(9, title: "4h EMA Length", min: 2, max: 100)
)
plot (
h1Ema = plot.line(title: "1h EMA", color: color.orange, width: 2)
h4Ema = plot.line(title: "4h EMA", color: color.purple, width: 2)
aligned = plot.bg(title: "Aligned Trend")
)
on chart.update {
// Confirmed higher-timeframe values only: the filter must not flicker
// while a 1h or 4h bar is still forming.
let e1 = ta.ema(h1.close, h1Len)
let e4 = ta.ema(h4.close, h4Len)
h1Ema.plot(e1)
h4Ema.plot(e4)
if e1 == null || e4 == null {
return
}
let price = (chart.close ?? 0.0)
let bullish = price > (e1 ?? 0.0) && (e1 ?? 0.0) > (e4 ?? 0.0)
let bearish = price < (e1 ?? 0.0) && (e1 ?? 0.0) < (e4 ?? 0.0)
// Tint only full alignment; mixed states stay untinted.
if bullish {
aligned.plot(color: color.withAlpha(color.up, 18))
}
if bearish {
aligned.plot(color: color.withAlpha(color.down, 18))
}
}One subscription per timeframe
Each subscribe(data.ohlcv, timeframe: ...) follows its own timeframe. Reading h1.close inside the chart's update handler gives the latest completed 1-hour candle available at that moment.
Confirmed values hold still
h1.close only changes when a 1h bar completes. That is exactly what a filter needs: a signal that stays put for the full hour instead of flickering intrabar. Use h1.forming.close when watching the developing bar matters more than stability, and label it clearly.
Show when the value changes
The EMAs use stepped lines to show that their values change only when a higher-timeframe bar closes.