Lesson 10: Other timeframes and symbols
Read a higher timeframe in your script with data.series, know which bar you get, use earlier days and weeks, and see why another symbol is refused.
Lesson 10 of 18. A script runs on the bars of its chart, but it can also read a longer timeframe: the daily high on a 5-minute chart, the 4-hour trend under a 15-minute chart. In this lesson you read other timeframes and earlier days, and you learn what the engine refuses.
A value from a longer timeframe#
indicator "Daily range on any chart"
edition = 1
display = price
let (day_high, day_low) = data.series("1D", (high, low), symbol = symbol.id)
plot(day_high, name = "High of the last closed day", color = color.green, style = step_break)
plot(day_low, name = "Low of the last closed day", color = color.red, style = step_break)data.series(timeframe, expression, symbol = symbol.id)works out the expression on the bars of the other timeframe and gives the result on every bar of your chart.symbol.idis the chart's own symbol.- The expression here is a pair of values,
(high, low), taken apart withlet (day_high, day_low) = .... - The timeframe is a text:
"60"is 60 minutes, and"15m","4h","1D","1W"and"1M"are 15 minutes, 4 hours, a day, a week and a month. It must be longer than the chart's own timeframe.
Which bar you get#
By default data.series gives the last bar of the other timeframe that has closed. On a 5-minute chart today, the program above shows yesterday's high and low, not today's: today's day bar is not finished, so its high can still change. This is the only reading that gives the same line on the newest bar and in the history, which is why it is the default.
You can ask for the bar that is still forming with live = true. The engine allows it but warns (W-LIVE-READ), because the value on the newest bar keeps moving until that bar closes:
indicator "The forming day"
edition = 1
display = price
let forming_high = data.series("1D", high, symbol = symbol.id, live = true)
plot(forming_high, name = "High of the forming day", color = color.green)future = true goes further: it shows each bar's final figure from its first chart bar on. That is a look into the future, so the engine warns (W-FUTURE-READ) and a strategy may not use it. Other timeframes and symbols has the details, and Results that look too good explains why such reads mislead.
An indicator of a longer timeframe#
The expression can be any calculation. It runs on the longer timeframe's own bars, with their own history:
indicator "Higher timeframe trend"
edition = 1
display = price
input tf: tf = "240", name = "Higher timeframe"
input length: int = 50, name = "Length", min = 2
let slope = data.series(tf, tech.change(tech.ema(close, length)), symbol = symbol.id)
let average = data.series(tf, tech.ema(close, length), symbol = symbol.id)
let up = color.fade(color.green, 90)
let down = color.fade(color.red, 90)
chart.background(if is_na(slope) then na else if slope > 0 then up else down, name = "Trend")
plot(average, name = "Higher timeframe average", color = color.orange, width = 2)tech.change(x) is the difference between x and its value one bar before. Worked out on the 4-hour bars, it says whether the 4-hour average rose over the last closed 4-hour bar. An input of the type tf can be given straight to data.series.
Earlier days and weeks#
For the figures of whole days and weeks there is a shorter way, built from the chart's own bars: prior(day) is the last day that has finished, current(day) the day that is still forming, and the same for week and month.
indicator "Today against yesterday"
edition = 1
display = pane
let yesterday_range = prior(day).high - prior(day).low
let today_range = current(day).high - current(day).low
plot(
100 * today_range / yesterday_range,
name = "Today's range, % of yesterday's",
color = color.purple,
)
level(100, name = "Yesterday's range", color = color.gray, style = dashed)Use it on an intraday chart: on a daily chart prior(day) stops the script with E-PERIOD-TF.
Another symbol#
Data for a symbol other than the chart's is not available in this release. A symbol written in the script must first be declared with uses; without it the script is refused:
indicator "A symbol that is not declared"
edition = 1
plot(data.series("60", close, symbol = "BTCUSDT"))With the declaration the script is accepted, but it stops when it runs, with an error that names the symbol and says that the chart reads only its own:
indicator "A symbol that has no data here"
edition = 1
uses "BTCUSDT"
plot(data.series("60", close, symbol = "BTCUSDT"))The engine never makes up an empty series for a symbol it cannot read: the script stops, so a result is never a silent gap. For now, write symbol = symbol.id.
Try changing#
- In "Daily range on any chart", change
"1D"to"1W". The lines now show the high and low of the last closed week. - Add
live = trueto the same call and compare: on the newest bars the lines follow today's high and low, and the editor lists the noteW-LIVE-READ. - In "Higher timeframe trend", set the timeframe to
60on a 5-minute chart. The background changes colour more often, because the hourly average turns more often. - Open "Today against yesterday" on a daily chart and read the message the editor shows.
Checkpoint#
- You can read a value of a longer timeframe with
data.seriesandsymbol = symbol.id. - You can say which bar the default read gives, and what
live = trueandfuture = truechange. - You can use
prior(day)andcurrent(day)and know they need an intraday chart. - You know that another symbol is refused in this release, and with which codes.