MQL5

System expectancy (expectancy in R)

Computes win rate, average win and average loss over the history, then expectancy per trade in currency AND in multiples of R — the single number that tells you whether the system is worth running.

Prerequisites

MetaTrader 5, MQL5

MQL5
void ExpectancyReport(int days)
{
   if(!HistorySelect(TimeCurrent() - days * 86400, TimeCurrent()))
      return;
   double gains = 0, losses = 0;
   int nw = 0, nl = 0;
   for(int i = 0; i < HistoryDealsTotal(); i++)
   {
      ulong d = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(d, DEAL_ENTRY) != DEAL_ENTRY_OUT)
         continue;
      double p = HistoryDealGetDouble(d, DEAL_PROFIT);
      if(p >= 0) { gains += p; nw++; } else { losses += -p; nl++; }
   }
   int n = nw + nl;
   if(n < 20 || nl == 0)
   {
      PrintFormat("Expectancy : échantillon insuffisant (%d trades)", n);
      return;
   }
   double avgWin = nw > 0 ? gains / nw : 0, avgLoss = losses / nl;
   double winrate = (double)nw / n;
   double expectancy = winrate * avgWin - (1 - winrate) * avgLoss;
   PrintFormat("=== EXPECTANCY (%d jours, %d trades) ===", days, n);
   PrintFormat("Winrate %.1f %% | gain moyen %.2f | perte moyenne %.2f",
               100 * winrate, avgWin, avgLoss);
   PrintFormat("Espérance par trade : %+.2f soit %+.2f R", expectancy, expectancy / avgLoss);
   PrintFormat("Verdict : %s", expectancy > 0 ? "espérance POSITIVE — le système a un edge"
                                              : "système PERDANT sur la période");
}

Result

2026.06.10 22:10:30.456  EdgeStats (EURUSD,H1)  === EXPECTANCY (60 jours, 87 trades) ===
2026.06.10 22:10:30.457  EdgeStats (EURUSD,H1)  Winrate 58.6 % | gain moyen 42.18 | perte moyenne 38.50
2026.06.10 22:10:30.457  EdgeStats (EURUSD,H1)  Espérance par trade : +8.78 soit +0.23 R
2026.06.10 22:10:30.458  EdgeStats (EURUSD,H1)  Verdict : espérance POSITIVE — le système a un edge
ExpectancyStatistiquesWinrateR-multiple

Related snippets

Back to the Data Lab