Problem with history update in while loop of indicator

 

Hi,

I’ve experienced the following strange behaviour regarding the history update:

When using a while-loop in an indicator, with refreshrates() in between, the history data doesn’t get updated within the loop, even when I run the loop for 60 seconds. Instead the data is updated immediately after the loop has finished.

When I use the same code in an EA, the update works within the loop.

See attached code and logs.

Why is that?

I know that the EA is running in an own thread, while the indicator is running within the main thread, but still.

Is there a way to get the data updated within the while loop of an indicator?

//+------------------------------------------------------------------+
//|                                GA_Test Error 4066.mq4            |
//+------------------------------------------------------------------+
#property indicator_chart_window

#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>
   
int MaxWaitTime = 60;  
int PrintInterval = 2;
int Error;
int IterationCount;

string EURJPY = "EURJPY";

datetime WaitingStartTime;
datetime TheLastPrint;
datetime TheTime;

bool DoExecute = true;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int init()
{     
   return;
}
//+------------------------------------------------------------------+
int start()
{       
   IterationCount++;
   
   WaitingStartTime = TimeLocal();   
   TheLastPrint = WaitingStartTime - PrintInterval; 
   
   if (DoExecute)
      {               
         TheTime = iTime(EURJPY, PERIOD_M15, 0);
   
         Error = GetLastError();
   
         Print("before loop: TheTime: ", TimeToStr(TheTime, TIME_DATE|TIME_SECONDS), " Error: ", ErrorDescription(Error));
         Print(" ");
         
         while (true )                              
            {
               RefreshRates();
      
               if (TimeLocal() >= WaitingStartTime + MaxWaitTime)
                  {
                     Print(" ");
                     Print("MaxWaitTime exceeded");
                     DoExecute = false;
                     break; 
                  }
           
               if (TimeLocal() >= TheLastPrint + PrintInterval)         
                  {
                     TheTime = iTime(EURJPY, PERIOD_M15, 0);
   
                     Print("in loop: TheTime: ", TimeToStr(TheTime, TIME_DATE|TIME_SECONDS));                  
               
                     TheLastPrint = TimeLocal();
                     DoExecute = false;
                  }     
            }  // while true
      } // if (DoExecute)

   TheTime = iTime(EURJPY, PERIOD_M15, 0);

   Print(" ");
   Print(IterationCount, " after loop: TheTime: ", TimeToStr(TheTime, TIME_DATE|TIME_SECONDS));  
}

Thanx.

 
mt4forum:

Hi,

I’ve experienced the following strange behaviour regarding the history update:

When using a while-loop in an indicator, with refreshrates() in between, the history data doesn’t get updated within the loop, even when I run the loop for 60 seconds. Instead the data is updated immediately after the loop has finished.

What do you think RefreshRates() does ? if this Indicator were on a EURJPY chart there is no need for RefreshRates() as the incoming tick updates anyway, if the Indicator isn't on EURJPY RefreshRates() is refreshing information you aren't interested in . . . you can't slow down the Interface thread.
 

Okay, I've overestimated refreshrates() a bit.

I don't run the indicator/EA on EURJPY.

But why is the data updated within the EA loop, if refreshrates() is only updating the data for the current symbol?

 
TheTime = iTime(EURJPY, PERIOD_M15, 0);
In a EA only, if TheTime is zero, then capture GetLastError, and if it is ERR_HISTORY_WILL_BE_UPDATED THEN and ONLY THEN should you wait 15 seconds and retry. If you get anything other than 4066, you are done, there is no history for the pair/tf specified. RefreshRates if you slept and you need updated predefined variables after the data arrives.
 
mt4forum:

Okay, I've overestimated refreshrates() a bit.

I don't run the indicator/EA on EURJPY.

But why is the data updated within the EA loop, if refreshrates() is only updating the data for the current symbol?

Because you ask for data and it isn't there, hence the error 4066, then you wait . . . and while waiting the data arrives. You can't wait in an Indicator, "you can't slow down the Interface thread."

Refreshrates() is irrelevant to this issue, don't take my word for it, read the documentation for RefreshRates() and predefined variables and see for yourself . . .
 

Okay, I understand. Thank you!

Reason: