matrix::CopyRates() vs CopyRates() - Inconsistent Data Mapping

 

Hi,

I am experiencing a discrepancy in data integrity when using matrix::CopyRates() compared to the legacy CopyRates() function.


For the same symbol, timeframe, and depth, I am unable to retrieve the same Close values, and the resulting matrix structure appears inconsistent with the requested parameters.

Is there a documented layout I am missing, or is there a bug in the matrix::CopyRates() implementation?


Here is a reproduction of the issue

//+------------------------------------------------------------------+
//|                                           Benchmark_Matrix.mq5   |
//|                                  Copyright 2026, MetaQuotes Bug  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input string   gu_symbol = "XAUUSD";
input int      gu_depth  = 10;

int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
{
   MqlRates i_rates[];
   matrix   im_ohlct;
   matrix   im_ohlc;
   string   s_report = "";
   
   ArraySetAsSeries(i_rates, false);
   CopyRates(gu_symbol, _Period, 0, gu_depth, i_rates);
   
   im_ohlct.CopyRates(gu_symbol, _Period, COPY_RATES_OHLCT, 0, gu_depth);
   im_ohlc.CopyRates(gu_symbol, _Period, COPY_RATES_OHLC, 0, gu_depth);
   
   s_report  = "--- EVIDENCE OF DATA CORRUPTION ---\n";
   s_report += "Legacy Close[0]: " + (string)i_rates[0].close + "\n\n";
   
   // Vérification OHLC(T) sur la dimension inversée
   // Si Rows=5, cols=10, le Close est dans la colonne 4, mais peut être en ligne 4 selon transposition
   s_report += "Matrix OHLCT[0,4]: " + (string)im_ohlct[0, 4] + "\n";
   s_report += "Matrix OHLCT [4,0]: " + (string)im_ohlct[4, 0] + "\n\n";
   
   s_report += "Matrix OHLC[0,3]: " + (string)im_ohlc[0, 3] + "\n";
   s_report += "Matrix OHLC [3,0]: " + (string)im_ohlc[3, 0] + "\n";

   Comment(s_report);
   
   return rates_total;
}
Output received:
"--- EVIDENCE OF DATA CORRUPTION ---
Legacy Close[0]: 3271.8

Matrix OHLCT[0,4]: 3309.67
Matrix OHLCT [4,0]: 1746021600.0

Matrix OHLC[0,3]: 3308.55
Matrix OHLC [3,0]: 3271.8
" (length: 174)
 

Probably you need to add the flag COPY_RATES_VERTICAL.

PS. For troubleshooting it's better to print out complete arrays/matrices.

 
Gerard William G J B M Dinh Sy:

Hi,

I am experiencing a discrepancy in data integrity when using matrix::CopyRates() compared to the legacy CopyRates() function.


For the same symbol, timeframe, and depth, I am unable to retrieve the same Close values, and the resulting matrix structure appears inconsistent with the requested parameters.

Is there a documented layout I am missing, or is there a bug in the matrix::CopyRates() implementation?


Here is a reproduction of the issue

Output received:
"--- EVIDENCE OF DATA CORRUPTION ---
Legacy Close[0]: 3271.8

Matrix OHLCT[0,4]: 3309.67
Matrix OHLCT [4,0]: 1746021600.0

Matrix OHLC[0,3]: 3308.55
Matrix OHLC [3,0]: 3271.8

" (length: 174)

All is correct. You are probably confused in the indexes. By default (horizontal alignment) OHLC/OHLCT values for 1 candle are copied in a column, so Close values are always in row 3.

Should be :

   s_report += "Matrix OHLCT [3,0]: " + (string)im_ohlct[3, 0] + "\n\n";