overlapping instances of EA on successive ticks

 

hi,

i have an EA which does a bit of number crunching. as a result, each tick the EA typically runs for a few seconds. now, in general this is fine but sometimes the next tick arrives before the previous instance of the EA has exited. and this causes all sorts of snarl ups. for example, the first invocation is supposed to set some static variables which are then used in subsequent instances of the EA. but if the first one has not exited before the second one starts then they are not set. is there any way of checking whether an instance of an EA is already running?

and, to make things a little more complicated, i am running this EA on multiple currency pairs. multiple instances of the EA are allowed at the same time if they are running on different currency pairs... they should just not happen on the SAME pair.

thanks in advance for your cunning suggestions.

best regards, andrew.

 

okay, just to clarify the above a bit further. my EA runs a specific bit of code at the beginning of each new candle. i used to use the following function to identify the start of a candle. i got the idea from somewhere on this forum.

bool NewCandle()
{
   return(Volume[0] == 1);
}

the only problem with this is that if the first tick of a new candle is slightly delayed then the condition specified in NewCandle() is no longer satisfied. this means that the EA completely misses this new candle.

so, as an alternative, i wrote the following function which does the trick most of the time.

bool NewBar()
{
   static datetime last = 0;
   
   if (!last) last = Time[0];
   
   if (CurTime() >= last + Period() * 60) {
      last = Time[0];
      return(true);
   }
   
   return(false);
}
the only snag is that if a second tick arrives while the EA is still active, the value of the static variable last has not been updated, so NewBar() returns true again. so, now, instead of sometimes missing a new candle, now i get the signal for a new candle twice!
 

Try this.

   // Identify new bars
   bool Fun_New_Bar()
      {
      static datetime New_Time = 0;
      bool New_Bar = false;
      if (New_Time!= Time[0])
         {
         New_Time = Time[0];
         New_Bar = true;
         }
      return(New_Bar);
      }
V
 
yeah, that does the trick. not sure why... but thanks!
Reason: