Pause in init() when switching timeframes

 

Hello all,

I have an indicator which draws some 'permanent' objects (i.e. they aren't subsequently deleted by the indicator).

The problem I have is when I switch time frames - during the lag between the new timeframe opening and the candles updating, the indicator paints objects based on the old info.

Is there some way I can create a pause in init() when switching time frames, in order to allow all the tick data to catch up before the indicator starts working?

Many thanks in advance for any assistance. 

 
toast:

Hello all,

I have an indicator which draws some 'permanent' objects (i.e. they aren't subsequently deleted by the indicator).

The problem I have is when I switch time frames - during the lag between the new timeframe opening and the candles updating, the indicator paints objects based on the old info.

Is there some way I can create a pause in init() when switching time frames, in order to allow all the tick data to catch up before the indicator starts working?

Many thanks in advance for any assistance. 

What about using UninitializeReason() and Sleep().

For example:

init() {
   if (UninitializeReason() == REASON_CHARTCHANGE)
      // Pause for 2 seconds in half second intervals
      for (int i = 0; i <= 3; i++)
         sleep(500);
}

  

 

 
toast:

Hello all,

I have an indicator which draws some 'permanent' objects (i.e. they aren't subsequently deleted by the indicator).

The problem I have is when I switch time frames - during the lag between the new timeframe opening and the candles updating, the indicator paints objects based on the old info.

Is there some way I can create a pause in init() when switching time frames, in order to allow all the tick data to catch up before the indicator starts working?

Many thanks in advance for any assistance. 

No,  don't pause in init()  it has to return within, approx., 2 seconds,  get init to set a flag  (bool)  check for the flag in start() and wait for a time period to elapse,  you can also check for error 4066
 
RaptorUK:
No,  don't pause in init()  it has to return within, approx., 2 seconds, ...

Hmmm...I know that deinit() must complete within 2.5 seconds (or be forcibly terminated by Terminal) and start() must complete within 2.5 seconds if a termination call is made (or be forcibly terminated by Terminal).  See Special Functions.  But I'm unaware that init() is constrained by that same timing rule.

The code below seems to show that init() does not have to return within 2.5 seconds:

int init() {
   for (int i = 1; i <= 10; i++) {
      Sleep(500);
      Print (i, " - Just completed a half-second sleep.");
   }
}

That code produced the following output:

Sleep demo in Init() 

EDIT: The above code is for an EA, not an indicator. 

 
toast:

I have an indicator which draws some 'permanent' objects (i.e. they aren't subsequently deleted by the indicator).

The problem I have is when I switch time frames - during the lag between the new timeframe opening and the candles updating, the indicator paints objects based on the old info.

Is there some way I can create a pause in init() when switching time frames, in order to allow all the tick data to catch up before the indicator starts working?

Your indicator shouldn't be drawing anything except in start(). There may be no bars, market info, etc yet. Just fix your indicator, don't try to band aid it.

Indicators can not sleep. EAs may now be able to since there are multiple (8) trading threads in the latest versions. Just because it can, doesn't mean it should.

 
Thirteen:

Hmmm...I know that deinit() must complete within 2.5 seconds (or be forcibly terminated by Terminal) and start() must complete within 2.5 seconds if a termination call is made (or be forcibly terminated by Terminal).  See Special Functions.  But I'm unaware that init() is constrained by that same timing rule.

The code below seems to show that init() does not have to return within 2.5 seconds: That code produced the following output:

Thank you for the test.
 
WHRoeder:

Indicators can not sleep. ...

You are right...sleep() is not available for custom indicators.  I didn't realize (even though I should have) that the OP was talking about an indicator, not an EA.

 

Many thanks for the comments.


This is how I (think) I've fixed it: 

bool FirstRun;

int WaitTime;

 

int init()

{

   FirstRun = true;

   WaitTime = TimeLocal() + 5;

   return(0);

} 

int start()
{
 if(FirstRun == true)
  {
   if(TimeLocal()<WaitTime)
     {
      return(0);
     }
   else
     {
      FirstRun = false;
     }
  }
} 
Reason: