Understanding Pine Script's request.security() Function

·

When working with Pine Script on TradingView, you’ll often need data that isn’t available directly on your current chart. Whether you're analyzing a different asset, monitoring a higher timeframe, or comparing correlated instruments, the request.security() function is your go-to tool for pulling external data into your script.

This powerful function allows you to access price data, indicators, or custom calculations from any symbol or timeframe—making it indispensable for advanced trading strategies.

What Does request.security() Do?

At its core, request.security() enables cross-timeframe and cross-symbol data retrieval. Imagine you're viewing a 5-minute chart of Apple (NASDAQ:AAPL), but you want to incorporate Bitcoin’s (BINANCE:BTCUSDT) daily closing price into your strategy logic. Without request.security(), this wouldn’t be possible.

👉 Discover how to integrate multi-asset insights seamlessly into your trading scripts.

The function acts like a data bridge: it requests specific information from a remote source (a different symbol or timeframe) and returns it to your active script. This opens up possibilities for sophisticated conditional logic, trend confirmation, and market context analysis—all within a single indicator or strategy.

Syntax and Core Components

The basic structure of the function is:

data = request.security(symbol, timeframe, expression)

Let’s break down each component:

For example, to get the daily simple moving average of Bitcoin:

daily_sma = request.security("BINANCE:BTCUSDT", "1D", ta.sma(close, 20))

This line fetches the 20-period SMA of BTCUSDT’s daily close and makes it available on your current chart, regardless of its native timeframe.

Practical Use Cases

Confirming Trends Across Timeframes

Day traders often use lower timeframes (like 5-minute charts) but align their trades with higher-timeframe trends. By pulling the daily closing price or a weekly RSI value, you can avoid counter-trend entries.

Example:

daily_trend = request.security(syminfo.tickerid, "1D", close > ta.sma(close, 50))

This checks whether the current asset is above its daily 50-period SMA—ideal for filtering long entries.

Monitoring Market Correlations

Assets often move in tandem. For instance, tech stocks may follow NASDAQ, and altcoins often react to Bitcoin’s momentum. You can use request.security() to monitor these relationships in real time.

btc_momentum = request.security("BINANCE:BTCUSDT", "4H", ta.rsi(close, 14))

Use this to adjust your crypto strategy when Bitcoin enters overbought or oversold territory.

Building Composite Indicators

Create sector strength meters by pulling closing prices from multiple stocks in the same industry and averaging them. Or build a dollar index proxy by combining forex pairs like USD/JPY and EUR/USD.

👉 Learn how professional traders leverage multi-source data for smarter decisions.

Key Best Practices

While request.security() is powerful, misuse can lead to performance issues or script errors. Keep these guidelines in mind:

Frequently Asked Questions

Q: Can I use request.security() with user-defined functions?
A: Yes! You can pass complex expressions, including custom functions, as the third argument. Just ensure they return a valid series.

Q: Why does my indicator show flat lines when using higher timeframes?
A: Higher timeframe values only update at the end of each bar. On a 1-minute chart, the daily close remains constant for 1,440 minutes—this is normal behavior.

Q: Is there a performance cost to using many security calls?
A: Yes. Each request consumes computational resources. Too many can slow down chart rendering or trigger rate limits.

Q: Can I pull data from non-standard timeframes like ‘tick’ or ‘volume-based’ bars?
A: No. request.security() only supports regular time intervals (minute, hour, day, etc.).

Q: Does request.security() work in real-time?
A: Absolutely. It updates with each new bar, making it suitable for live alerts and dynamic visuals.

Q: Can I use it in alerts?
A: Yes—conditions based on request.security() data can trigger TradingView alerts effectively.

Final Thoughts

Mastering request.security() unlocks a new dimension in Pine Script development. From confirming macro trends to building intelligent filters based on external assets, this function empowers traders to create nuanced, context-aware strategies.

Start small—pull a single daily value onto your intraday chart. Then expand: compare assets, validate signals, and layer in intermarket analysis. With careful optimization and smart design, your scripts can evolve from simple indicators into robust analytical tools.

👉 Start applying multi-timeframe logic in your trading today—explore powerful tools that simplify advanced scripting.