Features of the mql5 language, subtleties and tricks - page 340

Transcoding error in the platform header \MQL5\Include\Math\Stat\Math.mqh after converting the file from UTF-16 to UTF-8.

Line 5299:

//| Reference:                                                       |
//| Hyndman, R. J. and Fan, Y. (1996)                                |
//| "Sample quantiles in statistical packages",                      |
//| American Statistician, vol.50, pp. 361–365.                      |

amrali #:

Transcoding error in the platform header \MQL5\Include\Math\Stat\Math.mqh after converting the file from UTF-16 to UTF-8.

Line 5299:


Seems something on your side ?

I also checked with B6090.

Alain Verleyen #:

Seems something on your side ?

I also checked with B6090.

Thanks for the confirmation.
b6157: When the array is resized, ArrayResize forcibly sets any additional elements to zero.
double GetTrash( const uint Size = 1 e8 )
{  
  double Res = 0;  
  double Array[];
  
  for (int i = ArrayResize(Array, Size); i-- > 0;)
    Res += Array[i];
    
  return(Size ? Res / Size : 1);  
}

void OnStart()
{
  Print(GetTrash()); // 0
}
Static arrays (and variables) are not initialised.
fxsaber ArrayResize ` forcibly sets the additional elements to zero.
Well, I don’t know – without confirmation from the developers, I wouldn’t rely on that. And there’s no mention of it anywhere in the documentation.

Pattern for real-time trailing drawdown enforcement in MQL5:

Prop firm EAs require a trailing maximum drawdown guard that tracks the highest equity peak and flattens all positions if current equity drops below a threshold from that peak. The subtlety most MQL5 developers miss: AccountInfoDouble(ACCOUNT_EQUITY) includes floating unrealized P&L, which is correct for this purpose — but the equity peak must be updated only on OnTick() , not on OnTradeTransaction() , to avoid phantom peaks from partially-filled orders:

double g_peakEquity = 0;
double g_maxDrawdownPct = 0.08; // 8% trailing drawdown

void OnTick()
{
   double equity = AccountInfoDouble(ACCOUNT_EQUITY);
   double balance = AccountInfoDouble(ACCOUNT_BALANCE);

   // Update peak only from confirmed equity, not mid-transaction
   if(equity > g_peakEquity)
      g_peakEquity = equity;

   // Check trailing drawdown breach
   if(g_peakEquity > 0)
   {
      double drawdown = (g_peakEquity - equity) / g_peakEquity;
      if(drawdown >= g_maxDrawdownPct)
      {
         CloseAllPositions(); // atomic flatten
         ExpertRemove();
      }
   }
}

Worth noting: initializing g_peakEquity to AccountInfoDouble(ACCOUNT_BALANCE) in OnInit() rather than 0 prevents a false drawdown trigger on the very first tick if floating loss already exists when the EA starts.

fxsaber ArrayResize ` forcibly sets additional elements to zero.

Zeroing is not guaranteed.

Slava #:

In the Tester, Balance/Equity chart data is written to a tst file.

//+------------------------------------------------------------------+
//| Structure for the test plan                               |
//+------------------------------------------------------------------+
struct TesterTradeState
{
  __int64           time;                // current test time
  double            balance;             // current balance
  double            equity;              // current equity
  double            value;               // current calculated deposit load value


I ran a single pass of the expert advisor, which opens a single position at the start, and the Tester automatically closes it at the end of the pass. Here is the chart.


The tabular data for this chart.

                  [time] [balance] [equity] [value]
[ 0] 2026.10.16 00:00:00  10000.00 10000.00    0.00
[ 1] 2026.10.16 04:34:40  10000.00  9752.10    0.00
[ 2] 2026.10.16 17:27:40  10000.00 10270.10    4.06
[ 3] 2026.10.19 12:12:00  10000.00  9999.00    4.17
[ 4] 2026.10.19 14:16:40  10000.00 10051.60    4.15
[ 5] 2026.10.20 13:09:40  10000.00  9396.80    4.44
[ 6] 2026.10.20 23:19:00  10000.00  9508.00    4.39
[ 7] 2026.10.22 01:00:40  10000.00  9737.30    4.28
[ 8] 2026.10.22 08:48:20  10000.00  8947.20    4.66
[ 9] 2026.10.22 11:26:00  10000.00  9189.80    4.54
[10] 2026.10.22 11:27:20  10000.00  9221.00    4.52
[11] 2026.10.23 17:30:20  10000.00  8035.00    5.19
[12] 2026.10.23 22:33:40  10000.00  8257.60    5.05
[13] 2026.10.26 03:03:20  10000.00  8060.90    5.18
[14] 2026.10.26 19:13:40  10000.00  8578.60    4.86
[15] 2026.10.27 09:46:20  10000.00  8330.40    5.01
[16] 2026.10.27 10:53:20  10000.00  8170.90    5.11
[17] 2026.10.28 02:10:40  10000.00  8987.70    4.64
[18] 2026.10.28 20:54:20  10000.00  8714.60    4.79
[19] 2026.10.28 22:01:40  10000.00  9105.90    4.58
[20] 2026.10.29 20:32:40  10000.00  8332.30    5.01
[21] 2026.10.29 23:00:30   8492.10  8492.10    0.00


I don’t understand the algorithm used to decide when to make the next entry. I’ve highlighted two entries in colour, with 80 seconds between them. I looked at the price chart for those 80 seconds – there was no movement. Anyway, could you please explain? But I have a suggestion as well.


When putting together a portfolio of two trading systems, one tries to calculate the correlation between their results.

  1. To do this, they take two equity vectors – the equity values from each backtest at identical time points (for example, at the start of every hour or at the start of every day).
  2. After conversion, the correlation between them is calculated.
    vector GetVector( const double &Array[] )
    {
      vector Vector;
      
      Vector.Assign(Array);
      
      return(Vector);
    }
    
    double GetCorr( const double &Array1[], const double &Array2[] )
    {
      return(GetVector(Array1).CorrCoef(GetVector(Array2)));
    }
    
    void ChangeEquity( double &Equity[] )
    {
      for (int i = ArraySize(Equity) - 1; i > 0; i--)
        Equity[i] = Equity[i] / Equity[i - 1] - 1;
          
      Equity[0] = 0; // ArrayRemove(Equity, 0, 1);
    }
    
    void OnStart()
    {
      double Equity1[]; // Equity of the first trading system – recorded every hour.
      double Equity2[]; // Equity of the second trading system – recorded every hour.
    
      ChangeEquity(Equity1);
      ChangeEquity(Equity2);
      
      Print(GetCorr(Equity1, Equity2)); // Correlation between two trading systems.
    }

So, I’d like to ask you to consider the possibility of always recording the Equity/Balance value every hour in the TST file during a single run.

This would provide the data needed to calculate correlations between trading systems. We would also obtain an Equity/Balance graph, where the x-axis represents time. Thank you.

Search term: Improvement 165.