How to trade only at first tick of new bar

 

Hi,

I want to open/close trades only at the first tick of new bar, how can I write the condition? thanks.

 

GM

static datetime lastbar;

init()
{

lastbar=Time[1];

return (0);
}

start ()
{

  //----------- Do every tick stuff here





  //----------- Do once-per-bar stuff here

   if (IsNewBar())
     {

      // Do new bar stuff



     }
  

return (0);
}



bool IsNewBar() 
{ 
datetime curbar = Time[0]; // Open time of current bar

if (lastbar!=curbar) 
   { 
    lastbar=curbar; 
    return (true); 
   } 

  return(false); 

}

Good Luck

 
int start(){
   static datetime Time0; if (Time0 == Time[0]) return(0); Time0=Time[0];
   ...