Capture the account balance on day 1 of the month

 

hi devs,

How can I capture the account balance on day 1 of the month?

I tried the following code but it failed:

If(Day() == 1 && Hour() == 0 && Minute() == 0 && Seconds() == 0)
{
    Start_Balance = AccountBalance()
}
 
Hany Mhmd Th Abrahym Aljml:

hi devs,

How can I capture the account balance on day 1 of the month?

I tried the following code but it failed:

double MonthlyBalance=0;
//+------------------------------------------------------------------+
void OnTick()
{
  static int month=-1;
  MqlDateTime dt_struct;
  datetime dt_array[];
  int count=CopyTime(_Symbol,PERIOD_CURRENT,0,1,dt_array);
  if(count>0)
   {
    bool res=TimeToStruct(dt_array[0],dt_struct);
    if(!res)
      Print("Error with TimeToStruct(). Error code ",GetLastError());
    if(month==-1)
     {
      month=dt_struct.mon;
      MonthlyBalance=AccountInfoDouble(ACCOUNT_BALANCE);
      Print("Current Balance at ",TimeCurrent()," is ",DoubleToString(MonthlyBalance,2));
     }
    else
     {
      if(month!=dt_struct.mon) //New Month.
       {
        month=dt_struct.mon;
        MonthlyBalance=AccountInfoDouble(ACCOUNT_BALANCE);
        Print("Current Balance at ",TimeCurrent()," is ",DoubleToString(MonthlyBalance,2));
       }
     }
   }
}
//+------------------------------------------------------------------+

The first trading day of the month may not be the 1st.

The first tick of the month may not be 0 hour, 0 minutes,0 seconds.

The above code can also be used in MQL5.

 
Keith Watford #:

The first trading day of the month may not be the 1st.

The first tick of the month may not be 0 hour, 0 minutes,0 seconds.

The above code can also be used in MQL5.

Thank you for your help.
Is there anyway I can write this code in mql4?
 
Hany Mhmd Th Abrahym Aljml #:
Thank you for your help.
Is there anyway I can write this code in mql4?

Please read my post

Keith Watford #:

The above code can also be used in MQL5.

It is MQL4 code.

 
Keith Watford #:

Please read my post

It is MQL4 code.

Many thanks. I didn't notice that.
I will try it.
 
  1. Hany Mhmd Th Abrahym Aljml: I tried the following code but it failed:
    If(Day() == 1 && Hour() == 0 && Minute() == 0 && Seconds() == 0)
    {
        Start_Balance = AccountBalance()
    }

    It fails when you don't have a tick at the exact second.

    // int Month(){
    //   MqlDateTime tm; TimeCurrent(tm);  return(tm.mon);
    // }
    
    double monthlyBalance=0;
    void OnTick(){
        static int currMonth = -1; int prevMonth=currMonth; currMonth=Month();
        if(prevMonth != currMonth)   monthlyBalance = AccountBalance();
  2. Note that the above code and Keith's fail if the terminal is restarted. For that you must remember the value in persistent storage (files, or global variables w/flush)
 
William Roeder #:

  1. Note that the above code and Keith's fail if the terminal is restarted. For that you must remember the value in persistent storage (files, or global variables w/flush)

Yes, that's the reason I actually had the print state that the balance is at the current time

Print("Current Balance at ",TimeCurrent()," is ",DoubleToString(MonthlyBalance,2));
 
Keith Watford #:

The first trading day of the month may not be the 1st.

The first tick of the month may not be 0 hour, 0 minutes,0 seconds.

The above code can also be used in MQL5.

It works fine and as expected. Many thanks Keith 
 
William Roeder #:
  1. It fails when you don't have a tick at the exact second.

  2. Note that the above code and Keith's fail if the terminal is restarted. For that you must remember the value in persistent storage (files, or global variables w/flush)
Thanks William, I will consider your note.