Skip to content
Omega Joctan edited this page Mar 3, 2024 · 4 revisions

MatrixExtend(MatrixExtend.mqh)

The MatrixExtend class in MQL5 extends the capabilities of the built-in MetaTrader 5 matrix class, providing a diverse array of utility functions and methods for efficient matrix manipulation, data transformations, and machine learning tasks within your trading strategies. This class aims to streamline your MQL5 coding experience by encapsulating common matrix operations and offering convenient functionalities.

Key Functionalities

  • File I/O:

    • WriteCsv(csv_name, matrix_, header[], bool common=false, int digits=5): Writes a matrix to a CSV file with optional headers and formatting control.
    • WriteCsv(csv_name, matrix_, string header_string="", bool common=false, int digits=5): Alternative version with a single header string.
    • ReadCsv(string file_name, string &headers, string delimiter=",", bool common=false): Reads a CSV file into a matrix, extracting headers.
    • DBtoMatrix(int db_handle, string table_name, string &column_names[], int total=WHOLE_ARRAY): Retrieves data from a MetaTrader 5 database table into a matrix.
  • Manipulations:

    • RemoveCol(matrix &mat, ulong col): Removes a specified column from a matrix.
    • RemoveMultCols(matrix &mat, int &cols[]): Removes multiple columns from a matrix using an integer array of column indices.
    • RemoveMultCols(matrix &mat, int from, int total=WHOLE_ARRAY): Removes a range of consecutive columns from a matrix.
    • RemoveRow(matrix &mat, ulong row): Removes a specified row from a matrix.
    • VectorRemoveIndex(vector &v, ulong index): Removes an element from a vector at a given index.
  • Machine Learning:

    • XandYSplitMatrices(const matrix<T> &matrix_, matrix<T> &xmatrix, vector<T> &y_vector, int y_column=-1): Splits a matrix into separate feature (X) and target (y) matrices for machine learning.
    • TrainTestSplitMatrices(const matrix<T> &matrix_, matrix<T> &x_train, vector<T> &y_train, matrix<T> &x_test, vector<T> &y_test, double train_size=0.7, int random_state=-1): Splits a matrix into training and testing sets for machine learning, with optional randomization.
    • DesignMatrix(matrix &x_matrix): Creates a design matrix (one-hot encoded) from a categorical feature matrix.
    • OneHotEncoding(const vector &v): Performs one-hot encoding on a vector of categorical values.
    • Sign(matrix &x) and Sign(vector &x): Applies the sign function (element-wise) to a matrix or vector.
    • eye(uint num_features): Generates an identity matrix of the specified size.
  • Detection:

    • Unique(const string &Array[], string &classes_arr[]) and Unique(const vector &v): Identifies unique elements (classes) within an array or vector.
    • Unique_count(vector &v): Counts the occurrences of each unique element in a vector.
  • Random Data Generation:

    • Random(T min, T max, int size, int random_state=-1): Generates a random vector of a specified type with optional random seed.
    • Random(double min, double max, ulong rows, ulong cols, int random_state=-1): Generates a random matrix within a specified range.
  • Search:

    • Search(const vector<T> &v, T value): Searches for a value within a vector and returns its indices.
  • Transformations:

    • VectorToMatrix(const vector &v, ulong cols=1): Converts a vector into a matrix with a specified number of columns.
    • MatrixToVector(const matrix<T> &mat): Converts a matrix into a vector.
    • ArrayToVector(const T &Arr[]) and VectorToArray(const vector<T> &v, T &arr[]): Converts between arrays and vectors.
  • Manipulations:

    • concatenate(const matrix &mat1, const matrix &mat2, int axis = 0): Concatenates two matrices along a specified axis (0 for rows, 1 for columns).
    • concatenate(const matrix<T> &mat, const vector<T> &v, int axis=1): Concatenates a matrix and a vector along a specified axis.
    • Copy(const vector<T> &src, vector<T> &dst, ulong src_start, ulong total=WHOLE_ARRAY): Copies a portion of a source vector to a destination vector.
    • Reverse(vector<T> &v) and Reverse(matrix<T> &mat): Reverses the order of elements in a vector or matrix.
    • HadamardProduct(matrix &a, matrix &b): Performs element-wise multiplication between two matrices (Hadamard product).
  • Randomization:

    • Randomize(vector<T> &v, int random_state=-1, bool replace=false) and Randomize(matrix<T> &matrix_, int random_state=-1, bool replace=false): Randomizes elements in a vector or matrix with optional replacement.
  • Normalization:

    • NormalizeDouble_(vector<T> &v, int digits=3) and NormalizeDouble_(matrix<T> &mat, int digits=3): Normalizes double-precision values in a vector or matrix to a specified number of decimal places.
  • Data Access:

    • CopyBufferVector(int handle, int buff_num, int start_pos, int count, vector &v): Copies data from a MetaTrader 5 buffer to a vector.
    • Stringfy(vector &v, int digits = 2): Converts a vector into a string representation.
    • Zeros(ulong rows, ulong cols) and Zeros(ulong size): Creates zero matrices or vectors of specified dimensions.
    • Get(const matrix &mat, ulong start_index, ulong end_index) and Get(const vector &v, ulong start_index, ulong end_index): Extracts a sub-matrix or sub-vector based on starting and ending indices.
  • Sorting:

    • Sort(vector<T> &v, ENUM_SORT_MODE sort_mode=SORT_ASCENDING): Sorts a vector in ascending or descending order.
    • ArgSort(vector<T> &v): Returns the indices of the sorted elements in a vector.
  • Others:

    • PrintShort(matrix &matrix_, ulong rows=5, int digits=5): Prints a concise representation of a matrix, showing the first rows rows with digits decimal places.

Important Notes:

  • The MatrixExtend class provides template-based functions, allowing you to use it with various data types (e.g., double, int, string).
  • For detailed information and usage examples of each function, refer to the MQL5 documentation or the code itself.