Home » Pinescript Syntax » Understanding Tuples in Pine Script

Understanding Tuples in Pine Script

Photo of author
Published on

Tuples in Pine Script offer a compact and versatile way to work with multiple values within a single operation. This feature is particularly beneficial when dealing with functions that need to return more than one result. Through the use of tuples, Pine Script enhances its functionality, allowing for more sophisticated data manipulation and analysis within the trading scripts. Let’s delve deeper into the concept of tuples, their syntax, and their practical applications in Pine Script.

Introduction to Tuples in Pine Script

A tuple is essentially a collection of values grouped. In Pine Script, tuples are represented as a comma-separated list of expressions enclosed in square brackets. They are most commonly used when a function or method needs to return multiple values.

Example: User-defined Function Returning a Tuple

Consider a user-defined function calculateSumProduct, which returns both the sum and product of two floating-point numbers:

indicator("Sum and Product Example", overlay = true)

calculateSumProduct(float num1, float num2) =>
    sum = num1 + num2
    product = num1 * num2
    [sum, product]

Here, calculateSumProduct computes the sum (sum) and product (product) of num1 and num2, and returns these two values as a tuple.

Using Tuples for Variable Declaration

When calling a function that returns a tuple, you can declare multiple variables to store the returned values:

[totalSum, totalProduct] = calculateSumProduct(high, low)

This line declares two variables, totalSum and totalProduct, which store the sum and product of high and low prices, respectively.

Tuples with Multiple Types

Tuples are not limited to containing values of a single type. For instance, the chartDetails function demonstrates returning a tuple with mixed types:

chartDetails() =>
    firstVisibleBarTime = chart.left_visible_bar_time
    firstVisibleClosePrice = ta.valuewhen(ta.cross(time, firstVisibleBarTime), close, 0)
    isChartStandard = chart.is_standard
    chartForegroundColor = chart.fg_color
    chartTickerID = syminfo.tickerid
    [firstVisibleBarTime, firstVisibleClosePrice, isChartStandard, chartForegroundColor, chartTickerID]

This function returns a tuple comprising an integer, a float, a boolean, a color, and a string, illustrating the flexibility of tuples in handling diverse data types.

Practical Use Case: security Function with Tuples

A common application of tuples is in the security function, where they can simplify requesting multiple data points in a single call. For example:

[roundedOpen, roundedHigh, roundedLow, roundedClose] = request.security(syminfo.tickerid, "D", [math.round_to_mintick(open), math.round_to_mintick(high), math.round_to_mintick(low), math.round_to_mintick(close)])

This line demonstrates using a tuple to request daily OHLC values, rounded to the nearest tick value, in a single security function call.

Conditional Structures Returning Tuples

Pine Script also supports returning tuples from local blocks of conditional structures such as if and switch statements:

[value1, value2] = if close > open
    [high, close]
    [close, low]


This construct allows for conditional assignment to multiple variables based on a single condition.

Key Features and Takeaways

  • Flexibility and Efficiency: Tuples enable the return and handling of multiple values from functions efficiently.
  • Type Inference: Pine Script automatically infers the types of variables declared to store tuple values, simplifying syntax and reducing code verbosity.
  • Mixed Type Support: Tuples can contain a mix of different types, making them versatile for various use cases.
  • Enhanced security Function Usage: Tuples streamline the process of requesting multiple data points with the security function.
  • Conditional Assignment: Tuples can be used within conditional structures to assign multiple values based on conditions.

By leveraging tuples, Pine Script developers can write more concise, readable, and functional code, enhancing the capabilities of their trading strategies and analysis tools.

Leave a Comment