Other timeframes
request.security runs an expression on a higher timeframe and hands the result back to each chart bar.
//@version=5
indicator("Daily EMA", overlay = true)
tf = input.timeframe("D", "Timeframe")
daily = request.security(syminfo.tickerid, tf, ta.ema(close, 20))
plot(daily, "Daily EMA", color.orange, 2, plot.style_stepline)
The call is request.security(symbol, timeframe, expression, gaps). It regroups the chart's own bars into the higher timeframe and runs expression on the result.
Only closed bars
A higher-timeframe bar is shown from the chart bar that ends it onwards. Until then you keep seeing the previous closed one. On an M5 chart, "60" shows the previous hour for the first eleven bars of an hour, and the new hour from its last bar.
A script cannot see a bar that has not finished, so replay and a full run give the same picture and nothing repaints. This is why lookahead_on is refused. You do not need the close[1] plus lookahead_on trick: the default already never looks ahead.
The stepped line is the correct output, not a glitch. A daily EMA changes once a day, so on a lower timeframe it moves in steps.
Rules
- Timeframes: minutes (
"15","240"), days ("D","3D"), or""for the chart's own. It must be a whole multiple of the chart's timeframe and not lower. Weekly and monthly are not offered, because the app's bars stop at one day. Days are UTC days. - Symbol: the chart's own (
syminfo.tickerid,syminfo.ticker, or""). Other symbols are not available, and neither isrequest.security_lower_tf. A script is given the chart's own bars, so there is no finer data to ask for. gaps = barmerge.gaps_onreturnsnaexcept on the chart bar where a higher-timeframe bar closed.- The expression can be a tuple:
[a, b] = request.security(..., [ta.rsi(close, 14), high]). - Call it at the top level of the script, not inside a function and not inside another
request.security. - The first higher-timeframe bar is built from whatever chart bars exist, so on a chart that starts mid-bucket it is a partial one.
Variables inside the expression
Variables used in the expression are recomputed on the higher timeframe from their definition, as Pine does. m = ta.ema(src, len) then request.security(..., m) works, and so do inputs. A variable that is changed with :=, declared with var, or a function that reads script variables cannot be recomputed. The error says so. Put the calculation inside the expression instead.