Home » Mathemtical Functions » Understanding matrix.eigenvectors() Function in Pine Script

Understanding matrix.eigenvectors() Function in Pine Script

Photo of author
Published on

In the world of trading algorithms and technical analysis, Pine Script has become a pivotal tool for creating custom indicators and strategies. Today, we’re delving into a particularly advanced feature: matrix.eigenvectors().

Syntax

The matrix.eigenvectors() function is designed to return a matrix of eigenvectors, where each column represents an eigenvector of the input matrix. Pine Script supports this function in two formats, catering to matrices of integers and floating-point numbers:

  • matrix.eigenvectors(id) → matrix<float>
  • matrix.eigenvectors(id) → matrix<int>

Arguments:

  • id (matrix<int/float>): This argument takes a matrix object, which can be either integer or floating-point type, depending on your specific needs.

Example

Let’s dive into a practical example to understand how matrix.eigenvectors() is used in Pine Script:

//@version=5
indicator("`matrix.eigenvectors()` Example")

// Execute this code only once for efficiency.
if barstate.islastconfirmedhistory
    // Create a 2x2 integer matrix 
    var m1 = matrix.new<int>(2, 2, 1)
    // Populate the matrix with specific values.
    matrix.set(m1, 0, 0, 2)
    matrix.set(m1, 0, 1, 4)
    matrix.set(m1, 1, 0, 6)
    matrix.set(m1, 1, 1, 8)
    
    // Obtain the eigenvectors of the matrix.
    m2 = matrix.eigenvectors(m1)
    
    // Display the original matrix and its eigenvectors.
    var t = table.new(position.top_right, 2, 2, color.green)
    table.cell(t, 0, 0, "Matrix Elements:")
    table.cell(t, 0, 1, str.tostring(m1))
    table.cell(t, 1, 0, "Matrix Eigenvectors:")
    table.cell(t, 1, 1, str.tostring(m2))
Example

Walkthrough

  1. Version Declaration: The script starts with //@version=5, indicating it uses version 5 of Pine Script, which supports matrices and other advanced features.
  2. Indicator Declaration: The indicator() function is used to declare a new indicator script titled “matrix.eigenvectors() Example”. This title appears in the TradingView indicators list and on the chart.
  3. Efficiency Check: The if barstate.islastconfirmedhistory condition ensures that the matrix calculations and the subsequent operations are executed only once, specifically on the last historical bar of the chart. This is done to optimize performance, as calculating eigenvectors can be computationally intensive.
  4. Matrix Creation: A 2×2 integer matrix m1 is initialized with all elements set to 1, using the matrix.new<int>(2, 2, 1) function. This creates a variable matrix of integers with dimensions 2×2.
  5. Matrix Population: The matrix.set() function is used four times to populate m1 with specific values, replacing the initial values. The matrix is filled as follows:
    • Top-left element (0,0) is set to 2.
    • Top-right element (0,1) is set to 4.
    • Bottom-left element (1,0) is set to 6.
    • Bottom-right element (1,1) is set to 8.
  6. Eigenvectors Calculation: The eigenvectors of the matrix m1 are calculated using the matrix.eigenvectors(m1) function. The result is stored in a new matrix m2.
  7. Display Setup: A table t is created and positioned at the top right of the chart with 2 columns and 2 rows, using table.new(position.top_right, 2, 2, color.green). This table is used to display the matrix and its eigenvectors.
  8. Displaying Information:
    • The first cell in the top row of the table is labeled “Matrix Elements:”, serving as a header for displaying the original matrix m1.
    • The second cell in the top row displays the string representation of m1, using str.tostring(m1).
    • The first cell in the second row is labeled “Matrix Eigenvectors:”, serving as a header for displaying the eigenvectors matrix m2.
    • The second cell in the second row displays the string representation of m2, using str.tostring(m2).

Key Features and Takeaways

  • Function Usability: matrix.eigenvectors() is versatile, supporting matrices of both integer and float types, making it suitable for a wide range of mathematical and financial calculations in Pine Script.
  • Syntax and Application: The function simplifies complex mathematical operations, allowing traders and developers to incorporate advanced mathematical concepts like eigenvectors into their custom indicators and strategies.
  • Practical Use Case: Understanding and utilizing eigenvectors can be crucial for strategies that involve multivariate data analysis or optimization problems, enhancing the sophistication and effectiveness of trading algorithms.

Leave a Comment