Back to TradingView PineScript for CFD Traders
Part 4

Multi-timeframe analysis with request.security

Higher-timeframe context in your strategy — but only if you avoid the two look-ahead bugs Pine warns about.

Advanced12 min readBeginner → AdvancedLesson 14 / 17
Try it in TradingView
Daily 200 EMA on any intraday chart

lookahead=barmerge.lookahead_off is what stops the daily bar from repainting. Remove it and watch the line shift as the day forms — that's the bug.

//@version=5
indicator("Lesson 14 - Multi-timeframe", overlay=true)

// Daily 200 EMA, pulled onto whatever timeframe you're viewing.
// lookahead_off = only use data that was already closed. No repainting.
dailyEMA = request.security(syminfo.tickerid, "D", ta.ema(close, 200),
     lookahead=barmerge.lookahead_off)

plot(dailyEMA, "Daily EMA 200", color=color.gray, linewidth=3)

// Shade by which side of the daily trend price is on.
aboveTrend = close > dailyEMA
bgcolor(aboveTrend ? color.new(color.green, 92) : color.new(color.red, 92))

// Mark the crossovers.
plotshape(aboveTrend and not aboveTrend[1], "Crossed above daily", shape.triangleup,
          location.belowbar, color=color.green, size=size.tiny)
plotshape(not aboveTrend and aboveTrend[1], "Crossed below daily", shape.triangledown,
          location.abovebar, color=color.red, size=size.tiny)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

What request.security does

request.security lets your script pull a series from a different timeframe or symbol. The basic form:

dailyClose = request.security(syminfo.tickerid, "D", close)

This gives you the daily close as a series on any intraday chart.

Look-ahead bug #1: repainting

If you call request.security(... , close) on a 1H chart, you are pulling the daily close — but during the day, the daily bar is still forming, so the value changes as new hourly bars arrive. Your 1H chart will repaint in real time. Pine has a built-in guard:

dailyClose = request.security(syminfo.tickerid, "D", close, lookahead=barmerge.lookahead_off)

This is now safe — you get the prior daily close until the current daily bar closes.

Look-ahead bug #2: same-bar decisions

If your strategy uses the daily close to decide whether to enter on the next 1H bar, make sure the daily close you read is actually closed. Use barmerge.lookahead_off and remember that intraday strategies can only act on data that was closed before the decision bar.

The tuple-return pattern

You can pull multiple series in one call:

[dHigh, dLow, dClose] = request.security(syminfo.tickerid, "D", [high, low, close], lookahead=barmerge.lookahead_off)

Common MTF build: higher-TF trend filter

ema200 = request.security(syminfo.tickerid, "D", ta.ema(close, 200), lookahead=barmerge.lookahead_off)
longSignal = ta.crossover(close, ta.ema(close, 20)) and close > ema200
shortSignal = ta.crossunder(close, ta.ema(close, 20)) and close < ema200

Only trade with the daily trend. Trend filters cut drawdown roughly in half on most intraday strategies — the math is well-documented in the CTA literature.

Performance warning

Every request.security call adds execution time. Pine's limit is generous but you can hit it with dozens of calls per bar. If you find your chart lagging, count your security calls.

What you just did

Lesson 14 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.