Home » Ticker Functions » Understanding the request.security_lower_tf() Function in Pine Script

Understanding the request.security_lower_tf() Function in Pine Script

Photo of author
Published on

Introduction to request.security_lower_tf()

Pine Script, the scripting language used on the TradingView platform, enables users to create custom indicators, strategies, and various studies for financial charts. One of the powerful functions available in Pine Script Version 5 is request.security_lower_tf(). This function is pivotal for traders and analysts who wish to retrieve intrabar data, specifically data from a timeframe lower than the chart’s current timeframe.

request.security_lower_tf()

Syntax and Usage

lowerTimeframeData = request.security_lower_tf(symbol, timeframe, expression, gaps, lookahead)

LowerTimeframeData a variables that will store the result obtained from the lower timeframe. The request.security_lower_tf() function takes several parameters:

  • symbol: This is the ticker symbol for which you want to retrieve data.
  • timeframe: Specifies the lower timeframe from which you want to get the data.
  • expression: The expression or data that you want to access from the lower timeframe.
  • gaps and lookahead: These optional parameters manage how gaps in data and future-looking data are handled.

Example

//@version=5
indicator("Lower TF MA", overlay=true)

// Define the lower timeframe
lower_tf = "15"

// Requesting lower timeframe data
lower_tf_data = request.security_lower_tf(syminfo.tickerid, lower_tf, ta.sma(close, 14))

// Initializing a variable for the converted series
var float lower_tf_ma = na

if barstate.islast
    for i = 0 to array.size(lower_tf_data) - 1
        lower_tf_ma := array.get(lower_tf_data, i)

plot(lower_tf_ma, title="15-Minute SMA", color=color.blue)
Example

Detailed Walkthrough of the Modified Code

  • var float lower_tf_ma = na: We initialize a variable lower_tf_ma to store our series data. The var keyword ensures that the variable retains its value between bars.
  • if barstate.islast…: This condition ensures that our loop runs only on the last bar, which is necessary because request.security_lower_tf returns data as an array with one element per bar.
  • for i = 0 to array.size(lower_tf_data) - 1: This loop iterates over each element in the lower_tf_data array.
  • lower_tf_ma := array.get(lower_tf_data, i): Inside the loop, we use array.get to retrieve each element from the lower_tf_data array and assign it to lower_tf_ma. The := operator is used to update the value of lower_tf_ma on each iteration.

Key Features and Takeaways

  • Data Type Handling: It’s crucial to match the data types expected by Pine Script functions. Arrays and series are different types and must be handled accordingly.
  • Conversion Techniques: Converting an array to a series involves iterating over the array and extracting its values.
  • Performance Considerations: Be mindful of performance impacts when using loops and data conversions in Pine Script, especially when dealing with large datasets or complex calculations.

Leave a Comment