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.