Introduction

Gold (XAU/USD) is not just another trading instrument it is a global safe‑haven asset whose price movements often reflect broader economic uncertainty. Unlike currency pairs, gold contracts carry unique volatility patterns and margin requirements that can easily distort an investor’s perception of risk if calculated incorrectly.

This article introduces the development of an Account Position Calculator tailored for XAU/USD, designed to provide transparent and precise monitoring of leveraged positions.

Computes position size, margin usage, and floating profit/loss with formulas adapted to gold’s contract specifications.

Automatically reports Equity, Free Margin, and Margin Level, ensuring traders maintain a clear view of their account health.

Outputs are expressed directly in USD, aligning with the global pricing standard for gold.

Thanks to its modular code structure, the calculator seamlessly supports both BUY and SELL scenarios.

Integrated Stop Out Level logic highlights critical thresholds, helping investors safeguard against sudden market swings.

By applying this calculator, traders can approach gold trading with greater confidence, balancing opportunity with disciplined risk management.

Open Buy Position

Let’s create a new class AccountPositionCalculator.

  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
150
151
152
153
154
155
156
157
158
159
160
161
162
//+-------------------------------------------------------------------+
//|                                   Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com. |
//| XAU/USD Account Position Calculator                               |
//+-------------------------------------------------------------------+
class AccountPositionCalculator
{
private:
   double balance;
   double contractSize;
   double leverage;

   string orderType;
   string symbol;
   double lot;
   double openPrice;
   double currentPrice;
   double stopLoss;
   double takeProfit;
   double commission;
   double swap;

   double pipSize;

public:
   // --- Constructor ---
   AccountPositionCalculator(double bal, double cs, double lev,
                             string ordType, string sym, double l,
                             double oPrice, double cPrice,
                             double sl, double tp,
                             double comm, double sw,
                             double pipSz)
   {
      balance      = bal;
      contractSize = cs;
      leverage     = lev;

      orderType    = ordType;
      symbol       = sym;
      lot          = l;
      openPrice    = oPrice;
      currentPrice = cPrice;
      stopLoss     = sl;
      takeProfit   = tp;
      commission   = comm;
      swap         = sw;

      pipSize      = pipSz;
   }

   // --- Position Size ---
   double PositionSize()
   {
      return lot * contractSize;
   }

   // --- Spread ---
   double Spread()
   {
      return currentPrice - openPrice;
   }

   // --- Pips ---
   double Pips()
   {
      return Spread() / pipSize;
   }

   // --- Pip Value ---
   double PipValueUSD()
   {
      return (pipSize / currentPrice) * contractSize * lot;
   }

   // --- Profit ---
   double Profit()
   {
      double spread = Spread();
      double positionSize = PositionSize();

      if(orderType == "BUY")
         return spread * positionSize;
      else if(orderType == "SELL")
         return -(spread * positionSize);
      return 0.0;
   }

   // --- Net Profit ---
   double NetProfit()
   {
      return Profit() - commission + swap;
   }

   // --- Equity ---
   double Equity()
   {
      return balance + Profit() - commission + swap;
   }

   // --- Margin ---
   double Margin()
   {
      return (PositionSize() * openPrice) / leverage;
   }

   // --- Free Margin ---
   double FreeMargin()
   {
      return Equity() - Margin();
   }

   // --- Margin Level ---
   double MarginLevel()
   {
      double m = Margin();
      return m > 0 ? (Equity() / m) * 100 : 0;
   }

   // --- Report ---
   void Report()
   {
      Print("=== Account Position Report (XAU/USD) ===");
      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));
      Print("XAUUSD Pip Size : ", DoubleToString(pipSize, 2));
      Print("Pip Value       : ", DoubleToString(PipValueUSD(), 4), " USD");
   }
};

//+-------------------------------------------------------------------+
//| Script Entry Point                                                |
//+-------------------------------------------------------------------+
void OnStart()
{
   AccountPositionCalculator calc(
      100076.00,   // Balance
      100.00,      // Contract Size (1 lot = 100 ons)
      10.0,        // Leverage      
      "XAUUSD",    // Symbol
      "BUY",       // Order Type
      1.00,        // Lot
      4109.92,     // Open Price
      4111.44,     // Current Price
      0.0,         // Stop Loss
      0.0,         // Take Profit
      0.0,         // Commission
      0.0,         // Swap
      0.01         // Pip Size
   );

   calc.Report();
}

“It shows the real-time financial status of a 1 lot BUY position opened on XAU/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
14
15
=== Account Position Report (XAU/USD) ===
Symbol          : XAUUSD
Order Type      : BUY
Balance         : 100076.00
Equity          : 100228.00
Margin          : 41099.00
Free Margin     : 59128.80
Margin Level    : 243.87%
Profit          : 152.00
Net Profit      : 152.00
Swap            : 0.00
Spread          : 1.52000
Spread (Pips)   : 152.00
XAUUSD Pip Size : 0.01
Pip Value       : 0.0002 USD (for 1 lot)

Open Sell Position

Account Position Calculator XAUUSD

Change class paramerter AccountPositionCalculator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//+-------------------------------------------------------------------+
//| Script Entry Point                                                |
//+-------------------------------------------------------------------+
void OnStart()
{
   AccountPositionCalculator calc(
      95446.00,   // Balance
      100.00,     // Contract Size (1 lot = 100 ons)
      10.0,       // Leverage      
      "XAUUSD",   // Symbol
      "SELL",     // Order Type
      1.00,       // Lot
      4065.84,    // Open Price
      4062.35,    // Current Price
      0.0,        // Stop Loss
      0.0,        // Take Profit
      0.0,        // Commission
      0.0,        // Swap
      0.01        // Pip Size
   );

   calc.Report();
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
=== Account Position Report (XAU/USD) ===
Symbol          : XAUUSD
Order Type      : SELL
Balance         : 95446.00
Equity          : 95795.00
Margin          : 40658.40
Free Margin     : 55136.60
Margin Level    : 235.61%
Profit          : 349.00
Net Profit      : 349.00
Swap            : 0.00
Spread          : -3.49000
Spread (Pips)   : -349.00
XAUUSD Pip Size : 0.01
Pip Value       : 0.0002 USD

Conclusion

In the case of XAU/USD, you will obtain an MQL5 script that correctly reports account positions for gold trading.

You will learn how margin usage and risk management calculations are performed using formulas adapted to gold’s contract specifications.

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

As a result, investors will gain clearer insights into their gold positions, make more informed decisions, and manage their risks more effectively in volatile market conditions.