🚀 AI One-Click Generation of mql4 Strategy Code

Calculating Real-Time Profit & Loss in Your Account's Currency with Mql4

mql4 | Published on: 2025/12/1
The following content is generated by EasyQuant .

Problem Description

How do I calculate the dollar profit for an open trade?

Solution

To calculate the dollar profit (or the profit in your account's deposit currency) for an open trade in MQL4, you must first select the order using OrderSelect().

The total net profit is calculated by summing three components:

  1. OrderProfit(): The profit/loss from price movement.
  2. OrderSwap(): The cumulative swap interest charged or earned.
  3. OrderCommission(): The commission charged by the broker.

Below are two examples: one to calculate profit for a specific ticket, and another to calculate the total floating profit for all open trades.

1. Calculate Profit for a Specific Ticket

This function takes a ticket number, selects the order, and returns the net profit.

//+------------------------------------------------------------------+
//| Function to calculate Net Profit for a specific ticket           |
//+------------------------------------------------------------------+
double GetNetProfitByTicket(int ticket)
{
   // Try to select the order by its ticket number
   if(OrderSelect(ticket, SELECT_BY_TICKET))
   {
      // Calculate Net Profit: Trade Profit + Swap + Commission
      double netProfit = OrderProfit() + OrderSwap() + OrderCommission();
      
      return(netProfit);
   }
   else
   {
      Print("OrderSelect failed for ticket #", ticket, ". Error: ", GetLastError());
      return(0.0);
   }
}

2. Calculate Total Profit for All Open Trades

This script loops through all currently open positions and sums up the net profit.

//+------------------------------------------------------------------+
//| Script to calculate Total Floating Profit                        |
//+------------------------------------------------------------------+
void OnStart()
{
   double totalNetProfit = 0.0;
   
   // Loop through all open orders
   for(int i = 0; i < OrdersTotal(); i++)
   {
      // Select order by position index
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         // Optional: Filter by Symbol or Magic Number if needed
         // if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;

         // Sum up Profit, Swap, and Commission
         double tradeProfit = OrderProfit();
         double tradeSwap   = OrderSwap();
         double tradeComm   = OrderCommission();
         
         totalNetProfit += (tradeProfit + tradeSwap + tradeComm);
      }
   }
   
   // Output the result
   Print("Total Net Floating Profit: ", DoubleToString(totalNetProfit, 2));
}

Key Functions Explained

  • OrderSelect(index, flags, pool): This is mandatory. You cannot access order data without selecting it first.
    • SELECT_BY_TICKET: Used when you know the specific Order ID.
    • SELECT_BY_POS: Used when looping through the list of orders.
  • OrderProfit(): Returns the profit or loss of the selected order in the account currency. If your account is in USD, this returns dollars.
  • OrderCommission(): Returns the commission cost. Note that some brokers record this as a negative value immediately upon opening, while others apply it at closing.
  • OrderSwap(): Returns the overnight interest. This can be positive or negative.

Q&A

Q: Does OrderProfit() include the spread?
A: Yes and no. OrderProfit() is calculated based on the current Bid (for Buy orders) or Ask (for Sell orders) price. Therefore, the cost of the spread is naturally reflected in the floating P&L, but it is not a separate value you add to the calculation.

Q: Why is my calculated profit slightly different from the terminal's "Profit" column?
A: The "Profit" column in the MT4 terminal usually shows OrderProfit(). It often does not include OrderSwap() or OrderCommission() in that specific column unless you look at the summary line at the bottom. The code provided above calculates the Net profit (what you would actually keep if you closed the trade immediately).

Q: Will this work if my account is in EUR or JPY?
A: Yes. OrderProfit() always returns the value in the Account Deposit Currency. If your account is in EUR, the result will be in Euros, not Dollars.

Q: How do I calculate profit in Pips instead of Dollars?
A: You cannot use OrderProfit() for pips. You must calculate the distance between OrderOpenPrice() and the current price (Bid or Ask), then divide by Point or _Point.