Home » Mathemtical Functions » Understanding Reading and Writing Matrix Elements in Pine Script

Understanding Reading and Writing Matrix Elements in Pine Script

Photo of author
Published on

In this tutorial, we’ll delve into the powerful capabilities of Pine Script for handling matrices, focusing on the matrix.get() and matrix.set() functions. These functions are pivotal for manipulating matrix elements by retrieving and updating their values based on specific row and column indices. 

Understanding matrix.get() and matrix.set()

  • matrix.get() Function: This function plays a crucial role in accessing the value of a matrix element at a given row and column index. It’s particularly useful when you need to read and utilize the current value of a matrix element in your calculations or for display purposes.
  • matrix.set() Function: Conversely, the matrix.set() function allows you to modify the value of a matrix element at a specified row and column index. This is essential when you want to update matrix elements based on new data or calculations as your script executes over time.

Practical Example: Modifying Matrix Elements

Let’s explore a practical example to illustrate how these functions can be implemented in a script. We’ll define a square matrix named numMatrix with dimensions 2×2 and initialize all elements to 0.0 at the first bar. Our goal is to increment the values of these elements at different intervals using the matrix.get() and matrix.set() methods, and then plot these values on the chart.

//@version=5
indicator("Matrix Element Modification Demo")

// Define a 2x2 square matrix of `float` values.
var numMatrix = matrix.new<float>(2, 2, 0.0)

switch
    bar_index % 11 == 0 => numMatrix.set(0, 0, numMatrix.get(0, 0) + 1.0) // Increment value at row 0, column 0 every 11th bar.
    bar_index % 7  == 0 => numMatrix.set(0, 1, numMatrix.get(0, 1) + 1.0) // Increment value at row 0, column 1 every 7th bar.
    bar_index % 5  == 0 => numMatrix.set(1, 0, numMatrix.get(1, 0) + 1.0) // Increment value at row 1, column 0 every 5th bar.
    bar_index % 3  == 0 => numMatrix.set(1, 1, numMatrix.get(1, 1) + 1.0) // Increment value at row 1, column 1 every 3rd bar.

plot(numMatrix.get(0, 0), "Row 0, Column 0 Value", color.red, 2)
plot(numMatrix.get(0, 1), "Row 0, Column 1 Value", color.orange, 2)
plot(numMatrix.get(1, 0), "Row 1, Column 0 Value", color.green, 2)
plot(numMatrix.get(1, 1), "Row 1, Column 1 Value", color.blue, 2)
Example
  1. Version Declaration: The script starts with //@version=5, specifying that it uses version 5 of Pine Script. This ensures compatibility with the latest features and syntax of the language.
  2. Indicator Declaration: indicator("Matrix Element Modification Demo") defines a new indicator named “Matrix Element Modification Demo”. This line makes the script available for adding to charts on the TradingView platform.
  3. Matrix Initialization:
    • var numMatrix = matrix.new<float>(2, 2, 0.0) initializes a 2×2 matrix named numMatrix with all elements set to 0.0. This matrix is declared with var to ensure its value persists across chart bars.
  4. Conditional Updates Using switch:
    • The script uses a switch statement to conditionally update matrix elements based on the current bar_index. The bar_index represents the index of the current bar on the chart, starting from 0.
    • Each condition within the switch statement checks if the bar_index modulo a certain number equals 0, determining if the script should update a specific matrix element.
  5. Matrix Element Updates:
    • The conditions within the switch are as follows:
      • Every 11th bar, increment the value at row 0, column 0 by 1.0.
      • Every 7th bar, increment the value at row 0, column 1 by 1.0.
      • Every 5th bar, increment the value at row 1, column 0 by 1.0.
      • Every 3rd bar, increment the value at row 1, column 1 by 1.0.
    • These updates are performed using numMatrix.set(row, column, numMatrix.get(row, column) + 1.0), which retrieves the current value with numMatrix.get(row, column), adds 1.0 to it, and then updates the element with the new value using numMatrix.set().
  6. Plotting Matrix Values:
    • The script plots the current values of each matrix element on the chart using the plot() function. Each plot is labeled and colored differently to distinguish between the matrix elements:
      • “Row 0, Column 0 Value” is plotted in red.
      • “Row 0, Column 1 Value” is plotted in orange.
      • “Row 1, Column 0 Value” is plotted in green.
      • “Row 1, Column 1 Value” is plotted in blue.

Walkthrough of the Example

  • Matrix Initialization: The matrix numMatrix is initialized with zero values. This serves as our starting point for the demonstration.
  • Element Modification: Through the use of conditional statements (switch and modulo operations), we determine the specific bars at which each element’s value should be incremented. This showcases the dynamic nature of matrix manipulation over time.
  • Plotting Values: Finally, we plot the current values of each matrix element on the chart. This visual representation aids in understanding the behavior of the script and the impact of our matrix operations.

Key Features and Takeaways

  • Function Usability: The matrix.get() and matrix.set() functions are essential for reading and modifying matrix elements, respectively. They allow for dynamic data manipulation within Pine Script projects.
  • Syntax and Application: The syntax for these functions is straightforward, requiring the row and column indices for targeting specific elements. This example demonstrates their application in a practical scenario.
  • Enhancing Scripts with Matrix Operations: Incorporating matrices and these functions into your scripts can significantly enhance their capabilities, allowing for complex data storage and manipulation.

By mastering matrix.get() and matrix.set(), you unlock new possibilities for data manipulation in Pine Script, enhancing the functionality and efficiency of your trading algorithms.

Leave a Comment