What is the best way to call a function when a the current bar closes and a new bar is started?

 

For example, if you are using 1 minute bars, call this fucntion when time changes from (DD:HH:MM format) XX:XX:X1 to XX:XX:X2.

If you are using five minute bars, call this function when the time changes from XX:XX:X1 to XX:XX:X6.

???

 

if(Volume[0] == 1) do something

or

static int oldTime;

init(){

oldTime = Time[0];

}

start(){

if(oldTime != Time[0]) {

oldTime = Time[0];

do something

}

return;

}

 
Thanks, Phy.  This doesn't work well in a slow market, as start() is only called on every tick.  If there is no volume for over sixty seconds, this will get thrown off.  Perhaps a better way would be to create a semi-endless loop?
 

In EA or Script:

int periodInSeconds = 60; // 300 = 5 minutes, etc

int hitsThisBar = 1;
start(){

   while(1){
      if( MathMod(TimeLocal(), periodInSeconds) == 0 && hitsThisBar == 0){

         hitsThisBar++;
         RefreshRates();
         do something...
      }
      if( MathMod(TimeLocal(), periodInSeconds) != 0) hitsThisBar = 0;
      Sleep(100);
   }
return(0);
}
Sleep is necessary, do not omit.
 

Thanks for your continued help, Phy.  The last EA you posted does indeed trigger every minute, on the minute.  However, because the loop is run through in much less time than a second, "do something" gets executed several times.  I tried to go around this by triggering a boolean which will execute something somewhere else, but then nothing happens at all.  Ideas?

Basically I'm trying to figure this out because there seems to be no easy way to access system time on an un-ending, constant basis.  It is a shame that MTL can't do this easily.  Hopefully MTL5 will address this.


int periodInSeconds = 60; // 300 = 5 minutes, etc
int Count = 1;
int Always = 1;
int Play = 0;
 
int start()
{
 
while(Always==1)
{
   if( MathMod(TimeLocal(), periodInSeconds) == 0)
   {
      RefreshRates();
      Play = 1;      
   }
   Sleep(100);
}
 
if(Play == 1)
{
   Print("minute"+Count+"volume:"+Volume[0]);
   Play = 0;
   Count++;
}      
 
return(0);
}
 
Ok, updated previous code...