Code to Calculate Total Profit on All Open Trades

 

Hi All,

I am looking for some code (or Function) that will Calculate, in my Expert Advisor (MQL4), regardless of which currency I am looking at, the Current Profit on on All open trades.


For example:

Curr1 =  500

Curr2 =  250

Curr3 = -250


Comment

(

Net_Profit on Open Trades =  $500

);


Many Thanks

Kody_Devl

 
rcrill:

Hi All,

I am looking for some code (or Function) that will Calculate, in my Expert Advisor (MQL4), regardless of which currency I am looking at, the Current Profit on on All open

 //Global variables 
double Profit=0,ProfitSymbol=0;

double CheckTotalProfits()
  {

   Profit=0;
   ProfitSymbol=0;

   for(int l_pos_0=OrdersTotal()-1; l_pos_0>=0; l_pos_0--)
     {
        bool order=OrderSelect(l_pos_0,SELECT_BY_POS,MODE_TRADES);
      
      if(!order)
        {
         continue;
        }

      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
        {

         double order_profit=OrderProfit()+OrderSwap()+OrderCommission();
         Profit+=order_profit;

        
            ProfitSymbol+=order_profit;
         

        }
     
     }
      return(ProfitSymbol);
  }
 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. Do you really want get profit "on All open trades?" Don't you mean all open trades for the current symbol?

  4. Loop through all open trades, filter by magic number, non-pending, symbol. Sum OrderProfit()+OrderSwap()+OrderCommission()
    Do not count on OrderCommission and OrderSwap. Some brokers don't use those fields. (Maybe related to Government required accounting/tax laws.)
              please help: Mt4 commission - Trading Accounts - MQL4 programming forum
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum
    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing
 
Catalin Zachiu:

A bit more stylish

double CheckTotalProfits( int _magic )
{
   double profit = 0.0;

   for( int pos = OrdersTotal()-1; pos >= 0; pos-- )
        if ( OrderSelect( pos, SELECT_BY_POS ) && OrderType() < 2 && OrderSymbol() == _Symbol && OrderMagicNumber() == _magic )
            profit += OrderProfit() + OrderSwap() + OrderCommission();
 
   return( profit );
}
 
Catalin Zachiu:

Code Works Beautifully!


Thank you!


rcrill

 
I want same code but for profit of all day open and closed trade