Introduction#
In forex trading, profit is determined by the spread between the opening and closing prices. For EURUSD, one pip equals 0.0001.
The spread is calculated as closePrice − openPrice, and the number of pips is spread ÷ pipSize.
Pip value represents the USD worth of each pip, while profit is positive for BUY positions and negative for SELL positions.
This simple formula ensures accurate risk and reward calculations across different symbols.
pipSize → automatically calculated based on the number of digits of the symbol (EURUSD → 5 digits → pip = 0.0001).
spread → price difference (closePrice − openPrice).
pips → spread divided by pipSize.
pipValueUSD → the USD value per pip.
profit → positive for BUY positions, negative for SELL positions.
This way, the terminology is clear and consistent with international trading standards.
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
|
//+------------------------------------------------------------------+
//| Copyright 2026, MetaQuotes Ltd. |
//| https://www.mql5.com |
//| Spread & Profit Calculator.mq5 |
//+------------------------------------------------------------------+
double CalculateSpreadProfit(string symbol,
string orderType,
double lot,
double openPrice,
double closePrice)
{
// Contract size (units per 1 lot)
double contractSize = 100000.0;
// Point and pip size
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
// Pip size: for 5-digit pairs (EURUSD) → 0.0001, for 3-digit (USDJPY) → 0.01
double pipSize = (digits == 5 || digits == 3) ? point * 10 : point;
// Position size in base currency
double positionSize = lot * contractSize;
// Spread difference (closePrice - openPrice)
double spread = closePrice - openPrice;
// Pips = spread / pipSize
double pips = spread / pipSize;
// Pip value in USD
double pipValueUSD = (positionSize * pipSize) / closePrice;
// Profit calculation
double profit = 0.0;
if(orderType == "BUY")
profit = spread * positionSize; // BUY → positive if price rises
else if(orderType == "SELL")
profit = -(spread * positionSize); // SELL → positive if price falls
// Report output
Print("=== Spread & Profit Report ===");
Print("Symbol : ", symbol);
Print("Order Type : ", orderType);
Print("Open Price : ", DoubleToString(openPrice,digits));
Print("Close Price : ", DoubleToString(closePrice,digits));
Print("Spread : ", DoubleToString(spread,digits));
Print("Spread (Pips) : ", DoubleToString(pips,2));
Print("Pip Value USD : ", DoubleToString(pipValueUSD,2));
Print("Profit : ", DoubleToString(profit,2)," USD");
return profit;
}
//+------------------------------------------------------------------+
//| Example usage |
//+------------------------------------------------------------------+
void OnStart()
{
double profit = CalculateSpreadProfit("EURUSD","BUY",1.0,1.1000,1.1050);
Print("Final Profit = ", DoubleToString(profit,2)," USD");
}
|
1
2
3
4
5
6
7
8
9
|
=== Spread & Profit Report ===
Symbol : EURUSD
Order Type : BUY
Open Price : 1.10000
Close Price : 1.10500
Spread : 0.00500
Spread (Pips) : 500.00
Pip Value USD : 0.90
Profit : 500.00 USD per lot
|