Request more data from your scripts

May 31, 2022

Two new Pine Script™ functions, request.security_lower_tf() and request.economic(), and improvements to the existing request.security() function allow your scripts to access more data.

request.security_lower_tf()

The new request.security_lower_tf() function makes it easier to request data from a lower timeframe than the chart’s. Before this new function, accessing all the 1min intrabars composing a 60min chart bar required complex user-defined functions and request.security() calls. The new request.security_lower_tf() now makes it a doddle by returning an array containing one value of the supplied expression for each intrabar. Note that the number of intrabars may vary for each chart bar.

The following indicator looks at the direction of the 1min intrabars in the dilation of each chart bar. If the majority of intrabars are not the same polarity as the chart bar, we color its body orange.

//@version=5
indicator("Polarity Divergences", overlay = true)
// Fetch an array containing the +1/0/-1 direction of each 1min intrabar.
array<float> directionsArray = request.security_lower_tf(syminfo.tickerid, "1", math.sign(close - open))
// Color the chart bar orange when the majority of 
// intrabar directions does not match the chart bar's direction.
barcolor(math.sign(array.sum(directionsArray)) != math.sign(close - open) ? color.orange : na)
// Plot the number of intrabars in indicator values and the Data Window.
plotchar(array.size(directionsArray), "Intrabars", "", location.top)

request.economic()

The request.economic() function fetches economic data for a country or a region. Economic data includes information such as the state of a country’s economy (GDP, inflation rate, etc.) or of a particular industry (steel production, ICU beds, etc.):

//@version=5
indicator("Gross domestic product of the US")
plot(request.economic("US", "GDP"))

Our Help Center article lists all the countries/regions and metrics available. An additional Help Center article for each metric describes it and lists the countries/regions for which it is available. This is the one for GDP.

request.security() improvements

request.security() can now return arrays, strings, booleans, colors, and tuples of these types:

//@version=5
indicator("")

// Create an array containing OHLC values.
array<float>  ohlc = array.from(open, high, low, close)

// Request a tuple evaluated in the context of the "NASDAQ:TSLA" symbol at the chart's timeframe.
// The tuple contains:
//   - The array of OHLC values.
//   - A string representation of the OHLC values using the symbol's tick precision.
//   - The currency string of the symbol.
[ohlcArray, ohlcString, curString]  = request.security("NASDAQ:TSLA", timeframe.period, [ohlc, str.tostring(ohlc, format.mintick), syminfo.currency])

if barstate.islastconfirmedhistory
    label.new(bar_index, high, 
      "OHLC values: " + str.tostring(ohlcArray) +
      "\nFormatted OHLC values: " + ohlcString +
      "\nCurrency: " + curString)

To stay informed on new Pine features, please keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box Telegram channel, Twitter account, and from the “Pine Script™ Q&A” public chat on TradingView.

We hope you find these improvements useful. Please continue sending us your feedback. We build TradingView for our users and we love hearing from you.

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart