Needs to know how to get the AccountBalance () and the AccountEquity () on different dates

 
Is it possible to define the AccountBalance() and the AccountEquity multiple times based on different dates? The script below is an example of what I would like to do
Files:
EA.mq4  3 kb
 
Samir Ahmane:
Is it possible to define the AccountBalance() and the AccountEquity multiple times based on different dates? The script below is an example of what I would like to do

Each call will return you the latest values. But nothing stops you from calling AT different, appropriate, times. Alternatively, one can also, theoretically, iterate through all historical trades and compute the balance/equity back then.

From your code, it looks like you're pre-setting the time for the next 21 days, and as the days come along, you capture the balance/equity values... logic-wise this will work (although there are many areas that require fixing).

 
Seng Joo Thio:

Each call will return you the latest values. But nothing stops you from calling AT different, appropriate, times. Alternatively, one can also, theoretically, iterate through all historical trades and compute the balance/equity back then.

From your code, it looks like you're pre-setting the time for the next 21 days, and as the days come along, you capture the balance/equity values... logic-wise this will work (although there are many areas that require fixing).

Correct! I have been able to get into a solution, but there is still a problem. The EA, is printing only two different values from two different times. Look at the code source below, tell me what are

your insights, the results can be looked upon at Expert messages.

Files:
EA.mq4  5 kb
 
Seng Joo Thio: Alternatively, one can also, theoretically, iterate through all historical trades and compute the balance/equity back then.
Do not assume history has only closed orders.
Do not assume history is ordered by date, it's not.
          Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum
 
Samir Ahmane:

Correct! I have been able to get into a solution, but there is still a problem. The EA, is printing only two different values from two different times. Look at the code source below, tell me what are

your insights, the results can be looked upon at Expert messages.

There are several issues:

(1) Time_Ahead_20 should be assigned as TimeCurrent() in OnInit() rather than OnTick(), because you won't want it's value to change as time goes by.

(2) You cannot "predict" the time that OnTick() will be triggered in the future, because it depends on the available of ticks coming in. So you mustn't use == when comparing variables like Time_20, Time_19, etc. with TimeCurrent().

(3) The way your 'if' conditions are nested, coupled with the use of '==' in time comparison, prevented Time_19 and below from being checked.

(4) When you have to use variables like Time_20, Time_19, and eventually down to Time_01, you should seriously consider using Arrays.

(5) Instead of doing everything in OnTick(), I suggest you make use of OnTimer() instead.

I've included a sample that takes into consideration the above points (you can change the variables to capturing once everyday for 20 days easily):

// To capture iNumOfCaptures times of AccountBalance() and AccountEquity() at interval of iIntervalInSeconds,
// between iStartTime to iEndTime, where iStartTime will be TimeCurrent()+iWaitTimeInSeconds.
datetime iStartTime = 0;
datetime iEndTime = 0;
int iWaitTimeInSeconds = 0;
int iNumOfCaptures = 5;
int iIntervalInSeconds = 10;
double daBalances[], daEquities[];

datetime iProgramStartTime = TimeCurrent();
int iCount = 0;
double dTotBalance = 0, dTotEquity = 0;

int OnInit()
  {
   EventSetTimer(iIntervalInSeconds);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
   if (IsTesting()) // This is needed if you run it in tester... otherwise, when run in real time, the timer will do it's work.
      if (iStartTime==0 || TimeCurrent()>=iStartTime+iCount*iIntervalInSeconds)
         OnTimer();
  }

void OnTimer()
  {
   if (iStartTime==0)
   {
      if (TimeCurrent()>=iProgramStartTime+iWaitTimeInSeconds)
      {
         iStartTime = TimeCurrent();
         iEndTime = iStartTime + iNumOfCaptures*iIntervalInSeconds;
         ArrayResize(daBalances,iNumOfCaptures);
         ArrayResize(daEquities,iNumOfCaptures);
      }
   }
   else
   if (iCount<iNumOfCaptures)
   {
      daBalances[iCount] = AccountBalance();
      daEquities[iCount] = AccountEquity();
      Print ("Time ", TimeCurrent(), ", Balance = ", daBalances[iCount], ", Equity = ", daEquities[iCount]);
      dTotBalance += daBalances[iCount];
      dTotEquity += daEquities[iCount];
      iCount++;
   }

   if (iCount==iNumOfCaptures)
   {
      Print ("Average Balances = ", dTotBalance/iNumOfCaptures, ", Average Equities = ", dTotEquity/iNumOfCaptures);
      iCount++;
   }
  }
Reason: