how to get total profit/loss monthly on mt4

 

pls is my code correct..


{
   double profit=0.0;
   datetime from=0, to=0;
   double start_balance=AccountInfoDouble(ACCOUNT_BALANCE);
   double end_balance=AccountInfoDouble(ACCOUNT_BALANCE);
   datetime last_order_time=0;
   
   int trades=OrdersHistoryTotal();
   for(int i=0; i<trades; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
      datetime open_time=OrderOpenTime();
      if(from==0 || from>open_time) from=open_time;
      if(to==0 || to<open_time) to=open_time;
      
      if(OrderCloseTime()>last_order_time)
         last_order_time=OrderCloseTime();
         
      double profit_trade=OrderProfit();
      profit+=profit_trade;
   }
   
   int days=MathMax(1,TimeDay(last_order_time)-TimeDay(from)+1);
   double monthly_profit=(profit/start_balance)*(30.0/days);
   return(monthly_profit);
}
 
Chukwudi Joshua Obiekwe:

pls is my code correct..


I suggest finding "from" like this:

from=iTime(_Symbol, PERIOD_MN1,0); //profit from start of month
from=TimeCurrent()-30*24*3600;     //profit from one month ago
 
Yashar Seyyedin #: I suggest finding "from" like this:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

 
Chukwudi Joshua Obiekwe:

pls is my code correct..


double profit=0.0;
   datetime from=0, to=0;
   double start_balance=AccountInfoDouble(ACCOUNT_BALANCE);
   double end_balance=AccountInfoDouble(ACCOUNT_BALANCE);
   datetime last_order_time=0;
   
   int trades=OrdersHistoryTotal()-1;
   for(int i=trades; i>=0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;

if(OrderCloseTime() == iTime(NULL,PERIOD_MN1,0)){
    profit += OrderProfit() + OrderSwap()+OrderCommision();
}
   }
   

You can try it like this .decrement loop is efficient when looping through history orders

 
Chukwudi Joshua Obiekwe: pls is my code correct..
   for(int i=0; i<trades; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

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

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)