MQL5

Net exposure by currency

Breaks each Forex position down into long/short exposure per currency (base bought, quote sold) to reveal hidden concentration — three "different" trades can be a single USD bet.

Prerequisites

MetaTrader 5, MQL5, symboles Forex standard

MQL5
void PrintCurrencyExposure()
{
   string curr[]; double expo[];

   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if(PositionGetTicket(i) == 0) continue;
      string sym  = PositionGetString(POSITION_SYMBOL);
      string base = SymbolInfoString(sym, SYMBOL_CURRENCY_BASE);
      string quot = SymbolInfoString(sym, SYMBOL_CURRENCY_PROFIT);
      double lots = PositionGetDouble(POSITION_VOLUME);
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         lots = -lots;

      AddExposure(curr, expo, base,  lots);  // acheter EURUSD = long EUR
      AddExposure(curr, expo, quot, -lots);  //                = short USD
   }
   for(int i = 0; i < ArraySize(curr); i++)
      PrintFormat("%s : %+.2f lots nets", curr[i], expo[i]);
}

void AddExposure(string &curr[], double &expo[], string c, double lots)
{
   for(int i = 0; i < ArraySize(curr); i++)
      if(curr[i] == c) { expo[i] += lots; return; }
   int n = ArraySize(curr);
   ArrayResize(curr, n + 1); ArrayResize(expo, n + 1);
   curr[n] = c; expo[n] = lots;
}

Result

2026.06.10 11:45:00.330  ExpoMap (EURUSD,M15)  Positions : BUY 0.50 EURUSD | BUY 0.30 GBPUSD | SELL 0.20 USDJPY
2026.06.10 11:45:00.331  ExpoMap (EURUSD,M15)  EUR : +0.50 lots nets
2026.06.10 11:45:00.331  ExpoMap (EURUSD,M15)  USD : -1.00 lots nets
2026.06.10 11:45:00.332  ExpoMap (EURUSD,M15)  GBP : +0.30 lots nets
2026.06.10 11:45:00.332  ExpoMap (EURUSD,M15)  JPY : +0.20 lots nets
2026.06.10 11:45:00.333  ExpoMap (EURUSD,M15)  Concentration : 3 trades "différents" = 1 seul pari anti-USD
ExpositionDevisesPortefeuilleRisk Management

Related snippets

Back to the Data Lab