External Integration ๐
Pine Script lives inside TradingView, but trading rarely stays inside one platform. You need data from other symbols, signals sent to bots, alerts pushed to your phone, and strategies that talk to brokers through webhooks. This chapter covers every integration technique available in Pine Script v5: pulling in external data with request.security(), formatting structured alert payloads, building multi-symbol screeners, and connecting to external services through webhooks.
Working with External Data
The request.security() Function
The request.security() function is the primary gateway to external data in Pine Script. It lets you fetch price data, indicator values, or any calculated expression from a different symbol or timeframe.
The basic syntax is:
request.security(symbol, timeframe, expression, gaps, lookahead)- symbol -- the ticker identifier (e.g.,
"BTCUSD","AAPL", orsyminfo.tickerid) - timeframe -- the resolution string (e.g.,
"D"for daily,"60"for hourly,""for the chart timeframe) - expression -- any Pine Script expression to evaluate on that symbol/timeframe
- gaps -- how to handle gaps between bars (
barmerge.gaps_offfills them,barmerge.gaps_onleavesna) - lookahead -- controls whether future data leaks into historical bars
Here is a complete example that fetches BTC and ETH closing prices and plots their rolling correlation:
//@version=5
indicator("BTC-ETH Correlation", overlay=false)
// Fetch closing prices from two symbols on the current timeframe
btcClose = request.security("BTCUSD", timeframe.period, close)
ethClose = request.security("ETHUSD", timeframe.period, close)
// Calculate 50-bar Pearson correlation
corrLength = input.int(50, "Correlation Length", minval=10)
correlation = ta.correlation(btcClose, ethClose, corrLength)
// Plot correlation with reference lines
plot(correlation, "BTC-ETH Correlation", color=color.yellow, linewidth=2)
hline(0.8, "Strong Positive", color=color.green, linestyle=hline.style_dashed)
hline(0.0, "Zero", color=color.gray, linestyle=hline.style_dotted)
hline(-0.8, "Strong Negative", color=color.red, linestyle=hline.style_dashed)
// Background color based on correlation strength
bgColor = correlation > 0.8 ? color.new(color.green, 90) : correlation < -0.8 ? color.new(color.red, 90) : na
bgcolor(bgColor)This script requests close data for both BTCUSD and ETHUSD, computes a 50-bar rolling Pearson correlation, and plots it with visual reference zones. When the correlation is above 0.8, the background turns green; below -0.8, it turns red.
Continue reading
Sign in or create a free account to unlock External Integration and access the full academy.
Free account ยท No credit card required