Matrices and vectors in MQL5 - page 3

 

Forum on trading, automated trading systems and testing of trading strategies

Machine learning in trading: theory, models, practice and algo trading

Renat Fatkhullin , 2025.06.09 14:15

For your information, new for machine learning in MQL5:

  • MQL5: Added matrix multiplication operator @ . It works according to the rules of linear algebra and allows you to multiply matrices and vectors, as well as perform the scalar product of vectors.

    Matrix multiplication (matrix × matrix)
    matrix A( 2 , 3 );
    matrix B( 3 , 2 );
    matrix C = A @ B; // Результат: матрица C размером [2,2]
    Matrix multiplication (matrix × vector)
    matrix M( 2 , 3 );
    vector V( 3 );
    vector R = M @ V; // Результат: вектор R из 2 элементов
    Matrix multiplication (vector x matrix)
    matrix M( 2 , 3 );
    vector V( 1 , 2 );
    vector R = V @ M; // Результат: вектор R из 3 элементов
    Dot product (vector × vector)
     vector V1( 1 , 3 ), V2( 1 , 3 );
    double r = V1 @ V2; // Результат: скаляр
  • MQL5: Added the ddof parameter to the Std , Var and Cov methods. It defines the number of degrees of freedom that are subtracted from the divisor when calculating the standard deviation. For Std and Var, the default parameter is 0, for Cov it is 1.

    How does ddof affect:

    • By default ddof=0, and the standard deviation is calculated for the entire population (population standard deviation).
    • If ddof=1, then the sample standard deviation is used, which corrects the estimate for the final sample (used in statistics when working with a subset of data).

  • MQL5: Added new OpenBLAS methods:

    Calculating eigenvalues and eigenvectors

    • EigenTridiagonalDC - computes eigenvalues and eigenvectors of a symmetric tridiagonal matrix using a divide-and-conquer algorithm (LAPACK function STEVD).
    • EigenTridiagonalQR - computes eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the QR algorithm (LAPACK STEV function).
    • EigenTridiagonalRobust - computes the eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the MRRR (Multiple Relatively Robust Representations) algorithm (LAPACK function STEVR).
    • EigenTridiagonalBisect - computes the eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the bisection algorithm (LAPACK function STEVX).
    • ReduceToBidiagonal — reduces a general m×n real or complex matrix to upper or lower bidiagonal form B using the orthogonal transformation: Q**T * A * P = B. If m≥n, then B is upper bidiagonal, otherwise lower bidiagonal. (LAPACK function GEBRD).
    • ReflectBidiagonalToQP — generates orthogonal matrices Q and P**T (or P**H for complex types) defined by the ReduceToBidiagonal method when reducing a real or complex matrix A to bidiagonal form: A = Q * B * P**T. Q and P**T are products of elementary reflectors H(i) or G(i), respectively. (LAPACK functions ORGBR, UNGBR).
    • ReduceSymmetricToTridiagonal — reduces a real symmetric or complex Hermitian matrix A to tridiagonal form B using an orthogonal similarity transformation: Q**T * A * Q = B. (LAPACK functions SYTRD, HETRD).
    • ReflectTridiagonalToQ — generates an orthogonal matrix Q that is the product of n-1 elementary reflectors of order n returned by the ReduceSymmetricToTridiagonal function.

    • LinearEquationsSolution — calculates a system of linear equations with a square matrix of coefficients A and several right-hand sides.
    • LinearEquationsSolutionTriangular — calculates a system of linear equations with a square-triangular matrix of coefficients A and several right-hand sides.
    • LinearEquationsSolutionSy — computes a system of linear equations with a symmetric or Hermitian conjugate matrix A and several right-hand sides.
    • LinearEquationsSolutionComplexSy — calculates a system of linear equations with a complex symmetric matrix A and several right-hand sides.
    • LinearEquationsSolutionGeTrid — computes a system of linear equations with a symmetric or Hermitian conjugate positive definite matrix A and several right-hand sides.
    • LinearEquationsSolutionSyPD — calculates a system of linear equations with a common (non-symmetric) tridiagonal coefficient matrix A and several right-hand sides.
    • LinearEquationsSolutionSyTridPD — computes a system of linear equations with a symmetric tridiagonal positive definite matrix A and several right-hand sides.
    • FactorizationQR - computes the QR factorization of a general m-by-n matrix: A = Q * R (the LAPACK function of GEQRF).
    • FactorizationQRNonNeg — computes the QR factorization of a general m-by-n matrix: A = Q * R, where R is an upper triangular matrix with non-negative entries on the diagonal (LAPACK function GEQRFP).
    • FactorizationQRPivot - computes the QR factorization of a general m-by-n matrix with column permutation: A * P = Q * R (GEQP3 LAPACK function).
    • FactorizationLQ — performs the LQ factorization of a general m-by-n matrix: A = L * Q (LAPACK function of GELQF).
    • FactorizationQL - performs the QL factorization of a general m-by-n matrix: A = Q * L (the LAPACK function GEQLF).
    • FactorizationRQ - performs the RQ factorization of a general m-by-n matrix: A = R * Q (LAPACK function GERQF).
    • FactorizationPLU - Computes the LU factorization of a general m-by-n matrix A using partial pivot selection and row permutations (LAPACK function GETRF).
    • FactorizationPLUGeTrid - computes the LU factorization of a general (non-symmetric) n-by-n tridiagonal matrix A using partial pivot selection and row permutations (LAPACK function GTTRF).
    • FactorizationLDL — computes the factorization for a real symmetric or complex Hermitian matrix A using diagonal pivot selection according to the Bunch-Kaufman method (LAPACK functions SYTRF and HETRF).
    • FactorizationLDLSyTridPD — computes the factorization of a symmetric positive definite (for real data) or Hermitian positive definite (for complex data) tridiagonal matrix A (LAPACK PTTRF).
    • FactorizationCholesky — computes the factorization for a real symmetric or complex Hermitian positive-definite matrix A (LAPACK POTRF function).
    • FactorizationCholeskySyPS — computes the complete pivoting Cholesky factorization of a real symmetric (or complex Hermitian) positive semidefinite n-by-n matrix A (LAPACK function PSTRF).

  • MQL5: Added the Random function and method for filling vectors and matrices with random values. Random values are generated uniformly in a given range.
     static vector vector ::Random(
       const ulong    size,       // длина вектора
       const double   min= 0.0 ,     // минимальное значение
       const double   max= 1.0      // максимальное значение
       );
    
    static matrix matrix ::Random(
       const ulong    rows,       // количество строк
       const ulong    cols         // количество столбцов
       const float    min= 0.0 ,     // минимальное значение
       const float    max= 1.0      // максимальное значение
       );

  •  

    New maths functions (interesting to check): MQL5 Reference - Matrix and Vector - MethodsOpenBLAS - https://www.mql5.com/en/docs/matrix/openblas 

    More in details: New MetaTrader 5 Platform Build 5200: Extended OpenBLAS support and enhanced control in MQL5

    Documentation on MQL5: Matrix and Vector Methods / OpenBLAS
    Documentation on MQL5: Matrix and Vector Methods / OpenBLAS
    • www.mql5.com
    OpenBLAS is a high-performance open-source linear algebra library that implements BLAS (Basic Linear Algebra Subprograms) and some LAPACK functions...
     

    Forum on trading, automated trading systems and testing trading strategies

    Self learning expert

    Sergey Golubev, 2025.08.27 05:48

    Self Optimizing Expert Advisors in MQL5 (Part 13): A Gentle Introduction To Control Theory Using Matrix Factorization

    Self Optimizing Expert Advisors in MQL5 (Part 13): A Gentle Introduction To Control Theory Using Matrix Factorization

    There are many reasons we can use to explain this behavior in trading applications. However, one key understanding is that once our applications have been developed and deployed, their behavior remains fixed and cannot usually be modified without human intervention. This means our strategies are vulnerable to repeating the same mistakes over and over again without ever profiting from failure or learning from past mistakes.

     

    Forum on trading, automated trading systems and testing trading strategies

    Self learning expert

    Sergey Golubev, 2025.09.12 05:18

    Self Optimizing Expert Advisors in MQL5 (Part 14): Viewing Data Transformations as Tuning Parameters of Our Feedback Controller

    Self Optimizing Expert Advisors in MQL5 (Part 14): Viewing Data Transformations as Tuning Parameters of Our Feedback Controller

    It is an important control knob in the pipeline that is often hidden away in the shadows of its bigger brothers. Commonly, optimizers or shiny model architectures mainly get the focus and research work, and large amounts of academia are poured into those directions. But little time is spent studying the effects of pre-processing techniques.

    Silently, the pre-processing that we apply to the data at hand impacts model performance in ways that can be surprisingly large. Even small percentage improvements made in pre-processing can compound over time and materially affect the profitability and risk of our trading applications.


     

    Forum on trading, automated trading systems and testing trading strategies

    Self learning expert

    Sergey Golubev, 2025.10.16 08:47

    Self Optimizing Expert Advisors in MQL5 (Part 15): Linear System Identification

    Self Optimizing Expert Advisors in MQL5 (Part 15): Linear System Identification

    Trading systems are complex applications expected to operate in chaotic, dynamic environments — a challenge for even the most experienced developers. It’s nearly impossible to define every correct action a trading application should take, as market outcomes are virtually infinite. Maintaining control and ensuring consistent profitability under such uncertainty remains one of the greatest challenges in algorithmic trading.

     

    Forum on trading, automated trading systems and testing trading strategies

    Self learning expert

    Sergey Golubev, 2025.11.01 10:47

    Self Optimizing Expert Advisors in MQL5 (Part 16): Supervised Linear System Identification

    Self Optimizing Expert Advisors in MQL5 (Part 16): Supervised Linear System Identification

    In our previous discussion on feedback controllers, we learned that these systems can stabilize the performance of trading strategies by first observing their behavior in action. We have provided a quick link to the previous discussion, here. This application design, allowed us to capture the dominant correlational structures that persisted across both winning and losing trades. In essence, feedback controllers helped our trading application learn how to behave optimally under current market conditions—much like human traders, who focus less on predicting the future and more on responding intelligently to the present.