Multiple timeframes
Read higher timeframes next to the chart's, choose confirmed or forming values, and build timeframes from the chart's.
Declare another subscription when a calculation needs a different timeframe:
script "Higher-timeframe EMA"
input length = input.int(21, title: "4h EMA length", min: 2, max: 200)
data (
chart = subscribe(data.ohlcv)
h4 = subscribe(data.ohlcv, timeframe: 4h)
)
plot (
confirmed = plot.line(title: "4h EMA", color: color.blue, width: 2, style: linestyle.step)
developing = plot.line(title: "4h EMA (forming)", color: color.gray, style: linestyle.dashed)
)
state ema: float? = null
on h4.close {
ema = ta.ema(h4.close, length)
}
on chart.update {
confirmed.plot(ema)
}
on h4.update {
// Inside its own update handler, h4 reads its forming candle.
developing.plot(ta.ema(h4.close, length))
}The confirmed EMA advances once per completed 4-hour candle and stays put afterwards; the forming one follows the developing candle. Label the difference so traders know which they see. See repainting and confirmed values.
What the chart provides
On a chart, other timeframes come from the chart’s own bars: a subscription serves
the chart’s interval or a whole multiple of it, aggregated (open of the first
bar, highest high, lowest low, close of the last, summed volumes). A 15m chart serves
1h and 4h; it cannot serve 5m, because finer bars are not there. Change the
chart to a finer interval for those.
Reading other timeframes
- In
h4’s ownopenandupdatehandlers,h4.closeis the forming candle; in itsclosehandler, the candle just completed. - In handlers of other subscriptions,
h4.closeis the last confirmed 4-hour close andh4.forming.closethe developing one. The confirmed value needs no[1]offset. h4.close[1]is the 4-hour close before the last confirmed one.
Drawing at the live edge
To show only the latest higher-timeframe levels, compute them on every bar in
on chart.update and draw under chart.isLast there:
script "Previous day high and low"
data (
chart = subscribe(data.ohlcv)
daily = subscribe(data.ohlcv, timeframe: 1D)
)
state levels = entities.linePool(max: 2)
on chart.update {
let t = chart.time
let start = daily.time
let hi = daily.high
let lo = daily.low
if chart.isLast && t != null && start != null && hi != null && lo != null {
levels.get("pdh").set(start, hi, t, hi, color: color.red, extend: extend.right, text: "PDH")
levels.get("pdl").set(start, lo, t, lo, color: color.green, extend: extend.right, text: "PDL")
}
}Timeframes from the chart’s
There is no timeframe * n; go through seconds with time.tfSeconds and
time.tfFromSeconds. The result is a setup value, so it can drive a subscription:
script "Four times the chart's timeframe"
input mult = input.int(4, title: "Multiple", min: 1, max: 60)
setup higherTf = time.tfFromSeconds(time.tfSeconds(context.timeframe) * mult)
data (
chart = subscribe(data.ohlcv)
higher = subscribe(data.ohlcv, timeframe: higherTf)
)
pane ticksPane = pane(title: "Ticks from the higher close", height: 0.2)
plot ticksAway = plot.histogram(title: "Ticks", on: ticksPane)
on chart.update {
let c = chart.close
let h = higher.close
if c != null && h != null {
ticksAway.plot((c - h) / context.tickSize)
}
}Where values are plotted
Plots on the chart and its panes use the chart’s time axis. A value written from a
4-hour handler lands on the chart bar containing that time, so a confirmed 4-hour
value drawn from on h4.close appears once per 4-hour candle; drawing it from
on chart.update repeats it on every chart bar, as in the first example.