t is often necessary to execute code only at new bar opening. It may be useful for sending e-mails or showing alerts, when it has to be done once for every bar. There are surely a lot of use cases. I present you the code of a function returning true if this is the first tick of a new bar:
bool isNewBar() { static int prevTime; bool newBar=false; if(Time[0]!=prevTime) { newBar=true; prevTime=Time[0]; } return(newBar); }
Then you only need to check the value of isNewBar:
if(isNewBar()) { // code to execute in a new bar }