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.
- 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();
- 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 #:
-
- 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));
William Roeder #:
Thanks William, I will consider your note.
-
It fails when you don't have a tick at the exact second.
- 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)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
hi devs,
How can I capture the account balance on day 1 of the month?
I tried the following code but it failed: