Problems with ERR_HISTORY_WILL_UPDATED (4066 ) & weekends

athan
249

Hi

i m building a custom indicator and got problems with the 4066 Error. As i m building an indicator i ve got the following While loop to check if data have arrived

       dt=iTime(Symbol(),PERIOD_M1,0);
        Error=GetLastError();
    while (Error==4066)
   {
            _time_waiting = TimeCurrent() + timeToWait;
            while (TimeCurrent() < _time_waiting)
            {         
             } //while
            
            dt=iTime(Symbol(),PERIOD_M1,0);
            
      Error = GetLastError();
   
   }//while

 this code is part of a function that i call whenever i m looking for a candle from the start() and especially in the first occurance.
But here are the problems i get:

1.  if we re within a weekend and actually from Friday night, when the markets close, the indicator freese there in the while loop. The GetLastError is always returning a 4066.
My question here is why is that happening? i mean manually even during weekends metatrader does give you access to history chart data so although there are no ticks it should return data feed.

2. If for any reason brokers server goes down for a while - it does happens quite often - cause of the while loop again the indicator will freeze.

Just to note here that i m trying to access 1M timeframe from a higher timeframe like 1H, 4H etc 

So is there a way to solve this situation? I definately need to handle 4066 error eitherwise i get back wrong data.  

 Thanks

Simon Gniadkowski
17411
athanfx:

Hi

i m building a custom indicator and got problems with the 4066 Error. As i m building an indicator i ve got the following While loop to check if data have arrived

 this code is part of a function that i call whenever i m looking for a candle from the start() and especially in the first occurance.

As far as I know when you access the "other" timeframe it will be updated,  so try to access bar 1 instead of bar 0,  the attempt to access bar 1 should bring the data for bar 0 as well,  if there is any,  but specifically trying to access bar 0 may be causing your issue . . .  change you code to bar 1 and give it a go.
athan
249
athan  
RaptorUK:
As far as I know when you access the "other" timeframe it will be updated,  so try to access bar 1 instead of bar 0,  the attempt to access bar 1 should bring the data for bar 0 as well,  if there is any,  but specifically trying to access bar 0 may be causing your issue . . .  change you code to bar 1 and give it a go.

Thanks for your answer 

 problem still there. even accessing bar 1 i get 4066 error and after that i get error4054. Any other thoughts? Is there a wayto check if server is down or if market is open or close meaning like if it is a weekend or bank holiday so forex is not working etc?

 Also using this while loop it to simulate sleep it makes the metatrader freeze. is there another way to do that ?

William Roeder
25856
William Roeder  
athanfx:

i m building a custom indicator and got problems with the 4066 Error. As i m building an indicator i ve got the following While loop to check if data have arrived
 dt=iTime(Symbol(),PERIOD_M1,0);
        Error=GetLastError();
 while (Error==4066)

Indicators can not wait. They must return immediately.

The first time you will get 4066. On subsequent calls dt will still be zero but GLE will NOT be 4066. Just return.

athan
249
athan  
WHRoeder:

Indicators can not wait. They must return immediately.

The first time you will get 4066. On subsequent calls dt will still be zero but GLE will NOT be 4066. Just return.


Thanks.

i based my logic of the while loop on another post i found on this forum.

i tried what you suggested. the problem that now arrises is that the first time the start() does returns but the second cause of 4066 but the second time the counted_bars=Bars-IndicatorCounted() is 0. Cause of that causes troubles to the while loop i use to browse the candles. Ofcourse i suppose this is normal but the problem is that the first time i called start() i returned (cause of 4066) without browsing the candles and making the appropriate calculations i had to do.

Any ideas on how to handle this? i m thinking of using a global var as a flag but i m not sure if this is the way to solve this

William Roeder
25856
William Roeder  
bool isHistoryLoading;
int init(){ isHistoryLoading = true; .. }
int start(){
   if (isHistoryLoading){
      dt=iTime(Symbol(),PERIOD_M1,0); if (dt == 0) return;
      // or if( !iBars(Symbol(), PERIOD_M1)) return;
      isHistoryLoading = false;
      int counted = 0;
   }
   else counted = IndicatorCounted();
   for(int iBar = Bars - 1 - counted; iBar >= 0; iBar--){ ...
athan
249
athan  
WHRoeder:


thanks for your help and the code.

i tried it out and it works. although the problem is that in the for loop later on (i ve got a while loop to be more specific) i try to get dt=iTime(Symbol(),PERIOD_M1,iBar)again but it returns 0.
so i check for dt==0 and if true i set isHistoryLoading to true and then return ( so exit the while).  That causes the following problem: almost every 1sec the indicator to do the same loop through all the candles and so the metatrader to freeze every 1 sec for a few milliseconds. 

 

bool isHistoryLoading;
int init(){ isHistoryLoading = true; .. }
int start(){
   if (isHistoryLoading){
      dt=iTime(Symbol(),PERIOD_M1,0); if (dt == 0) return;
      // or if( !iBars(Symbol(), PERIOD_M1)) return;
      isHistoryLoading = false;
      int counted = 0;
   }
   else counted = IndicatorCounted();
   for(int iBar = Bars - 1 - counted; iBar >= 0; iBar--){
     dt=iTime(Symbol(),PERIOD_M1,iBar); 
     if (dt == 0) { isHistoryLoading = true;return;}
      .....}  
William Roeder
25856
William Roeder  

athanfx:

i tried it out and it works. although the problem is that in the for loop later on (i ve got a while loop to be more specific) i try to get dt=iTime(Symbol(),PERIOD_M1,iBar)again but it returns 0.

   for(int iBar = Bars - 1 - counted; iBar >= 0; iBar--){
     dt=iTime(Symbol(),PERIOD_M1,iBar); 

IBar is the index into the chart's bars. Unless you are running on the M1 chart iTime(M1,iBar) makes no sense. You are probably running off the end of the M1 chart.

  1. You need to find the size of the M1 chart
    int BarsM1 = iBars(NULL, PERIOD_M1);
  2. You need to find which M1 bar correspond to your chart's iBar
    int iBarM1 = iBarShift(NULL, PERIOD_M1, Time[iBar]);
  3. The number of bars in the M1 chart is controlled by options/bars on chart.