Introduction

Especially in pairs such as EURUSD, incorrectly calculated values of Equity, Margin, Free Margin, and Margin Level can mislead investors and weaken risk management.

This article addresses the development of an Account Position Calculator to help investors transparently and reliably track their positions.

Calculates position size, margin, and floating P/L using precise formulas.

Automatically reports Equity, Free Margin, and Margin Level values.

Thanks to its modular code structure, it can be easily applied to both BUY and SELL scenarios.

Opening Sell Positions

Let’s create a new class CAccountReport.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//+-----------------------------------------------------------------------------+
//|                                             Copyright 2026, MetaQuotes Ltd. |
//|                                                       https://www.mql5.com. |
//| EUR/USD Account Position Calculator                                         |
//+-----------------------------------------------------------------------------+
class CAccountReport
{
private:
   string   symbol;
   string   orderType;
   double   balance;
   double   contractSize;
   double   leverage;
   double   lot;
   double   openPrice;
   double   currentPrice;
   double   stopLoss;
   double   takeProfit;
   double   commission;
   double   swap;
   double   pipSize;

   double   spread;
   double   pips;
   double   positionSize;
   double   profit;
   double   pipValueUSD;
   double   netProfit;
   double   equity;
   double   margin;
   double   freeMargin;
   double   marginLevel;

public:
  // Constructor
  CAccountReport(string _symbol,
                string _orderType,
                double _balance,
                double _contractSize,
                double _leverage,
                double _lot,
                double _openPrice,
                double _currentPrice,
                double _stopLoss,
                double _takeProfit,
                double _commission,
                double _swap)
  {
    symbol       = _symbol;
    orderType    = _orderType;
    balance      = _balance;
    contractSize = _contractSize;
    leverage     = _leverage;
    lot          = _lot;
    openPrice    = _openPrice;
    currentPrice = _currentPrice;
    stopLoss     = _stopLoss;
    takeProfit   = _takeProfit;
    commission   = _commission;
    swap         = _swap;

    // For EURUSD → 5-digit pricing, point = 0.00001, pip = 0.0001
    pipSize      = 0.00001;

    Calculate();
  }

  // Calculation method
  void Calculate()
  {
    
    /// Position size in base currency
    // Order size (e.g., 1.00 lot)
    // Contract Size → Contract size for 1 lot (usually 100,000 in Forex)
    positionSize = lot * contractSize;

    // Spread and pips calculation
    spread       = currentPrice - openPrice;
    pips         = spread / pipSize;
    pipValueUSD  = (positionSize * pipSize) / currentPrice;

    // Profit calculation
    if(orderType == "BUY")
        profit = spread * positionSize;       // BUY → positive
    else if(orderType == "SELL")
        profit = -(spread * positionSize);    // SELL → negative

    // Net profit including commission and swap
    netProfit = profit - commission + swap;

    // Equity
    equity = balance + profit - commission + swap;

    // Margin
    margin = (positionSize * openPrice) / leverage;

    // Free Margin
    freeMargin = equity - margin;

    // Margin Level
    marginLevel = (margin > 0 ? (equity / margin) * 100 : 0);
  }

   // Print report
  void PrintReport()
  {
      Print("=== Account Position Report (",symbol,") ===");
      Print("Symbol          : ",symbol);
      Print("Order Type      : ",orderType);
      Print("Balance         : ",DoubleToString(balance,2));
      Print("Equity          : ",DoubleToString(equity,2));
      Print("Margin          : ",DoubleToString(margin,2));
      Print("Free Margin     : ",DoubleToString(freeMargin,2));
      Print("Margin Level    : ",DoubleToString(marginLevel,2),"%");
      Print("Profit          : ",DoubleToString(profit,2));
      Print("Net Profit      : ",DoubleToString(netProfit,2));
      Print("Swap            : ",DoubleToString(swap,2));
      Print("Spread          : ",DoubleToString(spread,5));
      Print("Spread (Pips)   : ",DoubleToString(pips,2)," - ",
            DoubleToString(pipValueUSD,5)," ≈ USD per pip");
  }
};

//+-----------------------------------------------------------------------------+
//| Script Entry Point                                                          |
//+-----------------------------------------------------------------------------+
void OnStart()
{
   
   double leverage   = (double)AccountInfoInteger(ACCOUNT_LEVERAGE);
   
   //Print("Leverage = ", DoubleToString(leverage,0));
   
   CAccountReport report(
                        "EURUSD",
                        "SELL",
                         99988.00,   // Balance
                         100000,     // Contract size
                         leverage,   // Leverage
                         1.0,        // Lot
                         1.16293,    // Open price
                         1.16283,    // Current price
                         0.0,        // StopLoss
                         0.0,        // TakeProfit
                         0.0,        // Commission
                         9.0);       // Swap

   report.PrintReport();
}

“It shows the real-time financial status of a 1 lot SELL position opened on EUR/USD. The difference between Balance and Equity reflects the position’s profit; Margin and Free Margin indicate risk management; Spread and Pip Value explain the transaction costs and the monetary impact of price movements.”

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
=== Account Position Report (EUR/USD) ===
Symbol          : EURUSD
Order Type      : SELL
Balance         : 99988.00
Equity          : 100007.00
Margin          : 11629.00
Free Margin     : 88377.80
Margin Level    : 859.96%
Profit          : 10.00
Net Profit      : 19.00
Swap            : 9.00
Spread          : -0.00010
Spread (Pips)   : -10.00 - 0.85997 ≈ USD per pip

Conclusion

In the case of EURUSD, you will obtain an MQL5 script that correctly reports account positions.

You will learn how margin and risk management calculations are performed using formulas.

You will be able to adapt the code output to your own scenarios, ensuring transparent and reliable reporting.

As a result, investors will be able to make more informed decisions and manage their risks more effectively.